Annotation of mandoc/man_validate.c, Revision 1.65
1.65 ! kristaps 1: /* $Id: man_validate.c,v 1.64 2011/03/17 11:56:17 kristaps Exp $ */
1.1 kristaps 2: /*
1.63 schwarze 3: * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
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.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.41 kristaps 33: #include "mandoc.h"
1.1 kristaps 34: #include "libman.h"
1.15 kristaps 35: #include "libmandoc.h"
1.1 kristaps 36:
1.43 kristaps 37: #define CHKARGS struct man *m, struct man_node *n
1.1 kristaps 38:
1.17 kristaps 39: typedef int (*v_check)(CHKARGS);
1.1 kristaps 40:
41: struct man_valid {
1.17 kristaps 42: v_check *pres;
43: v_check *posts;
1.1 kristaps 44: };
45:
1.17 kristaps 46: static int check_bline(CHKARGS);
47: static int check_eq0(CHKARGS);
1.55 kristaps 48: static int check_ft(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);
55: static int check_sec(CHKARGS);
56: static int check_text(CHKARGS);
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.60 schwarze 73: static v_check posts_th[] = { check_ge2, check_le5, post_TH, NULL };
1.51 kristaps 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 */
1.59 kristaps 83: { pres_bline, NULL }, /* TP */
1.17 kristaps 84: { pres_bline, posts_par }, /* LP */
85: { pres_bline, posts_par }, /* PP */
86: { pres_bline, posts_par }, /* P */
1.59 kristaps 87: { pres_bline, NULL }, /* IP */
88: { pres_bline, NULL }, /* 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. */
101: { NULL, posts_le1 }, /* sp */ /* FIXME: should warn only. */
1.51 kristaps 102: { pres_bline, posts_nf }, /* nf */
103: { pres_bline, posts_fi }, /* fi */
1.19 kristaps 104: { NULL, NULL }, /* RE */
1.23 kristaps 105: { NULL, posts_part }, /* RS */
1.21 kristaps 106: { NULL, NULL }, /* DT */
1.51 kristaps 107: { NULL, posts_uc }, /* UC */
1.26 kristaps 108: { NULL, NULL }, /* PD */
1.51 kristaps 109: { NULL, posts_at }, /* AT */
1.47 kristaps 110: { NULL, NULL }, /* in */
1.55 kristaps 111: { NULL, posts_ft }, /* ft */
1.1 kristaps 112: };
113:
114:
115: int
1.43 kristaps 116: man_valid_pre(struct man *m, struct man_node *n)
1.17 kristaps 117: {
118: v_check *cp;
119:
1.57 kristaps 120: switch (n->type) {
121: case (MAN_TEXT):
122: /* FALLTHROUGH */
123: case (MAN_ROOT):
1.61 kristaps 124: /* FALLTHROUGH */
125: case (MAN_EQN):
1.57 kristaps 126: /* FALLTHROUGH */
127: case (MAN_TBL):
1.17 kristaps 128: return(1);
1.57 kristaps 129: default:
130: break;
131: }
1.17 kristaps 132:
133: if (NULL == (cp = man_valids[n->tok].pres))
134: return(1);
135: for ( ; *cp; cp++)
136: if ( ! (*cp)(m, n))
137: return(0);
138: return(1);
139: }
140:
141:
142: int
1.1 kristaps 143: man_valid_post(struct man *m)
144: {
1.17 kristaps 145: v_check *cp;
1.1 kristaps 146:
147: if (MAN_VALID & m->last->flags)
148: return(1);
149: m->last->flags |= MAN_VALID;
150:
151: switch (m->last->type) {
152: case (MAN_TEXT):
1.11 kristaps 153: return(check_text(m, m->last));
1.1 kristaps 154: case (MAN_ROOT):
1.14 kristaps 155: return(check_root(m, m->last));
1.62 kristaps 156: case (MAN_EQN):
157: /* FALLTHROUGH */
1.57 kristaps 158: case (MAN_TBL):
159: return(1);
1.1 kristaps 160: default:
161: break;
162: }
163:
164: if (NULL == (cp = man_valids[m->last->tok].posts))
165: return(1);
166: for ( ; *cp; cp++)
167: if ( ! (*cp)(m, m->last))
168: return(0);
169:
170: return(1);
171: }
172:
173:
1.11 kristaps 174: static int
1.17 kristaps 175: check_root(CHKARGS)
1.14 kristaps 176: {
1.17 kristaps 177:
178: if (MAN_BLINE & m->flags)
1.54 kristaps 179: man_nmsg(m, n, MANDOCERR_SCOPEEXIT);
180: else if (MAN_ELINE & m->flags)
181: man_nmsg(m, n, MANDOCERR_SCOPEEXIT);
1.22 kristaps 182:
183: m->flags &= ~MAN_BLINE;
184: m->flags &= ~MAN_ELINE;
1.17 kristaps 185:
1.41 kristaps 186: if (NULL == m->first->child) {
187: man_nmsg(m, n, MANDOCERR_NODOCBODY);
188: return(0);
189: } else if (NULL == m->meta.title) {
1.54 kristaps 190: man_nmsg(m, n, MANDOCERR_NOTITLE);
191:
1.34 kristaps 192: /*
193: * If a title hasn't been set, do so now (by
194: * implication, date and section also aren't set).
195: */
1.54 kristaps 196:
1.34 kristaps 197: m->meta.title = mandoc_strdup("unknown");
1.37 kristaps 198: m->meta.msec = mandoc_strdup("1");
1.65 ! kristaps 199: m->meta.date = mandoc_normdate
! 200: (m->parse, NULL, n->line, n->pos);
1.34 kristaps 201: }
1.32 kristaps 202:
203: return(1);
204: }
205:
206:
207: static int
1.17 kristaps 208: check_text(CHKARGS)
1.11 kristaps 209: {
1.43 kristaps 210: char *p;
1.15 kristaps 211: int pos, c;
1.46 kristaps 212: size_t sz;
1.11 kristaps 213:
1.46 kristaps 214: for (p = n->string, pos = n->pos + 1; *p; p++, pos++) {
215: sz = strcspn(p, "\t\\");
216: p += (int)sz;
217:
218: if ('\0' == *p)
219: break;
220:
221: pos += (int)sz;
1.11 kristaps 222:
1.46 kristaps 223: if ('\t' == *p) {
224: if (MAN_LITERAL & m->flags)
225: continue;
1.64 kristaps 226: man_pmsg(m, n->line, pos, MANDOCERR_BADTAB);
227: continue;
1.15 kristaps 228: }
1.45 kristaps 229:
1.46 kristaps 230: /* Check the special character. */
1.15 kristaps 231:
1.46 kristaps 232: c = mandoc_special(p);
233: if (c) {
234: p += c - 1;
235: pos += c - 1;
1.49 schwarze 236: } else
237: man_pmsg(m, n->line, pos, MANDOCERR_BADESCAPE);
1.11 kristaps 238: }
239:
240: return(1);
1.1 kristaps 241: }
242:
243:
244: #define INEQ_DEFINE(x, ineq, name) \
245: static int \
1.17 kristaps 246: check_##name(CHKARGS) \
1.1 kristaps 247: { \
1.11 kristaps 248: if (n->nchild ineq (x)) \
1.1 kristaps 249: return(1); \
1.65 ! kristaps 250: mandoc_vmsg(MANDOCERR_ARGCOUNT, m->parse, n->line, n->pos, \
1.41 kristaps 251: "line arguments %s %d (have %d)", \
252: #ineq, (x), n->nchild); \
1.60 schwarze 253: return(1); \
1.1 kristaps 254: }
255:
256: INEQ_DEFINE(0, ==, eq0)
1.25 kristaps 257: INEQ_DEFINE(1, <=, le1)
1.1 kristaps 258: INEQ_DEFINE(2, >=, ge2)
259: INEQ_DEFINE(5, <=, le5)
260:
1.55 kristaps 261: static int
262: check_ft(CHKARGS)
263: {
264: char *cp;
265: int ok;
266:
267: if (0 == n->nchild)
268: return(1);
269:
270: ok = 0;
271: cp = n->child->string;
272: switch (*cp) {
273: case ('1'):
274: /* FALLTHROUGH */
275: case ('2'):
276: /* FALLTHROUGH */
277: case ('3'):
278: /* FALLTHROUGH */
279: case ('4'):
280: /* FALLTHROUGH */
281: case ('I'):
282: /* FALLTHROUGH */
283: case ('P'):
284: /* FALLTHROUGH */
285: case ('R'):
286: if ('\0' == cp[1])
287: ok = 1;
288: break;
289: case ('B'):
290: if ('\0' == cp[1] || ('I' == cp[1] && '\0' == cp[2]))
291: ok = 1;
292: break;
293: case ('C'):
294: if ('W' == cp[1] && '\0' == cp[2])
295: ok = 1;
296: break;
297: default:
298: break;
299: }
300:
301: if (0 == ok) {
1.65 ! kristaps 302: mandoc_vmsg
! 303: (MANDOCERR_BADFONT, m->parse,
! 304: n->line, n->pos, "%s", cp);
1.55 kristaps 305: *cp = '\0';
306: }
307:
308: if (1 < n->nchild)
1.65 ! kristaps 309: mandoc_vmsg
! 310: (MANDOCERR_ARGCOUNT, m->parse, n->line,
! 311: n->pos, "want one child (have %d)",
! 312: n->nchild);
1.55 kristaps 313:
314: return(1);
315: }
1.16 kristaps 316:
317: static int
1.17 kristaps 318: check_sec(CHKARGS)
319: {
1.16 kristaps 320:
1.41 kristaps 321: if (MAN_HEAD == n->type && 0 == n->nchild) {
322: man_nmsg(m, n, MANDOCERR_SYNTARGCOUNT);
323: return(0);
324: } else if (MAN_BODY == n->type && 0 == n->nchild)
1.54 kristaps 325: man_nmsg(m, n, MANDOCERR_NOBODY);
1.41 kristaps 326:
1.16 kristaps 327: return(1);
328: }
1.17 kristaps 329:
330:
331: static int
1.23 kristaps 332: check_part(CHKARGS)
333: {
334:
335: if (MAN_BODY == n->type && 0 == n->nchild)
1.54 kristaps 336: man_nmsg(m, n, MANDOCERR_NOBODY);
337:
1.23 kristaps 338: return(1);
339: }
340:
341:
342: static int
1.17 kristaps 343: check_par(CHKARGS)
344: {
345:
1.59 kristaps 346: switch (n->type) {
347: case (MAN_BLOCK):
348: if (0 == n->body->nchild)
349: man_node_delete(m, n);
350: break;
351: case (MAN_BODY):
352: if (0 == n->nchild)
353: man_nmsg(m, n, MANDOCERR_IGNPAR);
354: break;
355: case (MAN_HEAD):
356: if (n->nchild)
357: man_nmsg(m, n, MANDOCERR_ARGSLOST);
358: break;
359: default:
360: break;
361: }
1.17 kristaps 362:
363: return(1);
364: }
365:
366:
367: static int
368: check_bline(CHKARGS)
369: {
370:
1.22 kristaps 371: assert( ! (MAN_ELINE & m->flags));
1.41 kristaps 372: if (MAN_BLINE & m->flags) {
373: man_nmsg(m, n, MANDOCERR_SYNTLINESCOPE);
374: return(0);
375: }
1.31 kristaps 376:
1.18 kristaps 377: return(1);
1.17 kristaps 378: }
379:
1.51 kristaps 380: static int
381: post_TH(CHKARGS)
382: {
1.60 schwarze 383: const char *p;
1.63 schwarze 384: int line, pos;
1.51 kristaps 385:
386: if (m->meta.title)
387: free(m->meta.title);
388: if (m->meta.vol)
389: free(m->meta.vol);
390: if (m->meta.source)
391: free(m->meta.source);
392: if (m->meta.msec)
393: free(m->meta.msec);
1.63 schwarze 394: if (m->meta.date)
395: free(m->meta.date);
1.51 kristaps 396:
1.63 schwarze 397: line = n->line;
398: pos = n->pos;
399: m->meta.title = m->meta.vol = m->meta.date =
1.51 kristaps 400: m->meta.msec = m->meta.source = NULL;
401:
402: /* ->TITLE<- MSEC DATE SOURCE VOL */
403:
404: n = n->child;
1.60 schwarze 405: if (n && n->string) {
406: for (p = n->string; '\0' != *p; p++) {
407: /* Only warn about this once... */
408: if (isalpha((u_char)*p) && ! isupper((u_char)*p)) {
409: man_nmsg(m, n, MANDOCERR_UPPERCASE);
410: break;
411: }
412: }
413: m->meta.title = mandoc_strdup(n->string);
414: } else
415: m->meta.title = mandoc_strdup("");
1.51 kristaps 416:
417: /* TITLE ->MSEC<- DATE SOURCE VOL */
418:
1.60 schwarze 419: if (n)
420: n = n->next;
421: if (n && n->string)
422: m->meta.msec = mandoc_strdup(n->string);
423: else
424: m->meta.msec = mandoc_strdup("");
1.51 kristaps 425:
426: /* TITLE MSEC ->DATE<- SOURCE VOL */
427:
1.60 schwarze 428: if (n)
429: n = n->next;
1.63 schwarze 430: if (n)
431: pos = n->pos;
1.65 ! kristaps 432: m->meta.date = mandoc_normdate
! 433: (m->parse, n ? n->string : NULL, line, pos);
1.51 kristaps 434:
435: /* TITLE MSEC DATE ->SOURCE<- VOL */
436:
437: if (n && (n = n->next))
438: m->meta.source = mandoc_strdup(n->string);
439:
440: /* TITLE MSEC DATE SOURCE ->VOL<- */
441:
442: if (n && (n = n->next))
443: m->meta.vol = mandoc_strdup(n->string);
444:
445: /*
446: * Remove the `TH' node after we've processed it for our
447: * meta-data.
448: */
449: man_node_delete(m, m->last);
450: return(1);
451: }
452:
453: static int
454: post_nf(CHKARGS)
455: {
456:
457: if (MAN_LITERAL & m->flags)
458: man_nmsg(m, n, MANDOCERR_SCOPEREP);
459:
460: m->flags |= MAN_LITERAL;
461: return(1);
462: }
463:
464: static int
465: post_fi(CHKARGS)
466: {
467:
468: if ( ! (MAN_LITERAL & m->flags))
1.58 kristaps 469: man_nmsg(m, n, MANDOCERR_WNOSCOPE);
1.51 kristaps 470:
471: m->flags &= ~MAN_LITERAL;
472: return(1);
473: }
474:
475: static int
476: post_UC(CHKARGS)
477: {
478: static const char * const bsd_versions[] = {
479: "3rd Berkeley Distribution",
480: "4th Berkeley Distribution",
481: "4.2 Berkeley Distribution",
482: "4.3 Berkeley Distribution",
483: "4.4 Berkeley Distribution",
484: };
485:
486: const char *p, *s;
487:
488: n = n->child;
489: n = m->last->child;
490:
491: if (NULL == n || MAN_TEXT != n->type)
492: p = bsd_versions[0];
493: else {
494: s = n->string;
495: if (0 == strcmp(s, "3"))
496: p = bsd_versions[0];
497: else if (0 == strcmp(s, "4"))
498: p = bsd_versions[1];
499: else if (0 == strcmp(s, "5"))
500: p = bsd_versions[2];
501: else if (0 == strcmp(s, "6"))
502: p = bsd_versions[3];
503: else if (0 == strcmp(s, "7"))
504: p = bsd_versions[4];
505: else
506: p = bsd_versions[0];
507: }
508:
509: if (m->meta.source)
510: free(m->meta.source);
511:
512: m->meta.source = mandoc_strdup(p);
513: return(1);
514: }
515:
516: static int
517: post_AT(CHKARGS)
518: {
519: static const char * const unix_versions[] = {
520: "7th Edition",
521: "System III",
522: "System V",
523: "System V Release 2",
524: };
525:
526: const char *p, *s;
527: struct man_node *nn;
528:
529: n = n->child;
530:
531: if (NULL == n || MAN_TEXT != n->type)
532: p = unix_versions[0];
533: else {
534: s = n->string;
535: if (0 == strcmp(s, "3"))
536: p = unix_versions[0];
537: else if (0 == strcmp(s, "4"))
538: p = unix_versions[1];
539: else if (0 == strcmp(s, "5")) {
540: nn = n->next;
541: if (nn && MAN_TEXT == nn->type && nn->string[0])
542: p = unix_versions[3];
543: else
544: p = unix_versions[2];
545: } else
546: p = unix_versions[0];
547: }
548:
549: if (m->meta.source)
550: free(m->meta.source);
551:
552: m->meta.source = mandoc_strdup(p);
553: return(1);
554: }
CVSweb