Annotation of src/usr.bin/mandoc/man_validate.c, Revision 1.34
1.34 ! schwarze 1: /* $Id: man_validate.c,v 1.33 2010/11/29 02:26:45 schwarze Exp $ */
1.1 kristaps 2: /*
1.28 schwarze 3: * Copyright (c) 2008, 2009, 2010 Kristaps Dzonsons <kristaps@bsd.lv>
1.32 schwarze 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.2 schwarze 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.2 schwarze 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: */
18: #include <sys/types.h>
19:
20: #include <assert.h>
21: #include <ctype.h>
1.6 schwarze 22: #include <errno.h>
23: #include <limits.h>
1.1 kristaps 24: #include <stdarg.h>
25: #include <stdlib.h>
1.28 schwarze 26: #include <string.h>
1.1 kristaps 27:
1.25 schwarze 28: #include "mandoc.h"
1.1 kristaps 29: #include "libman.h"
1.5 schwarze 30: #include "libmandoc.h"
1.1 kristaps 31:
1.34 ! schwarze 32: #include "out.h"
! 33: #include "term.h"
! 34: #include "tbl.h"
! 35:
1.27 schwarze 36: #define CHKARGS struct man *m, struct man_node *n
1.1 kristaps 37:
1.7 schwarze 38: typedef int (*v_check)(CHKARGS);
1.1 kristaps 39:
40: struct man_valid {
1.7 schwarze 41: v_check *pres;
42: v_check *posts;
1.1 kristaps 43: };
44:
1.7 schwarze 45: static int check_bline(CHKARGS);
46: static int check_eq0(CHKARGS);
1.10 schwarze 47: static int check_le1(CHKARGS);
1.7 schwarze 48: static int check_ge2(CHKARGS);
49: static int check_le5(CHKARGS);
1.32 schwarze 50: static int check_ft(CHKARGS);
1.7 schwarze 51: static int check_par(CHKARGS);
1.8 schwarze 52: static int check_part(CHKARGS);
1.7 schwarze 53: static int check_root(CHKARGS);
54: static int check_sec(CHKARGS);
55: static int check_text(CHKARGS);
1.15 schwarze 56: static int check_title(CHKARGS);
1.7 schwarze 57:
1.34 ! schwarze 58: static int post_AT(CHKARGS);
! 59: static int post_fi(CHKARGS);
! 60: static int post_nf(CHKARGS);
! 61: static int post_TH(CHKARGS);
! 62: static int post_TS(CHKARGS);
! 63: static int post_UC(CHKARGS);
! 64:
! 65: static v_check posts_at[] = { post_AT, NULL };
1.7 schwarze 66: static v_check posts_eq0[] = { check_eq0, NULL };
1.34 ! schwarze 67: static v_check posts_fi[] = { check_eq0, post_fi, NULL };
! 68: static v_check posts_le1[] = { check_le1, NULL };
! 69: static v_check posts_nf[] = { check_eq0, post_nf, NULL };
1.32 schwarze 70: static v_check posts_ft[] = { check_ft, NULL };
1.7 schwarze 71: static v_check posts_par[] = { check_par, NULL };
1.8 schwarze 72: static v_check posts_part[] = { check_part, NULL };
1.7 schwarze 73: static v_check posts_sec[] = { check_sec, NULL };
1.34 ! schwarze 74: static v_check posts_th[] = { check_ge2, check_le5, check_title, post_TH, NULL };
! 75: static v_check posts_ts[] = { post_TS, NULL };
! 76: static v_check posts_uc[] = { post_UC, NULL };
1.7 schwarze 77: static v_check pres_bline[] = { check_bline, NULL };
1.1 kristaps 78:
1.34 ! schwarze 79:
1.1 kristaps 80: static const struct man_valid man_valids[MAN_MAX] = {
1.12 schwarze 81: { NULL, posts_eq0 }, /* br */
1.15 schwarze 82: { pres_bline, posts_th }, /* TH */
1.7 schwarze 83: { pres_bline, posts_sec }, /* SH */
84: { pres_bline, posts_sec }, /* SS */
85: { pres_bline, posts_par }, /* TP */
86: { pres_bline, posts_par }, /* LP */
87: { pres_bline, posts_par }, /* PP */
88: { pres_bline, posts_par }, /* P */
89: { pres_bline, posts_par }, /* IP */
90: { pres_bline, posts_par }, /* HP */
1.8 schwarze 91: { NULL, NULL }, /* SM */
92: { NULL, NULL }, /* SB */
1.7 schwarze 93: { NULL, NULL }, /* BI */
94: { NULL, NULL }, /* IB */
95: { NULL, NULL }, /* BR */
96: { NULL, NULL }, /* RB */
1.8 schwarze 97: { NULL, NULL }, /* R */
98: { NULL, NULL }, /* B */
99: { NULL, NULL }, /* I */
1.7 schwarze 100: { NULL, NULL }, /* IR */
101: { NULL, NULL }, /* RI */
1.28 schwarze 102: { NULL, posts_eq0 }, /* na */ /* FIXME: should warn only. */
1.7 schwarze 103: { NULL, NULL }, /* i */
1.28 schwarze 104: { NULL, posts_le1 }, /* sp */ /* FIXME: should warn only. */
1.34 ! schwarze 105: { pres_bline, posts_nf }, /* nf */
! 106: { pres_bline, posts_fi }, /* fi */
1.7 schwarze 107: { NULL, NULL }, /* r */
1.8 schwarze 108: { NULL, NULL }, /* RE */
109: { NULL, posts_part }, /* RS */
110: { NULL, NULL }, /* DT */
1.34 ! schwarze 111: { NULL, posts_uc }, /* UC */
1.11 schwarze 112: { NULL, NULL }, /* PD */
1.34 ! schwarze 113: { NULL, posts_at }, /* AT */
1.28 schwarze 114: { NULL, NULL }, /* in */
1.34 ! schwarze 115: { NULL, posts_ts }, /* TS */
1.30 schwarze 116: { NULL, NULL }, /* TE */
1.32 schwarze 117: { NULL, posts_ft }, /* ft */
1.1 kristaps 118: };
119:
120:
121: int
1.27 schwarze 122: man_valid_pre(struct man *m, struct man_node *n)
1.7 schwarze 123: {
124: v_check *cp;
125:
126: if (MAN_TEXT == n->type)
127: return(1);
128: if (MAN_ROOT == n->type)
129: return(1);
130:
131: if (NULL == (cp = man_valids[n->tok].pres))
132: return(1);
133: for ( ; *cp; cp++)
134: if ( ! (*cp)(m, n))
135: return(0);
136: return(1);
137: }
138:
139:
140: int
1.1 kristaps 141: man_valid_post(struct man *m)
142: {
1.7 schwarze 143: v_check *cp;
1.1 kristaps 144:
145: if (MAN_VALID & m->last->flags)
146: return(1);
147: m->last->flags |= MAN_VALID;
148:
149: switch (m->last->type) {
150: case (MAN_TEXT):
1.4 schwarze 151: return(check_text(m, m->last));
1.1 kristaps 152: case (MAN_ROOT):
1.4 schwarze 153: return(check_root(m, m->last));
1.1 kristaps 154: default:
155: break;
156: }
157:
158: if (NULL == (cp = man_valids[m->last->tok].posts))
159: return(1);
160: for ( ; *cp; cp++)
161: if ( ! (*cp)(m, m->last))
162: return(0);
163:
164: return(1);
165: }
166:
167:
1.4 schwarze 168: static int
1.7 schwarze 169: check_root(CHKARGS)
1.4 schwarze 170: {
1.7 schwarze 171:
172: if (MAN_BLINE & m->flags)
1.25 schwarze 173: return(man_nmsg(m, n, MANDOCERR_SCOPEEXIT));
1.7 schwarze 174: if (MAN_ELINE & m->flags)
1.25 schwarze 175: return(man_nmsg(m, n, MANDOCERR_SCOPEEXIT));
1.8 schwarze 176:
177: m->flags &= ~MAN_BLINE;
178: m->flags &= ~MAN_ELINE;
1.7 schwarze 179:
1.25 schwarze 180: if (NULL == m->first->child) {
181: man_nmsg(m, n, MANDOCERR_NODOCBODY);
182: return(0);
183: } else if (NULL == m->meta.title) {
184: if ( ! man_nmsg(m, n, MANDOCERR_NOTITLE))
1.17 schwarze 185: return(0);
1.18 schwarze 186: /*
187: * If a title hasn't been set, do so now (by
188: * implication, date and section also aren't set).
189: *
190: * FIXME: this should be in man_action.c.
191: */
1.17 schwarze 192: m->meta.title = mandoc_strdup("unknown");
1.18 schwarze 193: m->meta.date = time(NULL);
1.21 schwarze 194: m->meta.msec = mandoc_strdup("1");
1.17 schwarze 195: }
1.15 schwarze 196:
197: return(1);
198: }
199:
200:
201: static int
202: check_title(CHKARGS)
203: {
204: const char *p;
205:
206: assert(n->child);
1.25 schwarze 207: /* FIXME: is this sufficient? */
208: if ('\0' == *n->child->string) {
209: man_nmsg(m, n, MANDOCERR_SYNTARGCOUNT);
210: return(0);
211: }
1.15 schwarze 212:
213: for (p = n->child->string; '\0' != *p; p++)
214: if (isalpha((u_char)*p) && ! isupper((u_char)*p))
1.25 schwarze 215: if ( ! man_nmsg(m, n, MANDOCERR_UPPERCASE))
1.15 schwarze 216: return(0);
1.4 schwarze 217:
218: return(1);
219: }
220:
221:
222: static int
1.7 schwarze 223: check_text(CHKARGS)
1.4 schwarze 224: {
1.27 schwarze 225: char *p;
1.5 schwarze 226: int pos, c;
1.28 schwarze 227: size_t sz;
1.4 schwarze 228:
1.28 schwarze 229: for (p = n->string, pos = n->pos + 1; *p; p++, pos++) {
230: sz = strcspn(p, "\t\\");
231: p += (int)sz;
232:
233: if ('\0' == *p)
234: break;
235:
236: pos += (int)sz;
1.4 schwarze 237:
1.28 schwarze 238: if ('\t' == *p) {
239: if (MAN_LITERAL & m->flags)
240: continue;
241: if (man_pmsg(m, n->line, pos, MANDOCERR_BADTAB))
1.5 schwarze 242: continue;
1.28 schwarze 243: return(0);
244: }
245:
246: /* Check the special character. */
1.25 schwarze 247:
1.28 schwarze 248: c = mandoc_special(p);
249: if (c) {
250: p += c - 1;
251: pos += c - 1;
1.29 schwarze 252: } else
253: man_pmsg(m, n->line, pos, MANDOCERR_BADESCAPE);
1.4 schwarze 254: }
255:
256: return(1);
1.1 kristaps 257: }
258:
259:
260: #define INEQ_DEFINE(x, ineq, name) \
261: static int \
1.7 schwarze 262: check_##name(CHKARGS) \
1.1 kristaps 263: { \
1.4 schwarze 264: if (n->nchild ineq (x)) \
1.1 kristaps 265: return(1); \
1.25 schwarze 266: man_vmsg(m, MANDOCERR_SYNTARGCOUNT, n->line, n->pos, \
267: "line arguments %s %d (have %d)", \
268: #ineq, (x), n->nchild); \
269: return(0); \
1.1 kristaps 270: }
271:
272: INEQ_DEFINE(0, ==, eq0)
1.10 schwarze 273: INEQ_DEFINE(1, <=, le1)
1.1 kristaps 274: INEQ_DEFINE(2, >=, ge2)
275: INEQ_DEFINE(5, <=, le5)
1.32 schwarze 276:
277:
278: static int
279: check_ft(CHKARGS)
280: {
281: char *cp;
282: int ok;
283:
284: if (0 == n->nchild)
285: return(1);
286:
287: ok = 0;
288: cp = n->child->string;
289: switch (*cp) {
290: case ('1'):
291: /* FALLTHROUGH */
292: case ('2'):
293: /* FALLTHROUGH */
294: case ('3'):
295: /* FALLTHROUGH */
296: case ('4'):
297: /* FALLTHROUGH */
298: case ('I'):
299: /* FALLTHROUGH */
300: case ('P'):
301: /* FALLTHROUGH */
302: case ('R'):
303: if ('\0' == cp[1])
304: ok = 1;
305: break;
306: case ('B'):
307: if ('\0' == cp[1] || ('I' == cp[1] && '\0' == cp[2]))
308: ok = 1;
309: break;
310: case ('C'):
311: if ('W' == cp[1] && '\0' == cp[2])
312: ok = 1;
313: break;
314: default:
315: break;
316: }
317:
318: if (0 == ok) {
319: man_vmsg(m, MANDOCERR_BADFONT, n->line, n->pos, "%s", cp);
320: *cp = '\0';
321: }
322:
323: if (1 < n->nchild)
324: man_vmsg(m, MANDOCERR_ARGCOUNT, n->line, n->pos,
325: "want one child (have %d)", n->nchild);
326:
327: return(1);
328: }
1.7 schwarze 329:
330:
331: static int
332: check_sec(CHKARGS)
333: {
1.6 schwarze 334:
1.25 schwarze 335: if (MAN_HEAD == n->type && 0 == n->nchild) {
336: man_nmsg(m, n, MANDOCERR_SYNTARGCOUNT);
337: return(0);
338: } else if (MAN_BODY == n->type && 0 == n->nchild)
339: return(man_nmsg(m, n, MANDOCERR_NOBODY));
340:
1.6 schwarze 341: return(1);
342: }
1.7 schwarze 343:
344:
345: static int
1.8 schwarze 346: check_part(CHKARGS)
347: {
348:
349: if (MAN_BODY == n->type && 0 == n->nchild)
1.25 schwarze 350: return(man_nmsg(m, n, MANDOCERR_NOBODY));
1.8 schwarze 351: return(1);
352: }
353:
1.34 ! schwarze 354: static int
! 355: post_TH(CHKARGS)
! 356: {
! 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);
! 366: if (m->meta.rawdate)
! 367: free(m->meta.rawdate);
! 368:
! 369: m->meta.title = m->meta.vol = m->meta.rawdate =
! 370: m->meta.msec = m->meta.source = NULL;
! 371: m->meta.date = 0;
! 372:
! 373: /* ->TITLE<- MSEC DATE SOURCE VOL */
! 374:
! 375: n = n->child;
! 376: assert(n);
! 377: m->meta.title = mandoc_strdup(n->string);
! 378:
! 379: /* TITLE ->MSEC<- DATE SOURCE VOL */
! 380:
! 381: n = n->next;
! 382: assert(n);
! 383: m->meta.msec = mandoc_strdup(n->string);
! 384:
! 385: /* TITLE MSEC ->DATE<- SOURCE VOL */
! 386:
! 387: /*
! 388: * Try to parse the date. If this works, stash the epoch (this
! 389: * is optimal because we can reformat it in the canonical form).
! 390: * If it doesn't parse, isn't specified at all, or is an empty
! 391: * string, then use the current date.
! 392: */
! 393:
! 394: n = n->next;
! 395: if (n && n->string && *n->string) {
! 396: m->meta.date = mandoc_a2time
! 397: (MTIME_ISO_8601, n->string);
! 398: if (0 == m->meta.date) {
! 399: man_nmsg(m, n, MANDOCERR_BADDATE);
! 400: m->meta.rawdate = mandoc_strdup(n->string);
! 401: }
! 402: } else
! 403: m->meta.date = time(NULL);
! 404:
! 405: /* TITLE MSEC DATE ->SOURCE<- VOL */
! 406:
! 407: if (n && (n = n->next))
! 408: m->meta.source = mandoc_strdup(n->string);
! 409:
! 410: /* TITLE MSEC DATE SOURCE ->VOL<- */
! 411:
! 412: if (n && (n = n->next))
! 413: m->meta.vol = mandoc_strdup(n->string);
! 414:
! 415: /*
! 416: * Remove the `TH' node after we've processed it for our
! 417: * meta-data.
! 418: */
! 419: man_node_delete(m, m->last);
! 420: return(1);
! 421: }
! 422:
! 423: static int
! 424: post_nf(CHKARGS)
! 425: {
! 426:
! 427: if (MAN_LITERAL & m->flags)
! 428: man_nmsg(m, n, MANDOCERR_SCOPEREP);
! 429:
! 430: m->flags |= MAN_LITERAL;
! 431: return(1);
! 432: }
! 433:
! 434: static int
! 435: post_fi(CHKARGS)
! 436: {
! 437:
! 438: if ( ! (MAN_LITERAL & m->flags))
! 439: man_nmsg(m, n, MANDOCERR_NOSCOPE);
! 440:
! 441: m->flags &= ~MAN_LITERAL;
! 442: return(1);
! 443: }
! 444:
! 445: static int
! 446: post_UC(CHKARGS)
! 447: {
! 448: static const char * const bsd_versions[] = {
! 449: "3rd Berkeley Distribution",
! 450: "4th Berkeley Distribution",
! 451: "4.2 Berkeley Distribution",
! 452: "4.3 Berkeley Distribution",
! 453: "4.4 Berkeley Distribution",
! 454: };
! 455:
! 456: const char *p, *s;
! 457:
! 458: n = n->child;
! 459: n = m->last->child;
! 460:
! 461: if (NULL == n || MAN_TEXT != n->type)
! 462: p = bsd_versions[0];
! 463: else {
! 464: s = n->string;
! 465: if (0 == strcmp(s, "3"))
! 466: p = bsd_versions[0];
! 467: else if (0 == strcmp(s, "4"))
! 468: p = bsd_versions[1];
! 469: else if (0 == strcmp(s, "5"))
! 470: p = bsd_versions[2];
! 471: else if (0 == strcmp(s, "6"))
! 472: p = bsd_versions[3];
! 473: else if (0 == strcmp(s, "7"))
! 474: p = bsd_versions[4];
! 475: else
! 476: p = bsd_versions[0];
! 477: }
! 478:
! 479: if (m->meta.source)
! 480: free(m->meta.source);
! 481:
! 482: m->meta.source = mandoc_strdup(p);
! 483: return(1);
! 484: }
! 485:
! 486: static int
! 487: post_AT(CHKARGS)
! 488: {
! 489: static const char * const unix_versions[] = {
! 490: "7th Edition",
! 491: "System III",
! 492: "System V",
! 493: "System V Release 2",
! 494: };
! 495:
! 496: const char *p, *s;
! 497: struct man_node *nn;
! 498:
! 499: n = n->child;
! 500:
! 501: if (NULL == n || MAN_TEXT != n->type)
! 502: p = unix_versions[0];
! 503: else {
! 504: s = n->string;
! 505: if (0 == strcmp(s, "3"))
! 506: p = unix_versions[0];
! 507: else if (0 == strcmp(s, "4"))
! 508: p = unix_versions[1];
! 509: else if (0 == strcmp(s, "5")) {
! 510: nn = n->next;
! 511: if (nn && MAN_TEXT == nn->type && nn->string[0])
! 512: p = unix_versions[3];
! 513: else
! 514: p = unix_versions[2];
! 515: } else
! 516: p = unix_versions[0];
! 517: }
! 518:
! 519: if (m->meta.source)
! 520: free(m->meta.source);
! 521:
! 522: m->meta.source = mandoc_strdup(p);
! 523: return(1);
! 524: }
! 525:
! 526: static int
! 527: post_TS(CHKARGS)
! 528: {
! 529:
! 530: if (MAN_HEAD == n->type)
! 531: n->parent->data.TS = tbl_alloc();
! 532:
! 533: return(1);
! 534: }
1.8 schwarze 535:
536: static int
1.7 schwarze 537: check_par(CHKARGS)
538: {
539:
540: if (MAN_BODY == n->type)
541: switch (n->tok) {
542: case (MAN_IP):
543: /* FALLTHROUGH */
544: case (MAN_HP):
545: /* FALLTHROUGH */
546: case (MAN_TP):
547: /* Body-less lists are ok. */
548: break;
549: default:
550: if (n->nchild)
551: break;
1.25 schwarze 552: return(man_nmsg(m, n, MANDOCERR_NOBODY));
1.7 schwarze 553: }
554: if (MAN_HEAD == n->type)
555: switch (n->tok) {
556: case (MAN_PP):
557: /* FALLTHROUGH */
558: case (MAN_P):
559: /* FALLTHROUGH */
560: case (MAN_LP):
561: if (0 == n->nchild)
562: break;
1.25 schwarze 563: return(man_nmsg(m, n, MANDOCERR_ARGSLOST));
1.7 schwarze 564: default:
1.31 schwarze 565: break;
1.7 schwarze 566: }
567:
568: return(1);
569: }
570:
571:
572: static int
573: check_bline(CHKARGS)
574: {
575:
1.8 schwarze 576: assert( ! (MAN_ELINE & m->flags));
1.25 schwarze 577: if (MAN_BLINE & m->flags) {
578: man_nmsg(m, n, MANDOCERR_SYNTLINESCOPE);
579: return(0);
580: }
1.14 schwarze 581:
1.7 schwarze 582: return(1);
583: }
584: