Annotation of mandoc/man_validate.c, Revision 1.70
1.70 ! kristaps 1: /* $Id: man_validate.c,v 1.69 2011/04/13 09:57:08 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.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_bline(CHKARGS);
48: static int check_eq0(CHKARGS);
1.55 kristaps 49: static int check_ft(CHKARGS);
1.25 kristaps 50: static int check_le1(CHKARGS);
1.17 kristaps 51: static int check_ge2(CHKARGS);
52: static int check_le5(CHKARGS);
53: static int check_par(CHKARGS);
1.23 kristaps 54: static int check_part(CHKARGS);
1.17 kristaps 55: static int check_root(CHKARGS);
56: static int check_sec(CHKARGS);
1.68 kristaps 57: static void check_text(CHKARGS);
1.17 kristaps 58:
1.51 kristaps 59: static int post_AT(CHKARGS);
1.70 ! kristaps 60: static int post_vs(CHKARGS);
1.51 kristaps 61: static int post_fi(CHKARGS);
62: static int post_nf(CHKARGS);
63: static int post_TH(CHKARGS);
64: static int post_UC(CHKARGS);
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.55 kristaps 70: static v_check posts_ft[] = { check_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.17 kristaps 74: static v_check posts_sec[] = { check_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.17 kristaps 78: static v_check pres_bline[] = { check_bline, NULL };
1.1 kristaps 79:
80: static const struct man_valid man_valids[MAN_MAX] = {
1.70 ! kristaps 81: { NULL, posts_br }, /* br */
1.32 kristaps 82: { pres_bline, posts_th }, /* TH */
1.17 kristaps 83: { pres_bline, posts_sec }, /* SH */
84: { pres_bline, posts_sec }, /* SS */
1.59 kristaps 85: { pres_bline, NULL }, /* TP */
1.17 kristaps 86: { pres_bline, posts_par }, /* LP */
87: { pres_bline, posts_par }, /* PP */
88: { pres_bline, posts_par }, /* P */
1.59 kristaps 89: { pres_bline, NULL }, /* IP */
90: { pres_bline, 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.51 kristaps 104: { pres_bline, posts_nf }, /* nf */
105: { pres_bline, 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) {
154: case (MAN_TEXT):
1.68 kristaps 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);
207: }
208:
1.68 kristaps 209: static void
1.17 kristaps 210: check_text(CHKARGS)
1.11 kristaps 211: {
1.68 kristaps 212: char *p, *pp, *cpp;
213: int pos;
1.46 kristaps 214: size_t sz;
1.11 kristaps 215:
1.68 kristaps 216: p = n->string;
217: pos = n->pos + 1;
218:
219: while ('\0' != *p) {
1.46 kristaps 220: sz = strcspn(p, "\t\\");
1.68 kristaps 221:
1.46 kristaps 222: p += (int)sz;
1.68 kristaps 223: pos += (int)sz;
1.46 kristaps 224:
1.68 kristaps 225: if ('\t' == *p) {
226: if ( ! (MAN_LITERAL & m->flags))
227: man_pmsg(m, n->line, pos, MANDOCERR_BADTAB);
228: p++;
229: pos++;
230: continue;
231: } else if ('\0' == *p)
1.46 kristaps 232: break;
233:
1.68 kristaps 234: pos++;
235: pp = ++p;
1.11 kristaps 236:
1.68 kristaps 237: if (ESCAPE_ERROR == mandoc_escape
238: ((const char **)&pp, NULL, NULL)) {
239: man_pmsg(m, n->line, pos, MANDOCERR_BADESCAPE);
240: break;
1.15 kristaps 241: }
1.45 kristaps 242:
1.68 kristaps 243: cpp = p;
244: while (NULL != (cpp = memchr(cpp, ASCII_HYPH, pp - cpp)))
245: *cpp = '-';
1.15 kristaps 246:
1.68 kristaps 247: pos += pp - p;
248: p = pp;
1.11 kristaps 249: }
1.1 kristaps 250: }
251:
252: #define INEQ_DEFINE(x, ineq, name) \
253: static int \
1.17 kristaps 254: check_##name(CHKARGS) \
1.1 kristaps 255: { \
1.11 kristaps 256: if (n->nchild ineq (x)) \
1.1 kristaps 257: return(1); \
1.65 kristaps 258: mandoc_vmsg(MANDOCERR_ARGCOUNT, m->parse, n->line, n->pos, \
1.41 kristaps 259: "line arguments %s %d (have %d)", \
260: #ineq, (x), n->nchild); \
1.60 schwarze 261: return(1); \
1.1 kristaps 262: }
263:
264: INEQ_DEFINE(0, ==, eq0)
1.25 kristaps 265: INEQ_DEFINE(1, <=, le1)
1.1 kristaps 266: INEQ_DEFINE(2, >=, ge2)
267: INEQ_DEFINE(5, <=, le5)
268:
1.55 kristaps 269: static int
270: check_ft(CHKARGS)
271: {
272: char *cp;
273: int ok;
274:
275: if (0 == n->nchild)
276: return(1);
277:
278: ok = 0;
279: cp = n->child->string;
280: switch (*cp) {
281: case ('1'):
282: /* FALLTHROUGH */
283: case ('2'):
284: /* FALLTHROUGH */
285: case ('3'):
286: /* FALLTHROUGH */
287: case ('4'):
288: /* FALLTHROUGH */
289: case ('I'):
290: /* FALLTHROUGH */
291: case ('P'):
292: /* FALLTHROUGH */
293: case ('R'):
294: if ('\0' == cp[1])
295: ok = 1;
296: break;
297: case ('B'):
298: if ('\0' == cp[1] || ('I' == cp[1] && '\0' == cp[2]))
299: ok = 1;
300: break;
301: case ('C'):
302: if ('W' == cp[1] && '\0' == cp[2])
303: ok = 1;
304: break;
305: default:
306: break;
307: }
308:
309: if (0 == ok) {
1.65 kristaps 310: mandoc_vmsg
311: (MANDOCERR_BADFONT, m->parse,
312: n->line, n->pos, "%s", cp);
1.55 kristaps 313: *cp = '\0';
314: }
315:
316: if (1 < n->nchild)
1.65 kristaps 317: mandoc_vmsg
318: (MANDOCERR_ARGCOUNT, m->parse, n->line,
319: n->pos, "want one child (have %d)",
320: n->nchild);
1.55 kristaps 321:
322: return(1);
323: }
1.16 kristaps 324:
325: static int
1.17 kristaps 326: check_sec(CHKARGS)
327: {
1.16 kristaps 328:
1.69 kristaps 329: if ( ! (MAN_HEAD == n->type && 0 == n->nchild))
330: return(1);
1.41 kristaps 331:
1.69 kristaps 332: man_nmsg(m, n, MANDOCERR_SYNTARGCOUNT);
333: return(0);
1.16 kristaps 334: }
1.17 kristaps 335:
336:
337: static int
1.23 kristaps 338: check_part(CHKARGS)
339: {
340:
341: if (MAN_BODY == n->type && 0 == n->nchild)
1.67 kristaps 342: mandoc_msg(MANDOCERR_ARGCWARN, m->parse, n->line,
343: n->pos, "want children (have none)");
1.54 kristaps 344:
1.23 kristaps 345: return(1);
346: }
347:
348:
349: static int
1.17 kristaps 350: check_par(CHKARGS)
351: {
352:
1.59 kristaps 353: switch (n->type) {
354: case (MAN_BLOCK):
355: if (0 == n->body->nchild)
356: man_node_delete(m, n);
357: break;
358: case (MAN_BODY):
359: if (0 == n->nchild)
360: man_nmsg(m, n, MANDOCERR_IGNPAR);
361: break;
362: case (MAN_HEAD):
363: if (n->nchild)
364: man_nmsg(m, n, MANDOCERR_ARGSLOST);
365: break;
366: default:
367: break;
368: }
1.17 kristaps 369:
370: return(1);
371: }
372:
373:
374: static int
375: check_bline(CHKARGS)
376: {
377:
1.22 kristaps 378: assert( ! (MAN_ELINE & m->flags));
1.41 kristaps 379: if (MAN_BLINE & m->flags) {
380: man_nmsg(m, n, MANDOCERR_SYNTLINESCOPE);
381: return(0);
382: }
1.31 kristaps 383:
1.18 kristaps 384: return(1);
1.17 kristaps 385: }
386:
1.51 kristaps 387: static int
388: post_TH(CHKARGS)
389: {
1.60 schwarze 390: const char *p;
1.63 schwarze 391: int line, pos;
1.51 kristaps 392:
393: if (m->meta.title)
394: free(m->meta.title);
395: if (m->meta.vol)
396: free(m->meta.vol);
397: if (m->meta.source)
398: free(m->meta.source);
399: if (m->meta.msec)
400: free(m->meta.msec);
1.63 schwarze 401: if (m->meta.date)
402: free(m->meta.date);
1.51 kristaps 403:
1.63 schwarze 404: line = n->line;
405: pos = n->pos;
406: m->meta.title = m->meta.vol = m->meta.date =
1.51 kristaps 407: m->meta.msec = m->meta.source = NULL;
408:
409: /* ->TITLE<- MSEC DATE SOURCE VOL */
410:
411: n = n->child;
1.60 schwarze 412: if (n && n->string) {
413: for (p = n->string; '\0' != *p; p++) {
414: /* Only warn about this once... */
415: if (isalpha((u_char)*p) && ! isupper((u_char)*p)) {
416: man_nmsg(m, n, MANDOCERR_UPPERCASE);
417: break;
418: }
419: }
420: m->meta.title = mandoc_strdup(n->string);
421: } else
422: m->meta.title = mandoc_strdup("");
1.51 kristaps 423:
424: /* TITLE ->MSEC<- DATE SOURCE VOL */
425:
1.60 schwarze 426: if (n)
427: n = n->next;
428: if (n && n->string)
429: m->meta.msec = mandoc_strdup(n->string);
430: else
431: m->meta.msec = mandoc_strdup("");
1.51 kristaps 432:
433: /* TITLE MSEC ->DATE<- SOURCE VOL */
434:
1.60 schwarze 435: if (n)
436: n = n->next;
1.63 schwarze 437: if (n)
438: pos = n->pos;
1.65 kristaps 439: m->meta.date = mandoc_normdate
440: (m->parse, n ? n->string : NULL, line, pos);
1.51 kristaps 441:
442: /* TITLE MSEC DATE ->SOURCE<- VOL */
443:
444: if (n && (n = n->next))
445: m->meta.source = mandoc_strdup(n->string);
446:
447: /* TITLE MSEC DATE SOURCE ->VOL<- */
448:
449: if (n && (n = n->next))
450: m->meta.vol = mandoc_strdup(n->string);
451:
452: /*
453: * Remove the `TH' node after we've processed it for our
454: * meta-data.
455: */
456: man_node_delete(m, m->last);
457: return(1);
458: }
459:
460: static int
461: post_nf(CHKARGS)
462: {
463:
464: if (MAN_LITERAL & m->flags)
465: man_nmsg(m, n, MANDOCERR_SCOPEREP);
466:
467: m->flags |= MAN_LITERAL;
468: return(1);
469: }
470:
471: static int
472: post_fi(CHKARGS)
473: {
474:
475: if ( ! (MAN_LITERAL & m->flags))
1.58 kristaps 476: man_nmsg(m, n, MANDOCERR_WNOSCOPE);
1.51 kristaps 477:
478: m->flags &= ~MAN_LITERAL;
479: return(1);
480: }
481:
482: static int
483: post_UC(CHKARGS)
484: {
485: static const char * const bsd_versions[] = {
486: "3rd Berkeley Distribution",
487: "4th Berkeley Distribution",
488: "4.2 Berkeley Distribution",
489: "4.3 Berkeley Distribution",
490: "4.4 Berkeley Distribution",
491: };
492:
493: const char *p, *s;
494:
495: n = n->child;
496: n = m->last->child;
497:
498: if (NULL == n || MAN_TEXT != n->type)
499: p = bsd_versions[0];
500: else {
501: s = n->string;
502: if (0 == strcmp(s, "3"))
503: p = bsd_versions[0];
504: else if (0 == strcmp(s, "4"))
505: p = bsd_versions[1];
506: else if (0 == strcmp(s, "5"))
507: p = bsd_versions[2];
508: else if (0 == strcmp(s, "6"))
509: p = bsd_versions[3];
510: else if (0 == strcmp(s, "7"))
511: p = bsd_versions[4];
512: else
513: p = bsd_versions[0];
514: }
515:
516: if (m->meta.source)
517: free(m->meta.source);
518:
519: m->meta.source = mandoc_strdup(p);
520: return(1);
521: }
522:
523: static int
524: post_AT(CHKARGS)
525: {
526: static const char * const unix_versions[] = {
527: "7th Edition",
528: "System III",
529: "System V",
530: "System V Release 2",
531: };
532:
533: const char *p, *s;
534: struct man_node *nn;
535:
536: n = n->child;
537:
538: if (NULL == n || MAN_TEXT != n->type)
539: p = unix_versions[0];
540: else {
541: s = n->string;
542: if (0 == strcmp(s, "3"))
543: p = unix_versions[0];
544: else if (0 == strcmp(s, "4"))
545: p = unix_versions[1];
546: else if (0 == strcmp(s, "5")) {
547: nn = n->next;
548: if (nn && MAN_TEXT == nn->type && nn->string[0])
549: p = unix_versions[3];
550: else
551: p = unix_versions[2];
552: } else
553: p = unix_versions[0];
554: }
555:
556: if (m->meta.source)
557: free(m->meta.source);
558:
559: m->meta.source = mandoc_strdup(p);
1.70 ! kristaps 560: return(1);
! 561: }
! 562:
! 563: static int
! 564: post_vs(CHKARGS)
! 565: {
! 566:
! 567: /*
! 568: * Don't warn about this because it occurs in pod2man and would
! 569: * cause considerable (unfixable) warnage.
! 570: */
! 571: if (NULL == n->prev && MAN_ROOT == n->parent->type)
! 572: man_node_delete(m, n);
! 573:
1.51 kristaps 574: return(1);
575: }
CVSweb