summaryrefslogtreecommitdiffstats
path: root/mdocml.c
blob: 843242e1629753823056af998533f94b67ee4c81 (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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
/* $Id$ */
/*
 * 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/stat.h>
#include <sys/param.h>

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

#include "mdoc.h"

#define	MD_LINE_SZ	(256)

struct	md_parse {
	int		 warn;
#define	MD_WARN_ALL	(1 << 0)
#define	MD_WARN_ERR	(1 << 1)
	int		 dbg;
	struct mdoc	*mdoc;
	char		*buf;
	u_long		 bufsz;
	char		*name;
	int		 fd;
	int		 lnn;
	char		*line;
};

static	void		 usage(void);

static	int		 parse_begin(struct md_parse *);
static	int		 parse_leave(struct md_parse *, int);
static	int		 io_begin(struct md_parse *);
static	int		 io_leave(struct md_parse *, int);
static	int		 buf_begin(struct md_parse *);
static	int		 buf_leave(struct md_parse *, int);

static	int		 msg_err(void *, int, int, enum mdoc_err);
static	int		 msg_warn(void *, int, int, enum mdoc_warn);
static	void		 msg_msg(void *, int, const char *);

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

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

	extern char	*optarg;
	extern int	 optind;

	(void)memset(&parser, 0, sizeof(struct md_parse));

	while (-1 != (c = getopt(argc, argv, "vW:")))
		switch (c) {
		case ('v'):
			parser.dbg++;
			break;
		case ('W'):
			opts = optarg;
			while (*opts) 
				switch (getsubopt(&opts, toks, &v)) {
				case (ALL):
					parser.warn |= MD_WARN_ALL;
					break;
				case (ERROR):
					parser.warn |= MD_WARN_ERR;
					break;
				default:
					usage();
					return(1);
				}
			break;
		default:
			usage();
			return(1);
		}

	argv += optind;
	argc -= optind;

	parser.name = "-";
	if (1 == argc)
		parser.name = *argv++;

	if ( ! io_begin(&parser))
		return(EXIT_FAILURE);

	return(EXIT_SUCCESS);
}


static int
io_leave(struct md_parse *p, int code)
{

	if (-1 == p->fd || STDIN_FILENO == p->fd)
		return(code);

	if (-1 == close(p->fd)) {
		warn("%s", p->name);
		code = 0;
	}
	return(code);
}


static int
io_begin(struct md_parse *p)
{

	p->fd = STDIN_FILENO;
	if (0 != strncmp(p->name, "-", 1))
		if (-1 == (p->fd = open(p->name, O_RDONLY, 0))) {
			warn("%s", p->name);
			return(io_leave(p, 0));
		}

	return(io_leave(p, buf_begin(p)));
}


static int
buf_leave(struct md_parse *p, int code)
{

	if (p->buf)
		free(p->buf);
	return(code);
}


static int
buf_begin(struct md_parse *p)
{
	struct stat	 st;

	if (-1 == fstat(p->fd, &st)) {
		warn("%s", p->name);
		return(1);
	} 

	p->bufsz = MAX(st.st_blksize, BUFSIZ);

	if (NULL == (p->buf = malloc(p->bufsz))) {
		warn("malloc");
		return(buf_leave(p, 0));
	}

	return(buf_leave(p, parse_begin(p)));
}


static void
print_node(const struct mdoc_node *n, int indent)
{
	const char	 *p, *t;
	int		  i, j;
	size_t		  argc, sz;
	char		**params;
	struct mdoc_arg	 *argv;

	argv = NULL;
	argc = 0;
	params = NULL;
	sz = 0;

	switch (n->type) {
	case (MDOC_TEXT):
		assert(NULL == n->child);
		p = n->data.text.string;
		t = "text";
		break;
	case (MDOC_BODY):
		p = mdoc_macronames[n->data.body.tok];
		t = "block-body";
		break;
	case (MDOC_HEAD):
		p = mdoc_macronames[n->data.head.tok];
		t = "block-head";
		break;
	case (MDOC_TAIL):
		p = mdoc_macronames[n->data.tail.tok];
		t = "block-tail";
		break;
	case (MDOC_ELEM):
		p = mdoc_macronames[n->data.elem.tok];
		t = "element";
		argv = n->data.elem.argv;
		argc = n->data.elem.argc;
		params = n->data.elem.args;
		sz = n->data.elem.sz;
		break;
	case (MDOC_BLOCK):
		p = mdoc_macronames[n->data.block.tok];
		t = "block";
		argv = n->data.block.argv;
		argc = n->data.block.argc;
		break;
	default:
		abort();
		/* NOTREACHED */
	}

	for (i = 0; i < indent; i++)
		(void)printf("    ");
	(void)printf("%s (%s)", p, t);

	for (i = 0; i < (int)argc; i++) {
		(void)printf(" -%s", mdoc_argnames[argv[i].arg]);
		for (j = 0; j < (int)argv[i].sz; j++)
			(void)printf(" \"%s\"", argv[i].value[j]);
	}

	for (i = 0; i < (int)sz; i++)
		(void)printf(" \"%s\"", params[i]);

	(void)printf("\n");

	if (n->child)
		print_node(n->child, indent + 1);
	if (n->next)
		print_node(n->next, indent);
}


