Annotation of mandoc/man_validate.c, Revision 1.59
1.59 ! kristaps 1: /* $Id: man_validate.c,v 1.58 2011/01/12 15:50:42 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.55 kristaps 47: static int check_ft(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);
54: static int check_sec(CHKARGS);
55: static int check_text(CHKARGS);
1.32 kristaps 56: static int check_title(CHKARGS);
1.17 kristaps 57:
1.51 kristaps 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_UC(CHKARGS);
63:
64: static v_check posts_at[] = { post_AT, NULL };
1.17 kristaps 65: static v_check posts_eq0[] = { check_eq0, NULL };
1.51 kristaps 66: static v_check posts_fi[] = { check_eq0, post_fi, NULL };
67: static v_check posts_le1[] = { check_le1, NULL };
1.55 kristaps 68: static v_check posts_ft[] = { check_ft, NULL };
1.51 kristaps 69: static v_check posts_nf[] = { check_eq0, post_nf, NULL };
1.17 kristaps 70: static v_check posts_par[] = { check_par, NULL };
1.23 kristaps 71: static v_check posts_part[] = { check_part, NULL };
1.17 kristaps 72: static v_check posts_sec[] = { check_sec, NULL };
1.51 kristaps 73: static v_check posts_th[] = { check_ge2, check_le5, check_title, post_TH, NULL };
74: static v_check posts_uc[] = { post_UC, NULL };
1.17 kristaps 75: static v_check pres_bline[] = { check_bline, NULL };
1.1 kristaps 76:
1.51 kristaps 77:
1.1 kristaps 78: static const struct man_valid man_valids[MAN_MAX] = {
1.29 kristaps 79: { NULL, posts_eq0 }, /* br */
1.32 kristaps 80: { pres_bline, posts_th }, /* TH */
1.17 kristaps 81: { pres_bline, posts_sec }, /* SH */
82: { pres_bline, posts_sec }, /* SS */
1.59 ! kristaps 83: { pres_bline, NULL }, /* TP */
1.17 kristaps 84: { pres_bline, posts_par }, /* LP */
85: { pres_bline, posts_par }, /* PP */
86: { pres_bline, posts_par }, /* P */
1.59 ! kristaps 87: { pres_bline, NULL }, /* IP */
! 88: { pres_bline, NULL }, /* HP */
1.22 kristaps 89: { NULL, NULL }, /* SM */
90: { NULL, NULL }, /* SB */
1.17 kristaps 91: { NULL, NULL }, /* BI */
92: { NULL, NULL }, /* IB */
93: { NULL, NULL }, /* BR */
94: { NULL, NULL }, /* RB */
1.22 kristaps 95: { NULL, NULL }, /* R */
96: { NULL, NULL }, /* B */
97: { NULL, NULL }, /* I */
1.17 kristaps 98: { NULL, NULL }, /* IR */
99: { NULL, NULL }, /* RI */
1.47 kristaps 100: { NULL, posts_eq0 }, /* na */ /* FIXME: should warn only. */
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.19 kristaps 104: { NULL, NULL }, /* RE */
1.23 kristaps 105: { NULL, posts_part }, /* RS */
1.21 kristaps 106: { NULL, NULL }, /* DT */
1.51 kristaps 107: { NULL, posts_uc }, /* UC */
1.26 kristaps 108: { NULL, NULL }, /* PD */
1.51 kristaps 109: { NULL, posts_at }, /* AT */
1.47 kristaps 110: { NULL, NULL }, /* in */
1.55 kristaps 111: { NULL, posts_ft }, /* ft */
1.1 kristaps 112: };
113:
114:
115: int
1.43 kristaps 116: man_valid_pre(struct man *m, struct man_node *n)
1.17 kristaps 117: {
118: v_check *cp;
119:
1.57 kristaps 120: switch (n->type) {
121: case (MAN_TEXT):
122: /* FALLTHROUGH */
123: case (MAN_ROOT):
124: /* FALLTHROUGH */
125: case (MAN_TBL):
1.17 kristaps 126: return(1);
1.57 kristaps 127: default:
128: break;
129: }
1.17 kristaps 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.17 kristaps 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.11 kristaps 151: return(check_text(m, m->last));
1.1 kristaps 152: case (MAN_ROOT):
1.14 kristaps 153: return(check_root(m, m->last));
1.57 kristaps 154: case (MAN_TBL):
155: return(1);
1.1 kristaps 156: default:
157: break;
158: }
159:
160: if (NULL == (cp = man_valids[m->last->tok].posts))
161: return(1);
162: for ( ; *cp; cp++)
163: if ( ! (*cp)(m, m->last))
164: return(0);
165:
166: return(1);
167: }
168:
169:
1.11 kristaps 170: static int
1.17 kristaps 171: check_root(CHKARGS)
1.14 kristaps 172: {
1.17 kristaps 173:
174: if (MAN_BLINE & m->flags)
1.54 kristaps 175: man_nmsg(m, n, MANDOCERR_SCOPEEXIT);
176: else if (MAN_ELINE & m->flags)
177: man_nmsg(m, n, MANDOCERR_SCOPEEXIT);
1.22 kristaps 178:
179: m->flags &= ~MAN_BLINE;
180: m->flags &= ~MAN_ELINE;
1.17 kristaps 181:
1.41 kristaps 182: if (NULL == m->first->child) {
183: man_nmsg(m, n, MANDOCERR_NODOCBODY);
184: return(0);
185: } else if (NULL == m->meta.title) {
1.54 kristaps 186: man_nmsg(m, n, MANDOCERR_NOTITLE);
187:
1.34 kristaps 188: /*
189: * If a title hasn't been set, do so now (by
190: * implication, date and section also aren't set).
191: */
1.54 kristaps 192:
1.34 kristaps 193: m->meta.title = mandoc_strdup("unknown");
194: m->meta.date = time(NULL);
1.37 kristaps 195: m->meta.msec = mandoc_strdup("1");
1.34 kristaps 196: }
1.32 kristaps 197:
198: return(1);
199: }
200:
201:
202: static int
203: check_title(CHKARGS)
204: {
205: const char *p;
206:
207: assert(n->child);
1.41 kristaps 208: /* FIXME: is this sufficient? */
209: if ('\0' == *n->child->string) {
210: man_nmsg(m, n, MANDOCERR_SYNTARGCOUNT);
211: return(0);
212: }
1.32 kristaps 213:
214: for (p = n->child->string; '\0' != *p; p++)
1.54 kristaps 215: /* Only warn about this once... */
216: if (isalpha((u_char)*p) && ! isupper((u_char)*p)) {
217: man_nmsg(m, n, MANDOCERR_UPPERCASE);
218: break;
219: }
1.14 kristaps 220:
221: return(1);
222: }
223:
224:
225: static int
1.17 kristaps 226: check_text(CHKARGS)
1.11 kristaps 227: {
1.43 kristaps 228: char *p;
1.15 kristaps 229: int pos, c;
1.46 kristaps 230: size_t sz;
1.11 kristaps 231:
1.46 kristaps 232: for (p = n->string, pos = n->pos + 1; *p; p++, pos++) {
233: sz = strcspn(p, "\t\\");
234: p += (int)sz;
235:
236: if ('\0' == *p)
237: break;
238:
239: pos += (int)sz;
1.11 kristaps 240:
1.46 kristaps 241: if ('\t' == *p) {
242: if (MAN_LITERAL & m->flags)
243: continue;
244: if (man_pmsg(m, n->line, pos, MANDOCERR_BADTAB))
1.15 kristaps 245: continue;
1.46 kristaps 246: return(0);
1.15 kristaps 247: }
1.45 kristaps 248:
1.46 kristaps 249: /* Check the special character. */
1.15 kristaps 250:
1.46 kristaps 251: c = mandoc_special(p);
252: if (c) {
253: p += c - 1;
254: pos += c - 1;
1.49 schwarze 255: } else
256: man_pmsg(m, n->line, pos, MANDOCERR_BADESCAPE);
1.11 kristaps 257: }
258:
259: return(1);
1.1 kristaps 260: }
261:
262:
263: #define INEQ_DEFINE(x, ineq, name) \
264: static int \
1.17 kristaps 265: check_##name(CHKARGS) \
1.1 kristaps 266: { \
1.11 kristaps 267: if (n->nchild ineq (x)) \
1.1 kristaps 268: return(1); \
1.41 kristaps 269: man_vmsg(m, MANDOCERR_SYNTARGCOUNT, n->line, n->pos, \
270: "line arguments %s %d (have %d)", \
271: #ineq, (x), n->nchild); \
272: return(0); \
1.1 kristaps 273: }
274:
275: INEQ_DEFINE(0, ==, eq0)
1.25 kristaps 276: INEQ_DEFINE(1, <=, le1)
1.1 kristaps 277: INEQ_DEFINE(2, >=, ge2)
278: INEQ_DEFINE(5, <=, le5)
279:
1.55 kristaps 280: static int
281: check_ft(CHKARGS)
282: {
283: char *cp;
284: int ok;
285:
286: if (0 == n->nchild)
287: return(1);
288:
289: ok = 0;
290: cp = n->child->string;
291: switch (*cp) {
292: case ('1'):
293: /* FALLTHROUGH */
294: case ('2'):
295: /* FALLTHROUGH */
296: case ('3'):
297: /* FALLTHROUGH */
298: case ('4'):
299: /* FALLTHROUGH */
300: case ('I'):
301: /* FALLTHROUGH */
302: case ('P'):
303: /* FALLTHROUGH */
304: case ('R'):
305: if ('\0' == cp[1])
306: ok = 1;
307: break;
308: case ('B'):
309: if ('\0' == cp[1] || ('I' == cp[1] && '\0' == cp[2]))
310: ok = 1;
311: break;
312: case ('C'):
313: if ('W' == cp[1] && '\0' == cp[2])
314: ok = 1;
315: break;
316: default:
317: break;
318: }
319:
320: if (0 == ok) {
321: man_vmsg(m, MANDOCERR_BADFONT,
322: n->line, n->pos, "%s", cp);
323: *cp = '\0';
324: }
325:
326: if (1 < n->nchild)
327: man_vmsg(m, MANDOCERR_ARGCOUNT, n->line, n->pos,
328: "want one child (have %d)", n->nchild);
329:
330: return(1);
331: }
1.16 kristaps 332:
333: static int
1.17 kristaps 334: check_sec(CHKARGS)
335: {
1.16 kristaps 336:
1.41 kristaps 337: if (MAN_HEAD == n->type && 0 == n->nchild) {
338: man_nmsg(m, n, MANDOCERR_SYNTARGCOUNT);
339: return(0);
340: } else if (MAN_BODY == n->type && 0 == n->nchild)
1.54 kristaps 341: man_nmsg(m, n, MANDOCERR_NOBODY);
1.41 kristaps 342:
1.16 kristaps 343: return(1);
344: }
1.17 kristaps 345:
346:
347: static int
1.23 kristaps 348: check_part(CHKARGS)
349: {
350:
351: if (MAN_BODY == n->type && 0 == n->nchild)
1.54 kristaps 352: man_nmsg(m, n, MANDOCERR_NOBODY);
353:
1.23 kristaps 354: return(1);
355: }
356:
357:
358: static int
1.17 kristaps 359: check_par(CHKARGS)
360: {
361:
1.59 ! kristaps 362: switch (n->type) {
! 363: case (MAN_BLOCK):
! 364: if (0 == n->body->nchild)
! 365: man_node_delete(m, n);
! 366: break;
! 367: case (MAN_BODY):
! 368: if (0 == n->nchild)
! 369: man_nmsg(m, n, MANDOCERR_IGNPAR);
! 370: break;
! 371: case (MAN_HEAD):
! 372: if (n->nchild)
! 373: man_nmsg(m, n, MANDOCERR_ARGSLOST);
! 374: break;
! 375: default:
! 376: break;
! 377: }
1.17 kristaps 378:
379: return(1);
380: }
381:
382:
383: static int
384: check_bline(CHKARGS)
385: {
386:
1.22 kristaps 387: assert( ! (MAN_ELINE & m->flags));
1.41 kristaps 388: if (MAN_BLINE & m->flags) {
389: man_nmsg(m, n, MANDOCERR_SYNTLINESCOPE);
390: return(0);
391: }
1.31 kristaps 392:
1.18 kristaps 393: return(1);
1.17 kristaps 394: }
395:
1.51 kristaps 396: static int
397: post_TH(CHKARGS)
398: {
399:
400: if (m->meta.title)
401: free(m->meta.title);
402: if (m->meta.vol)
403: free(m->meta.vol);
404: if (m->meta.source)
405: free(m->meta.source);
406: if (m->meta.msec)
407: free(m->meta.msec);
408: if (m->meta.rawdate)
409: free(m->meta.rawdate);
410:
411: m->meta.title = m->meta.vol = m->meta.rawdate =
412: m->meta.msec = m->meta.source = NULL;
413: m->meta.date = 0;
414:
415: /* ->TITLE<- MSEC DATE SOURCE VOL */
416:
417: n = n->child;
418: assert(n);
419: m->meta.title = mandoc_strdup(n->string);
420:
421: /* TITLE ->MSEC<- DATE SOURCE VOL */
422:
423: n = n->next;
424: assert(n);
425: m->meta.msec = mandoc_strdup(n->string);
426:
427: /* TITLE MSEC ->DATE<- SOURCE VOL */
428:
429: /*
430: * Try to parse the date. If this works, stash the epoch (this
431: * is optimal because we can reformat it in the canonical form).
432: * If it doesn't parse, isn't specified at all, or is an empty
433: * string, then use the current date.
434: */
435:
436: n = n->next;
437: if (n && n->string && *n->string) {
438: m->meta.date = mandoc_a2time
439: (MTIME_ISO_8601, n->string);
440: if (0 == m->meta.date) {
441: man_nmsg(m, n, MANDOCERR_BADDATE);
442: m->meta.rawdate = mandoc_strdup(n->string);
443: }
444: } else
445: m->meta.date = time(NULL);
446:
447: /* TITLE MSEC DATE ->SOURCE<- VOL */
448:
449: if (n && (n = n->next))
450: m->meta.source = mandoc_strdup(n->string);
451:
452: /* TITLE MSEC DATE SOURCE ->VOL<- */
453:
454: if (n && (n = n->next))
455: m->meta.vol = mandoc_strdup(n->string);
456:
457: /*
458: * Remove the `TH' node after we've processed it for our
459: * meta-data.
460: */
461: man_node_delete(m, m->last);
462: return(1);
463: }
464:
465: static int
466: post_nf(CHKARGS)
467: {
468:
469: if (MAN_LITERAL & m->flags)
470: man_nmsg(m, n, MANDOCERR_SCOPEREP);
471:
472: m->flags |= MAN_LITERAL;
473: return(1);
474: }
475:
476: static int
477: post_fi(CHKARGS)
478: {
479:
480: if ( ! (MAN_LITERAL & m->flags))
1.58 kristaps 481: man_nmsg(m, n, MANDOCERR_WNOSCOPE);
1.51 kristaps 482:
483: m->flags &= ~MAN_LITERAL;
484: return(1);
485: }
486:
487: static int
488: post_UC(CHKARGS)
489: {
490: static const char * const bsd_versions[] = {
491: "3rd Berkeley Distribution",
492: "4th Berkeley Distribution",
493: "4.2 Berkeley Distribution",
494: "4.3 Berkeley Distribution",
495: "4.4 Berkeley Distribution",
496: };
497:
498: const char *p, *s;
499:
500: n = n->child;
501: n = m->last->child;
502:
503: if (NULL == n || MAN_TEXT != n->type)
504: p = bsd_versions[0];
505: else {
506: s = n->string;
507: if (0 == strcmp(s, "3"))
508: p = bsd_versions[0];
509: else if (0 == strcmp(s, "4"))
510: p = bsd_versions[1];
511: else if (0 == strcmp(s, "5"))
512: p = bsd_versions[2];
513: else if (0 == strcmp(s, "6"))
514: p = bsd_versions[3];
515: else if (0 == strcmp(s, "7"))
516: p = bsd_versions[4];
517: else
518: p = bsd_versions[0];
519: }
520:
521: if (m->meta.source)
522: free(m->meta.source);
523:
524: m->meta.source = mandoc_strdup(p);
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:
561: if (m->meta.source)
562: free(m->meta.source);
563:
564: m->meta.source = mandoc_strdup(p);
565: return(1);
566: }
CVSweb