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