static int
parse_leave(struct md_parse *p, int code)
{
	const struct mdoc_node *n;

	if (p->mdoc) {
		if ((n = mdoc_result(p->mdoc)))
			print_node(n, 0);
		mdoc_free(p->mdoc);
	}
	return(code);
}


static int
parse_begin(struct md_parse *p)
{
	ssize_t		 sz, i;
	size_t		 pos;
	char		 line[256], sv[256];
	struct mdoc_cb	 cb;

	cb.mdoc_err = msg_err;
	cb.mdoc_warn = msg_warn;
	cb.mdoc_msg = msg_msg;

	if (NULL == (p->mdoc = mdoc_alloc(p, &cb)))
		return(parse_leave(p, 0));

	p->lnn = 1;
	p->line = sv;

	for (pos = 0; ; ) {
		if (-1 == (sz = read(p->fd, p->buf, p->bufsz))) {
			warn("%s", p->name);
			return(parse_leave(p, 0));
		} else if (0 == sz) 
			break;

		for (i = 0; i < sz; i++) {
			if ('\n' != p->buf[i]) {
				if (pos < sizeof(line)) {
					sv[(int)pos] = p->buf[(int)i];
					line[(int)pos++] = 
						p->buf[(int)i];
					continue;
				}
				warnx("%s: line %d too long", 
						p->name, p->lnn);
				return(parse_leave(p, 0));
			}
	
			line[(int)pos] = sv[(int)pos] = 0;
			if ( ! mdoc_parseln(p->mdoc, line))
				return(parse_leave(p, 0));

			p->lnn++;
			pos = 0;
		}
	}

	return(parse_leave(p, 1));
}


static int
msg_err(void *arg, int tok, int col, enum mdoc_err type)
{
	char		 *fmt, *lit;
	struct md_parse	 *p;
	int		  i;

	p = (struct md_parse *)arg;

	fmt = lit = NULL;

	switch (type) {
	case (ERR_SYNTAX_NOTEXT):
		lit = "syntax: context-free text disallowed";
		break;
	case (ERR_SYNTAX_QUOTE):
		lit = "syntax: disallowed argument quotation";
		break;
	case (ERR_SYNTAX_UNQUOTE):
		lit = "syntax: unterminated quotation";
		break;
	case (ERR_SYNTAX_WS):
		lit = "syntax: whitespace in argument";
		break;
	case (ERR_SYNTAX_ARGFORM):
		fmt = "syntax: macro `%s' arguments malformed";
		break;
	case (ERR_SYNTAX_NOPUNCT):
		fmt = "syntax: macro `%s' doesn't understand punctuation";
		break;
	case (ERR_SYNTAX_ARG):
		fmt = "syntax: unknown argument for macro `%s'";
		break;
	case (ERR_SCOPE_BREAK):
		/* Which scope is broken? */
		fmt = "scope: macro `%s' breaks prior explicit scope";
		break;
	case (ERR_SCOPE_NOCTX):
		fmt = "scope: closure macro `%s' has no context";
		break;
	case (ERR_SCOPE_NONEST):
		fmt = "scope: macro `%s' may not be nested in the current context";
		break;
	case (ERR_MACRO_NOTSUP):
		lit = "macro not supported";
		break;
	case (ERR_MACRO_NOTCALL):
		fmt = "macro `%s' not callable";
		break;
	case (ERR_SEC_PROLOGUE):
		fmt = "macro `%s' cannot be called in the prologue";
		break;
	case (ERR_SEC_NPROLOGUE):
		fmt = "macro `%s' called outside of prologue";
		break;
	case (ERR_ARGS_EQ0):
		fmt = "macro `%s' expects zero arguments";
		break;
	case (ERR_ARGS_EQ1):
		fmt = "macro `%s' expects one argument";
		break;
	case (ERR_ARGS_GE1):
		fmt = "macro `%s' expects one or more arguments";
		break;
	case (ERR_ARGS_LE2):
		fmt = "macro `%s' expects two or fewer arguments";
		break;
	case (ERR_ARGS_MANY):
		fmt = "macro `%s' has too many arguments";
		break;
	case (ERR_SEC_PROLOGUE_OO):
		fmt = "prologue macro `%s' is out-of-order";
		break;
	case (ERR_SEC_PROLOGUE_REP):
		fmt = "prologue macro `%s' repeated";
		break;
	case (ERR_SEC_NAME):
		lit = "`NAME' section must be first";
		break;
	case (ERR_SYNTAX_ARGVAL):
		lit = "syntax: expected value for macro argument";
		break;
	case (ERR_SYNTAX_ARGBAD):
		lit = "syntax: invalid value for macro argument";
		break;
	case (ERR_SYNTAX_ARGMANY):
		lit = "syntax: too many values for macro argument";
		break;
	case (ERR_SYNTAX_CHILDHEAD):
		lit = "syntax: expected only block-header section";
		break;
	case (ERR_SYNTAX_CHILDBODY):
		lit = "syntax: expected only a block-body section";
		break;
	case (ERR_SYNTAX_EMPTYHEAD):
		lit = "syntax: block-header section may not be empty";
		break;
	case (ERR_SYNTAX_EMPTYBODY):
		lit = "syntax: block-body section may not be empty";
		break;
	default:
		abort();
		/* NOTREACHED */
	}

	if (fmt) {
		(void)fprintf(stderr, "%s:%d: error: ",
				p->name, p->lnn);
		(void)fprintf(stderr, fmt, mdoc_macronames[tok]);
	} else
		(void)fprintf(stderr, "%s:%d: error: %s",
				p->name, p->lnn, lit);

	if (p->dbg < 1) {
		if (-1 != col)
			(void)fprintf(stderr, " (column %d)\n", col);
		return(0);
	} else if (-1 == col) {
		(void)fprintf(stderr, "\nFrom: %s", p->line);
		return(0);
	}

	(void)fprintf(stderr, "\nFrom: %s\n      ", p->line);
	for (i = 0; i < col; i++)
		(void)fprintf(stderr, " ");
	(void)fprintf(stderr, "^\n");

	return(0);
}


