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