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