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