Annotation of mandoc/man_validate.c, Revision 1.55
1.55 ! kristaps 1: /* $Id: man_validate.c,v 1.54 2010/12/06 13:56:56 kristaps Exp $ */
1.1 kristaps 2: /*
1.48 schwarze 3: * Copyright (c) 2008, 2009, 2010 Kristaps Dzonsons <kristaps@bsd.lv>
1.1 kristaps 4: *
5: * Permission to use, copy, modify, and distribute this software for any
1.8 kristaps 6: * purpose with or without fee is hereby granted, provided that the above
7: * copyright notice and this permission notice appear in all copies.
1.1 kristaps 8: *
1.8 kristaps 9: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12: * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13: * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14: * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15: * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1.1 kristaps 16: */
1.28 kristaps 17: #ifdef HAVE_CONFIG_H
18: #include "config.h"
19: #endif
20:
1.1 kristaps 21: #include <sys/types.h>
22:
23: #include <assert.h>
24: #include <ctype.h>
1.16 kristaps 25: #include <errno.h>
26: #include <limits.h>
1.1 kristaps 27: #include <stdarg.h>
28: #include <stdlib.h>
1.46 kristaps 29: #include <string.h>
1.50 kristaps 30: #include <time.h>
1.1 kristaps 31:
1.41 kristaps 32: #include "mandoc.h"
1.1 kristaps 33: #include "libman.h"
1.15 kristaps 34: #include "libmandoc.h"
1.1 kristaps 35:
1.43 kristaps 36: #define CHKARGS struct man *m, struct man_node *n
1.1 kristaps 37:
1.17 kristaps 38: typedef int (*v_check)(CHKARGS);
1.1 kristaps 39:
40: struct man_valid {
1.17 kristaps 41: v_check *pres;
42: v_check *posts;
1.1 kristaps 43: };
44:
1.17 kristaps 45: static int check_bline(CHKARGS);
46: static int check_eq0(CHKARGS);
1.55 ! kristaps 47: static int check_ft(CHKARGS);
1.25 kristaps 48: static int check_le1(CHKARGS);
1.17 kristaps 49: static int check_ge2(CHKARGS);
50: static int check_le5(CHKARGS);
51: static int check_par(CHKARGS);
1.23 kristaps 52: static int check_part(CHKARGS);
1.17 kristaps 53: static int check_root(CHKARGS);
54: static int check_sec(CHKARGS);
55: static int check_text(CHKARGS);
1.32 kristaps 56: static int check_title(CHKARGS);
1.17 kristaps 57:
1.51 kristaps 58: static int post_AT(CHKARGS);
59: static int post_fi(CHKARGS);
60: static int post_nf(CHKARGS);
61: static int post_TH(CHKARGS);
62: static int post_UC(CHKARGS);
63:
64: static v_check posts_at[] = { post_AT, NULL };
1.17 kristaps 65: static v_check posts_eq0[] = { check_eq0, NULL };
1.51 kristaps 66: static v_check posts_fi[] = { check_eq0, post_fi, NULL };
67: static v_check posts_le1[] = { check_le1, NULL };
1.55 ! kristaps 68: static v_check posts_ft[] = { check_ft, NULL };
1.51 kristaps 69: static v_check posts_nf[] = { check_eq0, post_nf, NULL };
1.17 kristaps 70: static v_check posts_par[] = { check_par, NULL };
1.23 kristaps 71: static v_check posts_part[] = { check_part, NULL };
1.17 kristaps 72: static v_check posts_sec[] = { check_sec, NULL };
1.51 kristaps 73: static v_check posts_th[] = { check_ge2, check_le5, check_title, post_TH, NULL };
74: static v_check posts_uc[] = { post_UC, NULL };
1.17 kristaps 75: static v_check pres_bline[] = { check_bline, NULL };
1.1 kristaps 76:
1.51 kristaps 77:
1.1 kristaps 78: static const struct man_valid man_valids[MAN_MAX] = {
1.29 kristaps 79: { NULL, posts_eq0 }, /* br */
1.32 kristaps 80: { pres_bline, posts_th }, /* TH */
1.17 kristaps 81: { pres_bline, posts_sec }, /* SH */
82: { pres_bline, posts_sec }, /* SS */
83: { pres_bline, posts_par }, /* TP */
84: { pres_bline, posts_par }, /* LP */
85: { pres_bline, posts_par }, /* PP */
86: { pres_bline, posts_par }, /* P */
87: { pres_bline, posts_par }, /* IP */
88: { pres_bline, posts_par }, /* HP */
1.22 kristaps 89: { NULL, NULL }, /* SM */
90: { NULL, NULL }, /* SB */
1.17 kristaps 91: { NULL, NULL }, /* BI */
92: { NULL, NULL }, /* IB */
93: { NULL, NULL }, /* BR */
94: { NULL, NULL }, /* RB */
1.22 kristaps 95: { NULL, NULL }, /* R */
96: { NULL, NULL }, /* B */
97: { NULL, NULL }, /* I */
1.17 kristaps 98: { NULL, NULL }, /* IR */
99: { NULL, NULL }, /* RI */
1.47 kristaps 100: { NULL, posts_eq0 }, /* na */ /* FIXME: should warn only. */
1.17 kristaps 101: { NULL, NULL }, /* i */
1.47 kristaps 102: { NULL, posts_le1 }, /* sp */ /* FIXME: should warn only. */
1.51 kristaps 103: { pres_bline, posts_nf }, /* nf */
104: { pres_bline, posts_fi }, /* fi */
1.17 kristaps 105: { NULL, NULL }, /* r */
1.19 kristaps 106: { NULL, NULL }, /* RE */
1.23 kristaps 107: { NULL, posts_part }, /* RS */
1.21 kristaps 108: { NULL, NULL }, /* DT */
1.51 kristaps 109: { NULL, posts_uc }, /* UC */
1.26 kristaps 110: { NULL, NULL }, /* PD */
1.51 kristaps 111: { NULL, posts_at }, /* AT */
1.47 kristaps 112: { NULL, NULL }, /* in */
1.55 ! kristaps 113: { NULL, posts_ft }, /* ft */
1.1 kristaps 114: };
115:
116:
117: int
1.43 kristaps 118: man_valid_pre(struct man *m, struct man_node *n)
1.17 kristaps 119: {
120: v_check *cp;
121:
122: if (MAN_TEXT == n->type)
123: return(1);
124: if (MAN_ROOT == n->type)
125: return(1);
126:
127: if (NULL == (cp = man_valids[n->tok].pres))
128: return(1);
129: for ( ; *cp; cp++)
130: if ( ! (*cp)(m, n))
131: return(0);
132: return(1);
133: }
134:
135:
136: int
1.1 kristaps 137: man_valid_post(struct man *m)
138: {
1.17 kristaps 139: v_check *cp;
1.1 kristaps 140:
141: if (MAN_VALID & m->last->flags)
142: return(1);
143: m->last->flags |= MAN_VALID;
144:
145: switch (m->last->type) {
146: case (MAN_TEXT):
1.11 kristaps 147: return(check_text(m, m->last));
1.1 kristaps 148: case (MAN_ROOT):
1.14 kristaps 149: return(check_root(m, m->last));
1.1 kristaps 150: default:
151: break;
152: }
153:
154: if (NULL == (cp = man_valids[m->last->tok].posts))
155: return(1);
156: for ( ; *cp; cp++)
157: if ( ! (*cp)(m, m->last))
158: return(0);
159:
160: return(1);
161: }
162:
163:
1.11 kristaps 164: static int
1.17 kristaps 165: check_root(CHKARGS)
1.14 kristaps 166: {
1.17 kristaps 167:
168: if (MAN_BLINE & m->flags)
1.54 kristaps 169: man_nmsg(m, n, MANDOCERR_SCOPEEXIT);
170: else if (MAN_ELINE & m->flags)
171: man_nmsg(m, n, MANDOCERR_SCOPEEXIT);
1.22 kristaps 172:
173: m->flags &= ~MAN_BLINE;
174: m->flags &= ~MAN_ELINE;
1.17 kristaps 175:
1.41 kristaps 176: if (NULL == m->first->child) {
177: man_nmsg(m, n, MANDOCERR_NODOCBODY);
178: return(0);
179: } else if (NULL == m->meta.title) {
1.54 kristaps 180: man_nmsg(m, n, MANDOCERR_NOTITLE);
181:
1.34 kristaps 182: /*
183: * If a title hasn't been set, do so now (by
184: * implication, date and section also aren't set).
185: */
1.54 kristaps 186:
1.34 kristaps 187: m->meta.title = mandoc_strdup("unknown");
188: m->meta.date = time(NULL);
1.37 kristaps 189: m->meta.msec = mandoc_strdup("1");
1.34 kristaps 190: }
1.32 kristaps 191:
192: return(1);
193: }
194:
195:
196: static int
197: check_title(CHKARGS)
198: {
199: const char *p;
200:
201: assert(n->child);
1.41 kristaps 202: /* FIXME: is this sufficient? */
203: if ('\0' == *n->child->string) {
204: man_nmsg(m, n, MANDOCERR_SYNTARGCOUNT);
205: return(0);
206: }
1.32 kristaps 207:
208: for (p = n->child->string; '\0' != *p; p++)
1.54 kristaps 209: /* Only warn about this once... */
210: if (isalpha((u_char)*p) && ! isupper((u_char)*p)) {
211: man_nmsg(m, n, MANDOCERR_UPPERCASE);
212: break;
213: }
1.14 kristaps 214:
215: return(1);
216: }
217:
218:
219: static int
1.17 kristaps 220: check_text(CHKARGS)
1.11 kristaps 221: {
1.43 kristaps 222: char *p;
1.15 kristaps 223: int pos, c;
1.46 kristaps 224: size_t sz;
1.11 kristaps 225:
1.46 kristaps 226: for (p = n->string, pos = n->pos + 1; *p; p++, pos++) {
227: sz = strcspn(p, "\t\\");
228: p += (int)sz;
229:
230: if ('\0' == *p)
231: break;
232:
233: pos += (int)sz;
1.11 kristaps 234:
1.46 kristaps 235: if ('\t' == *p) {
236: if (MAN_LITERAL & m->flags)
237: continue;
238: if (man_pmsg(m, n->line, pos, MANDOCERR_BADTAB))
1.15 kristaps 239: continue;
1.46 kristaps 240: return(0);
1.15 kristaps 241: }
1.45 kristaps 242:
1.46 kristaps 243: /* Check the special character. */
1.15 kristaps 244:
1.46 kristaps 245: c = mandoc_special(p);
246: if (c) {
247: p += c - 1;
248: pos += c - 1;
1.49 schwarze 249: } else
250: man_pmsg(m, n->line, pos, MANDOCERR_BADESCAPE);
1.11 kristaps 251: }
252:
253: return(1);
1.1 kristaps 254: }
255:
256:
257: #define INEQ_DEFINE(x, ineq, name) \
258: static int \
1.17 kristaps 259: check_##name(CHKARGS) \
1.1 kristaps 260: { \
1.11 kristaps 261: if (n->nchild ineq (x)) \
1.1 kristaps 262: return(1); \
1.41 kristaps 263: man_vmsg(m, MANDOCERR_SYNTARGCOUNT, n->line, n->pos, \
264: "line arguments %s %d (have %d)", \
265: #ineq, (x), n->nchild); \
266: return(0); \
1.1 kristaps 267: }
268:
269: INEQ_DEFINE(0, ==, eq0)
1.25 kristaps 270: INEQ_DEFINE(1, <=, le1)
1.1 kristaps 271: INEQ_DEFINE(2, >=, ge2)
272: INEQ_DEFINE(5, <=, le5)
273:
1.55 ! kristaps 274: static int
! 275: check_ft(CHKARGS)
! 276: {
! 277: char *cp;
! 278: int ok;
! 279:
! 280: if (0 == n->nchild)
! 281: return(1);
! 282:
! 283: ok = 0;
! 284: cp = n->child->string;
! 285: switch (*cp) {
! 286: case ('1'):
! 287: /* FALLTHROUGH */
! 288: case ('2'):
! 289: /* FALLTHROUGH */
! 290: case ('3'):
! 291: /* FALLTHROUGH */
! 292: case ('4'):
! 293: /* FALLTHROUGH */
! 294: case ('I'):
! 295: /* FALLTHROUGH */
! 296: case ('P'):
! 297: /* FALLTHROUGH */
! 298: case ('R'):
! 299: if ('\0' == cp[1])
! 300: ok = 1;
! 301: break;
! 302: case ('B'):
! 303: if ('\0' == cp[1] || ('I' == cp[1] && '\0' == cp[2]))
! 304: ok = 1;
! 305: break;
! 306: case ('C'):
! 307: if ('W' == cp[1] && '\0' == cp[2])
! 308: ok = 1;
! 309: break;
! 310: default:
! 311: break;
! 312: }
! 313:
! 314: if (0 == ok) {
! 315: man_vmsg(m, MANDOCERR_BADFONT,
! 316: n->line, n->pos, "%s", cp);
! 317: *cp = '\0';
! 318: }
! 319:
! 320: if (1 < n->nchild)
! 321: man_vmsg(m, MANDOCERR_ARGCOUNT, n->line, n->pos,
! 322: "want one child (have %d)", n->nchild);
! 323:
! 324: return(1);
! 325: }
1.16 kristaps 326:
327: static int
1.17 kristaps 328: check_sec(CHKARGS)
329: {
1.16 kristaps 330:
1.41 kristaps 331: if (MAN_HEAD == n->type && 0 == n->nchild) {
332: man_nmsg(m, n, MANDOCERR_SYNTARGCOUNT);
333: return(0);
334: } else if (MAN_BODY == n->type && 0 == n->nchild)
1.54 kristaps 335: man_nmsg(m, n, MANDOCERR_NOBODY);
1.41 kristaps 336:
1.16 kristaps 337: return(1);
338: }
1.17 kristaps 339:
340:
341: static int
1.23 kristaps 342: check_part(CHKARGS)
343: {
344:
345: if (MAN_BODY == n->type && 0 == n->nchild)
1.54 kristaps 346: man_nmsg(m, n, MANDOCERR_NOBODY);
347:
1.23 kristaps 348: return(1);
349: }
350:
351:
352: static int
1.17 kristaps 353: check_par(CHKARGS)
354: {
355:
356: if (MAN_BODY == n->type)
357: switch (n->tok) {
358: case (MAN_IP):
359: /* FALLTHROUGH */
360: case (MAN_HP):
361: /* FALLTHROUGH */
362: case (MAN_TP):
363: /* Body-less lists are ok. */
364: break;
365: default:
1.54 kristaps 366: if (0 == n->nchild)
367: man_nmsg(m, n, MANDOCERR_NOBODY);
368: break;
1.17 kristaps 369: }
370: if (MAN_HEAD == n->type)
371: switch (n->tok) {
372: case (MAN_PP):
373: /* FALLTHROUGH */
374: case (MAN_P):
375: /* FALLTHROUGH */
376: case (MAN_LP):
1.54 kristaps 377: if (n->nchild)
378: man_nmsg(m, n, MANDOCERR_ARGSLOST);
379: break;
1.17 kristaps 380: default:
1.53 kristaps 381: break;
1.17 kristaps 382: }
383:
384: return(1);
385: }
386:
387:
388: static int
389: check_bline(CHKARGS)
390: {
391:
1.22 kristaps 392: assert( ! (MAN_ELINE & m->flags));
1.41 kristaps 393: if (MAN_BLINE & m->flags) {
394: man_nmsg(m, n, MANDOCERR_SYNTLINESCOPE);
395: return(0);
396: }
1.31 kristaps 397:
1.18 kristaps 398: return(1);
1.17 kristaps 399: }
400:
1.51 kristaps 401: static int
402: post_TH(CHKARGS)
403: {
404:
405: if (m->meta.title)
406: free(m->meta.title);
407: if (m->meta.vol)
408: free(m->meta.vol);
409: if (m->meta.source)
410: free(m->meta.source);
411: if (m->meta.msec)
412: free(m->meta.msec);
413: if (m->meta.rawdate)
414: free(m->meta.rawdate);
415:
416: m->meta.title = m->meta.vol = m->meta.rawdate =
417: m->meta.msec = m->meta.source = NULL;
418: m->meta.date = 0;
419:
420: /* ->TITLE<- MSEC DATE SOURCE VOL */
421:
422: n = n->child;
423: assert(n);
424: m->meta.title = mandoc_strdup(n->string);
425:
426: /* TITLE ->MSEC<- DATE SOURCE VOL */
427:
428: n = n->next;
429: assert(n);
430: m->meta.msec = mandoc_strdup(n->string);
431:
432: /* TITLE MSEC ->DATE<- SOURCE VOL */
433:
434: /*
435: * Try to parse the date. If this works, stash the epoch (this
436: * is optimal because we can reformat it in the canonical form).
437: * If it doesn't parse, isn't specified at all, or is an empty
438: * string, then use the current date.
439: */
440:
441: n = n->next;
442: if (n && n->string && *n->string) {
443: m->meta.date = mandoc_a2time
444: (MTIME_ISO_8601, n->string);
445: if (0 == m->meta.date) {
446: man_nmsg(m, n, MANDOCERR_BADDATE);
447: m->meta.rawdate = mandoc_strdup(n->string);
448: }
449: } else
450: m->meta.date = time(NULL);
451:
452: /* TITLE MSEC DATE ->SOURCE<- VOL */
453:
454: if (n && (n = n->next))
455: m->meta.source = mandoc_strdup(n->string);
456:
457: /* TITLE MSEC DATE SOURCE ->VOL<- */
458:
459: if (n && (n = n->next))
460: m->meta.vol = mandoc_strdup(n->string);
461:
462: /*
463: * Remove the `TH' node after we've processed it for our
464: * meta-data.
465: */
466: man_node_delete(m, m->last);
467: return(1);
468: }
469:
470: static int
471: post_nf(CHKARGS)
472: {
473:
474: if (MAN_LITERAL & m->flags)
475: man_nmsg(m, n, MANDOCERR_SCOPEREP);
476:
477: m->flags |= MAN_LITERAL;
478: return(1);
479: }
480:
481: static int
482: post_fi(CHKARGS)
483: {
484:
485: if ( ! (MAN_LITERAL & m->flags))
486: man_nmsg(m, n, MANDOCERR_NOSCOPE);
487:
488: m->flags &= ~MAN_LITERAL;
489: return(1);
490: }
491:
492: static int
493: post_UC(CHKARGS)
494: {
495: static const char * const bsd_versions[] = {
496: "3rd Berkeley Distribution",
497: "4th Berkeley Distribution",
498: "4.2 Berkeley Distribution",
499: "4.3 Berkeley Distribution",
500: "4.4 Berkeley Distribution",
501: };
502:
503: const char *p, *s;
504:
505: n = n->child;
506: n = m->last->child;
507:
508: if (NULL == n || MAN_TEXT != n->type)
509: p = bsd_versions[0];
510: else {
511: s = n->string;
512: if (0 == strcmp(s, "3"))
513: p = bsd_versions[0];
514: else if (0 == strcmp(s, "4"))
515: p = bsd_versions[1];
516: else if (0 == strcmp(s, "5"))
517: p = bsd_versions[2];
518: else if (0 == strcmp(s, "6"))
519: p = bsd_versions[3];
520: else if (0 == strcmp(s, "7"))
521: p = bsd_versions[4];
522: else
523: p = bsd_versions[0];
524: }
525:
526: if (m->meta.source)
527: free(m->meta.source);
528:
529: m->meta.source = mandoc_strdup(p);
530: return(1);
531: }
532:
533: static int
534: post_AT(CHKARGS)
535: {
536: static const char * const unix_versions[] = {
537: "7th Edition",
538: "System III",
539: "System V",
540: "System V Release 2",
541: };
542:
543: const char *p, *s;
544: struct man_node *nn;
545:
546: n = n->child;
547:
548: if (NULL == n || MAN_TEXT != n->type)
549: p = unix_versions[0];
550: else {
551: s = n->string;
552: if (0 == strcmp(s, "3"))
553: p = unix_versions[0];
554: else if (0 == strcmp(s, "4"))
555: p = unix_versions[1];
556: else if (0 == strcmp(s, "5")) {
557: nn = n->next;
558: if (nn && MAN_TEXT == nn->type && nn->string[0])
559: p = unix_versions[3];
560: else
561: p = unix_versions[2];
562: } else
563: p = unix_versions[0];
564: }
565:
566: if (m->meta.source)
567: free(m->meta.source);
568:
569: m->meta.source = mandoc_strdup(p);
570: return(1);
571: }
CVSweb