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