GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usr.bin/mandoc/man_validate.c Lines: 164 198 82.8 %
Date: 2017-11-07 Branches: 124 174 71.3 %

Line Branch Exec Source
1
/*	$OpenBSD: man_validate.c,v 1.104 2017/07/26 10:33:02 schwarze Exp $ */
2
/*
3
 * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4
 * Copyright (c) 2010, 2012-2017 Ingo Schwarze <schwarze@openbsd.org>
5
 *
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.
9
 *
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.
17
 */
18
#include <sys/types.h>
19
20
#include <assert.h>
21
#include <ctype.h>
22
#include <errno.h>
23
#include <limits.h>
24
#include <stdarg.h>
25
#include <stdlib.h>
26
#include <string.h>
27
#include <time.h>
28
29
#include "mandoc_aux.h"
30
#include "mandoc.h"
31
#include "roff.h"
32
#include "man.h"
33
#include "libmandoc.h"
34
#include "roff_int.h"
35
#include "libman.h"
36
37
#define	CHKARGS	  struct roff_man *man, struct roff_node *n
38
39
typedef	void	(*v_check)(CHKARGS);
40
41
static	void	  check_par(CHKARGS);
42
static	void	  check_part(CHKARGS);
43
static	void	  check_root(CHKARGS);
44
static	void	  check_text(CHKARGS);
45
46
static	void	  post_AT(CHKARGS);
47
static	void	  post_IP(CHKARGS);
48
static	void	  post_OP(CHKARGS);
49
static	void	  post_TH(CHKARGS);
50
static	void	  post_UC(CHKARGS);
51
static	void	  post_UR(CHKARGS);
52
static	void	  post_in(CHKARGS);
53
static	void	  post_vs(CHKARGS);
54
55
static	const v_check __man_valids[MAN_MAX - MAN_TH] = {
56
	post_TH,    /* TH */
57
	NULL,       /* SH */
58
	NULL,       /* SS */
59
	NULL,       /* TP */
60
	check_par,  /* LP */
61
	check_par,  /* PP */
62
	check_par,  /* P */
63
	post_IP,    /* IP */
64
	NULL,       /* HP */
65
	NULL,       /* SM */
66
	NULL,       /* SB */
67
	NULL,       /* BI */
68
	NULL,       /* IB */
69
	NULL,       /* BR */
70
	NULL,       /* RB */
71
	NULL,       /* R */
72
	NULL,       /* B */
73
	NULL,       /* I */
74
	NULL,       /* IR */
75
	NULL,       /* RI */
76
	NULL,       /* nf */
77
	NULL,       /* fi */
78
	NULL,       /* RE */
79
	check_part, /* RS */
80
	NULL,       /* DT */
81
	post_UC,    /* UC */
82
	NULL,       /* PD */
83
	post_AT,    /* AT */
84
	post_in,    /* in */
85
	post_OP,    /* OP */
86
	NULL,       /* EX */
87
	NULL,       /* EE */
88
	post_UR,    /* UR */
89
	NULL,       /* UE */
90
	post_UR,    /* MT */
91
	NULL,       /* ME */
92
};
93
static	const v_check *man_valids = __man_valids - MAN_TH;
94
95
96
void
97
man_node_validate(struct roff_man *man)
98
{
99
	struct roff_node *n;
100
	const v_check	 *cp;
101
102
4518046
	n = man->last;
103
2259023
	man->last = man->last->child;
104
6769990
	while (man->last != NULL) {
105
2251944
		man_node_validate(man);
106
2251944
		if (man->last == n)
107
9860
			man->last = man->last->child;
108
		else
109
2242084
			man->last = man->last->next;
110
	}
111
112
4380238
	man->last = n;
113
4380238
	man->next = ROFF_NEXT_SIBLING;
114

4380238
	switch (n->type) {
115
	case ROFFT_TEXT:
116
1220044
		check_text(man, n);
117
1220044
		break;
118
	case ROFFT_ROOT:
119
7079
		check_root(man, n);
120
7079
		break;
121
	case ROFFT_EQN:
122
	case ROFFT_TBL:
123
		break;
124
	default:
125
1024155
		if (n->tok < ROFF_MAX) {
126
130063
			switch (n->tok) {
127
			case ROFF_br:
128
			case ROFF_sp:
129
60577
				post_vs(man, n);
130
60577
				break;
131
			default:
132
69486
				roff_validate(man);
133
69486
				break;
134
			}
135
			break;
136
		}
137

1788184
		assert(n->tok >= MAN_TH && n->tok < MAN_MAX);
138
894092
		cp = man_valids + n->tok;
139
894092
		if (*cp)
140
615202
			(*cp)(man, n);
141
894092
		if (man->last == n)
142
886630
			man_state(man, n);
143
		break;
144
	}
145
2259023
}
146
147
static void
148
check_root(CHKARGS)
149
{
150
151
14158
	assert((man->flags & (MAN_BLINE | MAN_ELINE)) == 0);
152
153
7079
	if (NULL == man->first->child)
154
56
		mandoc_msg(MANDOCERR_DOC_EMPTY, man->parse,
155
28
		    n->line, n->pos, NULL);
156
	else
157
7051
		man->meta.hasbody = 1;
158
159
7079
	if (NULL == man->meta.title) {
160
20
		mandoc_msg(MANDOCERR_TH_NOTITLE, man->parse,
161
10
		    n->line, n->pos, NULL);
162
163
		/*
164
		 * If a title hasn't been set, do so now (by
165
		 * implication, date and section also aren't set).
166
		 */
167
168
10
		man->meta.title = mandoc_strdup("");
169
10
		man->meta.msec = mandoc_strdup("");
170
30
		man->meta.date = man->quick ? mandoc_strdup("") :
171
10
		    mandoc_normdate(man, NULL, n->line, n->pos);
172
10
	}
173
174

10976
	if (man->meta.os_e &&
175
3897
	    (man->meta.rcsids & (1 << man->meta.os_e)) == 0)
176
3762
		mandoc_msg(MANDOCERR_RCS_MISSING, man->parse, 0, 0,
177
1881
		    man->meta.os_e == MANDOC_OS_OPENBSD ?
178
		    "(OpenBSD)" : "(NetBSD)");
179
7079
}
180
181
static void
182
check_text(CHKARGS)
183
{
184
	char		*cp, *p;
185
186
2440088
	if (MAN_LITERAL & man->flags)
187
246749
		return;
188
189
973295
	cp = n->string;
190
1948330
	for (p = cp; NULL != (p = strchr(p, '\t')); p++)
191
1740
		mandoc_msg(MANDOCERR_FI_TAB, man->parse,
192
870
		    n->line, n->pos + (p - cp), NULL);
193
2193339
}
194
195
static void
196
post_OP(CHKARGS)
197
{
198
199
144
	if (n->child == NULL)
200
36
		mandoc_msg(MANDOCERR_OP_EMPTY, man->parse,
201
18
		    n->line, n->pos, "OP");
202

90
	else if (n->child->next != NULL && n->child->next->next != NULL) {
203
		n = n->child->next->next;
204
36
		mandoc_vmsg(MANDOCERR_ARG_EXCESS, man->parse,
205
18
		    n->line, n->pos, "OP ... %s", n->string);
206
18
	}
207
72
}
208
209
static void
210
post_UR(CHKARGS)
211
{
212

1764
	if (n->type == ROFFT_HEAD && n->child == NULL)
213
72
		mandoc_msg(MANDOCERR_UR_NOHEAD, man->parse,
214
36
		    n->line, n->pos, roff_name[n->tok]);
215
756
	check_part(man, n);
216
756
}
217
218
static void
219
check_part(CHKARGS)
220
{
221
222

74942
	if (n->type == ROFFT_BODY && n->child == NULL)
223
7416
		mandoc_msg(MANDOCERR_BLK_EMPTY, man->parse,
224
3708
		    n->line, n->pos, roff_name[n->tok]);
225
32118
}
226
227
static void
228
check_par(CHKARGS)
229
{
230
231

750987
	switch (n->type) {
232
	case ROFFT_BLOCK:
233
83443
		if (n->body->child == NULL)
234
354
			roff_node_delete(man, n);
235
		break;
236
	case ROFFT_BODY:
237
83443
		if (n->child == NULL)
238
354
			mandoc_vmsg(MANDOCERR_PAR_SKIP,
239
354
			    man->parse, n->line, n->pos,
240
354
			    "%s empty", roff_name[n->tok]);
241
		break;
242
	case ROFFT_HEAD:
243
83443
		if (n->child != NULL)
244
55
			mandoc_vmsg(MANDOCERR_ARG_SKIP,
245
55
			    man->parse, n->line, n->pos, "%s %s%s",
246
55
			    roff_name[n->tok], n->child->string,
247
55
			    n->child->next != NULL ? " ..." : "");
248
		break;
249
	default:
250
		break;
251
	}
252
250329
}
253
254
static void
255
post_IP(CHKARGS)
256
{
257
258
866312
	switch (n->type) {
259
	case ROFFT_BLOCK:
260

108835
		if (n->head->child == NULL && n->body->child == NULL)
261
39
			roff_node_delete(man, n);
262
		break;
263
	case ROFFT_BODY:
264

108835
		if (n->parent->head->child == NULL && n->child == NULL)
265
39
			mandoc_vmsg(MANDOCERR_PAR_SKIP,
266
39
			    man->parse, n->line, n->pos,
267
39
			    "%s empty", roff_name[n->tok]);
268
		break;
269
	default:
270
		break;
271
	}
272
324867
}
273
274
static void
275
post_TH(CHKARGS)
276
{
277
	struct roff_node *nb;
278
	const char	*p;
279
280
14138
	free(man->meta.title);
281
7069
	free(man->meta.vol);
282
7069
	free(man->meta.os);
283
7069
	free(man->meta.msec);
284
7069
	free(man->meta.date);
285
286
7069
	man->meta.title = man->meta.vol = man->meta.date =
287
7069
	    man->meta.msec = man->meta.os = NULL;
288
289
	nb = n;
290
291
	/* ->TITLE<- MSEC DATE OS VOL */
292
293
7069
	n = n->child;
294

14120
	if (n && n->string) {
295
118142
		for (p = n->string; '\0' != *p; p++) {
296
			/* Only warn about this once... */
297

101615
			if (isalpha((unsigned char)*p) &&
298
47317
			    ! isupper((unsigned char)*p)) {
299
2278
				mandoc_vmsg(MANDOCERR_TITLE_CASE,
300
2278
				    man->parse, n->line,
301
2278
				    n->pos + (p - n->string),
302
				    "TH %s", n->string);
303
2278
				break;
304
			}
305
		}
306
7051
		man->meta.title = mandoc_strdup(n->string);
307
7051
	} else {
308
18
		man->meta.title = mandoc_strdup("");
309
36
		mandoc_msg(MANDOCERR_TH_NOTITLE, man->parse,
310
18
		    nb->line, nb->pos, "TH");
311
	}
312
313
	/* TITLE ->MSEC<- DATE OS VOL */
314
315
7069
	if (n)
316
7051
		n = n->next;
317

14084
	if (n && n->string)
318
7015
		man->meta.msec = mandoc_strdup(n->string);
319
	else {
320
54
		man->meta.msec = mandoc_strdup("");
321
108
		mandoc_vmsg(MANDOCERR_MSEC_MISSING, man->parse,
322
54
		    nb->line, nb->pos, "TH %s", man->meta.title);
323
	}
324
325
	/* TITLE MSEC ->DATE<- OS VOL */
326
327
7069
	if (n)
328
7015
		n = n->next;
329

21085
	if (n && n->string && '\0' != n->string[0]) {
330
19917
		man->meta.date = man->quick ?
331
		    mandoc_strdup(n->string) :
332
6639
		    mandoc_normdate(man, n->string, n->line, n->pos);
333
6639
	} else {
334
430
		man->meta.date = mandoc_strdup("");
335
860
		mandoc_msg(MANDOCERR_DATE_MISSING, man->parse,
336
430
		    n ? n->line : nb->line,
337
		    n ? n->pos : nb->pos, "TH");
338
	}
339
340
	/* TITLE MSEC DATE ->OS<- VOL */
341
342

14077
	if (n && (n = n->next))
343
4646
		man->meta.os = mandoc_strdup(n->string);
344
2423
	else if (man->os_s != NULL)
345
1998
		man->meta.os = mandoc_strdup(man->os_s);
346

20782
	if (man->meta.os_e == MANDOC_OS_OTHER && man->meta.os != NULL) {
347
6644
		if (strstr(man->meta.os, "OpenBSD") != NULL)
348
3897
			man->meta.os_e = MANDOC_OS_OPENBSD;
349
2747
		else if (strstr(man->meta.os, "NetBSD") != NULL)
350
			man->meta.os_e = MANDOC_OS_NETBSD;
351
	}
352
353
	/* TITLE MSEC DATE OS ->VOL<- */
354
	/* If missing, use the default VOL name for MSEC. */
355
356

11715
	if (n && (n = n->next))
357
4481
		man->meta.vol = mandoc_strdup(n->string);
358

5122
	else if ('\0' != man->meta.msec[0] &&
359
2534
	    (NULL != (p = mandoc_a2msec(man->meta.msec))))
360
2534
		man->meta.vol = mandoc_strdup(p);
361
362

11550
	if (n != NULL && (n = n->next) != NULL)
363
36
		mandoc_vmsg(MANDOCERR_ARG_EXCESS, man->parse,
364
18
		    n->line, n->pos, "TH ... %s", n->string);
365
366
	/*
367
	 * Remove the `TH' node after we've processed it for our
368
	 * meta-data.
369
	 */
370
7069
	roff_node_delete(man, man->last);
371
7069
}
372
373
static void
374
post_UC(CHKARGS)
375
{
376
	static const char * const bsd_versions[] = {
377
	    "3rd Berkeley Distribution",
378
	    "4th Berkeley Distribution",
379
	    "4.2 Berkeley Distribution",
380
	    "4.3 Berkeley Distribution",
381
	    "4.4 Berkeley Distribution",
382
	};
383
384
	const char	*p, *s;
385
386
	n = n->child;
387
388
	if (n == NULL || n->type != ROFFT_TEXT)
389
		p = bsd_versions[0];
390
	else {
391
		s = n->string;
392
		if (0 == strcmp(s, "3"))
393
			p = bsd_versions[0];
394
		else if (0 == strcmp(s, "4"))
395
			p = bsd_versions[1];
396
		else if (0 == strcmp(s, "5"))
397
			p = bsd_versions[2];
398
		else if (0 == strcmp(s, "6"))
399
			p = bsd_versions[3];
400
		else if (0 == strcmp(s, "7"))
401
			p = bsd_versions[4];
402
		else
403
			p = bsd_versions[0];
404
	}
405
406
	free(man->meta.os);
407
	man->meta.os = mandoc_strdup(p);
408
}
409
410
static void
411
post_AT(CHKARGS)
412
{
413
	static const char * const unix_versions[] = {
414
	    "7th Edition",
415
	    "System III",
416
	    "System V",
417
	    "System V Release 2",
418
	};
419
420
	struct roff_node *nn;
421
	const char	*p, *s;
422
423
	n = n->child;
424
425
	if (n == NULL || n->type != ROFFT_TEXT)
426
		p = unix_versions[0];
427
	else {
428
		s = n->string;
429
		if (0 == strcmp(s, "3"))
430
			p = unix_versions[0];
431
		else if (0 == strcmp(s, "4"))
432
			p = unix_versions[1];
433
		else if (0 == strcmp(s, "5")) {
434
			nn = n->next;
435
			if (nn != NULL &&
436
			    nn->type == ROFFT_TEXT &&
437
			    nn->string[0] != '\0')
438
				p = unix_versions[3];
439
			else
440
				p = unix_versions[2];
441
		} else
442
			p = unix_versions[0];
443
	}
444
445
	free(man->meta.os);
446
	man->meta.os = mandoc_strdup(p);
447
}
448
449
static void
450
post_in(CHKARGS)
451
{
452
1494
	char	*s;
453
454

756
	if (n->parent->tok != MAN_TP ||
455
51
	    n->parent->type != ROFFT_HEAD ||
456
9
	    n->child == NULL ||
457
9
	    *n->child->string == '+' ||
458
9
	    *n->child->string == '-')
459
738
		return;
460
9
	mandoc_asprintf(&s, "+%s", n->child->string);
461
9
	free(n->child->string);
462
9
	n->child->string = s;
463
756
}
464
465
static void
466
post_vs(CHKARGS)
467
{
468
469
121154
	if (NULL != n->prev)
470
		return;
471
472

58836
	switch (n->parent->tok) {
473
	case MAN_SH:
474
	case MAN_SS:
475
	case MAN_PP:
476
	case MAN_LP:
477
	case MAN_P:
478
736
		mandoc_vmsg(MANDOCERR_PAR_SKIP, man->parse, n->line, n->pos,
479
368
		    "%s after %s", roff_name[n->tok],
480
368
		    roff_name[n->parent->tok]);
481
		/* FALLTHROUGH */
482
	case TOKEN_NONE:
483
		/*
484
		 * Don't warn about this because it occurs in pod2man
485
		 * and would cause considerable (unfixable) warnage.
486
		 */
487
2771
		roff_node_delete(man, n);
488
2771
		break;
489
	default:
490
		break;
491
	}
492
60577
}