]> git.cameronkatri.com Git - mandoc.git/blob - man_validate.c
Fix incorrectly swapped labels of left/right arrows; from bentley@.
[mandoc.git] / man_validate.c
1 /* $OpenBSD$ */
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 "config.h"
19
20 #include <sys/types.h>
21
22 #include <assert.h>
23 #include <ctype.h>
24 #include <errno.h>
25 #include <limits.h>
26 #include <stdarg.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <time.h>
30
31 #include "mandoc_aux.h"
32 #include "mandoc.h"
33 #include "roff.h"
34 #include "man.h"
35 #include "libmandoc.h"
36 #include "roff_int.h"
37 #include "libman.h"
38
39 #define CHKARGS struct roff_man *man, struct roff_node *n
40
41 typedef void (*v_check)(CHKARGS);
42
43 static void check_par(CHKARGS);
44 static void check_part(CHKARGS);
45 static void check_root(CHKARGS);
46 static void check_text(CHKARGS);
47
48 static void post_AT(CHKARGS);
49 static void post_IP(CHKARGS);
50 static void post_OP(CHKARGS);
51 static void post_TH(CHKARGS);
52 static void post_UC(CHKARGS);
53 static void post_UR(CHKARGS);
54 static void post_in(CHKARGS);
55 static void post_vs(CHKARGS);
56
57 static const v_check __man_valids[MAN_MAX - MAN_TH] = {
58 post_TH, /* TH */
59 NULL, /* SH */
60 NULL, /* SS */
61 NULL, /* TP */
62 check_par, /* LP */
63 check_par, /* PP */
64 check_par, /* P */
65 post_IP, /* IP */
66 NULL, /* HP */
67 NULL, /* SM */
68 NULL, /* SB */
69 NULL, /* BI */
70 NULL, /* IB */
71 NULL, /* BR */
72 NULL, /* RB */
73 NULL, /* R */
74 NULL, /* B */
75 NULL, /* I */
76 NULL, /* IR */
77 NULL, /* RI */
78 NULL, /* nf */
79 NULL, /* fi */
80 NULL, /* RE */
81 check_part, /* RS */
82 NULL, /* DT */
83 post_UC, /* UC */
84 NULL, /* PD */
85 post_AT, /* AT */
86 post_in, /* in */
87 post_OP, /* OP */
88 NULL, /* EX */
89 NULL, /* EE */
90 post_UR, /* UR */
91 NULL, /* UE */
92 post_UR, /* MT */
93 NULL, /* ME */
94 };
95 static const v_check *man_valids = __man_valids - MAN_TH;
96
97
98 void
99 man_node_validate(struct roff_man *man)
100 {
101 struct roff_node *n;
102 const v_check *cp;
103
104 n = man->last;
105 man->last = man->last->child;
106 while (man->last != NULL) {
107 man_node_validate(man);
108 if (man->last == n)
109 man->last = man->last->child;
110 else
111 man->last = man->last->next;
112 }
113
114 man->last = n;
115 man->next = ROFF_NEXT_SIBLING;
116 switch (n->type) {
117 case ROFFT_TEXT:
118 check_text(man, n);
119 break;
120 case ROFFT_ROOT:
121 check_root(man, n);
122 break;
123 case ROFFT_EQN:
124 case ROFFT_TBL:
125 break;
126 default:
127 if (n->tok < ROFF_MAX) {
128 switch (n->tok) {
129 case ROFF_br:
130 case ROFF_sp:
131 post_vs(man, n);
132 break;
133 default:
134 roff_validate(man);
135 break;
136 }
137 break;
138 }
139 assert(n->tok >= MAN_TH && n->tok < MAN_MAX);
140 cp = man_valids + n->tok;
141 if (*cp)
142 (*cp)(man, n);
143 if (man->last == n)
144 man_state(man, n);
145 break;
146 }
147 }
148
149 static void
150 check_root(CHKARGS)
151 {
152
153 assert((man->flags & (MAN_BLINE | MAN_ELINE)) == 0);
154
155 if (NULL == man->first->child)
156 mandoc_msg(MANDOCERR_DOC_EMPTY, man->parse,
157 n->line, n->pos, NULL);
158 else
159 man->meta.hasbody = 1;
160
161 if (NULL == man->meta.title) {
162 mandoc_msg(MANDOCERR_TH_NOTITLE, man->parse,
163 n->line, n->pos, NULL);
164
165 /*
166 * If a title hasn't been set, do so now (by
167 * implication, date and section also aren't set).
168 */
169
170 man->meta.title = mandoc_strdup("");
171 man->meta.msec = mandoc_strdup("");
172 man->meta.date = man->quick ? mandoc_strdup("") :
173 mandoc_normdate(man, NULL, n->line, n->pos);
174 }
175
176 if (man->meta.os_e &&
177 (man->meta.rcsids & (1 << man->meta.os_e)) == 0)
178 mandoc_msg(MANDOCERR_RCS_MISSING, man->parse, 0, 0,
179 man->meta.os_e == MANDOC_OS_OPENBSD ?
180 "(OpenBSD)" : "(NetBSD)");
181 }
182
183 static void
184 check_text(CHKARGS)
185 {
186 char *cp, *p;
187
188 if (MAN_LITERAL & man->flags)
189 return;
190
191 cp = n->string;
192 for (p = cp; NULL != (p = strchr(p, '\t')); p++)
193 mandoc_msg(MANDOCERR_FI_TAB, man->parse,
194 n->line, n->pos + (p - cp), NULL);
195 }
196
197 static void
198 post_OP(CHKARGS)
199 {
200
201 if (n->child == NULL)
202 mandoc_msg(MANDOCERR_OP_EMPTY, man->parse,
203 n->line, n->pos, "OP");
204 else if (n->child->next != NULL && n->child->next->next != NULL) {
205 n = n->child->next->next;
206 mandoc_vmsg(MANDOCERR_ARG_EXCESS, man->parse,
207 n->line, n->pos, "OP ... %s", n->string);
208 }
209 }
210
211 static void
212 post_UR(CHKARGS)
213 {
214
215 if (n->type == ROFFT_HEAD && n->child == NULL)
216 mandoc_vmsg(MANDOCERR_UR_NOHEAD, man->parse,
217 n->line, n->pos, roff_name[n->tok]);
218 check_part(man, n);
219 }
220
221 static void
222 check_part(CHKARGS)
223 {
224
225 if (n->type == ROFFT_BODY && n->child == NULL)
226 mandoc_msg(MANDOCERR_BLK_EMPTY, man->parse,
227 n->line, n->pos, roff_name[n->tok]);
228 }
229
230 static void
231 check_par(CHKARGS)
232 {
233
234 switch (n->type) {
235 case ROFFT_BLOCK:
236 if (n->body->child == NULL)
237 roff_node_delete(man, n);
238 break;
239 case ROFFT_BODY:
240 if (n->child == NULL)
241 mandoc_vmsg(MANDOCERR_PAR_SKIP,
242 man->parse, n->line, n->pos,
243 "%s empty", roff_name[n->tok]);
244 break;
245 case ROFFT_HEAD:
246 if (n->child != NULL)
247 mandoc_vmsg(MANDOCERR_ARG_SKIP,
248 man->parse, n->line, n->pos, "%s %s%s",
249 roff_name[n->tok], n->child->string,
250 n->child->next != NULL ? " ..." : "");
251 break;
252 default:
253 break;
254 }
255 }
256
257 static void
258 post_IP(CHKARGS)
259 {
260
261 switch (n->type) {
262 case ROFFT_BLOCK:
263 if (n->head->child == NULL && n->body->child == NULL)
264 roff_node_delete(man, n);
265 break;
266 case ROFFT_BODY:
267 if (n->parent->head->child == NULL && n->child == NULL)
268 mandoc_vmsg(MANDOCERR_PAR_SKIP,
269 man->parse, n->line, n->pos,
270 "%s empty", roff_name[n->tok]);
271 break;
272 default:
273 break;
274 }
275 }
276
277 static void
278 post_TH(CHKARGS)
279 {
280 struct roff_node *nb;
281 const char *p;
282
283 free(man->meta.title);
284 free(man->meta.vol);
285 free(man->meta.os);
286 free(man->meta.msec);
287 free(man->meta.date);
288
289 man->meta.title = man->meta.vol = man->meta.date =
290 man->meta.msec = man->meta.os = NULL;
291
292 nb = n;
293
294 /* ->TITLE<- MSEC DATE OS VOL */
295
296 n = n->child;
297 if (n && n->string) {
298 for (p = n->string; '\0' != *p; p++) {
299 /* Only warn about this once... */
300 if (isalpha((unsigned char)*p) &&
301 ! isupper((unsigned char)*p)) {
302 mandoc_vmsg(MANDOCERR_TITLE_CASE,
303 man->parse, n->line,
304 n->pos + (p - n->string),
305 "TH %s", n->string);
306 break;
307 }
308 }
309 man->meta.title = mandoc_strdup(n->string);
310 } else {
311 man->meta.title = mandoc_strdup("");
312 mandoc_msg(MANDOCERR_TH_NOTITLE, man->parse,
313 nb->line, nb->pos, "TH");
314 }
315
316 /* TITLE ->MSEC<- DATE OS VOL */
317
318 if (n)
319 n = n->next;
320 if (n && n->string)
321 man->meta.msec = mandoc_strdup(n->string);
322 else {
323 man->meta.msec = mandoc_strdup("");
324 mandoc_vmsg(MANDOCERR_MSEC_MISSING, man->parse,
325 nb->line, nb->pos, "TH %s", man->meta.title);
326 }
327
328 /* TITLE MSEC ->DATE<- OS VOL */
329
330 if (n)
331 n = n->next;
332 if (n && n->string && '\0' != n->string[0]) {
333 man->meta.date = man->quick ?
334 mandoc_strdup(n->string) :
335 mandoc_normdate(man, n->string, n->line, n->pos);
336 } else {
337 man->meta.date = mandoc_strdup("");
338 mandoc_msg(MANDOCERR_DATE_MISSING, man->parse,
339 n ? n->line : nb->line,
340 n ? n->pos : nb->pos, "TH");
341 }
342
343 /* TITLE MSEC DATE ->OS<- VOL */
344
345 if (n && (n = n->next))
346 man->meta.os = mandoc_strdup(n->string);
347 else if (man->os_s != NULL)
348 man->meta.os = mandoc_strdup(man->os_s);
349 if (man->meta.os_e == MANDOC_OS_OTHER && man->meta.os != NULL) {
350 if (strstr(man->meta.os, "OpenBSD") != NULL)
351 man->meta.os_e = MANDOC_OS_OPENBSD;
352 else if (strstr(man->meta.os, "NetBSD") != NULL)
353 man->meta.os_e = MANDOC_OS_NETBSD;
354 }
355
356 /* TITLE MSEC DATE OS ->VOL<- */
357 /* If missing, use the default VOL name for MSEC. */
358
359 if (n && (n = n->next))
360 man->meta.vol = mandoc_strdup(n->string);
361 else if ('\0' != man->meta.msec[0] &&
362 (NULL != (p = mandoc_a2msec(man->meta.msec))))
363 man->meta.vol = mandoc_strdup(p);
364
365 if (n != NULL && (n = n->next) != NULL)
366 mandoc_vmsg(MANDOCERR_ARG_EXCESS, man->parse,
367 n->line, n->pos, "TH ... %s", n->string);
368
369 /*
370 * Remove the `TH' node after we've processed it for our
371 * meta-data.
372 */
373 roff_node_delete(man, man->last);
374 }
375
376 static void
377 post_UC(CHKARGS)
378 {
379 static const char * const bsd_versions[] = {
380 "3rd Berkeley Distribution",
381 "4th Berkeley Distribution",
382 "4.2 Berkeley Distribution",
383 "4.3 Berkeley Distribution",
384 "4.4 Berkeley Distribution",
385 };
386
387 const char *p, *s;
388
389 n = n->child;
390
391 if (n == NULL || n->type != ROFFT_TEXT)
392 p = bsd_versions[0];
393 else {
394 s = n->string;
395 if (0 == strcmp(s, "3"))
396 p = bsd_versions[0];
397 else if (0 == strcmp(s, "4"))
398 p = bsd_versions[1];
399 else if (0 == strcmp(s, "5"))
400 p = bsd_versions[2];
401 else if (0 == strcmp(s, "6"))
402 p = bsd_versions[3];
403 else if (0 == strcmp(s, "7"))
404 p = bsd_versions[4];
405 else
406 p = bsd_versions[0];
407 }
408
409 free(man->meta.os);
410 man->meta.os = mandoc_strdup(p);
411 }
412
413 static void
414 post_AT(CHKARGS)
415 {
416 static const char * const unix_versions[] = {
417 "7th Edition",
418 "System III",
419 "System V",
420 "System V Release 2",
421 };
422
423 struct roff_node *nn;
424 const char *p, *s;
425
426 n = n->child;
427
428 if (n == NULL || n->type != ROFFT_TEXT)
429 p = unix_versions[0];
430 else {
431 s = n->string;
432 if (0 == strcmp(s, "3"))
433 p = unix_versions[0];
434 else if (0 == strcmp(s, "4"))
435 p = unix_versions[1];
436 else if (0 == strcmp(s, "5")) {
437 nn = n->next;
438 if (nn != NULL &&
439 nn->type == ROFFT_TEXT &&
440 nn->string[0] != '\0')
441 p = unix_versions[3];
442 else
443 p = unix_versions[2];
444 } else
445 p = unix_versions[0];
446 }
447
448 free(man->meta.os);
449 man->meta.os = mandoc_strdup(p);
450 }
451
452 static void
453 post_in(CHKARGS)
454 {
455 char *s;
456
457 if (n->parent->tok != MAN_TP ||
458 n->parent->type != ROFFT_HEAD ||
459 n->child == NULL ||
460 *n->child->string == '+' ||
461 *n->child->string == '-')
462 return;
463 mandoc_asprintf(&s, "+%s", n->child->string);
464 free(n->child->string);
465 n->child->string = s;
466 }
467
468 static void
469 post_vs(CHKARGS)
470 {
471
472 if (NULL != n->prev)
473 return;
474
475 switch (n->parent->tok) {
476 case MAN_SH:
477 case MAN_SS:
478 case MAN_PP:
479 case MAN_LP:
480 case MAN_P:
481 mandoc_vmsg(MANDOCERR_PAR_SKIP, man->parse, n->line, n->pos,
482 "%s after %s", roff_name[n->tok],
483 roff_name[n->parent->tok]);
484 /* FALLTHROUGH */
485 case TOKEN_NONE:
486 /*
487 * Don't warn about this because it occurs in pod2man
488 * and would cause considerable (unfixable) warnage.
489 */
490 roff_node_delete(man, n);
491 break;
492 default:
493 break;
494 }
495 }