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