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