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