summaryrefslogtreecommitdiffstats
path: root/mandocd.c
blob: 109199299c942b1b27c3f192e9de5e17d0f7a0ad (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/* $Id$ */
/*
 * Copyright (c) 2017 Michael Stapelberg <stapelberg@debian.org>
 * Copyright (c) 2017, 2019, 2021 Ingo Schwarze <schwarze@openbsd.org>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */
#include "config.h"

#if NEED_XPG4_2
#define _XPG4_2
#endif

#include <sys/types.h>
#include <sys/socket.h>

#if HAVE_ERR
#include <err.h>
#endif
#include <limits.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "mandoc.h"
#if DEBUG_MEMORY
#define DEBUG_NODEF 1
#include "mandoc_dbg.h"
#endif
#include "roff.h"
#include "mdoc.h"
#include "man.h"
#include "mandoc_parse.h"
#include "main.h"
#include "manconf.h"

enum	outt {
	OUTT_ASCII = 0,
	OUTT_UTF8,
	OUTT_HTML
};

static	void	  process(struct mparse *, enum outt, void *);
static	int	  read_fds(int, int *);
static	void	  usage(void) __attribute__((__noreturn__));


#define NUM_FDS 3
static int
read_fds(int clientfd, int *fds)
{
	struct msghdr	 msg;
	struct iovec	 iov[1];
	unsigned char	 dummy[1];
	struct cmsghdr	*cmsg;
	int		*walk;
	int		 cnt;

	/* Union used for alignment. */
	union {
		uint8_t controlbuf[CMSG_SPACE(NUM_FDS * sizeof(int))];
		struct cmsghdr align;
	} u;

	memset(&msg, '\0', sizeof(msg));
	msg.msg_control = u.controlbuf;
	msg.msg_controllen = sizeof(u.controlbuf);

	/*
	 * Read a dummy byte - sendmsg cannot send an empty message,
	 * even if we are only interested in the OOB data.
	 */

	iov[0].iov_base = dummy;
	iov[0].iov_len = sizeof(dummy);
	msg.msg_iov = iov;
	msg.msg_iovlen = 1;

	switch (recvmsg(clientfd, &msg, 0)) {
	case -1:
		warn("recvmsg");
		return -1;
	case 0:
		return 0;
	default:
		break;
	}

	if ((cmsg = CMSG_FIRSTHDR(&msg)) == NULL) {
		warnx("CMSG_FIRSTHDR: missing control message");
		return -1;
	}

	if (cmsg->cmsg_level != SOL_SOCKET ||
	    cmsg->cmsg_type != SCM_RIGHTS ||
	    cmsg->cmsg_len != CMSG_LEN(NUM_FDS * sizeof(int))) {
		warnx("CMSG_FIRSTHDR: invalid control message");
		return -1;
	}

	walk = (int *)CMSG_DATA(cmsg);
	for (cnt = 0; cnt < NUM_FDS; cnt++)
		fds[cnt] = *walk++;

	return 1;
}

int
main(int argc, char *argv[])
{
	struct manoutput	 options;
	struct mparse		*parser;
	void			*formatter;
	const char		*defos;
	const char		*errstr;
	int			 clientfd;
	int			 old_stdin;
	int			 old_stdout;
	int			 old_stderr;
	int			 fds[3];
	int			 state, opt;
	enum outt		 outtype;

#if DEBUG_MEMORY
	mandoc_dbg_init(argc, argv);
#endif

	defos = NULL;
	outtype = OUTT_ASCII;
	while ((opt = getopt(argc, argv, "I:T:")) != -1) {
		switch (opt) {
		case 'I':
			if (strncmp(optarg, "os=", 3) == 0)
				defos = optarg + 3;
			else {
				warnx("-I %s: Bad argument", optarg);
				usage();
			}
			break;
		case 'T':
			if (strcmp(optarg, "ascii") == 0)
				outtype = OUTT_ASCII;
			else if (strcmp(optarg, "utf8") == 0)
				outtype = OUTT_UTF8;
			else if (strcmp(optarg, "html") == 0)
				outtype = OUTT_HTML;
			else {
				warnx("-T %s: Bad argument", optarg);
				usage();
			}
			break;
		default:
			usage();
		}
	}

	if (argc > 0) {
		argc -= optind;
		argv += optind;
	}
	if (argc != 1)
		usage();

	errstr = NULL;
	clientfd = strtonum(argv[0], 3, INT_MAX, &errstr);
	if (errstr)
		errx(1, "file descriptor %s %s", argv[1], errstr);

	mchars_alloc();
	parser = mparse_alloc(MPARSE_SO | MPARSE_UTF8 | MPARSE_LATIN1 |
	    MPARSE_VALIDATE, MANDOC_OS_OTHER, defos);

	memset(&options, 0, sizeof(options));
	switch (outtype) {
	case OUTT_ASCII:
		formatter = ascii_alloc(&options);
		break;
	case OUTT_UTF8:
		formatter = utf8_alloc(&options);
		break;
	case OUTT_HTML:
		options.fragment = 1;
		formatter = html_alloc(&options);
		break;
	}

	state = 1;  /* work to do */
	fflush(stdout);
	fflush(stderr);
	if ((old_stdin = dup(STDIN_FILENO)) == -1 ||
	    (old_stdout = dup(STDOUT_FILENO)) == -1 ||
	    (old_stderr = dup(STDERR_FILENO)) == -1) {
		warn("dup");
		state = -1;  /* error */
	}

	while (state == 1 && (state = read_fds(clientfd, fds)) == 1) {
		if (dup2(fds[0], STDIN_FILENO) == -1 ||
		    dup2(fds[1], STDOUT_FILENO) == -1 ||
		    dup2(fds[2], STDERR_FILENO) == -1) {
			warn("dup2");
			state = -1;
			break;
		}

		close(fds[0]);
		close(fds[1]);
		close(fds[2]);

		process(parser, outtype, formatter);
		mparse_reset(parser);
		if (outtype == OUTT_HTML)
			html_reset(formatter);

		fflush(stdout);
		fflush(stderr);
		/* Close file descriptors by restoring the old ones. */
		if (dup2(old_stderr, STDERR_FILENO) == -1 ||
		    dup2(old_stdout, STDOUT_FILENO) == -1 ||
		    dup2(old_stdin, STDIN_FILENO) == -1) {
			warn("dup2");
			state = -1;
			break;
		}
	}

	close(clientfd);
	switch (outtype) {
	case OUTT_ASCII:
	case OUTT_UTF8:
		ascii_free(formatter);
		break;
	case OUTT_HTML:
		html_free(formatter);
		break;
	}
	mparse_free(parser);
	mchars_free();
#if DEBUG_MEMORY
	mandoc_dbg_finish();
#endif
	return state == -1 ? 1 : 0;
}

static void
process(struct mparse *parser, enum outt outtype, void *formatter)
{
	struct roff_meta *meta;

	mparse_readfd(parser, STDIN_FILENO, "<unixfd>");
	meta = mparse_result(parser);
	if (meta->macroset == MACROSET_MDOC) {
		switch (outtype) {
		case OUTT_ASCII:
		case OUTT_UTF8:
			terminal_mdoc(formatter, meta);
			break;
		case OUTT_HTML:
			html_mdoc(formatter, meta);
			break;
		}
	}
	if (meta->macroset == MACROSET_MAN) {
		switch (outtype) {
		case OUTT_ASCII:
		case OUTT_UTF8:
			terminal_man(formatter, meta);
			break;
		case OUTT_HTML:
			html_man(formatter, meta);
			break;
		}
	}
}

void
usage(void)
{
	fprintf(stderr, "usage: mandocd [-I os=name] [-T output] socket_fd\n");
	exit(1);
}