Annotation of mandoc/man_validate.c, Revision 1.90
1.90 ! schwarze 1: /* $Id: man_validate.c,v 1.89 2014/03/23 11:25:26 schwarze Exp $ */
1.1 kristaps 2: /*
1.63 schwarze 3: * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
1.88 schwarze 4: * Copyright (c) 2010, 2012, 2013, 2014 Ingo Schwarze <schwarze@openbsd.org>
1.1 kristaps 5: *
6: * Permission to use, copy, modify, and distribute this software for any
1.8 kristaps 7: * purpose with or without fee is hereby granted, provided that the above
8: * copyright notice and this permission notice appear in all copies.
1.1 kristaps 9: *
1.8 kristaps 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.
1.1 kristaps 17: */
1.28 kristaps 18: #ifdef HAVE_CONFIG_H
19: #include "config.h"
20: #endif
21:
1.1 kristaps 22: #include <sys/types.h>
23:
24: #include <assert.h>
25: #include <ctype.h>
1.16 kristaps 26: #include <errno.h>
27: #include <limits.h>
1.1 kristaps 28: #include <stdarg.h>
29: #include <stdlib.h>
1.46 kristaps 30: #include <string.h>
1.50 kristaps 31: #include <time.h>
1.1 kristaps 32:
1.66 kristaps 33: #include "man.h"
1.41 kristaps 34: #include "mandoc.h"
1.89 schwarze 35: #include "mandoc_aux.h"
1.1 kristaps 36: #include "libman.h"
1.15 kristaps 37: #include "libmandoc.h"
1.1 kristaps 38:
1.85 schwarze 39: #define CHKARGS struct man *man, struct man_node *n
1.1 kristaps 40:
1.17 kristaps 41: typedef int (*v_check)(CHKARGS);
1.1 kristaps 42:
43: struct man_valid {
1.17 kristaps 44: v_check *pres;
45: v_check *posts;
1.1 kristaps 46: };
47:
1.17 kristaps 48: static int check_eq0(CHKARGS);
1.80 kristaps 49: static int check_eq2(CHKARGS);
1.25 kristaps 50: static int check_le1(CHKARGS);
1.17 kristaps 51: static int check_ge2(CHKARGS);
52: static int check_le5(CHKARGS);
1.86 schwarze 53: static int check_head1(CHKARGS);
1.17 kristaps 54: static int check_par(CHKARGS);
1.23 kristaps 55: static int check_part(CHKARGS);
1.17 kristaps 56: static int check_root(CHKARGS);
1.75 kristaps 57: static void check_text(CHKARGS);
1.17 kristaps 58:
1.51 kristaps 59: static int post_AT(CHKARGS);
1.83 schwarze 60: static int post_IP(CHKARGS);
1.70 kristaps 61: static int post_vs(CHKARGS);
1.51 kristaps 62: static int post_fi(CHKARGS);
1.73 kristaps 63: static int post_ft(CHKARGS);
1.51 kristaps 64: static int post_nf(CHKARGS);
1.73 kristaps 65: static int post_sec(CHKARGS);
1.51 kristaps 66: static int post_TH(CHKARGS);
67: static int post_UC(CHKARGS);
1.73 kristaps 68: static int pre_sec(CHKARGS);
1.51 kristaps 69:
70: static v_check posts_at[] = { post_AT, NULL };
1.70 kristaps 71: static v_check posts_br[] = { post_vs, check_eq0, NULL };
1.17 kristaps 72: static v_check posts_eq0[] = { check_eq0, NULL };
1.80 kristaps 73: static v_check posts_eq2[] = { check_eq2, NULL };
1.51 kristaps 74: static v_check posts_fi[] = { check_eq0, post_fi, NULL };
1.73 kristaps 75: static v_check posts_ft[] = { post_ft, NULL };
1.83 schwarze 76: static v_check posts_ip[] = { post_IP, NULL };
1.84 schwarze 77: static v_check posts_le1[] = { check_le1, NULL };
1.51 kristaps 78: static v_check posts_nf[] = { check_eq0, post_nf, NULL };
1.17 kristaps 79: static v_check posts_par[] = { check_par, NULL };
1.23 kristaps 80: static v_check posts_part[] = { check_part, NULL };
1.73 kristaps 81: static v_check posts_sec[] = { post_sec, NULL };
1.70 kristaps 82: static v_check posts_sp[] = { post_vs, check_le1, NULL };
1.60 schwarze 83: static v_check posts_th[] = { check_ge2, check_le5, post_TH, NULL };
1.51 kristaps 84: static v_check posts_uc[] = { post_UC, NULL };
1.86 schwarze 85: static v_check posts_ur[] = { check_head1, check_part, NULL };
1.78 schwarze 86: static v_check pres_sec[] = { pre_sec, NULL };
1.1 kristaps 87:
88: static const struct man_valid man_valids[MAN_MAX] = {
1.70 kristaps 89: { NULL, posts_br }, /* br */
1.78 schwarze 90: { NULL, posts_th }, /* TH */
1.73 kristaps 91: { pres_sec, posts_sec }, /* SH */
92: { pres_sec, posts_sec }, /* SS */
1.78 schwarze 93: { NULL, NULL }, /* TP */
94: { NULL, posts_par }, /* LP */
95: { NULL, posts_par }, /* PP */
96: { NULL, posts_par }, /* P */
1.83 schwarze 97: { NULL, posts_ip }, /* IP */
1.78 schwarze 98: { NULL, NULL }, /* HP */
1.22 kristaps 99: { NULL, NULL }, /* SM */
100: { NULL, NULL }, /* SB */
1.17 kristaps 101: { NULL, NULL }, /* BI */
102: { NULL, NULL }, /* IB */
103: { NULL, NULL }, /* BR */
104: { NULL, NULL }, /* RB */
1.22 kristaps 105: { NULL, NULL }, /* R */
106: { NULL, NULL }, /* B */
107: { NULL, NULL }, /* I */
1.17 kristaps 108: { NULL, NULL }, /* IR */
109: { NULL, NULL }, /* RI */
1.80 kristaps 110: { NULL, posts_eq0 }, /* na */
111: { NULL, posts_sp }, /* sp */
1.78 schwarze 112: { NULL, posts_nf }, /* nf */
113: { NULL, posts_fi }, /* fi */
1.19 kristaps 114: { NULL, NULL }, /* RE */
1.23 kristaps 115: { NULL, posts_part }, /* RS */
1.21 kristaps 116: { NULL, NULL }, /* DT */
1.51 kristaps 117: { NULL, posts_uc }, /* UC */
1.84 schwarze 118: { NULL, posts_le1 }, /* PD */
1.51 kristaps 119: { NULL, posts_at }, /* AT */
1.47 kristaps 120: { NULL, NULL }, /* in */
1.55 kristaps 121: { NULL, posts_ft }, /* ft */
1.80 kristaps 122: { NULL, posts_eq2 }, /* OP */
1.81 schwarze 123: { NULL, posts_nf }, /* EX */
124: { NULL, posts_fi }, /* EE */
1.86 schwarze 125: { NULL, posts_ur }, /* UR */
126: { NULL, NULL }, /* UE */
1.90 ! schwarze 127: { NULL, NULL }, /* ll */
1.1 kristaps 128: };
129:
130:
131: int
1.85 schwarze 132: man_valid_pre(struct man *man, struct man_node *n)
1.17 kristaps 133: {
134: v_check *cp;
135:
1.57 kristaps 136: switch (n->type) {
137: case (MAN_TEXT):
138: /* FALLTHROUGH */
139: case (MAN_ROOT):
1.61 kristaps 140: /* FALLTHROUGH */
141: case (MAN_EQN):
1.57 kristaps 142: /* FALLTHROUGH */
143: case (MAN_TBL):
1.17 kristaps 144: return(1);
1.57 kristaps 145: default:
146: break;
147: }
1.17 kristaps 148:
149: if (NULL == (cp = man_valids[n->tok].pres))
150: return(1);
151: for ( ; *cp; cp++)
1.85 schwarze 152: if ( ! (*cp)(man, n))
1.17 kristaps 153: return(0);
154: return(1);
155: }
156:
157:
158: int
1.85 schwarze 159: man_valid_post(struct man *man)
1.1 kristaps 160: {
1.17 kristaps 161: v_check *cp;
1.1 kristaps 162:
1.85 schwarze 163: if (MAN_VALID & man->last->flags)
1.1 kristaps 164: return(1);
1.85 schwarze 165: man->last->flags |= MAN_VALID;
1.1 kristaps 166:
1.85 schwarze 167: switch (man->last->type) {
1.75 kristaps 168: case (MAN_TEXT):
1.85 schwarze 169: check_text(man, man->last);
1.75 kristaps 170: return(1);
1.1 kristaps 171: case (MAN_ROOT):
1.85 schwarze 172: return(check_root(man, man->last));
1.62 kristaps 173: case (MAN_EQN):
174: /* FALLTHROUGH */
1.57 kristaps 175: case (MAN_TBL):
176: return(1);
1.1 kristaps 177: default:
178: break;
179: }
180:
1.85 schwarze 181: if (NULL == (cp = man_valids[man->last->tok].posts))
1.1 kristaps 182: return(1);
183: for ( ; *cp; cp++)
1.85 schwarze 184: if ( ! (*cp)(man, man->last))
1.1 kristaps 185: return(0);
186:
187: return(1);
188: }
189:
190:
1.11 kristaps 191: static int
1.17 kristaps 192: check_root(CHKARGS)
1.14 kristaps 193: {
1.17 kristaps 194:
1.85 schwarze 195: if (MAN_BLINE & man->flags)
196: man_nmsg(man, n, MANDOCERR_SCOPEEXIT);
197: else if (MAN_ELINE & man->flags)
198: man_nmsg(man, n, MANDOCERR_SCOPEEXIT);
1.22 kristaps 199:
1.85 schwarze 200: man->flags &= ~MAN_BLINE;
201: man->flags &= ~MAN_ELINE;
1.17 kristaps 202:
1.85 schwarze 203: if (NULL == man->first->child) {
204: man_nmsg(man, n, MANDOCERR_NODOCBODY);
1.41 kristaps 205: return(0);
1.85 schwarze 206: } else if (NULL == man->meta.title) {
207: man_nmsg(man, n, MANDOCERR_NOTITLE);
1.54 kristaps 208:
1.34 kristaps 209: /*
210: * If a title hasn't been set, do so now (by
211: * implication, date and section also aren't set).
212: */
1.54 kristaps 213:
1.85 schwarze 214: man->meta.title = mandoc_strdup("unknown");
215: man->meta.msec = mandoc_strdup("1");
1.88 schwarze 216: man->meta.date = man->quick ? mandoc_strdup("") :
217: mandoc_normdate(man->parse, NULL, n->line, n->pos);
1.34 kristaps 218: }
1.32 kristaps 219:
220: return(1);
1.75 kristaps 221: }
222:
223: static void
224: check_text(CHKARGS)
225: {
226: char *cp, *p;
227:
1.85 schwarze 228: if (MAN_LITERAL & man->flags)
1.76 schwarze 229: return;
230:
231: cp = n->string;
232: for (p = cp; NULL != (p = strchr(p, '\t')); p++)
1.85 schwarze 233: man_pmsg(man, n->line, (int)(p - cp), MANDOCERR_BADTAB);
1.1 kristaps 234: }
235:
236: #define INEQ_DEFINE(x, ineq, name) \
237: static int \
1.17 kristaps 238: check_##name(CHKARGS) \
1.1 kristaps 239: { \
1.11 kristaps 240: if (n->nchild ineq (x)) \
1.1 kristaps 241: return(1); \
1.85 schwarze 242: mandoc_vmsg(MANDOCERR_ARGCOUNT, man->parse, n->line, n->pos, \
1.41 kristaps 243: "line arguments %s %d (have %d)", \
244: #ineq, (x), n->nchild); \
1.60 schwarze 245: return(1); \
1.1 kristaps 246: }
247:
248: INEQ_DEFINE(0, ==, eq0)
1.80 kristaps 249: INEQ_DEFINE(2, ==, eq2)
1.25 kristaps 250: INEQ_DEFINE(1, <=, le1)
1.1 kristaps 251: INEQ_DEFINE(2, >=, ge2)
252: INEQ_DEFINE(5, <=, le5)
1.86 schwarze 253:
254: static int
255: check_head1(CHKARGS)
256: {
257:
258: if (MAN_HEAD == n->type && 1 != n->nchild)
259: mandoc_vmsg(MANDOCERR_ARGCOUNT, man->parse, n->line,
260: n->pos, "line arguments eq 1 (have %d)", n->nchild);
261:
262: return(1);
263: }
1.1 kristaps 264:
1.55 kristaps 265: static int
1.73 kristaps 266: post_ft(CHKARGS)
1.55 kristaps 267: {
268: char *cp;
269: int ok;
270:
271: if (0 == n->nchild)
272: return(1);
273:
274: ok = 0;
275: cp = n->child->string;
276: switch (*cp) {
277: case ('1'):
278: /* FALLTHROUGH */
279: case ('2'):
280: /* FALLTHROUGH */
281: case ('3'):
282: /* FALLTHROUGH */
283: case ('4'):
284: /* FALLTHROUGH */
285: case ('I'):
286: /* FALLTHROUGH */
287: case ('P'):
288: /* FALLTHROUGH */
289: case ('R'):
290: if ('\0' == cp[1])
291: ok = 1;
292: break;
293: case ('B'):
294: if ('\0' == cp[1] || ('I' == cp[1] && '\0' == cp[2]))
295: ok = 1;
296: break;
297: case ('C'):
298: if ('W' == cp[1] && '\0' == cp[2])
299: ok = 1;
300: break;
301: default:
302: break;
303: }
304:
305: if (0 == ok) {
1.65 kristaps 306: mandoc_vmsg
1.85 schwarze 307: (MANDOCERR_BADFONT, man->parse,
1.65 kristaps 308: n->line, n->pos, "%s", cp);
1.55 kristaps 309: *cp = '\0';
310: }
311:
312: if (1 < n->nchild)
1.65 kristaps 313: mandoc_vmsg
1.85 schwarze 314: (MANDOCERR_ARGCOUNT, man->parse, n->line,
1.65 kristaps 315: n->pos, "want one child (have %d)",
316: n->nchild);
1.55 kristaps 317:
318: return(1);
319: }
1.16 kristaps 320:
321: static int
1.73 kristaps 322: pre_sec(CHKARGS)
323: {
324:
325: if (MAN_BLOCK == n->type)
1.85 schwarze 326: man->flags &= ~MAN_LITERAL;
1.73 kristaps 327: return(1);
328: }
329:
330: static int
331: post_sec(CHKARGS)
1.17 kristaps 332: {
1.16 kristaps 333:
1.69 kristaps 334: if ( ! (MAN_HEAD == n->type && 0 == n->nchild))
335: return(1);
1.41 kristaps 336:
1.85 schwarze 337: man_nmsg(man, n, MANDOCERR_SYNTARGCOUNT);
1.69 kristaps 338: return(0);
1.16 kristaps 339: }
1.17 kristaps 340:
341: static int
1.23 kristaps 342: check_part(CHKARGS)
343: {
344:
345: if (MAN_BODY == n->type && 0 == n->nchild)
1.85 schwarze 346: mandoc_msg(MANDOCERR_ARGCWARN, man->parse, n->line,
1.67 kristaps 347: n->pos, "want children (have none)");
1.54 kristaps 348:
1.23 kristaps 349: return(1);
350: }
351:
352:
353: static int
1.17 kristaps 354: check_par(CHKARGS)
355: {
356:
1.59 kristaps 357: switch (n->type) {
358: case (MAN_BLOCK):
359: if (0 == n->body->nchild)
1.85 schwarze 360: man_node_delete(man, n);
1.59 kristaps 361: break;
362: case (MAN_BODY):
363: if (0 == n->nchild)
1.85 schwarze 364: man_nmsg(man, n, MANDOCERR_IGNPAR);
1.59 kristaps 365: break;
366: case (MAN_HEAD):
367: if (n->nchild)
1.85 schwarze 368: man_nmsg(man, n, MANDOCERR_ARGSLOST);
1.59 kristaps 369: break;
370: default:
371: break;
372: }
1.17 kristaps 373:
374: return(1);
375: }
376:
1.83 schwarze 377: static int
378: post_IP(CHKARGS)
379: {
380:
381: switch (n->type) {
382: case (MAN_BLOCK):
383: if (0 == n->head->nchild && 0 == n->body->nchild)
1.85 schwarze 384: man_node_delete(man, n);
1.83 schwarze 385: break;
386: case (MAN_BODY):
387: if (0 == n->parent->head->nchild && 0 == n->nchild)
1.85 schwarze 388: man_nmsg(man, n, MANDOCERR_IGNPAR);
1.83 schwarze 389: break;
390: default:
391: break;
392: }
393: return(1);
394: }
1.17 kristaps 395:
1.51 kristaps 396: static int
397: post_TH(CHKARGS)
398: {
1.60 schwarze 399: const char *p;
1.51 kristaps 400:
1.85 schwarze 401: free(man->meta.title);
402: free(man->meta.vol);
403: free(man->meta.source);
404: free(man->meta.msec);
405: free(man->meta.date);
1.51 kristaps 406:
1.85 schwarze 407: man->meta.title = man->meta.vol = man->meta.date =
408: man->meta.msec = man->meta.source = NULL;
1.51 kristaps 409:
410: /* ->TITLE<- MSEC DATE SOURCE VOL */
411:
412: n = n->child;
1.60 schwarze 413: if (n && n->string) {
414: for (p = n->string; '\0' != *p; p++) {
415: /* Only warn about this once... */
1.71 kristaps 416: if (isalpha((unsigned char)*p) &&
417: ! isupper((unsigned char)*p)) {
1.85 schwarze 418: man_nmsg(man, n, MANDOCERR_UPPERCASE);
1.60 schwarze 419: break;
420: }
421: }
1.85 schwarze 422: man->meta.title = mandoc_strdup(n->string);
1.60 schwarze 423: } else
1.85 schwarze 424: man->meta.title = mandoc_strdup("");
1.51 kristaps 425:
426: /* TITLE ->MSEC<- DATE SOURCE VOL */
427:
1.60 schwarze 428: if (n)
429: n = n->next;
430: if (n && n->string)
1.85 schwarze 431: man->meta.msec = mandoc_strdup(n->string);
1.60 schwarze 432: else
1.85 schwarze 433: man->meta.msec = mandoc_strdup("");
1.51 kristaps 434:
435: /* TITLE MSEC ->DATE<- SOURCE VOL */
436:
1.60 schwarze 437: if (n)
438: n = n->next;
1.77 schwarze 439: if (n && n->string && '\0' != n->string[0]) {
1.88 schwarze 440: man->meta.date = man->quick ?
441: mandoc_strdup(n->string) :
442: mandoc_normdate(man->parse, n->string,
443: n->line, n->pos);
1.77 schwarze 444: } else
1.85 schwarze 445: man->meta.date = mandoc_strdup("");
1.51 kristaps 446:
447: /* TITLE MSEC DATE ->SOURCE<- VOL */
448:
449: if (n && (n = n->next))
1.85 schwarze 450: man->meta.source = mandoc_strdup(n->string);
1.51 kristaps 451:
452: /* TITLE MSEC DATE SOURCE ->VOL<- */
1.79 schwarze 453: /* If missing, use the default VOL name for MSEC. */
1.51 kristaps 454:
455: if (n && (n = n->next))
1.85 schwarze 456: man->meta.vol = mandoc_strdup(n->string);
457: else if ('\0' != man->meta.msec[0] &&
458: (NULL != (p = mandoc_a2msec(man->meta.msec))))
459: man->meta.vol = mandoc_strdup(p);
1.51 kristaps 460:
461: /*
462: * Remove the `TH' node after we've processed it for our
463: * meta-data.
464: */
1.85 schwarze 465: man_node_delete(man, man->last);
1.51 kristaps 466: return(1);
467: }
468:
469: static int
470: post_nf(CHKARGS)
471: {
472:
1.85 schwarze 473: if (MAN_LITERAL & man->flags)
474: man_nmsg(man, n, MANDOCERR_SCOPEREP);
1.51 kristaps 475:
1.85 schwarze 476: man->flags |= MAN_LITERAL;
1.51 kristaps 477: return(1);
478: }
479:
480: static int
481: post_fi(CHKARGS)
482: {
483:
1.85 schwarze 484: if ( ! (MAN_LITERAL & man->flags))
485: man_nmsg(man, n, MANDOCERR_WNOSCOPE);
1.51 kristaps 486:
1.85 schwarze 487: man->flags &= ~MAN_LITERAL;
1.51 kristaps 488: return(1);
489: }
490:
491: static int
492: post_UC(CHKARGS)
493: {
494: static const char * const bsd_versions[] = {
495: "3rd Berkeley Distribution",
496: "4th Berkeley Distribution",
497: "4.2 Berkeley Distribution",
498: "4.3 Berkeley Distribution",
499: "4.4 Berkeley Distribution",
500: };
501:
502: const char *p, *s;
503:
504: n = n->child;
505:
506: if (NULL == n || MAN_TEXT != n->type)
507: p = bsd_versions[0];
508: else {
509: s = n->string;
510: if (0 == strcmp(s, "3"))
511: p = bsd_versions[0];
512: else if (0 == strcmp(s, "4"))
513: p = bsd_versions[1];
514: else if (0 == strcmp(s, "5"))
515: p = bsd_versions[2];
516: else if (0 == strcmp(s, "6"))
517: p = bsd_versions[3];
518: else if (0 == strcmp(s, "7"))
519: p = bsd_versions[4];
520: else
521: p = bsd_versions[0];
522: }
523:
1.85 schwarze 524: free(man->meta.source);
525: man->meta.source = mandoc_strdup(p);
1.51 kristaps 526: return(1);
527: }
528:
529: static int
530: post_AT(CHKARGS)
531: {
532: static const char * const unix_versions[] = {
533: "7th Edition",
534: "System III",
535: "System V",
536: "System V Release 2",
537: };
538:
539: const char *p, *s;
540: struct man_node *nn;
541:
542: n = n->child;
543:
544: if (NULL == n || MAN_TEXT != n->type)
545: p = unix_versions[0];
546: else {
547: s = n->string;
548: if (0 == strcmp(s, "3"))
549: p = unix_versions[0];
550: else if (0 == strcmp(s, "4"))
551: p = unix_versions[1];
552: else if (0 == strcmp(s, "5")) {
553: nn = n->next;
554: if (nn && MAN_TEXT == nn->type && nn->string[0])
555: p = unix_versions[3];
556: else
557: p = unix_versions[2];
558: } else
559: p = unix_versions[0];
560: }
561:
1.85 schwarze 562: free(man->meta.source);
563: man->meta.source = mandoc_strdup(p);
1.70 kristaps 564: return(1);
565: }
566:
567: static int
568: post_vs(CHKARGS)
569: {
570:
1.82 schwarze 571: if (NULL != n->prev)
572: return(1);
573:
574: switch (n->parent->tok) {
575: case (MAN_SH):
576: /* FALLTHROUGH */
577: case (MAN_SS):
1.85 schwarze 578: man_nmsg(man, n, MANDOCERR_IGNPAR);
1.82 schwarze 579: /* FALLTHROUGH */
580: case (MAN_MAX):
581: /*
582: * Don't warn about this because it occurs in pod2man
583: * and would cause considerable (unfixable) warnage.
584: */
1.85 schwarze 585: man_node_delete(man, n);
1.82 schwarze 586: break;
587: default:
588: break;
589: }
1.70 kristaps 590:
1.51 kristaps 591: return(1);
592: }
CVSweb