Annotation of mandoc/man_validate.c, Revision 1.56
1.56 ! kristaps 1: /* $Id: man_validate.c,v 1.55 2010/12/06 15:31:19 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 */
83: { pres_bline, posts_par }, /* TP */
84: { pres_bline, posts_par }, /* LP */
85: { pres_bline, posts_par }, /* PP */
86: { pres_bline, posts_par }, /* P */
87: { pres_bline, posts_par }, /* IP */
88: { pres_bline, posts_par }, /* 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:
120: if (MAN_TEXT == n->type)
121: return(1);
122: if (MAN_ROOT == n->type)
123: return(1);
124:
125: if (NULL == (cp = man_valids[n->tok].pres))
126: return(1);
127: for ( ; *cp; cp++)
128: if ( ! (*cp)(m, n))
129: return(0);
130: return(1);
131: }
132:
133:
134: int
1.1 kristaps 135: man_valid_post(struct man *m)
136: {
1.17 kristaps 137: v_check *cp;
1.1 kristaps 138:
139: if (MAN_VALID & m->last->flags)
140: return(1);
141: m->last->flags |= MAN_VALID;
142:
143: switch (m->last->type) {
144: case (MAN_TEXT):
1.11 kristaps 145: return(check_text(m, m->last));
1.1 kristaps 146: case (MAN_ROOT):
1.14 kristaps 147: return(check_root(m, m->last));
1.1 kristaps 148: default:
149: break;
150: }
151:
152: if (NULL == (cp = man_valids[m->last->tok].posts))
153: return(1);
154: for ( ; *cp; cp++)
155: if ( ! (*cp)(m, m->last))
156: return(0);
157:
158: return(1);
159: }
160:
161:
1.11 kristaps 162: static int
1.17 kristaps 163: check_root(CHKARGS)
1.14 kristaps 164: {
1.17 kristaps 165:
166: if (MAN_BLINE & m->flags)
1.54 kristaps 167: man_nmsg(m, n, MANDOCERR_SCOPEEXIT);
168: else if (MAN_ELINE & m->flags)
169: man_nmsg(m, n, MANDOCERR_SCOPEEXIT);
1.22 kristaps 170:
171: m->flags &= ~MAN_BLINE;
172: m->flags &= ~MAN_ELINE;
1.17 kristaps 173:
1.41 kristaps 174: if (NULL == m->first->child) {
175: man_nmsg(m, n, MANDOCERR_NODOCBODY);
176: return(0);
177: } else if (NULL == m->meta.title) {
1.54 kristaps 178: man_nmsg(m, n, MANDOCERR_NOTITLE);
179:
1.34 kristaps 180: /*
181: * If a title hasn't been set, do so now (by
182: * implication, date and section also aren't set).
183: */
1.54 kristaps 184:
1.34 kristaps 185: m->meta.title = mandoc_strdup("unknown");
186: m->meta.date = time(NULL);
1.37 kristaps 187: m->meta.msec = mandoc_strdup("1");
1.34 kristaps 188: }
1.32 kristaps 189:
190: return(1);
191: }
192:
193:
194: static int
195: check_title(CHKARGS)
196: {
197: const char *p;
198:
199: assert(n->child);
1.41 kristaps 200: /* FIXME: is this sufficient? */
201: if ('\0' == *n->child->string) {
202: man_nmsg(m, n, MANDOCERR_SYNTARGCOUNT);
203: return(0);
204: }
1.32 kristaps 205:
206: for (p = n->child->string; '\0' != *p; p++)
1.54 kristaps 207: /* Only warn about this once... */
208: if (isalpha((u_char)*p) && ! isupper((u_char)*p)) {
209: man_nmsg(m, n, MANDOCERR_UPPERCASE);
210: break;
211: }
1.14 kristaps 212:
213: return(1);
214: }
215:
216:
217: static int
1.17 kristaps 218: check_text(CHKARGS)
1.11 kristaps 219: {
1.43 kristaps 220: char *p;
1.15 kristaps 221: int pos, c;
1.46 kristaps 222: size_t sz;
1.11 kristaps 223:
1.46 kristaps 224: for (p = n->string, pos = n->pos + 1; *p; p++, pos++) {
225: sz = strcspn(p, "\t\\");
226: p += (int)sz;
227:
228: if ('\0' == *p)
229: break;
230:
231: pos += (int)sz;
1.11 kristaps 232:
1.46 kristaps 233: if ('\t' == *p) {
234: if (MAN_LITERAL & m->flags)
235: continue;
236: if (man_pmsg(m, n->line, pos, MANDOCERR_BADTAB))
1.15 kristaps 237: continue;
1.46 kristaps 238: return(0);
1.15 kristaps 239: }
1.45 kristaps 240:
1.46 kristaps 241: /* Check the special character. */
1.15 kristaps 242:
1.46 kristaps 243: c = mandoc_special(p);
244: if (c) {
245: p += c - 1;
246: pos += c - 1;
1.49 schwarze 247: } else
248: man_pmsg(m, n->line, pos, MANDOCERR_BADESCAPE);
1.11 kristaps 249: }
250:
251: return(1);
1.1 kristaps 252: }
253:
254:
255: #define INEQ_DEFINE(x, ineq, name) \
256: static int \
1.17 kristaps 257: check_##name(CHKARGS) \
1.1 kristaps 258: { \
1.11 kristaps 259: if (n->nchild ineq (x)) \
1.1 kristaps 260: return(1); \
1.41 kristaps 261: man_vmsg(m, MANDOCERR_SYNTARGCOUNT, n->line, n->pos, \
262: "line arguments %s %d (have %d)", \
263: #ineq, (x), n->nchild); \
264: return(0); \
1.1 kristaps 265: }
266:
267: INEQ_DEFINE(0, ==, eq0)
1.25 kristaps 268: INEQ_DEFINE(1, <=, le1)
1.1 kristaps 269: INEQ_DEFINE(2, >=, ge2)
270: INEQ_DEFINE(5, <=, le5)
271:
1.55 kristaps 272: static int
273: check_ft(CHKARGS)
274: {
275: char *cp;
276: int ok;
277:
278: if (0 == n->nchild)
279: return(1);
280:
281: ok = 0;
282: cp = n->child->string;
283: switch (*cp) {
284: case ('1'):
285: /* FALLTHROUGH */
286: case ('2'):
287: /* FALLTHROUGH */
288: case ('3'):
289: /* FALLTHROUGH */
290: case ('4'):
291: /* FALLTHROUGH */
292: case ('I'):
293: /* FALLTHROUGH */
294: case ('P'):
295: /* FALLTHROUGH */
296: case ('R'):
297: if ('\0' == cp[1])
298: ok = 1;
299: break;
300: case ('B'):
301: if ('\0' == cp[1] || ('I' == cp[1] && '\0' == cp[2]))
302: ok = 1;
303: break;
304: case ('C'):
305: if ('W' == cp[1] && '\0' == cp[2])
306: ok = 1;
307: break;
308: default:
309: break;
310: }
311:
312: if (0 == ok) {
313: man_vmsg(m, MANDOCERR_BADFONT,
314: n->line, n->pos, "%s", cp);
315: *cp = '\0';
316: }
317:
318: if (1 < n->nchild)
319: man_vmsg(m, MANDOCERR_ARGCOUNT, n->line, n->pos,
320: "want one child (have %d)", n->nchild);
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.41 kristaps 329: if (MAN_HEAD == n->type && 0 == n->nchild) {
330: man_nmsg(m, n, MANDOCERR_SYNTARGCOUNT);
331: return(0);
332: } else if (MAN_BODY == n->type && 0 == n->nchild)
1.54 kristaps 333: man_nmsg(m, n, MANDOCERR_NOBODY);
1.41 kristaps 334:
1.16 kristaps 335: return(1);
336: }
1.17 kristaps 337:
338:
339: static int
1.23 kristaps 340: check_part(CHKARGS)
341: {
342:
343: if (MAN_BODY == n->type && 0 == n->nchild)
1.54 kristaps 344: man_nmsg(m, n, MANDOCERR_NOBODY);
345:
1.23 kristaps 346: return(1);
347: }
348:
349:
350: static int
1.17 kristaps 351: check_par(CHKARGS)
352: {
353:
354: if (MAN_BODY == n->type)
355: switch (n->tok) {
356: case (MAN_IP):
357: /* FALLTHROUGH */
358: case (MAN_HP):
359: /* FALLTHROUGH */
360: case (MAN_TP):
361: /* Body-less lists are ok. */
362: break;
363: default:
1.54 kristaps 364: if (0 == n->nchild)
365: man_nmsg(m, n, MANDOCERR_NOBODY);
366: break;
1.17 kristaps 367: }
368: if (MAN_HEAD == n->type)
369: switch (n->tok) {
370: case (MAN_PP):
371: /* FALLTHROUGH */
372: case (MAN_P):
373: /* FALLTHROUGH */
374: case (MAN_LP):
1.54 kristaps 375: if (n->nchild)
376: man_nmsg(m, n, MANDOCERR_ARGSLOST);
377: break;
1.17 kristaps 378: default:
1.53 kristaps 379: break;
1.17 kristaps 380: }
381:
382: return(1);
383: }
384:
385:
386: static int
387: check_bline(CHKARGS)
388: {
389:
1.22 kristaps 390: assert( ! (MAN_ELINE & m->flags));
1.41 kristaps 391: if (MAN_BLINE & m->flags) {
392: man_nmsg(m, n, MANDOCERR_SYNTLINESCOPE);
393: return(0);
394: }
1.31 kristaps 395:
1.18 kristaps 396: return(1);
1.17 kristaps 397: }
398:
1.51 kristaps 399: static int
400: post_TH(CHKARGS)
401: {
402:
403: if (m->meta.title)
404: free(m->meta.title);
405: if (m->meta.vol)
406: free(m->meta.vol);
407: if (m->meta.source)
408: free(m->meta.source);
409: if (m->meta.msec)
410: free(m->meta.msec);
411: if (m->meta.rawdate)
412: free(m->meta.rawdate);
413:
414: m->meta.title = m->meta.vol = m->meta.rawdate =
415: m->meta.msec = m->meta.source = NULL;
416: m->meta.date = 0;
417:
418: /* ->TITLE<- MSEC DATE SOURCE VOL */
419:
420: n = n->child;
421: assert(n);
422: m->meta.title = mandoc_strdup(n->string);
423:
424: /* TITLE ->MSEC<- DATE SOURCE VOL */
425:
426: n = n->next;
427: assert(n);
428: m->meta.msec = mandoc_strdup(n->string);
429:
430: /* TITLE MSEC ->DATE<- SOURCE VOL */
431:
432: /*
433: * Try to parse the date. If this works, stash the epoch (this
434: * is optimal because we can reformat it in the canonical form).
435: * If it doesn't parse, isn't specified at all, or is an empty
436: * string, then use the current date.
437: */
438:
439: n = n->next;
440: if (n && n->string && *n->string) {
441: m->meta.date = mandoc_a2time
442: (MTIME_ISO_8601, n->string);
443: if (0 == m->meta.date) {
444: man_nmsg(m, n, MANDOCERR_BADDATE);
445: m->meta.rawdate = mandoc_strdup(n->string);
446: }
447: } else
448: m->meta.date = time(NULL);
449:
450: /* TITLE MSEC DATE ->SOURCE<- VOL */
451:
452: if (n && (n = n->next))
453: m->meta.source = mandoc_strdup(n->string);
454:
455: /* TITLE MSEC DATE SOURCE ->VOL<- */
456:
457: if (n && (n = n->next))
458: m->meta.vol = mandoc_strdup(n->string);
459:
460: /*
461: * Remove the `TH' node after we've processed it for our
462: * meta-data.
463: */
464: man_node_delete(m, m->last);
465: return(1);
466: }
467:
468: static int
469: post_nf(CHKARGS)
470: {
471:
472: if (MAN_LITERAL & m->flags)
473: man_nmsg(m, n, MANDOCERR_SCOPEREP);
474:
475: m->flags |= MAN_LITERAL;
476: return(1);
477: }
478:
479: static int
480: post_fi(CHKARGS)
481: {
482:
483: if ( ! (MAN_LITERAL & m->flags))
484: man_nmsg(m, n, MANDOCERR_NOSCOPE);
485:
486: m->flags &= ~MAN_LITERAL;
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: n = m->last->child;
505:
506: if (NULL == n || MAN_TEXT != n->type)
507: p = bsd_versions[0];
508: else {
509: s = n->string;
510: if (0 == strcmp(s, "3"))
511: p = bsd_versions[0];
512: else if (0 == strcmp(s, "4"))
513: p = bsd_versions[1];
514: else if (0 == strcmp(s, "5"))
515: p = bsd_versions[2];
516: else if (0 == strcmp(s, "6"))
517: p = bsd_versions[3];
518: else if (0 == strcmp(s, "7"))
519: p = bsd_versions[4];
520: else
521: p = bsd_versions[0];
522: }
523:
524: if (m->meta.source)
525: free(m->meta.source);
526:
527: m->meta.source = mandoc_strdup(p);
528: return(1);
529: }
530:
531: static int
532: post_AT(CHKARGS)
533: {
534: static const char * const unix_versions[] = {
535: "7th Edition",
536: "System III",
537: "System V",
538: "System V Release 2",
539: };
540:
541: const char *p, *s;
542: struct man_node *nn;
543:
544: n = n->child;
545:
546: if (NULL == n || MAN_TEXT != n->type)
547: p = unix_versions[0];
548: else {
549: s = n->string;
550: if (0 == strcmp(s, "3"))
551: p = unix_versions[0];
552: else if (0 == strcmp(s, "4"))
553: p = unix_versions[1];
554: else if (0 == strcmp(s, "5")) {
555: nn = n->next;
556: if (nn && MAN_TEXT == nn->type && nn->string[0])
557: p = unix_versions[3];
558: else
559: p = unix_versions[2];
560: } else
561: p = unix_versions[0];
562: }
563:
564: if (m->meta.source)
565: free(m->meta.source);
566:
567: m->meta.source = mandoc_strdup(p);
568: return(1);
569: }
CVSweb