Annotation of mandoc/man_validate.c, Revision 1.51
1.51 ! kristaps 1: /* $Id: man_validate.c,v 1.50 2010/10/11 15:45:36 kristaps Exp $ */
1.1 kristaps 2: /*
1.48 schwarze 3: * Copyright (c) 2008, 2009, 2010 Kristaps Dzonsons <kristaps@bsd.lv>
1.1 kristaps 4: *
5: * Permission to use, copy, modify, and distribute this software for any
1.8 kristaps 6: * purpose with or without fee is hereby granted, provided that the above
7: * copyright notice and this permission notice appear in all copies.
1.1 kristaps 8: *
1.8 kristaps 9: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12: * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13: * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14: * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15: * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1.1 kristaps 16: */
1.28 kristaps 17: #ifdef HAVE_CONFIG_H
18: #include "config.h"
19: #endif
20:
1.1 kristaps 21: #include <sys/types.h>
22:
23: #include <assert.h>
24: #include <ctype.h>
1.16 kristaps 25: #include <errno.h>
26: #include <limits.h>
1.1 kristaps 27: #include <stdarg.h>
28: #include <stdlib.h>
1.46 kristaps 29: #include <string.h>
1.50 kristaps 30: #include <time.h>
1.1 kristaps 31:
1.41 kristaps 32: #include "mandoc.h"
1.1 kristaps 33: #include "libman.h"
1.15 kristaps 34: #include "libmandoc.h"
1.1 kristaps 35:
1.43 kristaps 36: #define CHKARGS struct man *m, struct man_node *n
1.1 kristaps 37:
1.17 kristaps 38: typedef int (*v_check)(CHKARGS);
1.1 kristaps 39:
40: struct man_valid {
1.17 kristaps 41: v_check *pres;
42: v_check *posts;
1.1 kristaps 43: };
44:
1.17 kristaps 45: static int check_bline(CHKARGS);
46: static int check_eq0(CHKARGS);
1.25 kristaps 47: static int check_le1(CHKARGS);
1.17 kristaps 48: static int check_ge2(CHKARGS);
49: static int check_le5(CHKARGS);
50: static int check_par(CHKARGS);
1.23 kristaps 51: static int check_part(CHKARGS);
1.17 kristaps 52: static int check_root(CHKARGS);
53: static int check_sec(CHKARGS);
54: static int check_text(CHKARGS);
1.32 kristaps 55: static int check_title(CHKARGS);
1.17 kristaps 56:
1.51 ! kristaps 57: static int post_AT(CHKARGS);
! 58: static int post_fi(CHKARGS);
! 59: static int post_nf(CHKARGS);
! 60: static int post_TH(CHKARGS);
! 61: static int post_UC(CHKARGS);
! 62:
! 63: static v_check posts_at[] = { post_AT, NULL };
1.17 kristaps 64: static v_check posts_eq0[] = { check_eq0, NULL };
1.51 ! kristaps 65: static v_check posts_fi[] = { check_eq0, post_fi, NULL };
! 66: static v_check posts_le1[] = { check_le1, NULL };
! 67: static v_check posts_nf[] = { check_eq0, post_nf, NULL };
1.17 kristaps 68: static v_check posts_par[] = { check_par, NULL };
1.23 kristaps 69: static v_check posts_part[] = { check_part, NULL };
1.17 kristaps 70: static v_check posts_sec[] = { check_sec, NULL };
1.51 ! kristaps 71: static v_check posts_th[] = { check_ge2, check_le5, check_title, post_TH, NULL };
! 72: static v_check posts_uc[] = { post_UC, NULL };
! 73: static v_check posts_vb[] = { check_le1, post_nf, NULL };
1.17 kristaps 74: static v_check pres_bline[] = { check_bline, NULL };
1.1 kristaps 75:
1.51 ! kristaps 76:
1.1 kristaps 77: static const struct man_valid man_valids[MAN_MAX] = {
1.29 kristaps 78: { NULL, posts_eq0 }, /* br */
1.32 kristaps 79: { pres_bline, posts_th }, /* TH */
1.17 kristaps 80: { pres_bline, posts_sec }, /* SH */
81: { pres_bline, posts_sec }, /* SS */
82: { pres_bline, posts_par }, /* TP */
83: { pres_bline, posts_par }, /* LP */
84: { pres_bline, posts_par }, /* PP */
85: { pres_bline, posts_par }, /* P */
86: { pres_bline, posts_par }, /* IP */
87: { pres_bline, posts_par }, /* HP */
1.22 kristaps 88: { NULL, NULL }, /* SM */
89: { NULL, NULL }, /* SB */
1.17 kristaps 90: { NULL, NULL }, /* BI */
91: { NULL, NULL }, /* IB */
92: { NULL, NULL }, /* BR */
93: { NULL, NULL }, /* RB */
1.22 kristaps 94: { NULL, NULL }, /* R */
95: { NULL, NULL }, /* B */
96: { NULL, NULL }, /* I */
1.17 kristaps 97: { NULL, NULL }, /* IR */
98: { NULL, NULL }, /* RI */
1.47 kristaps 99: { NULL, posts_eq0 }, /* na */ /* FIXME: should warn only. */
1.17 kristaps 100: { NULL, NULL }, /* i */
1.47 kristaps 101: { NULL, posts_le1 }, /* sp */ /* FIXME: should warn only. */
1.51 ! kristaps 102: { pres_bline, posts_nf }, /* nf */
! 103: { pres_bline, posts_fi }, /* fi */
1.17 kristaps 104: { NULL, NULL }, /* r */
1.19 kristaps 105: { NULL, NULL }, /* RE */
1.23 kristaps 106: { NULL, posts_part }, /* RS */
1.21 kristaps 107: { NULL, NULL }, /* DT */
1.51 ! kristaps 108: { NULL, posts_uc }, /* UC */
1.26 kristaps 109: { NULL, NULL }, /* PD */
1.47 kristaps 110: { NULL, posts_le1 }, /* Sp */ /* FIXME: should warn only. */
1.51 ! kristaps 111: { pres_bline, posts_vb }, /* Vb */ /* FIXME: should warn only. */
! 112: { pres_bline, posts_fi }, /* Ve */
! 113: { NULL, posts_at }, /* AT */
1.47 kristaps 114: { NULL, NULL }, /* in */
1.1 kristaps 115: };
116:
117:
118: int
1.43 kristaps 119: man_valid_pre(struct man *m, struct man_node *n)
1.17 kristaps 120: {
121: v_check *cp;
122:
123: if (MAN_TEXT == n->type)
124: return(1);
125: if (MAN_ROOT == n->type)
126: return(1);
127:
128: if (NULL == (cp = man_valids[n->tok].pres))
129: return(1);
130: for ( ; *cp; cp++)
131: if ( ! (*cp)(m, n))
132: return(0);
133: return(1);
134: }
135:
136:
137: int
1.1 kristaps 138: man_valid_post(struct man *m)
139: {
1.17 kristaps 140: v_check *cp;
1.1 kristaps 141:
142: if (MAN_VALID & m->last->flags)
143: return(1);
144: m->last->flags |= MAN_VALID;
145:
146: switch (m->last->type) {
147: case (MAN_TEXT):
1.11 kristaps 148: return(check_text(m, m->last));
1.1 kristaps 149: case (MAN_ROOT):
1.14 kristaps 150: return(check_root(m, m->last));
1.1 kristaps 151: default:
152: break;
153: }
154:
155: if (NULL == (cp = man_valids[m->last->tok].posts))
156: return(1);
157: for ( ; *cp; cp++)
158: if ( ! (*cp)(m, m->last))
159: return(0);
160:
161: return(1);
162: }
163:
164:
1.11 kristaps 165: static int
1.17 kristaps 166: check_root(CHKARGS)
1.14 kristaps 167: {
1.17 kristaps 168:
169: if (MAN_BLINE & m->flags)
1.41 kristaps 170: return(man_nmsg(m, n, MANDOCERR_SCOPEEXIT));
1.17 kristaps 171: if (MAN_ELINE & m->flags)
1.41 kristaps 172: return(man_nmsg(m, n, MANDOCERR_SCOPEEXIT));
1.22 kristaps 173:
174: m->flags &= ~MAN_BLINE;
175: m->flags &= ~MAN_ELINE;
1.17 kristaps 176:
1.41 kristaps 177: if (NULL == m->first->child) {
178: man_nmsg(m, n, MANDOCERR_NODOCBODY);
179: return(0);
180: } else if (NULL == m->meta.title) {
181: if ( ! man_nmsg(m, n, MANDOCERR_NOTITLE))
1.34 kristaps 182: return(0);
183: /*
184: * If a title hasn't been set, do so now (by
185: * implication, date and section also aren't set).
186: *
187: * FIXME: this should be in man_action.c.
188: */
189: m->meta.title = mandoc_strdup("unknown");
190: m->meta.date = time(NULL);
1.37 kristaps 191: m->meta.msec = mandoc_strdup("1");
1.34 kristaps 192: }
1.32 kristaps 193:
194: return(1);
195: }
196:
197:
198: static int
199: check_title(CHKARGS)
200: {
201: const char *p;
202:
203: assert(n->child);
1.41 kristaps 204: /* FIXME: is this sufficient? */
205: if ('\0' == *n->child->string) {
206: man_nmsg(m, n, MANDOCERR_SYNTARGCOUNT);
207: return(0);
208: }
1.32 kristaps 209:
210: for (p = n->child->string; '\0' != *p; p++)
211: if (isalpha((u_char)*p) && ! isupper((u_char)*p))
1.41 kristaps 212: if ( ! man_nmsg(m, n, MANDOCERR_UPPERCASE))
1.32 kristaps 213: return(0);
1.14 kristaps 214:
215: return(1);
216: }
217:
218:
219: static int
1.17 kristaps 220: check_text(CHKARGS)
1.11 kristaps 221: {
1.43 kristaps 222: char *p;
1.15 kristaps 223: int pos, c;
1.46 kristaps 224: size_t sz;
1.11 kristaps 225:
1.46 kristaps 226: for (p = n->string, pos = n->pos + 1; *p; p++, pos++) {
227: sz = strcspn(p, "\t\\");
228: p += (int)sz;
229:
230: if ('\0' == *p)
231: break;
232:
233: pos += (int)sz;
1.11 kristaps 234:
1.46 kristaps 235: if ('\t' == *p) {
236: if (MAN_LITERAL & m->flags)
237: continue;
238: if (man_pmsg(m, n->line, pos, MANDOCERR_BADTAB))
1.15 kristaps 239: continue;
1.46 kristaps 240: return(0);
1.15 kristaps 241: }
1.45 kristaps 242:
1.46 kristaps 243: /* Check the special character. */
1.15 kristaps 244:
1.46 kristaps 245: c = mandoc_special(p);
246: if (c) {
247: p += c - 1;
248: pos += c - 1;
1.49 schwarze 249: } else
250: man_pmsg(m, n->line, pos, MANDOCERR_BADESCAPE);
1.11 kristaps 251: }
252:
253: return(1);
1.1 kristaps 254: }
255:
256:
257: #define INEQ_DEFINE(x, ineq, name) \
258: static int \
1.17 kristaps 259: check_##name(CHKARGS) \
1.1 kristaps 260: { \
1.11 kristaps 261: if (n->nchild ineq (x)) \
1.1 kristaps 262: return(1); \
1.41 kristaps 263: man_vmsg(m, MANDOCERR_SYNTARGCOUNT, n->line, n->pos, \
264: "line arguments %s %d (have %d)", \
265: #ineq, (x), n->nchild); \
266: return(0); \
1.1 kristaps 267: }
268:
269: INEQ_DEFINE(0, ==, eq0)
1.25 kristaps 270: INEQ_DEFINE(1, <=, le1)
1.1 kristaps 271: INEQ_DEFINE(2, >=, ge2)
272: INEQ_DEFINE(5, <=, le5)
273:
1.16 kristaps 274:
275: static int
1.17 kristaps 276: check_sec(CHKARGS)
277: {
1.16 kristaps 278:
1.41 kristaps 279: if (MAN_HEAD == n->type && 0 == n->nchild) {
280: man_nmsg(m, n, MANDOCERR_SYNTARGCOUNT);
281: return(0);
282: } else if (MAN_BODY == n->type && 0 == n->nchild)
283: return(man_nmsg(m, n, MANDOCERR_NOBODY));
284:
1.16 kristaps 285: return(1);
286: }
1.17 kristaps 287:
288:
289: static int
1.23 kristaps 290: check_part(CHKARGS)
291: {
292:
293: if (MAN_BODY == n->type && 0 == n->nchild)
1.41 kristaps 294: return(man_nmsg(m, n, MANDOCERR_NOBODY));
1.23 kristaps 295: return(1);
296: }
297:
298:
299: static int
1.17 kristaps 300: check_par(CHKARGS)
301: {
302:
303: if (MAN_BODY == n->type)
304: switch (n->tok) {
305: case (MAN_IP):
306: /* FALLTHROUGH */
307: case (MAN_HP):
308: /* FALLTHROUGH */
309: case (MAN_TP):
310: /* Body-less lists are ok. */
311: break;
312: default:
313: if (n->nchild)
314: break;
1.41 kristaps 315: return(man_nmsg(m, n, MANDOCERR_NOBODY));
1.17 kristaps 316: }
317: if (MAN_HEAD == n->type)
318: switch (n->tok) {
319: case (MAN_PP):
320: /* FALLTHROUGH */
321: case (MAN_P):
322: /* FALLTHROUGH */
323: case (MAN_LP):
324: if (0 == n->nchild)
325: break;
1.41 kristaps 326: return(man_nmsg(m, n, MANDOCERR_ARGSLOST));
1.17 kristaps 327: default:
328: if (n->nchild)
329: break;
1.41 kristaps 330: return(man_nmsg(m, n, MANDOCERR_NOARGS));
1.17 kristaps 331: }
332:
333: return(1);
334: }
335:
336:
337: static int
338: check_bline(CHKARGS)
339: {
340:
1.22 kristaps 341: assert( ! (MAN_ELINE & m->flags));
1.41 kristaps 342: if (MAN_BLINE & m->flags) {
343: man_nmsg(m, n, MANDOCERR_SYNTLINESCOPE);
344: return(0);
345: }
1.31 kristaps 346:
1.18 kristaps 347: return(1);
1.17 kristaps 348: }
349:
1.51 ! kristaps 350: static int
! 351: post_TH(CHKARGS)
! 352: {
! 353:
! 354: if (m->meta.title)
! 355: free(m->meta.title);
! 356: if (m->meta.vol)
! 357: free(m->meta.vol);
! 358: if (m->meta.source)
! 359: free(m->meta.source);
! 360: if (m->meta.msec)
! 361: free(m->meta.msec);
! 362: if (m->meta.rawdate)
! 363: free(m->meta.rawdate);
! 364:
! 365: m->meta.title = m->meta.vol = m->meta.rawdate =
! 366: m->meta.msec = m->meta.source = NULL;
! 367: m->meta.date = 0;
! 368:
! 369: /* ->TITLE<- MSEC DATE SOURCE VOL */
! 370:
! 371: n = n->child;
! 372: assert(n);
! 373: m->meta.title = mandoc_strdup(n->string);
! 374:
! 375: /* TITLE ->MSEC<- DATE SOURCE VOL */
! 376:
! 377: n = n->next;
! 378: assert(n);
! 379: m->meta.msec = mandoc_strdup(n->string);
! 380:
! 381: /* TITLE MSEC ->DATE<- SOURCE VOL */
! 382:
! 383: /*
! 384: * Try to parse the date. If this works, stash the epoch (this
! 385: * is optimal because we can reformat it in the canonical form).
! 386: * If it doesn't parse, isn't specified at all, or is an empty
! 387: * string, then use the current date.
! 388: */
! 389:
! 390: n = n->next;
! 391: if (n && n->string && *n->string) {
! 392: m->meta.date = mandoc_a2time
! 393: (MTIME_ISO_8601, n->string);
! 394: if (0 == m->meta.date) {
! 395: man_nmsg(m, n, MANDOCERR_BADDATE);
! 396: m->meta.rawdate = mandoc_strdup(n->string);
! 397: }
! 398: } else
! 399: m->meta.date = time(NULL);
! 400:
! 401: /* TITLE MSEC DATE ->SOURCE<- VOL */
! 402:
! 403: if (n && (n = n->next))
! 404: m->meta.source = mandoc_strdup(n->string);
! 405:
! 406: /* TITLE MSEC DATE SOURCE ->VOL<- */
! 407:
! 408: if (n && (n = n->next))
! 409: m->meta.vol = mandoc_strdup(n->string);
! 410:
! 411: /*
! 412: * Remove the `TH' node after we've processed it for our
! 413: * meta-data.
! 414: */
! 415: man_node_delete(m, m->last);
! 416: return(1);
! 417: }
! 418:
! 419: static int
! 420: post_nf(CHKARGS)
! 421: {
! 422:
! 423: if (MAN_LITERAL & m->flags)
! 424: man_nmsg(m, n, MANDOCERR_SCOPEREP);
! 425:
! 426: m->flags |= MAN_LITERAL;
! 427: return(1);
! 428: }
! 429:
! 430: static int
! 431: post_fi(CHKARGS)
! 432: {
! 433:
! 434: if ( ! (MAN_LITERAL & m->flags))
! 435: man_nmsg(m, n, MANDOCERR_NOSCOPE);
! 436:
! 437: m->flags &= ~MAN_LITERAL;
! 438: return(1);
! 439: }
! 440:
! 441: static int
! 442: post_UC(CHKARGS)
! 443: {
! 444: static const char * const bsd_versions[] = {
! 445: "3rd Berkeley Distribution",
! 446: "4th Berkeley Distribution",
! 447: "4.2 Berkeley Distribution",
! 448: "4.3 Berkeley Distribution",
! 449: "4.4 Berkeley Distribution",
! 450: };
! 451:
! 452: const char *p, *s;
! 453:
! 454: n = n->child;
! 455: n = m->last->child;
! 456:
! 457: if (NULL == n || MAN_TEXT != n->type)
! 458: p = bsd_versions[0];
! 459: else {
! 460: s = n->string;
! 461: if (0 == strcmp(s, "3"))
! 462: p = bsd_versions[0];
! 463: else if (0 == strcmp(s, "4"))
! 464: p = bsd_versions[1];
! 465: else if (0 == strcmp(s, "5"))
! 466: p = bsd_versions[2];
! 467: else if (0 == strcmp(s, "6"))
! 468: p = bsd_versions[3];
! 469: else if (0 == strcmp(s, "7"))
! 470: p = bsd_versions[4];
! 471: else
! 472: p = bsd_versions[0];
! 473: }
! 474:
! 475: if (m->meta.source)
! 476: free(m->meta.source);
! 477:
! 478: m->meta.source = mandoc_strdup(p);
! 479: return(1);
! 480: }
! 481:
! 482: static int
! 483: post_AT(CHKARGS)
! 484: {
! 485: static const char * const unix_versions[] = {
! 486: "7th Edition",
! 487: "System III",
! 488: "System V",
! 489: "System V Release 2",
! 490: };
! 491:
! 492: const char *p, *s;
! 493: struct man_node *nn;
! 494:
! 495: n = n->child;
! 496:
! 497: if (NULL == n || MAN_TEXT != n->type)
! 498: p = unix_versions[0];
! 499: else {
! 500: s = n->string;
! 501: if (0 == strcmp(s, "3"))
! 502: p = unix_versions[0];
! 503: else if (0 == strcmp(s, "4"))
! 504: p = unix_versions[1];
! 505: else if (0 == strcmp(s, "5")) {
! 506: nn = n->next;
! 507: if (nn && MAN_TEXT == nn->type && nn->string[0])
! 508: p = unix_versions[3];
! 509: else
! 510: p = unix_versions[2];
! 511: } else
! 512: p = unix_versions[0];
! 513: }
! 514:
! 515: if (m->meta.source)
! 516: free(m->meta.source);
! 517:
! 518: m->meta.source = mandoc_strdup(p);
! 519: return(1);
! 520: }
CVSweb