static void
msg_msg(void *arg, int col, const char *msg)
{
	struct md_parse	 *p;
	int		  i;

	p = (struct md_parse *)arg;

	if (p->dbg < 2)
		return;

	(void)printf("%s:%d: %s", p->name, p->lnn, msg);

	if (p->dbg < 3) {
		if (-1 != col)
			(void)printf(" (column %d)\n", col);
		return;
	} else if (-1 == col) {
		(void)printf("\nFrom %s\n", p->line);
		return;
	}

	(void)printf("\nFrom: %s\n      ", p->line);
	for (i = 0; i < col; i++)
		(void)printf(" ");
	(void)printf("^\n");
}


static int
msg_warn(void *arg, int tok, int col, enum mdoc_warn type)
{
	char		 *fmt, *lit;
	struct md_parse	 *p;
	int		  i;
	extern char	 *__progname;

	p = (struct md_parse *)arg;

	if ( ! (p->warn & MD_WARN_ALL))
		return(1);

	fmt = lit = NULL;

	switch (type) {
	case (WARN_SYNTAX_WS_EOLN):
		lit = "syntax: whitespace at end-of-line";
		break;
	case (WARN_SYNTAX_QUOTED):
		lit = "syntax: quotation mark starting string";
		break;
	case (WARN_SYNTAX_MACLIKE):
		lit = "syntax: macro-like argument";
		break;
	case (WARN_SYNTAX_ARGLIKE):
		lit = "syntax: argument-like value";
		break;
	case (WARN_SYNTAX_EMPTYBODY):
		lit = "syntax: empty block-body section";
		break;
	case (WARN_SEC_OO):
		lit = "section is out of conventional order";
		break;
	case (WARN_ARGS_GE1):
		fmt = "macro `%s' suggests one or more arguments";
		break;
	case (WARN_ARGS_EQ0):
		fmt = "macro `%s' suggests zero arguments";
		break;
	case (WARN_IGN_AFTER_BLK):
		fmt = "ignore: macro `%s' ignored after block macro";
		break;
	case (WARN_IGN_OBSOLETE):
		fmt = "ignore: macro `%s' is obsolete";
		break;
	case (WARN_IGN_BEFORE_BLK):
		fmt = "ignore: macro before block macro `%s' ignored";
		break;
	case (WARN_COMPAT_TROFF):
		fmt = "compat: macro `%s' behaves differently in troff and nroff";
		break;
	default:
		abort();
		/* NOTREACHED */
	}

	if (fmt) {
		(void)fprintf(stderr, "%s:%d: warning: ",
				p->name, p->lnn);
		(void)fprintf(stderr, fmt, mdoc_macronames[tok]);
	} else
		(void)fprintf(stderr, "%s:%d: warning: %s",
				p->name, p->lnn, lit);

	if (p->dbg >= 1) {
		(void)fprintf(stderr, "\nFrom: %s\n      ", p->line);
		for (i = 0; i < col; i++)
			(void)fprintf(stderr, " ");
		(void)fprintf(stderr, "^\n");
	} else
		(void)fprintf(stderr, " (column %d)\n", col);

	if (p->warn & MD_WARN_ERR) {
		(void)fprintf(stderr, "%s: considering warnings as "
				"errors\n", __progname);
		return(0);
	}

	return(1);
}


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

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