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