Annotation of mandoc/mdoc_validate.c, Revision 1.24
1.24 ! kristaps 1: /* $Id: mdoc_validate.c,v 1.23 2009/07/06 13:04:52 kristaps Exp $ */
1.1 kristaps 2: /*
1.7 kristaps 3: * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@kth.se>
1.1 kristaps 4: *
5: * Permission to use, copy, modify, and distribute this software for any
1.5 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.5 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: */
17: #include <sys/types.h>
18:
19: #include <assert.h>
20: #include <ctype.h>
21: #include <stdarg.h>
22: #include <stdlib.h>
1.2 kristaps 23: #include <string.h>
1.1 kristaps 24:
25: #include "libmdoc.h"
1.18 kristaps 26: #include "libmandoc.h"
1.1 kristaps 27:
28: /* FIXME: .Bl -diag can't have non-text children in HEAD. */
29: /* TODO: ignoring Pp (it's superfluous in some invocations). */
30:
31: #define PRE_ARGS struct mdoc *mdoc, const struct mdoc_node *n
32: #define POST_ARGS struct mdoc *mdoc
33:
34:
35: typedef int (*v_pre)(PRE_ARGS);
36: typedef int (*v_post)(POST_ARGS);
37:
38: struct valids {
39: v_pre *pre;
40: v_post *post;
41: };
42:
43: static int check_parent(PRE_ARGS, int, enum mdoc_type);
44: static int check_msec(PRE_ARGS, ...);
45: static int check_sec(PRE_ARGS, ...);
46: static int check_stdarg(PRE_ARGS);
47: static int check_text(struct mdoc *, int, int, const char *);
48: static int check_argv(struct mdoc *,
49: const struct mdoc_node *,
50: const struct mdoc_argv *);
51: static int check_args(struct mdoc *,
52: const struct mdoc_node *);
53: static int err_child_lt(struct mdoc *, const char *, int);
54: static int warn_child_lt(struct mdoc *, const char *, int);
55: static int err_child_gt(struct mdoc *, const char *, int);
56: static int warn_child_gt(struct mdoc *, const char *, int);
57: static int err_child_eq(struct mdoc *, const char *, int);
58: static int warn_child_eq(struct mdoc *, const char *, int);
1.4 kristaps 59: static int warn_print(struct mdoc *, int, int);
1.2 kristaps 60: static int warn_count(struct mdoc *, const char *,
1.1 kristaps 61: int, const char *, int);
1.2 kristaps 62: static int err_count(struct mdoc *, const char *,
1.1 kristaps 63: int, const char *, int);
64: static int pre_an(PRE_ARGS);
65: static int pre_bd(PRE_ARGS);
66: static int pre_bl(PRE_ARGS);
67: static int pre_cd(PRE_ARGS);
68: static int pre_dd(PRE_ARGS);
69: static int pre_display(PRE_ARGS);
70: static int pre_dt(PRE_ARGS);
71: static int pre_er(PRE_ARGS);
72: static int pre_ex(PRE_ARGS);
73: static int pre_fd(PRE_ARGS);
74: static int pre_it(PRE_ARGS);
75: static int pre_lb(PRE_ARGS);
76: static int pre_os(PRE_ARGS);
77: static int pre_rv(PRE_ARGS);
78: static int pre_sh(PRE_ARGS);
79: static int pre_ss(PRE_ARGS);
80: static int herr_ge1(POST_ARGS);
81: static int hwarn_le1(POST_ARGS);
82: static int herr_eq0(POST_ARGS);
83: static int eerr_eq0(POST_ARGS);
84: static int eerr_le2(POST_ARGS);
85: static int eerr_eq1(POST_ARGS);
86: static int eerr_ge1(POST_ARGS);
87: static int ewarn_eq0(POST_ARGS);
88: static int ewarn_eq1(POST_ARGS);
89: static int bwarn_ge1(POST_ARGS);
90: static int hwarn_eq1(POST_ARGS);
91: static int ewarn_ge1(POST_ARGS);
92: static int ebool(POST_ARGS);
93: static int post_an(POST_ARGS);
94: static int post_args(POST_ARGS);
95: static int post_at(POST_ARGS);
96: static int post_bf(POST_ARGS);
97: static int post_bl(POST_ARGS);
1.14 kristaps 98: static int post_bl_head(POST_ARGS);
1.1 kristaps 99: static int post_it(POST_ARGS);
100: static int post_nm(POST_ARGS);
101: static int post_root(POST_ARGS);
102: static int post_sh(POST_ARGS);
103: static int post_sh_body(POST_ARGS);
104: static int post_sh_head(POST_ARGS);
105: static int post_st(POST_ARGS);
106:
107: static v_pre pres_an[] = { pre_an, NULL };
108: static v_pre pres_bd[] = { pre_display, pre_bd, NULL };
109: static v_pre pres_bl[] = { pre_bl, NULL };
110: static v_pre pres_cd[] = { pre_cd, NULL };
1.12 kristaps 111: static v_pre pres_dd[] = { pre_dd, NULL };
1.1 kristaps 112: static v_pre pres_d1[] = { pre_display, NULL };
1.12 kristaps 113: static v_pre pres_dt[] = { pre_dt, NULL };
1.1 kristaps 114: static v_pre pres_er[] = { pre_er, NULL };
115: static v_pre pres_ex[] = { pre_ex, NULL };
116: static v_pre pres_fd[] = { pre_fd, NULL };
117: static v_pre pres_it[] = { pre_it, NULL };
118: static v_pre pres_lb[] = { pre_lb, NULL };
1.12 kristaps 119: static v_pre pres_os[] = { pre_os, NULL };
1.1 kristaps 120: static v_pre pres_rv[] = { pre_rv, NULL };
121: static v_pre pres_sh[] = { pre_sh, NULL };
122: static v_pre pres_ss[] = { pre_ss, NULL };
123: static v_post posts_bool[] = { eerr_eq1, ebool, NULL };
124: static v_post posts_bd[] = { herr_eq0, bwarn_ge1, NULL };
125: static v_post posts_text[] = { eerr_ge1, NULL };
126: static v_post posts_wtext[] = { ewarn_ge1, NULL };
127: static v_post posts_notext[] = { eerr_eq0, NULL };
128: static v_post posts_wline[] = { bwarn_ge1, herr_eq0, NULL };
129: static v_post posts_sh[] = { herr_ge1, bwarn_ge1, post_sh, NULL };
1.14 kristaps 130: static v_post posts_bl[] = { bwarn_ge1, post_bl, NULL };
1.1 kristaps 131: static v_post posts_it[] = { post_it, NULL };
132: static v_post posts_in[] = { ewarn_eq1, NULL };
133: static v_post posts_ss[] = { herr_ge1, NULL };
134: static v_post posts_pf[] = { eerr_eq1, NULL };
135: static v_post posts_lb[] = { eerr_eq1, NULL };
136: static v_post posts_st[] = { eerr_eq1, post_st, NULL };
137: static v_post posts_pp[] = { ewarn_eq0, NULL };
138: static v_post posts_ex[] = { eerr_eq0, post_args, NULL };
139: static v_post posts_rv[] = { eerr_eq0, post_args, NULL };
140: static v_post posts_an[] = { post_an, NULL };
141: static v_post posts_at[] = { post_at, NULL };
142: static v_post posts_xr[] = { eerr_ge1, eerr_le2, NULL };
143: static v_post posts_nm[] = { post_nm, NULL };
144: static v_post posts_bf[] = { hwarn_le1, post_bf, NULL };
145: static v_post posts_fo[] = { hwarn_eq1, bwarn_ge1, NULL };
146:
147: const struct valids mdoc_valids[MDOC_MAX] = {
1.10 kristaps 148: { NULL, NULL }, /* Ap */
1.1 kristaps 149: { pres_dd, posts_text }, /* Dd */
150: { pres_dt, NULL }, /* Dt */
151: { pres_os, NULL }, /* Os */
152: { pres_sh, posts_sh }, /* Sh */
153: { pres_ss, posts_ss }, /* Ss */
154: { NULL, posts_pp }, /* Pp */
155: { pres_d1, posts_wline }, /* D1 */
156: { pres_d1, posts_wline }, /* Dl */
157: { pres_bd, posts_bd }, /* Bd */
158: { NULL, NULL }, /* Ed */
159: { pres_bl, posts_bl }, /* Bl */
160: { NULL, NULL }, /* El */
161: { pres_it, posts_it }, /* It */
162: { NULL, posts_text }, /* Ad */
163: { pres_an, posts_an }, /* An */
164: { NULL, NULL }, /* Ar */
165: { pres_cd, posts_text }, /* Cd */
166: { NULL, NULL }, /* Cm */
167: { NULL, NULL }, /* Dv */
168: { pres_er, posts_text }, /* Er */
169: { NULL, NULL }, /* Ev */
170: { pres_ex, posts_ex }, /* Ex */
171: { NULL, NULL }, /* Fa */
172: { pres_fd, posts_wtext }, /* Fd */
173: { NULL, NULL }, /* Fl */
174: { NULL, posts_text }, /* Fn */
175: { NULL, posts_wtext }, /* Ft */
176: { NULL, posts_text }, /* Ic */
177: { NULL, posts_in }, /* In */
178: { NULL, NULL }, /* Li */
179: { NULL, posts_wtext }, /* Nd */
180: { NULL, posts_nm }, /* Nm */
181: { NULL, posts_wline }, /* Op */
182: { NULL, NULL }, /* Ot */
183: { NULL, NULL }, /* Pa */
184: { pres_rv, posts_rv }, /* Rv */
185: { NULL, posts_st }, /* St */
186: { NULL, NULL }, /* Va */
187: { NULL, posts_text }, /* Vt */
188: { NULL, posts_xr }, /* Xr */
189: { NULL, posts_text }, /* %A */
190: { NULL, posts_text }, /* %B */
191: { NULL, posts_text }, /* %D */
192: { NULL, posts_text }, /* %I */
193: { NULL, posts_text }, /* %J */
194: { NULL, posts_text }, /* %N */
195: { NULL, posts_text }, /* %O */
196: { NULL, posts_text }, /* %P */
197: { NULL, posts_text }, /* %R */
198: { NULL, posts_text }, /* %T */
199: { NULL, posts_text }, /* %V */
200: { NULL, NULL }, /* Ac */
201: { NULL, NULL }, /* Ao */
202: { NULL, posts_wline }, /* Aq */
203: { NULL, posts_at }, /* At */
204: { NULL, NULL }, /* Bc */
205: { NULL, posts_bf }, /* Bf */
206: { NULL, NULL }, /* Bo */
207: { NULL, posts_wline }, /* Bq */
208: { NULL, NULL }, /* Bsx */
209: { NULL, NULL }, /* Bx */
210: { NULL, posts_bool }, /* Db */
211: { NULL, NULL }, /* Dc */
212: { NULL, NULL }, /* Do */
213: { NULL, posts_wline }, /* Dq */
214: { NULL, NULL }, /* Ec */
215: { NULL, NULL }, /* Ef */
216: { NULL, NULL }, /* Em */
217: { NULL, NULL }, /* Eo */
218: { NULL, NULL }, /* Fx */
219: { NULL, posts_text }, /* Ms */
220: { NULL, posts_notext }, /* No */
221: { NULL, posts_notext }, /* Ns */
222: { NULL, NULL }, /* Nx */
223: { NULL, NULL }, /* Ox */
224: { NULL, NULL }, /* Pc */
225: { NULL, posts_pf }, /* Pf */
226: { NULL, NULL }, /* Po */
227: { NULL, posts_wline }, /* Pq */
228: { NULL, NULL }, /* Qc */
229: { NULL, posts_wline }, /* Ql */
230: { NULL, NULL }, /* Qo */
231: { NULL, posts_wline }, /* Qq */
232: { NULL, NULL }, /* Re */
233: { NULL, posts_wline }, /* Rs */
234: { NULL, NULL }, /* Sc */
235: { NULL, NULL }, /* So */
236: { NULL, posts_wline }, /* Sq */
237: { NULL, posts_bool }, /* Sm */
238: { NULL, posts_text }, /* Sx */
239: { NULL, posts_text }, /* Sy */
240: { NULL, posts_text }, /* Tn */
241: { NULL, NULL }, /* Ux */
242: { NULL, NULL }, /* Xc */
243: { NULL, NULL }, /* Xo */
244: { NULL, posts_fo }, /* Fo */
245: { NULL, NULL }, /* Fc */
246: { NULL, NULL }, /* Oo */
247: { NULL, NULL }, /* Oc */
248: { NULL, posts_wline }, /* Bk */
249: { NULL, NULL }, /* Ek */
250: { NULL, posts_notext }, /* Bt */
251: { NULL, NULL }, /* Hf */
252: { NULL, NULL }, /* Fr */
253: { NULL, posts_notext }, /* Ud */
254: { pres_lb, posts_lb }, /* Lb */
255: { NULL, posts_pp }, /* Lp */
1.9 kristaps 256: { NULL, NULL }, /* Lk */
1.1 kristaps 257: { NULL, posts_text }, /* Mt */
258: { NULL, posts_wline }, /* Brq */
259: { NULL, NULL }, /* Bro */
260: { NULL, NULL }, /* Brc */
261: { NULL, posts_text }, /* %C */
262: { NULL, NULL }, /* Es */
263: { NULL, NULL }, /* En */
264: { NULL, NULL }, /* Dx */
265: { NULL, posts_text }, /* %Q */
266: };
267:
268:
1.2 kristaps 269: #ifdef __linux__
270: extern size_t strlcat(char *, const char *, size_t);
271: #endif
272:
273:
1.1 kristaps 274: int
275: mdoc_valid_pre(struct mdoc *mdoc,
276: const struct mdoc_node *n)
277: {
278: v_pre *p;
279: int line, pos;
280: const char *tp;
281:
282: if (MDOC_TEXT == n->type) {
283: tp = n->string;
284: line = n->line;
285: pos = n->pos;
286: return(check_text(mdoc, line, pos, tp));
287: }
288:
289: if ( ! check_args(mdoc, n))
290: return(0);
291: if (NULL == mdoc_valids[n->tok].pre)
292: return(1);
293: for (p = mdoc_valids[n->tok].pre; *p; p++)
294: if ( ! (*p)(mdoc, n))
295: return(0);
296: return(1);
297: }
298:
299:
300: int
301: mdoc_valid_post(struct mdoc *mdoc)
302: {
303: v_post *p;
304:
305: /*
306: * This check occurs after the macro's children have been filled
307: * in: postfix validation. Since this happens when we're
308: * rewinding the scope tree, it's possible to have multiple
309: * invocations (as by design, for now), we set bit MDOC_VALID to
310: * indicate that we've validated.
311: */
312:
313: if (MDOC_VALID & mdoc->last->flags)
314: return(1);
315: mdoc->last->flags |= MDOC_VALID;
316:
317: if (MDOC_TEXT == mdoc->last->type)
318: return(1);
319: if (MDOC_ROOT == mdoc->last->type)
320: return(post_root(mdoc));
321:
322: if (NULL == mdoc_valids[mdoc->last->tok].post)
323: return(1);
324: for (p = mdoc_valids[mdoc->last->tok].post; *p; p++)
325: if ( ! (*p)(mdoc))
326: return(0);
327:
328: return(1);
329: }
330:
331:
332: static int
1.4 kristaps 333: warn_print(struct mdoc *m, int ln, int pos)
334: {
1.21 kristaps 335:
1.23 kristaps 336: if (MDOC_IGN_CHARS & m->pflags)
337: return(mdoc_pwarn(m, ln, pos, EPRINT));
338: return(mdoc_perr(m, ln, pos, EPRINT));
1.4 kristaps 339: }
340:
1.1 kristaps 341:
342: static inline int
343: warn_count(struct mdoc *m, const char *k,
344: int want, const char *v, int has)
345: {
346:
1.23 kristaps 347: return(mdoc_vwarn(m, m->last->line, m->last->pos,
348: "suggests %s %s %d (has %d)", v, k, want, has));
1.1 kristaps 349: }
350:
351:
352: static inline int
353: err_count(struct mdoc *m, const char *k,
354: int want, const char *v, int has)
355: {
356:
1.23 kristaps 357: return(mdoc_verr(m, m->last->line, m->last->pos,
1.1 kristaps 358: "requires %s %s %d (has %d)", v, k, want, has));
359: }
360:
361:
362: /*
363: * Build these up with macros because they're basically the same check
364: * for different inequalities. Yes, this could be done with functions,
365: * but this is reasonable for now.
366: */
367:
368: #define CHECK_CHILD_DEFN(lvl, name, ineq) \
369: static int \
370: lvl##_child_##name(struct mdoc *mdoc, const char *p, int sz) \
371: { \
1.16 kristaps 372: if (mdoc->last->nchild ineq sz) \
1.1 kristaps 373: return(1); \
1.16 kristaps 374: return(lvl##_count(mdoc, #ineq, sz, p, mdoc->last->nchild)); \
1.1 kristaps 375: }
376:
377: #define CHECK_BODY_DEFN(name, lvl, func, num) \
378: static int \
379: b##lvl##_##name(POST_ARGS) \
380: { \
381: if (MDOC_BODY != mdoc->last->type) \
382: return(1); \
383: return(func(mdoc, "multi-line arguments", (num))); \
384: }
385:
386: #define CHECK_ELEM_DEFN(name, lvl, func, num) \
387: static int \
388: e##lvl##_##name(POST_ARGS) \
389: { \
390: assert(MDOC_ELEM == mdoc->last->type); \
391: return(func(mdoc, "line arguments", (num))); \
392: }
393:
394: #define CHECK_HEAD_DEFN(name, lvl, func, num) \
395: static int \
396: h##lvl##_##name(POST_ARGS) \
397: { \
398: if (MDOC_HEAD != mdoc->last->type) \
399: return(1); \
400: return(func(mdoc, "line arguments", (num))); \
401: }
402:
403:
404: CHECK_CHILD_DEFN(warn, gt, >) /* warn_child_gt() */
405: CHECK_CHILD_DEFN(err, gt, >) /* err_child_gt() */
406: CHECK_CHILD_DEFN(warn, eq, ==) /* warn_child_eq() */
407: CHECK_CHILD_DEFN(err, eq, ==) /* err_child_eq() */
408: CHECK_CHILD_DEFN(err, lt, <) /* err_child_lt() */
409: CHECK_CHILD_DEFN(warn, lt, <) /* warn_child_lt() */
410: CHECK_BODY_DEFN(ge1, warn, warn_child_gt, 0) /* bwarn_ge1() */
411: CHECK_ELEM_DEFN(eq1, warn, warn_child_eq, 1) /* ewarn_eq1() */
412: CHECK_ELEM_DEFN(eq0, warn, warn_child_eq, 0) /* ewarn_eq0() */
413: CHECK_ELEM_DEFN(ge1, warn, warn_child_gt, 0) /* ewarn_gt1() */
414: CHECK_ELEM_DEFN(eq1, err, err_child_eq, 1) /* eerr_eq1() */
415: CHECK_ELEM_DEFN(le2, err, err_child_lt, 3) /* eerr_le2() */
416: CHECK_ELEM_DEFN(eq0, err, err_child_eq, 0) /* eerr_eq0() */
417: CHECK_ELEM_DEFN(ge1, err, err_child_gt, 0) /* eerr_ge1() */
418: CHECK_HEAD_DEFN(eq0, err, err_child_eq, 0) /* herr_eq0() */
419: CHECK_HEAD_DEFN(le1, warn, warn_child_lt, 2) /* hwarn_le1() */
420: CHECK_HEAD_DEFN(ge1, err, err_child_gt, 0) /* herr_ge1() */
421: CHECK_HEAD_DEFN(eq1, warn, warn_child_eq, 1) /* hwarn_eq1() */
422:
423:
424: static int
425: check_stdarg(PRE_ARGS)
426: {
427:
428: if (n->args && 1 == n->args->argc)
429: if (MDOC_Std == n->args->argv[0].arg)
430: return(1);
1.23 kristaps 431: return(mdoc_nwarn(mdoc, n, EARGVAL));
1.1 kristaps 432: }
433:
434:
435: static int
436: check_sec(PRE_ARGS, ...)
437: {
438: enum mdoc_sec sec;
439: va_list ap;
440:
441: va_start(ap, n);
442:
443: for (;;) {
444: /* LINTED */
445: sec = (enum mdoc_sec)va_arg(ap, int);
446: if (SEC_CUSTOM == sec)
447: break;
448: if (sec != mdoc->lastsec)
449: continue;
450: va_end(ap);
451: return(1);
452: }
453:
454: va_end(ap);
1.23 kristaps 455: return(mdoc_nwarn(mdoc, n, EBADSEC));
1.1 kristaps 456: }
457:
458:
459: static int
460: check_msec(PRE_ARGS, ...)
461: {
462: va_list ap;
463: int msec;
464:
465: va_start(ap, n);
466: for (;;) {
467: /* LINTED */
468: if (0 == (msec = va_arg(ap, int)))
469: break;
470: if (msec != mdoc->meta.msec)
471: continue;
472: va_end(ap);
473: return(1);
474: }
475:
476: va_end(ap);
1.23 kristaps 477: return(mdoc_nwarn(mdoc, n, EBADMSEC));
1.1 kristaps 478: }
479:
480:
481: static int
482: check_args(struct mdoc *m, const struct mdoc_node *n)
483: {
484: int i;
485:
486: if (NULL == n->args)
487: return(1);
488:
489: assert(n->args->argc);
490: for (i = 0; i < (int)n->args->argc; i++)
491: if ( ! check_argv(m, n, &n->args->argv[i]))
492: return(0);
493:
494: return(1);
495: }
496:
497:
498: static int
499: check_argv(struct mdoc *m, const struct mdoc_node *n,
500: const struct mdoc_argv *v)
501: {
502: int i;
503:
504: for (i = 0; i < (int)v->sz; i++)
505: if ( ! check_text(m, v->line, v->pos, v->value[i]))
506: return(0);
507:
508: if (MDOC_Std == v->arg) {
509: /* `Nm' name must be set. */
510: if (v->sz || m->meta.name)
511: return(1);
1.23 kristaps 512: return(mdoc_nerr(m, n, ENAME));
1.1 kristaps 513: }
514:
515: return(1);
516: }
517:
518:
519: static int
520: check_text(struct mdoc *mdoc, int line, int pos, const char *p)
521: {
1.18 kristaps 522: int c;
1.1 kristaps 523:
1.18 kristaps 524: for ( ; *p; p++, pos++) {
1.1 kristaps 525: if ('\t' == *p) {
526: if ( ! (MDOC_LITERAL & mdoc->flags))
1.4 kristaps 527: if ( ! warn_print(mdoc, line, pos))
528: return(0);
1.1 kristaps 529: } else if ( ! isprint((u_char)*p))
1.4 kristaps 530: if ( ! warn_print(mdoc, line, pos))
531: return(0);
1.1 kristaps 532:
533: if ('\\' != *p)
534: continue;
535:
1.18 kristaps 536: c = mandoc_special(p);
1.1 kristaps 537: if (c) {
1.18 kristaps 538: p += c - 1;
539: pos += c - 1;
1.1 kristaps 540: continue;
541: }
542: if ( ! (MDOC_IGN_ESCAPE & mdoc->pflags))
1.23 kristaps 543: return(mdoc_perr(mdoc, line, pos, EESCAPE));
544: if ( ! mdoc_perr(mdoc, line, pos, EESCAPE))
1.1 kristaps 545: return(0);
546: }
547:
548: return(1);
549: }
550:
551:
552:
553:
554: static int
555: check_parent(PRE_ARGS, int tok, enum mdoc_type t)
556: {
557:
558: assert(n->parent);
559: if ((MDOC_ROOT == t || tok == n->parent->tok) &&
560: (t == n->parent->type))
561: return(1);
562:
1.23 kristaps 563: return(mdoc_verr(mdoc, n->line, n->pos, "require parent %s",
1.1 kristaps 564: MDOC_ROOT == t ? "<root>" : mdoc_macronames[tok]));
565: }
566:
567:
568:
569: static int
570: pre_display(PRE_ARGS)
571: {
572: struct mdoc_node *node;
573:
574: /* Display elements (`Bd', `D1'...) cannot be nested. */
575:
576: if (MDOC_BLOCK != n->type)
577: return(1);
578:
579: /* LINTED */
580: for (node = mdoc->last->parent; node; node = node->parent)
581: if (MDOC_BLOCK == node->type)
582: if (MDOC_Bd == node->tok)
583: break;
584: if (NULL == node)
585: return(1);
586:
1.23 kristaps 587: return(mdoc_nerr(mdoc, n, ENESTDISP));
1.1 kristaps 588: }
589:
590:
591: static int
592: pre_bl(PRE_ARGS)
593: {
1.15 kristaps 594: int pos, type, width, offset;
1.1 kristaps 595:
596: if (MDOC_BLOCK != n->type)
597: return(1);
598: if (NULL == n->args)
1.23 kristaps 599: return(mdoc_nerr(mdoc, n, ELISTTYPE));
1.1 kristaps 600:
601: /* Make sure that only one type of list is specified. */
602:
1.15 kristaps 603: type = offset = width = -1;
1.1 kristaps 604:
605: /* LINTED */
1.8 kristaps 606: for (pos = 0; pos < (int)n->args->argc; pos++)
607: switch (n->args->argv[pos].arg) {
1.1 kristaps 608: case (MDOC_Bullet):
609: /* FALLTHROUGH */
610: case (MDOC_Dash):
611: /* FALLTHROUGH */
612: case (MDOC_Enum):
613: /* FALLTHROUGH */
614: case (MDOC_Hyphen):
615: /* FALLTHROUGH */
616: case (MDOC_Item):
617: /* FALLTHROUGH */
618: case (MDOC_Tag):
619: /* FALLTHROUGH */
620: case (MDOC_Diag):
621: /* FALLTHROUGH */
622: case (MDOC_Hang):
623: /* FALLTHROUGH */
624: case (MDOC_Ohang):
625: /* FALLTHROUGH */
626: case (MDOC_Inset):
627: /* FALLTHROUGH */
628: case (MDOC_Column):
1.8 kristaps 629: if (-1 != type)
1.23 kristaps 630: return(mdoc_nerr(mdoc, n, EMULTILIST));
1.8 kristaps 631: type = n->args->argv[pos].arg;
632: break;
1.1 kristaps 633: case (MDOC_Width):
1.8 kristaps 634: if (-1 != width)
1.23 kristaps 635: return(mdoc_nerr(mdoc, n, EARGREP));
1.8 kristaps 636: width = n->args->argv[pos].arg;
637: break;
1.1 kristaps 638: case (MDOC_Offset):
1.8 kristaps 639: if (-1 != offset)
1.23 kristaps 640: return(mdoc_nerr(mdoc, n, EARGREP));
1.8 kristaps 641: offset = n->args->argv[pos].arg;
642: break;
1.1 kristaps 643: default:
644: break;
645: }
646:
647: if (-1 == type)
1.23 kristaps 648: return(mdoc_nerr(mdoc, n, ELISTTYPE));
1.1 kristaps 649:
1.8 kristaps 650: /*
651: * Validate the width field. Some list types don't need width
652: * types and should be warned about them. Others should have it
653: * and must also be warned.
654: */
655:
1.1 kristaps 656: switch (type) {
1.8 kristaps 657: case (MDOC_Tag):
1.23 kristaps 658: if (-1 == width && ! mdoc_nwarn(mdoc, n, EMISSWIDTH))
1.8 kristaps 659: return(0);
660: break;
1.1 kristaps 661: case (MDOC_Column):
662: /* FALLTHROUGH */
663: case (MDOC_Diag):
664: /* FALLTHROUGH */
665: case (MDOC_Inset):
666: /* FALLTHROUGH */
667: case (MDOC_Item):
1.23 kristaps 668: if (-1 != width && ! mdoc_nwarn(mdoc, n, ENOWIDTH))
1.8 kristaps 669: return(0);
670: break;
671: default:
672: break;
673: }
674:
1.1 kristaps 675: return(1);
676: }
677:
678:
679: static int
680: pre_bd(PRE_ARGS)
681: {
682: int i, type, err;
683:
684: if (MDOC_BLOCK != n->type)
685: return(1);
686: if (NULL == n->args)
1.23 kristaps 687: return(mdoc_nerr(mdoc, n, EDISPTYPE));
1.1 kristaps 688:
689: /* Make sure that only one type of display is specified. */
690:
691: /* LINTED */
692: for (i = 0, err = type = 0; ! err &&
693: i < (int)n->args->argc; i++)
694: switch (n->args->argv[i].arg) {
695: case (MDOC_Ragged):
696: /* FALLTHROUGH */
697: case (MDOC_Unfilled):
698: /* FALLTHROUGH */
699: case (MDOC_Filled):
700: /* FALLTHROUGH */
701: case (MDOC_Literal):
702: /* FALLTHROUGH */
703: case (MDOC_File):
704: if (0 == type++)
705: break;
1.23 kristaps 706: return(mdoc_nerr(mdoc, n, EMULTIDISP));
1.1 kristaps 707: default:
708: break;
709: }
710:
711: if (type)
712: return(1);
1.23 kristaps 713: return(mdoc_nerr(mdoc, n, EDISPTYPE));
1.1 kristaps 714: }
715:
716:
717: static int
718: pre_ss(PRE_ARGS)
719: {
720:
721: if (MDOC_BLOCK != n->type)
722: return(1);
723: return(check_parent(mdoc, n, MDOC_Sh, MDOC_BODY));
724: }
725:
726:
727: static int
728: pre_sh(PRE_ARGS)
729: {
730:
731: if (MDOC_BLOCK != n->type)
732: return(1);
733: return(check_parent(mdoc, n, -1, MDOC_ROOT));
734: }
735:
736:
737: static int
738: pre_it(PRE_ARGS)
739: {
740:
741: if (MDOC_BLOCK != n->type)
742: return(1);
743: return(check_parent(mdoc, n, MDOC_Bl, MDOC_BODY));
744: }
745:
746:
747: static int
748: pre_an(PRE_ARGS)
749: {
750:
751: if (NULL == n->args || 1 == n->args->argc)
752: return(1);
1.23 kristaps 753: return(mdoc_verr(mdoc, n->line, n->pos,
754: "only one argument allowed"));
1.1 kristaps 755: }
756:
757:
758: static int
759: pre_lb(PRE_ARGS)
760: {
761:
762: return(check_sec(mdoc, n, SEC_LIBRARY, SEC_CUSTOM));
763: }
764:
765:
766: static int
767: pre_rv(PRE_ARGS)
768: {
769:
770: if ( ! check_msec(mdoc, n, 2, 3, 0))
771: return(0);
772: return(check_stdarg(mdoc, n));
773: }
774:
775:
776: static int
777: pre_ex(PRE_ARGS)
778: {
779:
780: if ( ! check_msec(mdoc, n, 1, 6, 8, 0))
781: return(0);
782: return(check_stdarg(mdoc, n));
783: }
784:
785:
786: static int
787: pre_er(PRE_ARGS)
788: {
789:
1.6 kristaps 790: return(check_msec(mdoc, n, 2, 3, 9, 0));
1.1 kristaps 791: }
792:
793:
794: static int
795: pre_cd(PRE_ARGS)
796: {
797:
798: return(check_msec(mdoc, n, 4, 0));
799: }
800:
801:
802: static int
803: pre_dt(PRE_ARGS)
804: {
805:
806: if (0 == mdoc->meta.date || mdoc->meta.os)
1.23 kristaps 807: if ( ! mdoc_nwarn(mdoc, n, EPROLOOO))
1.1 kristaps 808: return(0);
809: if (mdoc->meta.title)
1.23 kristaps 810: if ( ! mdoc_nwarn(mdoc, n, EPROLREP))
1.1 kristaps 811: return(0);
812: return(1);
813: }
814:
815:
816: static int
817: pre_os(PRE_ARGS)
818: {
819:
820: if (NULL == mdoc->meta.title || 0 == mdoc->meta.date)
1.23 kristaps 821: if ( ! mdoc_nwarn(mdoc, n, EPROLOOO))
1.1 kristaps 822: return(0);
823: if (mdoc->meta.os)
1.23 kristaps 824: if ( ! mdoc_nwarn(mdoc, n, EPROLREP))
1.1 kristaps 825: return(0);
826: return(1);
827: }
828:
829:
830: static int
831: pre_dd(PRE_ARGS)
832: {
833:
834: if (mdoc->meta.title || mdoc->meta.os)
1.23 kristaps 835: if ( ! mdoc_nwarn(mdoc, n, EPROLOOO))
1.1 kristaps 836: return(0);
837: if (mdoc->meta.date)
1.23 kristaps 838: if ( ! mdoc_nwarn(mdoc, n, EPROLREP))
1.1 kristaps 839: return(0);
840: return(1);
841: }
842:
843:
844: static int
845: post_bf(POST_ARGS)
846: {
847: char *p;
848: struct mdoc_node *head;
849:
850: if (MDOC_BLOCK != mdoc->last->type)
851: return(1);
852:
853: head = mdoc->last->head;
854:
1.12 kristaps 855: if (mdoc->last->args && head->child)
1.24 ! kristaps 856: return(mdoc_nerr(mdoc, mdoc->last, ELINE));
1.13 kristaps 857: else if (mdoc->last->args)
858: return(1);
1.12 kristaps 859:
1.13 kristaps 860: if (NULL == head->child || MDOC_TEXT != head->child->type)
1.24 ! kristaps 861: return(mdoc_nerr(mdoc, mdoc->last, ELINE));
1.1 kristaps 862:
1.13 kristaps 863: p = head->child->string;
1.1 kristaps 864:
1.13 kristaps 865: if (0 == strcmp(p, "Em"))
866: return(1);
867: else if (0 == strcmp(p, "Li"))
868: return(1);
869: else if (0 == strcmp(p, "Sm"))
870: return(1);
1.1 kristaps 871:
1.23 kristaps 872: return(mdoc_nerr(mdoc, head, EFONT));
1.1 kristaps 873: }
874:
875:
876: static int
877: post_nm(POST_ARGS)
878: {
879:
880: if (mdoc->last->child)
881: return(1);
882: if (mdoc->meta.name)
883: return(1);
1.23 kristaps 884: return(mdoc_nerr(mdoc, mdoc->last, ENAME));
1.1 kristaps 885: }
886:
887:
888: static int
889: post_at(POST_ARGS)
890: {
891:
892: if (NULL == mdoc->last->child)
893: return(1);
894: if (MDOC_TEXT != mdoc->last->child->type)
1.23 kristaps 895: return(mdoc_nerr(mdoc, mdoc->last, EATT));
1.1 kristaps 896: if (mdoc_a2att(mdoc->last->child->string))
897: return(1);
1.23 kristaps 898: return(mdoc_nerr(mdoc, mdoc->last, EATT));
1.1 kristaps 899: }
900:
901:
902: static int
903: post_an(POST_ARGS)
904: {
905:
906: if (mdoc->last->args) {
907: if (NULL == mdoc->last->child)
908: return(1);
1.23 kristaps 909: return(mdoc_nerr(mdoc, mdoc->last, ELINE));
1.1 kristaps 910: }
911:
912: if (mdoc->last->child)
913: return(1);
1.23 kristaps 914: return(mdoc_nerr(mdoc, mdoc->last, ELINE));
1.1 kristaps 915: }
916:
917:
918: static int
919: post_args(POST_ARGS)
920: {
921:
922: if (mdoc->last->args)
923: return(1);
1.23 kristaps 924: return(mdoc_nerr(mdoc, mdoc->last, ELINE));
1.1 kristaps 925: }
926:
927:
928: static int
929: post_it(POST_ARGS)
930: {
931: int type, i, cols;
932: struct mdoc_node *n, *c;
933:
934: if (MDOC_BLOCK != mdoc->last->type)
935: return(1);
936:
937: n = mdoc->last->parent->parent;
938: if (NULL == n->args)
1.23 kristaps 939: return(mdoc_nerr(mdoc, mdoc->last, ELISTTYPE));
1.1 kristaps 940:
941: /* Some types require block-head, some not. */
942:
943: /* LINTED */
944: for (cols = type = -1, i = 0; -1 == type &&
945: i < (int)n->args->argc; i++)
946: switch (n->args->argv[i].arg) {
947: case (MDOC_Tag):
948: /* FALLTHROUGH */
949: case (MDOC_Diag):
950: /* FALLTHROUGH */
951: case (MDOC_Hang):
952: /* FALLTHROUGH */
953: case (MDOC_Ohang):
954: /* FALLTHROUGH */
955: case (MDOC_Inset):
956: /* FALLTHROUGH */
957: case (MDOC_Bullet):
958: /* FALLTHROUGH */
959: case (MDOC_Dash):
960: /* FALLTHROUGH */
961: case (MDOC_Enum):
962: /* FALLTHROUGH */
963: case (MDOC_Hyphen):
964: /* FALLTHROUGH */
965: case (MDOC_Item):
966: type = n->args->argv[i].arg;
967: break;
968: case (MDOC_Column):
969: type = n->args->argv[i].arg;
970: cols = (int)n->args->argv[i].sz;
971: break;
972: default:
973: break;
974: }
975:
976: if (-1 == type)
1.23 kristaps 977: return(mdoc_nerr(mdoc, mdoc->last, ELISTTYPE));
1.1 kristaps 978:
979: switch (type) {
980: case (MDOC_Tag):
981: if (NULL == mdoc->last->head->child)
1.23 kristaps 982: if ( ! mdoc_nwarn(mdoc, mdoc->last, ELINE))
1.1 kristaps 983: return(0);
984: break;
985: case (MDOC_Hang):
986: /* FALLTHROUGH */
987: case (MDOC_Ohang):
988: /* FALLTHROUGH */
989: case (MDOC_Inset):
990: /* FALLTHROUGH */
991: case (MDOC_Diag):
992: if (NULL == mdoc->last->head->child)
1.23 kristaps 993: if ( ! mdoc_nwarn(mdoc, mdoc->last, ELINE))
1.1 kristaps 994: return(0);
995: if (NULL == mdoc->last->body->child)
1.23 kristaps 996: if ( ! mdoc_nwarn(mdoc, mdoc->last, EMULTILINE))
1.1 kristaps 997: return(0);
998: break;
999: case (MDOC_Bullet):
1000: /* FALLTHROUGH */
1001: case (MDOC_Dash):
1002: /* FALLTHROUGH */
1003: case (MDOC_Enum):
1004: /* FALLTHROUGH */
1005: case (MDOC_Hyphen):
1006: /* FALLTHROUGH */
1007: case (MDOC_Item):
1008: if (mdoc->last->head->child)
1.23 kristaps 1009: if ( ! mdoc_nwarn(mdoc, mdoc->last, ENOLINE))
1.1 kristaps 1010: return(0);
1011: if (NULL == mdoc->last->body->child)
1.23 kristaps 1012: if ( ! mdoc_nwarn(mdoc, mdoc->last, EMULTILINE))
1.1 kristaps 1013: return(0);
1014: break;
1015: case (MDOC_Column):
1016: if (NULL == mdoc->last->head->child)
1.23 kristaps 1017: if ( ! mdoc_nwarn(mdoc, mdoc->last, ELINE))
1.1 kristaps 1018: return(0);
1019: if (mdoc->last->body->child)
1.23 kristaps 1020: if ( ! mdoc_nwarn(mdoc, mdoc->last, ENOMULTILINE))
1.1 kristaps 1021: return(0);
1022: c = mdoc->last->child;
1023: for (i = 0; c && MDOC_HEAD == c->type; c = c->next)
1024: i++;
1025: if (i == cols)
1026: break;
1.23 kristaps 1027: return(mdoc_verr(mdoc, mdoc->last->line, mdoc->last->pos,
1028: "column mismatch (have %d, want %d)",
1029: i, cols));
1.1 kristaps 1030: default:
1031: break;
1032: }
1033:
1034: return(1);
1035: }
1036:
1037:
1038: static int
1.14 kristaps 1039: post_bl_head(POST_ARGS)
1040: {
1041: int i;
1042: const struct mdoc_node *n;
1043:
1044: n = mdoc->last->parent;
1045: assert(n->args);
1046:
1047: for (i = 0; i < (int)n->args->argc; i++)
1048: if (n->args->argv[i].arg == MDOC_Column)
1049: break;
1050:
1051: if (i == (int)n->args->argc)
1052: return(1);
1053:
1054: if (n->args->argv[i].sz && mdoc->last->child)
1.23 kristaps 1055: return(mdoc_nerr(mdoc, n, ECOLMIS));
1.14 kristaps 1056:
1057: return(1);
1058: }
1059:
1060:
1061: static int
1.1 kristaps 1062: post_bl(POST_ARGS)
1063: {
1064: struct mdoc_node *n;
1065:
1.14 kristaps 1066: if (MDOC_HEAD == mdoc->last->type)
1067: return(post_bl_head(mdoc));
1.1 kristaps 1068: if (MDOC_BODY != mdoc->last->type)
1069: return(1);
1070: if (NULL == mdoc->last->child)
1071: return(1);
1072:
1073: /* LINTED */
1074: for (n = mdoc->last->child; n; n = n->next) {
1075: if (MDOC_BLOCK == n->type)
1076: if (MDOC_It == n->tok)
1077: continue;
1.23 kristaps 1078: return(mdoc_verr(mdoc, n->line, n->pos,
1079: "bad child of parent %s",
1.1 kristaps 1080: mdoc_macronames[mdoc->last->tok]));
1081: }
1082:
1083: return(1);
1084: }
1085:
1086:
1087: static int
1088: ebool(struct mdoc *mdoc)
1089: {
1090: struct mdoc_node *n;
1091:
1092: /* LINTED */
1093: for (n = mdoc->last->child; n; n = n->next) {
1094: if (MDOC_TEXT != n->type)
1095: break;
1.2 kristaps 1096: if (0 == strcmp(n->string, "on"))
1.1 kristaps 1097: continue;
1.2 kristaps 1098: if (0 == strcmp(n->string, "off"))
1.1 kristaps 1099: continue;
1100: break;
1101: }
1102:
1103: if (NULL == n)
1104: return(1);
1.23 kristaps 1105: return(mdoc_nerr(mdoc, n, EBOOL));
1.1 kristaps 1106: }
1107:
1108:
1109: static int
1110: post_root(POST_ARGS)
1111: {
1112:
1113: if (NULL == mdoc->first->child)
1.23 kristaps 1114: return(mdoc_nerr(mdoc, mdoc->first, ENODAT));
1.12 kristaps 1115: if ( ! (MDOC_PBODY & mdoc->flags))
1.23 kristaps 1116: return(mdoc_nerr(mdoc, mdoc->first, ENOPROLOGUE));
1.1 kristaps 1117:
1118: if (MDOC_BLOCK != mdoc->first->child->type)
1.23 kristaps 1119: return(mdoc_nerr(mdoc, mdoc->first, ENODAT));
1.1 kristaps 1120: if (MDOC_Sh != mdoc->first->child->tok)
1.23 kristaps 1121: return(mdoc_nerr(mdoc, mdoc->first, ENODAT));
1.1 kristaps 1122:
1123: return(1);
1124: }
1125:
1126:
1127: static int
1128: post_st(POST_ARGS)
1129: {
1130:
1131: if (mdoc_a2st(mdoc->last->child->string))
1132: return(1);
1.23 kristaps 1133: return(mdoc_nwarn(mdoc, mdoc->last, EBADSTAND));
1.1 kristaps 1134: }
1135:
1136:
1137: static int
1138: post_sh(POST_ARGS)
1139: {
1140:
1141: if (MDOC_HEAD == mdoc->last->type)
1142: return(post_sh_head(mdoc));
1143: if (MDOC_BODY == mdoc->last->type)
1144: return(post_sh_body(mdoc));
1145:
1146: return(1);
1147: }
1148:
1149:
1150: static int
1151: post_sh_body(POST_ARGS)
1152: {
1153: struct mdoc_node *n;
1154:
1155: if (SEC_NAME != mdoc->lastnamed)
1156: return(1);
1157:
1158: /*
1159: * Warn if the NAME section doesn't contain the `Nm' and `Nd'
1160: * macros (can have multiple `Nm' and one `Nd'). Note that the
1161: * children of the BODY declaration can also be "text".
1162: */
1163:
1164: if (NULL == (n = mdoc->last->child))
1.23 kristaps 1165: return(mdoc_nwarn(mdoc, mdoc->last, ENAMESECINC));
1.1 kristaps 1166:
1167: for ( ; n && n->next; n = n->next) {
1168: if (MDOC_ELEM == n->type && MDOC_Nm == n->tok)
1169: continue;
1170: if (MDOC_TEXT == n->type)
1171: continue;
1.23 kristaps 1172: if ( ! mdoc_nwarn(mdoc, mdoc->last, ENAMESECINC))
1.1 kristaps 1173: return(0);
1174: }
1175:
1176: if (MDOC_ELEM == n->type && MDOC_Nd == n->tok)
1177: return(1);
1.23 kristaps 1178: return(mdoc_nwarn(mdoc, mdoc->last, ENAMESECINC));
1.1 kristaps 1179: }
1180:
1181:
1182: static int
1183: post_sh_head(POST_ARGS)
1184: {
1.2 kristaps 1185: char buf[64];
1186: enum mdoc_sec sec;
1187: const struct mdoc_node *n;
1.1 kristaps 1188:
1189: /*
1190: * Process a new section. Sections are either "named" or
1191: * "custom"; custom sections are user-defined, while named ones
1192: * usually follow a conventional order and may only appear in
1193: * certain manual sections.
1194: */
1195:
1.12 kristaps 1196: buf[0] = 0;
1.1 kristaps 1197:
1.2 kristaps 1198: for (n = mdoc->last->child; n; n = n->next) {
1.12 kristaps 1199: /* XXX - copied from compact(). */
1.2 kristaps 1200: assert(MDOC_TEXT == n->type);
1.12 kristaps 1201:
1.2 kristaps 1202: if (strlcat(buf, n->string, 64) >= 64)
1.23 kristaps 1203: return(mdoc_nerr(mdoc, n, ETOOLONG));
1.2 kristaps 1204: if (NULL == n->next)
1205: continue;
1206: if (strlcat(buf, " ", 64) >= 64)
1.23 kristaps 1207: return(mdoc_nerr(mdoc, n, ETOOLONG));
1.2 kristaps 1208: }
1.1 kristaps 1209:
1210: sec = mdoc_atosec(buf);
1211:
1.12 kristaps 1212: /*
1213: * Check: NAME should always be first, CUSTOM has no roles,
1214: * non-CUSTOM has a conventional order to be followed.
1215: */
1.1 kristaps 1216:
1.12 kristaps 1217: if (SEC_NAME != sec && SEC_NONE == mdoc->lastnamed)
1.23 kristaps 1218: return(mdoc_nerr(mdoc, mdoc->last, ESECNAME));
1.1 kristaps 1219: if (SEC_CUSTOM == sec)
1220: return(1);
1221: if (sec == mdoc->lastnamed)
1.23 kristaps 1222: return(mdoc_nwarn(mdoc, mdoc->last, ESECREP));
1.1 kristaps 1223: if (sec < mdoc->lastnamed)
1.23 kristaps 1224: return(mdoc_nwarn(mdoc, mdoc->last, ESECOOO));
1.1 kristaps 1225:
1.12 kristaps 1226: /*
1227: * Check particular section/manual conventions. LIBRARY can
1228: * only occur in msec 2, 3 (TODO: are there more of these?).
1229: */
1.1 kristaps 1230:
1231: switch (sec) {
1232: case (SEC_LIBRARY):
1233: switch (mdoc->meta.msec) {
1234: case (2):
1235: /* FALLTHROUGH */
1236: case (3):
1237: break;
1238: default:
1.23 kristaps 1239: return(mdoc_nwarn(mdoc, mdoc->last, EWRONGMSEC));
1.1 kristaps 1240: }
1241: break;
1242: default:
1243: break;
1244: }
1245:
1246: return(1);
1247: }
1248:
1249:
1250: static int
1251: pre_fd(PRE_ARGS)
1252: {
1253:
1254: return(check_sec(mdoc, n, SEC_SYNOPSIS, SEC_CUSTOM));
1255: }
CVSweb