summaryrefslogtreecommitdiffstatshomepage
path: root/mdocml.c
blob: d255d07ea864722f9c3aaa69af9d89cdbe43b2d2 (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
/* $Id: mdocml.c,v 1.19 2008/12/09 17:09:12 kristaps Exp $ */
/*
 * Copyright (c) 2008 Kristaps Dzonsons <kristaps@kth.se>
 *
 * 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 AUTHOR DISCLAIMS ALL
 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
 * AUTHOR 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 <sys/param.h>
#include <sys/stat.h>

#include <assert.h>
#include <err.h>
#include <fcntl.h>
#include <getopt.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "libmdocml.h"

#define	BUFFER_IN_DEF	BUFSIZ	 /* See begin_bufs. */
#define	BUFFER_OUT_DEF	BUFSIZ	 /* See begin_bufs. */

#ifdef	DEBUG
#define	CSS		"mdocml.css"
#else
#define	CSS		"/usr/local/share/mdocml/mdocml.css"
#endif

static	void		 usage(void);

static	int		 begin_io(const struct md_args *, 
				char *, char *);
static	int		 leave_io(const struct md_buf *, 
				const struct md_buf *, int);
static	int		 begin_bufs(const struct md_args *,
				struct md_buf *, struct md_buf *);
static int		 leave_bufs(const struct md_buf *, 
				const struct md_buf *, int);

#ifdef __linux__
extern	int		 getsubopt(char **, char *const *, char **);
#endif

int
main(int argc, char *argv[])
{
	int		 c;
	char		*out, *in, *opts, *v;
	struct md_args	 args;
#define ALL     	 0
#define ERROR     	 1
	char		*toks[] = { "all", "error", NULL };

	extern char	*optarg;
	extern int	 optind;

	out = in = NULL;

	(void)memset(&args, 0, sizeof(struct md_args));

	args.type = MD_XML;

	while (-1 != (c = getopt(argc, argv, "c:ef:o:vW:")))
		switch (c) {
		case ('c'):
			if (args.type != MD_HTML)
				errx(1, "-c only valid for -fhtml");
			args.params.html.css = optarg;
			break;
		case ('e'):
			if (args.type != MD_HTML)
				errx(1, "-e only valid for -fhtml");
			args.params.html.flags |= HTML_CSS_EMBED;
			break;
		case ('f'):
			if (0 == strcmp(optarg, "html"))
				args.type = MD_HTML;
			else if (0 == strcmp(optarg, "xml"))
				args.type = MD_XML;
			else
				errx(1, "invalid filter type");
			break;
		case ('o'):
			out = optarg;
			break;
		case ('v'):
			args.verbosity++;
			break;
		case ('W'):
			opts = optarg;
			while (*opts) 
				switch (getsubopt(&opts, toks, &v)) {
				case (ALL):
					args.warnings |= MD_WARN_ALL;
					break;
				case (ERROR):
					args.warnings |= MD_WARN_ERROR;
					break;
				default:
					usage();
					return(1);
				}
			break;
		default:
			usage();
			return(1);
		}

	if (MD_HTML == args.type)
		if (NULL == args.params.html.css) 
			args.params.html.css = CSS;

	argv += optind;
	argc -= optind;

	if (1 == argc)
		in = *argv++;

	return(begin_io(&args, out ? out : "-", in ? in : "-"));
}


/* 
 * Close out file descriptors opened in begin_io.  If the descriptor
 * refers to stdin/stdout, then do nothing.
 */
static int
leave_io(const struct md_buf *out, 
		const struct md_buf *in, int c)
{
	assert(out);
	assert(in);

	if (-1 != in->fd && -1 == close(in->fd)) {
		assert(in->name);
		warn("%s", in->name);
		c = 1;
	}
	if (-1 != out->fd && STDOUT_FILENO != out->fd &&
			-1 == close(out->fd)) {
		assert(out->name);
		warn("%s", out->name);
		c = 1;
	}
	if (1 == c && STDOUT_FILENO != out->fd)
		if (-1 == unlink(out->name))
			warn("%s", out->name);

	return(c);
}


/*
 * Open file descriptors or assign stdin/stdout, if dictated by the "-"
 * token instead of a filename.
 */
static int
begin_io(const struct md_args *args, char *out, char *in)
{
	struct md_buf	 fi;
	struct md_buf	 fo;

#define	FI_FL	O_RDONLY
#define	FO_FL	O_WRONLY|O_CREAT|O_TRUNC

	assert(args);
	assert(out);
	assert(in);

	bzero(&fi, sizeof(struct md_buf));
	bzero(&fo, sizeof(struct md_buf));

	fi.fd = STDIN_FILENO;
	fo.fd = STDOUT_FILENO;

	fi.name = in;
	fo.name = out;

	if (0 != strncmp(fi.name, "-", 1))
		if (-1 == (fi.fd = open(fi.name, FI_FL, 0))) {
			warn("%s", fi.name);
			return(leave_io(&fo, &fi, 1));
		}

	if (0 != strncmp(fo.name, "-", 1)) 
		if (-1 == (fo.fd = open(fo.name, FO_FL, 0644))) {
			warn("%s", fo.name);
			return(leave_io(&fo, &fi, 1));
		}

	return(leave_io(&fo, &fi, begin_bufs(args, &fo, &fi)));
}


/*
 * Free buffers allocated in begin_bufs.
 */
static int
leave_bufs(const struct md_buf *out, 
		const struct md_buf *in, int c)
{
	assert(out);
	assert(in);
	if (out->buf)
		free(out->buf);
	if (in->buf)
		free(in->buf);
	return(c);
}


/*
 * Allocate buffers to the maximum of either the input file's blocksize
 * or BUFFER_IN_DEF/BUFFER_OUT_DEF, which should be around BUFSIZE.
 */
static int
begin_bufs(const struct md_args *args, 
		struct md_buf *out, struct md_buf *in)
{
	struct stat	 stin, stout;
	int		 c;

	assert(args);
	assert(in);
	assert(out);

	if (-1 == fstat(in->fd, &stin)) {
		warn("%s", in->name);
		return(1);
	} else if (STDIN_FILENO != in->fd && 0 == stin.st_size) {
		warnx("%s: empty file", in->name);
		return(1);
	} else if (-1 == fstat(out->fd, &stout)) {
		warn("%s", out->name);
		return(1);
	}

	in->bufsz = MAX(stin.st_blksize, BUFFER_IN_DEF);
	out->bufsz = MAX(stout.st_blksize, BUFFER_OUT_DEF);

	if (NULL == (in->buf = malloc(in->bufsz))) {
		warn("malloc");
		return(leave_bufs(out, in, 1));
	} else if (NULL == (out->buf = malloc(out->bufsz))) {
		warn("malloc");
		return(leave_bufs(out, in, 1));
	}

	c = md_run(args, out, in);
	return(leave_bufs(out, in, -1 == c ? 1 : 0));
}


static void
usage(void)
{
	extern char	*__progname;

	(void)fprintf(stderr, "usage: %s [-v] [-Wwarn...]  "
			"[-f filter] [-o outfile] [infile]\n", 
			__progname);
}