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