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