GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usr.bin/mandoc/man_validate.c Lines: 164 198 82.8 %
Date: 2017-11-13 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
1404994
	n = man->last;
103
702497
	man->last = man->last->child;
104
2805466
	while (man->last != NULL) {
105
700236
		man_node_validate(man);
106
700236
		if (man->last == n)
107
3114
			man->last = man->last->child;
108
		else
109
697122
			man->last = man->last->next;
110
	}
111
112
1361989
	man->last = n;
113
1361989
	man->next = ROFF_NEXT_SIBLING;
114

1361989
	switch (n->type) {
115
	case ROFFT_TEXT:
116
378504
		check_text(man, n);
117
378504
		break;
118
	case ROFFT_ROOT:
119
2261
		check_root(man, n);
120
2261
		break;
121
	case ROFFT_EQN:
122
	case ROFFT_TBL:
123
		break;
124
	default:
125
319068
		if (n->tok < ROFF_MAX) {
126
40341
			switch (n->tok) {
127
			case ROFF_br:
128
			case ROFF_sp:
129
18504
				post_vs(man, n);
130
18504
				break;
131
			default:
132
21837
				roff_validate(man);
133
21837
				break;
134
			}
135
			break;
136
		}
137

557454
		assert(n->tok >= MAN_TH && n->tok < MAN_MAX);
138
278727
		cp = man_valids + n->tok;
139
278727
		if (*cp)
140
193706
			(*cp)(man, n);
141
278727
		if (man->last == n)
142
276358
			man_state(man, n);
143
		break;
144
	}
145
702497
}
146
147
static void
148
check_root(CHKARGS)
149
{
150
151
4522
	assert((man->flags & (MAN_BLINE | MAN_ELINE)) == 0);
152
153
2261
	if (NULL == man->first->child)
154
18
		mandoc_msg(MANDOCERR_DOC_EMPTY, man->parse,
155
9
		    n->line, n->pos, NULL);
156
	else
157
2252
		man->meta.hasbody = 1;
158
159
2261
	if (NULL == man->meta.title) {
160
6
		mandoc_msg(MANDOCERR_TH_NOTITLE, man->parse,
161
3
		    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
3
		man->meta.title = mandoc_strdup("");
169
3
		man->meta.msec = mandoc_strdup("");
170
9
		man->meta.date = man->quick ? mandoc_strdup("") :
171
3
		    mandoc_normdate(man, NULL, n->line, n->pos);
172
3
	}
173
174

3573
	if (man->meta.os_e &&
175
1312
	    (man->meta.rcsids & (1 << man->meta.os_e)) == 0)
176
1254
		mandoc_msg(MANDOCERR_RCS_MISSING, man->parse, 0, 0,
177
627
		    man->meta.os_e == MANDOC_OS_OPENBSD ?
178
		    "(OpenBSD)" : "(NetBSD)");
179
2261
}
180
181
static void
182
check_text(CHKARGS)
183
{
184
	char		*cp, *p;
185
186
757008
	if (MAN_LITERAL & man->flags)
187
75580
		return;
188
189
302924
	cp = n->string;
190
606402
	for (p = cp; NULL != (p = strchr(p, '\t')); p++)
191
554
		mandoc_msg(MANDOCERR_FI_TAB, man->parse,
192
277
		    n->line, n->pos + (p - cp), NULL);
193
681428
}
194
195
static void
196
post_OP(CHKARGS)
197
{
198
199
48
	if (n->child == NULL)
200
12
		mandoc_msg(MANDOCERR_OP_EMPTY, man->parse,
201
6
		    n->line, n->pos, "OP");
202

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

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

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

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

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

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

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

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

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

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

4496
	if (n && (n = n->next))
343
1438
		man->meta.os = mandoc_strdup(n->string);
344
820
	else if (man->os_s != NULL)
345
679
		man->meta.os = mandoc_strdup(man->os_s);
346

6633
	if (man->meta.os_e == MANDOC_OS_OTHER && man->meta.os != NULL) {
347
2117
		if (strstr(man->meta.os, "OpenBSD") != NULL)
348
1312
			man->meta.os_e = MANDOC_OS_OPENBSD;
349
805
		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

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

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

3658
	if (n != NULL && (n = n->next) != NULL)
363
12
		mandoc_vmsg(MANDOCERR_ARG_EXCESS, man->parse,
364
6
		    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
2258
	roff_node_delete(man, man->last);
371
2258
}
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
498
	char	*s;
453
454

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

17858
	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
230
		mandoc_vmsg(MANDOCERR_PAR_SKIP, man->parse, n->line, n->pos,
479
115
		    "%s after %s", roff_name[n->tok],
480
115
		    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
850
		roff_node_delete(man, n);
488
850
		break;
489
	default:
490
		break;
491
	}
492
18504
}