]> git.cameronkatri.com Git - mandoc.git/blob - man_validate.c
in the SYNOPSIS, add .Fo and first .Fn arguments to the names table
[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, 2013, 2014 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 AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR 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 "man.h"
32 #include "mandoc.h"
33 #include "mandoc_aux.h"
34 #include "libman.h"
35 #include "libmandoc.h"
36
37 #define CHKARGS struct man *man, struct man_node *n
38
39 typedef void (*v_check)(CHKARGS);
40
41 static void check_eq0(CHKARGS);
42 static void check_eq2(CHKARGS);
43 static void check_le1(CHKARGS);
44 static void check_le5(CHKARGS);
45 static void check_par(CHKARGS);
46 static void check_part(CHKARGS);
47 static void check_root(CHKARGS);
48 static void check_text(CHKARGS);
49
50 static void post_AT(CHKARGS);
51 static void post_IP(CHKARGS);
52 static void post_vs(CHKARGS);
53 static void post_fi(CHKARGS);
54 static void post_ft(CHKARGS);
55 static void post_nf(CHKARGS);
56 static void post_TH(CHKARGS);
57 static void post_UC(CHKARGS);
58 static void post_UR(CHKARGS);
59
60 static v_check man_valids[MAN_MAX] = {
61 post_vs, /* br */
62 post_TH, /* TH */
63 NULL, /* SH */
64 NULL, /* SS */
65 NULL, /* TP */
66 check_par, /* LP */
67 check_par, /* PP */
68 check_par, /* P */
69 post_IP, /* IP */
70 NULL, /* HP */
71 NULL, /* SM */
72 NULL, /* SB */
73 NULL, /* BI */
74 NULL, /* IB */
75 NULL, /* BR */
76 NULL, /* RB */
77 NULL, /* R */
78 NULL, /* B */
79 NULL, /* I */
80 NULL, /* IR */
81 NULL, /* RI */
82 check_eq0, /* na */
83 post_vs, /* sp */
84 post_nf, /* nf */
85 post_fi, /* fi */
86 NULL, /* RE */
87 check_part, /* RS */
88 NULL, /* DT */
89 post_UC, /* UC */
90 check_le1, /* PD */
91 post_AT, /* AT */
92 NULL, /* in */
93 post_ft, /* ft */
94 check_eq2, /* OP */
95 post_nf, /* EX */
96 post_fi, /* EE */
97 post_UR, /* UR */
98 NULL, /* UE */
99 NULL, /* ll */
100 };
101
102
103 void
104 man_valid_post(struct man *man)
105 {
106 struct man_node *n;
107 v_check *cp;
108
109 n = man->last;
110 if (n->flags & MAN_VALID)
111 return;
112 n->flags |= MAN_VALID;
113
114 switch (n->type) {
115 case MAN_TEXT:
116 check_text(man, n);
117 break;
118 case MAN_ROOT:
119 check_root(man, n);
120 break;
121 case MAN_EQN:
122 /* FALLTHROUGH */
123 case MAN_TBL:
124 break;
125 default:
126 cp = man_valids + n->tok;
127 if (*cp)
128 (*cp)(man, n);
129 break;
130 }
131 }
132
133 static void
134 check_root(CHKARGS)
135 {
136
137 assert((man->flags & (MAN_BLINE | MAN_ELINE)) == 0);
138
139 if (NULL == man->first->child)
140 mandoc_msg(MANDOCERR_DOC_EMPTY, man->parse,
141 n->line, n->pos, NULL);
142 else
143 man->meta.hasbody = 1;
144
145 if (NULL == man->meta.title) {
146 mandoc_msg(MANDOCERR_TH_NOTITLE, man->parse,
147 n->line, n->pos, NULL);
148
149 /*
150 * If a title hasn't been set, do so now (by
151 * implication, date and section also aren't set).
152 */
153
154 man->meta.title = mandoc_strdup("");
155 man->meta.msec = mandoc_strdup("");
156 man->meta.date = man->quick ? mandoc_strdup("") :
157 mandoc_normdate(man->parse, NULL, n->line, n->pos);
158 }
159 }
160
161 static void
162 check_text(CHKARGS)
163 {
164 char *cp, *p;
165
166 if (MAN_LITERAL & man->flags)
167 return;
168
169 cp = n->string;
170 for (p = cp; NULL != (p = strchr(p, '\t')); p++)
171 mandoc_msg(MANDOCERR_FI_TAB, man->parse,
172 n->line, n->pos + (p - cp), NULL);
173 }
174
175 #define INEQ_DEFINE(x, ineq, name) \
176 static void \
177 check_##name(CHKARGS) \
178 { \
179 if (n->nchild ineq (x)) \
180 return; \
181 mandoc_vmsg(MANDOCERR_ARGCOUNT, man->parse, n->line, n->pos, \
182 "line arguments %s %d (have %d)", \
183 #ineq, (x), n->nchild); \
184 }
185
186 INEQ_DEFINE(0, ==, eq0)
187 INEQ_DEFINE(2, ==, eq2)
188 INEQ_DEFINE(1, <=, le1)
189 INEQ_DEFINE(5, <=, le5)
190
191 static void
192 post_UR(CHKARGS)
193 {
194
195 if (MAN_HEAD == n->type && 1 != n->nchild)
196 mandoc_vmsg(MANDOCERR_ARGCOUNT, man->parse, n->line,
197 n->pos, "line arguments eq 1 (have %d)", n->nchild);
198 check_part(man, n);
199 }
200
201 static void
202 post_ft(CHKARGS)
203 {
204 char *cp;
205 int ok;
206
207 if (0 == n->nchild)
208 return;
209
210 ok = 0;
211 cp = n->child->string;
212 switch (*cp) {
213 case '1':
214 /* FALLTHROUGH */
215 case '2':
216 /* FALLTHROUGH */
217 case '3':
218 /* FALLTHROUGH */
219 case '4':
220 /* FALLTHROUGH */
221 case 'I':
222 /* FALLTHROUGH */
223 case 'P':
224 /* FALLTHROUGH */
225 case 'R':
226 if ('\0' == cp[1])
227 ok = 1;
228 break;
229 case 'B':
230 if ('\0' == cp[1] || ('I' == cp[1] && '\0' == cp[2]))
231 ok = 1;
232 break;
233 case 'C':
234 if ('W' == cp[1] && '\0' == cp[2])
235 ok = 1;
236 break;
237 default:
238 break;
239 }
240
241 if (0 == ok) {
242 mandoc_vmsg(MANDOCERR_FT_BAD, man->parse,
243 n->line, n->pos, "ft %s", cp);
244 *cp = '\0';
245 }
246
247 if (1 < n->nchild)
248 mandoc_vmsg(MANDOCERR_ARGCOUNT, man->parse, n->line,
249 n->pos, "want one child (have %d)", n->nchild);
250 }
251
252 static void
253 check_part(CHKARGS)
254 {
255
256 if (MAN_BODY == n->type && 0 == n->nchild)
257 mandoc_msg(MANDOCERR_ARGCWARN, man->parse, n->line,
258 n->pos, "want children (have none)");
259 }
260
261 static void
262 check_par(CHKARGS)
263 {
264
265 switch (n->type) {
266 case MAN_BLOCK:
267 if (0 == n->body->nchild)
268 man_node_delete(man, n);
269 break;
270 case MAN_BODY:
271 if (0 == n->nchild)
272 mandoc_vmsg(MANDOCERR_PAR_SKIP,
273 man->parse, n->line, n->pos,
274 "%s empty", man_macronames[n->tok]);
275 break;
276 case MAN_HEAD:
277 if (n->nchild)
278 mandoc_vmsg(MANDOCERR_ARG_SKIP,
279 man->parse, n->line, n->pos,
280 "%s %s%s", man_macronames[n->tok],
281 n->child->string,
282 n->nchild > 1 ? " ..." : "");
283 break;
284 default:
285 break;
286 }
287 }
288
289 static void
290 post_IP(CHKARGS)
291 {
292
293 switch (n->type) {
294 case MAN_BLOCK:
295 if (0 == n->head->nchild && 0 == n->body->nchild)
296 man_node_delete(man, n);
297 break;
298 case MAN_BODY:
299 if (0 == n->parent->head->nchild && 0 == n->nchild)
300 mandoc_vmsg(MANDOCERR_PAR_SKIP,
301 man->parse, n->line, n->pos,
302 "%s empty", man_macronames[n->tok]);
303 break;
304 default:
305 break;
306 }
307 }
308
309 static void
310 post_TH(CHKARGS)
311 {
312 struct man_node *nb;
313 const char *p;
314
315 check_le5(man, n);
316
317 free(man->meta.title);
318 free(man->meta.vol);
319 free(man->meta.source);
320 free(man->meta.msec);
321 free(man->meta.date);
322
323 man->meta.title = man->meta.vol = man->meta.date =
324 man->meta.msec = man->meta.source = NULL;
325
326 nb = n;
327
328 /* ->TITLE<- MSEC DATE SOURCE VOL */
329
330 n = n->child;
331 if (n && n->string) {
332 for (p = n->string; '\0' != *p; p++) {
333 /* Only warn about this once... */
334 if (isalpha((unsigned char)*p) &&
335 ! isupper((unsigned char)*p)) {
336 mandoc_vmsg(MANDOCERR_TITLE_CASE,
337 man->parse, n->line,
338 n->pos + (p - n->string),
339 "TH %s", n->string);
340 break;
341 }
342 }
343 man->meta.title = mandoc_strdup(n->string);
344 } else {
345 man->meta.title = mandoc_strdup("");
346 mandoc_msg(MANDOCERR_TH_NOTITLE, man->parse,
347 nb->line, nb->pos, "TH");
348 }
349
350 /* TITLE ->MSEC<- DATE SOURCE VOL */
351
352 if (n)
353 n = n->next;
354 if (n && n->string)
355 man->meta.msec = mandoc_strdup(n->string);
356 else {
357 man->meta.msec = mandoc_strdup("");
358 mandoc_vmsg(MANDOCERR_MSEC_MISSING, man->parse,
359 nb->line, nb->pos, "TH %s", man->meta.title);
360 }
361
362 /* TITLE MSEC ->DATE<- SOURCE VOL */
363
364 if (n)
365 n = n->next;
366 if (n && n->string && '\0' != n->string[0]) {
367 man->meta.date = man->quick ?
368 mandoc_strdup(n->string) :
369 mandoc_normdate(man->parse, n->string,
370 n->line, n->pos);
371 } else {
372 man->meta.date = mandoc_strdup("");
373 mandoc_msg(MANDOCERR_DATE_MISSING, man->parse,
374 n ? n->line : nb->line,
375 n ? n->pos : nb->pos, "TH");
376 }
377
378 /* TITLE MSEC DATE ->SOURCE<- VOL */
379
380 if (n && (n = n->next))
381 man->meta.source = mandoc_strdup(n->string);
382
383 /* TITLE MSEC DATE SOURCE ->VOL<- */
384 /* If missing, use the default VOL name for MSEC. */
385
386 if (n && (n = n->next))
387 man->meta.vol = mandoc_strdup(n->string);
388 else if ('\0' != man->meta.msec[0] &&
389 (NULL != (p = mandoc_a2msec(man->meta.msec))))
390 man->meta.vol = mandoc_strdup(p);
391
392 /*
393 * Remove the `TH' node after we've processed it for our
394 * meta-data.
395 */
396 man_node_delete(man, man->last);
397 }
398
399 static void
400 post_nf(CHKARGS)
401 {
402
403 check_eq0(man, n);
404
405 if (MAN_LITERAL & man->flags)
406 mandoc_msg(MANDOCERR_NF_SKIP, man->parse,
407 n->line, n->pos, "nf");
408
409 man->flags |= MAN_LITERAL;
410 }
411
412 static void
413 post_fi(CHKARGS)
414 {
415
416 check_eq0(man, n);
417
418 if ( ! (MAN_LITERAL & man->flags))
419 mandoc_msg(MANDOCERR_FI_SKIP, man->parse,
420 n->line, n->pos, "fi");
421
422 man->flags &= ~MAN_LITERAL;
423 }
424
425 static void
426 post_UC(CHKARGS)
427 {
428 static const char * const bsd_versions[] = {
429 "3rd Berkeley Distribution",
430 "4th Berkeley Distribution",
431 "4.2 Berkeley Distribution",
432 "4.3 Berkeley Distribution",
433 "4.4 Berkeley Distribution",
434 };
435
436 const char *p, *s;
437
438 n = n->child;
439
440 if (NULL == n || MAN_TEXT != n->type)
441 p = bsd_versions[0];
442 else {
443 s = n->string;
444 if (0 == strcmp(s, "3"))
445 p = bsd_versions[0];
446 else if (0 == strcmp(s, "4"))
447 p = bsd_versions[1];
448 else if (0 == strcmp(s, "5"))
449 p = bsd_versions[2];
450 else if (0 == strcmp(s, "6"))
451 p = bsd_versions[3];
452 else if (0 == strcmp(s, "7"))
453 p = bsd_versions[4];
454 else
455 p = bsd_versions[0];
456 }
457
458 free(man->meta.source);
459 man->meta.source = mandoc_strdup(p);
460 }
461
462 static void
463 post_AT(CHKARGS)
464 {
465 static const char * const unix_versions[] = {
466 "7th Edition",
467 "System III",
468 "System V",
469 "System V Release 2",
470 };
471
472 const char *p, *s;
473 struct man_node *nn;
474
475 n = n->child;
476
477 if (NULL == n || MAN_TEXT != n->type)
478 p = unix_versions[0];
479 else {
480 s = n->string;
481 if (0 == strcmp(s, "3"))
482 p = unix_versions[0];
483 else if (0 == strcmp(s, "4"))
484 p = unix_versions[1];
485 else if (0 == strcmp(s, "5")) {
486 nn = n->next;
487 if (nn && MAN_TEXT == nn->type && nn->string[0])
488 p = unix_versions[3];
489 else
490 p = unix_versions[2];
491 } else
492 p = unix_versions[0];
493 }
494
495 free(man->meta.source);
496 man->meta.source = mandoc_strdup(p);
497 }
498
499 static void
500 post_vs(CHKARGS)
501 {
502
503 if (n->tok == MAN_br)
504 check_eq0(man, n);
505 else
506 check_le1(man, n);
507
508 if (NULL != n->prev)
509 return;
510
511 switch (n->parent->tok) {
512 case MAN_SH:
513 /* FALLTHROUGH */
514 case MAN_SS:
515 mandoc_vmsg(MANDOCERR_PAR_SKIP, man->parse, n->line, n->pos,
516 "%s after %s", man_macronames[n->tok],
517 man_macronames[n->parent->tok]);
518 /* FALLTHROUGH */
519 case MAN_MAX:
520 /*
521 * Don't warn about this because it occurs in pod2man
522 * and would cause considerable (unfixable) warnage.
523 */
524 man_node_delete(man, n);
525 break;
526 default:
527 break;
528 }
529 }