]>
git.cameronkatri.com Git - mandoc.git/blob - mdoc_validate.c
1 /* $Id: mdoc_validate.c,v 1.66 2010/04/06 16:27:53 kristaps Exp $ */
3 * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@kth.se>
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
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.
21 #include <sys/types.h>
31 #include "libmandoc.h"
33 /* FIXME: .Bl -diag can't have non-text children in HEAD. */
34 /* TODO: ignoring Pp (it's superfluous in some invocations). */
36 #define PRE_ARGS struct mdoc *mdoc, const struct mdoc_node *n
37 #define POST_ARGS struct mdoc *mdoc
39 typedef int (*v_pre
)(PRE_ARGS
);
40 typedef int (*v_post
)(POST_ARGS
);
47 static int check_parent(PRE_ARGS
, enum mdoct
, enum mdoc_type
);
48 static int check_msec(PRE_ARGS
, ...);
49 static int check_sec(PRE_ARGS
, ...);
50 static int check_stdarg(PRE_ARGS
);
51 static int check_text(struct mdoc
*, int, int, const char *);
52 static int check_argv(struct mdoc
*,
53 const struct mdoc_node
*,
54 const struct mdoc_argv
*);
55 static int check_args(struct mdoc
*,
56 const struct mdoc_node
*);
57 static int err_child_lt(struct mdoc
*, const char *, int);
58 static int warn_child_lt(struct mdoc
*, const char *, int);
59 static int err_child_gt(struct mdoc
*, const char *, int);
60 static int warn_child_gt(struct mdoc
*, const char *, int);
61 static int err_child_eq(struct mdoc
*, const char *, int);
62 static int warn_child_eq(struct mdoc
*, const char *, int);
63 static int warn_print(struct mdoc
*, int, int);
64 static int warn_count(struct mdoc
*, const char *,
65 int, const char *, int);
66 static int err_count(struct mdoc
*, const char *,
67 int, const char *, int);
69 static int berr_ge1(POST_ARGS
);
70 static int bwarn_ge1(POST_ARGS
);
71 static int ebool(POST_ARGS
);
72 static int eerr_eq0(POST_ARGS
);
73 static int eerr_eq1(POST_ARGS
);
74 static int eerr_ge1(POST_ARGS
);
75 static int eerr_le1(POST_ARGS
);
76 static int ewarn_ge1(POST_ARGS
);
77 static int herr_eq0(POST_ARGS
);
78 static int herr_ge1(POST_ARGS
);
79 static int hwarn_eq1(POST_ARGS
);
80 static int hwarn_eq0(POST_ARGS
);
81 static int hwarn_le1(POST_ARGS
);
83 static int post_an(POST_ARGS
);
84 static int post_at(POST_ARGS
);
85 static int post_bf(POST_ARGS
);
86 static int post_bl(POST_ARGS
);
87 static int post_bl_head(POST_ARGS
);
88 static int post_it(POST_ARGS
);
89 static int post_lb(POST_ARGS
);
90 static int post_nm(POST_ARGS
);
91 static int post_root(POST_ARGS
);
92 static int post_rs(POST_ARGS
);
93 static int post_sh(POST_ARGS
);
94 static int post_sh_body(POST_ARGS
);
95 static int post_sh_head(POST_ARGS
);
96 static int post_st(POST_ARGS
);
97 static int post_vt(POST_ARGS
);
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_cd(PRE_ARGS
);
102 static int pre_dd(PRE_ARGS
);
103 static int pre_display(PRE_ARGS
);
104 static int pre_dt(PRE_ARGS
);
105 static int pre_er(PRE_ARGS
);
106 static int pre_ex(PRE_ARGS
);
107 static int pre_fd(PRE_ARGS
);
108 static int pre_it(PRE_ARGS
);
109 static int pre_lb(PRE_ARGS
);
110 static int pre_os(PRE_ARGS
);
111 static int pre_rv(PRE_ARGS
);
112 static int pre_sh(PRE_ARGS
);
113 static int pre_ss(PRE_ARGS
);
115 static v_post posts_an
[] = { post_an
, NULL
};
116 static v_post posts_at
[] = { post_at
, NULL
};
117 static v_post posts_bd
[] = { hwarn_eq0
, bwarn_ge1
, NULL
};
118 static v_post posts_bf
[] = { hwarn_le1
, post_bf
, NULL
};
119 static v_post posts_bl
[] = { bwarn_ge1
, post_bl
, NULL
};
120 static v_post posts_bool
[] = { eerr_eq1
, ebool
, NULL
};
121 static v_post posts_fo
[] = { hwarn_eq1
, bwarn_ge1
, NULL
};
122 static v_post posts_it
[] = { post_it
, NULL
};
123 static v_post posts_lb
[] = { eerr_eq1
, post_lb
, NULL
};
124 static v_post posts_nd
[] = { berr_ge1
, NULL
};
125 static v_post posts_nm
[] = { post_nm
, NULL
};
126 static v_post posts_notext
[] = { eerr_eq0
, NULL
};
127 static v_post posts_rs
[] = { berr_ge1
, herr_eq0
, post_rs
, NULL
};
128 static v_post posts_sh
[] = { herr_ge1
, bwarn_ge1
, post_sh
, NULL
};
129 static v_post posts_sp
[] = { eerr_le1
, NULL
};
130 static v_post posts_ss
[] = { herr_ge1
, NULL
};
131 static v_post posts_st
[] = { eerr_eq1
, post_st
, NULL
};
132 static v_post posts_text
[] = { eerr_ge1
, NULL
};
133 static v_post posts_text1
[] = { eerr_eq1
, NULL
};
134 static v_post posts_vt
[] = { post_vt
, NULL
};
135 static v_post posts_wline
[] = { bwarn_ge1
, herr_eq0
, NULL
};
136 static v_post posts_wtext
[] = { ewarn_ge1
, NULL
};
137 static v_post posts_xr
[] = { ewarn_ge1
, NULL
};
138 static v_pre pres_an
[] = { pre_an
, NULL
};
139 static v_pre pres_bd
[] = { pre_display
, pre_bd
, NULL
};
140 static v_pre pres_bl
[] = { pre_bl
, NULL
};
141 static v_pre pres_cd
[] = { pre_cd
, NULL
};
142 static v_pre pres_d1
[] = { pre_display
, NULL
};
143 static v_pre pres_dd
[] = { pre_dd
, NULL
};
144 static v_pre pres_dt
[] = { pre_dt
, NULL
};
145 static v_pre pres_er
[] = { pre_er
, NULL
};
146 static v_pre pres_ex
[] = { pre_ex
, NULL
};
147 static v_pre pres_fd
[] = { pre_fd
, NULL
};
148 static v_pre pres_it
[] = { pre_it
, NULL
};
149 static v_pre pres_lb
[] = { pre_lb
, NULL
};
150 static v_pre pres_os
[] = { pre_os
, NULL
};
151 static v_pre pres_rv
[] = { pre_rv
, NULL
};
152 static v_pre pres_sh
[] = { pre_sh
, NULL
};
153 static v_pre pres_ss
[] = { pre_ss
, NULL
};
155 const struct valids mdoc_valids
[MDOC_MAX
] = {
156 { NULL
, NULL
}, /* Ap */
157 { pres_dd
, posts_text
}, /* Dd */
158 { pres_dt
, NULL
}, /* Dt */
159 { pres_os
, NULL
}, /* Os */
160 { pres_sh
, posts_sh
}, /* Sh */
161 { pres_ss
, posts_ss
}, /* Ss */
162 { NULL
, posts_notext
}, /* Pp */
163 { pres_d1
, posts_wline
}, /* D1 */
164 { pres_d1
, posts_wline
}, /* Dl */
165 { pres_bd
, posts_bd
}, /* Bd */
166 { NULL
, NULL
}, /* Ed */
167 { pres_bl
, posts_bl
}, /* Bl */
168 { NULL
, NULL
}, /* El */
169 { pres_it
, posts_it
}, /* It */
170 { NULL
, posts_text
}, /* Ad */
171 { pres_an
, posts_an
}, /* An */
172 { NULL
, NULL
}, /* Ar */
173 { pres_cd
, posts_text
}, /* Cd */
174 { NULL
, NULL
}, /* Cm */
175 { NULL
, NULL
}, /* Dv */
176 { pres_er
, posts_text
}, /* Er */
177 { NULL
, NULL
}, /* Ev */
178 { pres_ex
, NULL
}, /* Ex */
179 { NULL
, NULL
}, /* Fa */
180 { pres_fd
, posts_wtext
}, /* Fd */
181 { NULL
, NULL
}, /* Fl */
182 { NULL
, posts_text
}, /* Fn */
183 { NULL
, posts_wtext
}, /* Ft */
184 { NULL
, posts_text
}, /* Ic */
185 { NULL
, posts_text1
}, /* In */
186 { NULL
, NULL
}, /* Li */
187 { NULL
, posts_nd
}, /* Nd */
188 { NULL
, posts_nm
}, /* Nm */
189 { NULL
, posts_wline
}, /* Op */
190 { NULL
, NULL
}, /* Ot */
191 { NULL
, NULL
}, /* Pa */
192 { pres_rv
, NULL
}, /* Rv */
193 { NULL
, posts_st
}, /* St */
194 { NULL
, NULL
}, /* Va */
195 { NULL
, posts_vt
}, /* Vt */
196 { NULL
, posts_xr
}, /* Xr */
197 { NULL
, posts_text
}, /* %A */
198 { NULL
, posts_text
}, /* %B */ /* FIXME: can be used outside Rs/Re. */
199 { NULL
, posts_text
}, /* %D */ /* FIXME: check date with mandoc_a2time(). */
200 { NULL
, posts_text
}, /* %I */
201 { NULL
, posts_text
}, /* %J */
202 { NULL
, posts_text
}, /* %N */
203 { NULL
, posts_text
}, /* %O */
204 { NULL
, posts_text
}, /* %P */
205 { NULL
, posts_text
}, /* %R */
206 { NULL
, posts_text
}, /* %T */ /* FIXME: can be used outside Rs/Re. */
207 { NULL
, posts_text
}, /* %V */
208 { NULL
, NULL
}, /* Ac */
209 { NULL
, NULL
}, /* Ao */
210 { NULL
, posts_wline
}, /* Aq */
211 { NULL
, posts_at
}, /* At */
212 { NULL
, NULL
}, /* Bc */
213 { NULL
, posts_bf
}, /* Bf */
214 { NULL
, NULL
}, /* Bo */
215 { NULL
, posts_wline
}, /* Bq */
216 { NULL
, NULL
}, /* Bsx */
217 { NULL
, NULL
}, /* Bx */
218 { NULL
, posts_bool
}, /* Db */
219 { NULL
, NULL
}, /* Dc */
220 { NULL
, NULL
}, /* Do */
221 { NULL
, posts_wline
}, /* Dq */
222 { NULL
, NULL
}, /* Ec */
223 { NULL
, NULL
}, /* Ef */
224 { NULL
, NULL
}, /* Em */
225 { NULL
, NULL
}, /* Eo */
226 { NULL
, NULL
}, /* Fx */
227 { NULL
, posts_text
}, /* Ms */
228 { NULL
, posts_notext
}, /* No */
229 { NULL
, posts_notext
}, /* Ns */
230 { NULL
, NULL
}, /* Nx */
231 { NULL
, NULL
}, /* Ox */
232 { NULL
, NULL
}, /* Pc */
233 { NULL
, posts_text1
}, /* Pf */
234 { NULL
, NULL
}, /* Po */
235 { NULL
, posts_wline
}, /* Pq */
236 { NULL
, NULL
}, /* Qc */
237 { NULL
, posts_wline
}, /* Ql */
238 { NULL
, NULL
}, /* Qo */
239 { NULL
, posts_wline
}, /* Qq */
240 { NULL
, NULL
}, /* Re */
241 { NULL
, posts_rs
}, /* Rs */
242 { NULL
, NULL
}, /* Sc */
243 { NULL
, NULL
}, /* So */
244 { NULL
, posts_wline
}, /* Sq */
245 { NULL
, posts_bool
}, /* Sm */
246 { NULL
, posts_text
}, /* Sx */
247 { NULL
, posts_text
}, /* Sy */
248 { NULL
, posts_text
}, /* Tn */
249 { NULL
, NULL
}, /* Ux */
250 { NULL
, NULL
}, /* Xc */
251 { NULL
, NULL
}, /* Xo */
252 { NULL
, posts_fo
}, /* Fo */
253 { NULL
, NULL
}, /* Fc */
254 { NULL
, NULL
}, /* Oo */
255 { NULL
, NULL
}, /* Oc */
256 { NULL
, posts_wline
}, /* Bk */
257 { NULL
, NULL
}, /* Ek */
258 { NULL
, posts_notext
}, /* Bt */
259 { NULL
, NULL
}, /* Hf */
260 { NULL
, NULL
}, /* Fr */
261 { NULL
, posts_notext
}, /* Ud */
262 { pres_lb
, posts_lb
}, /* Lb */
263 { NULL
, posts_notext
}, /* Lp */
264 { NULL
, posts_text
}, /* Lk */
265 { NULL
, posts_text
}, /* Mt */
266 { NULL
, posts_wline
}, /* Brq */
267 { NULL
, NULL
}, /* Bro */
268 { NULL
, NULL
}, /* Brc */
269 { NULL
, posts_text
}, /* %C */
270 { NULL
, NULL
}, /* Es */
271 { NULL
, NULL
}, /* En */
272 { NULL
, NULL
}, /* Dx */
273 { NULL
, posts_text
}, /* %Q */
274 { NULL
, posts_notext
}, /* br */
275 { NULL
, posts_sp
}, /* sp */
276 { NULL
, posts_text1
}, /* %U */
281 mdoc_valid_pre(struct mdoc
*mdoc
, const struct mdoc_node
*n
)
287 if (MDOC_TEXT
== n
->type
) {
291 return(check_text(mdoc
, line
, pos
, tp
));
294 if ( ! check_args(mdoc
, n
))
296 if (NULL
== mdoc_valids
[n
->tok
].pre
)
298 for (p
= mdoc_valids
[n
->tok
].pre
; *p
; p
++)
299 if ( ! (*p
)(mdoc
, n
))
306 mdoc_valid_post(struct mdoc
*mdoc
)
310 if (MDOC_VALID
& mdoc
->last
->flags
)
312 mdoc
->last
->flags
|= MDOC_VALID
;
314 if (MDOC_TEXT
== mdoc
->last
->type
)
316 if (MDOC_ROOT
== mdoc
->last
->type
)
317 return(post_root(mdoc
));
319 if (NULL
== mdoc_valids
[mdoc
->last
->tok
].post
)
321 for (p
= mdoc_valids
[mdoc
->last
->tok
].post
; *p
; p
++)
330 warn_print(struct mdoc
*m
, int ln
, int pos
)
333 if (MDOC_IGN_CHARS
& m
->pflags
)
334 return(mdoc_pwarn(m
, ln
, pos
, EPRINT
));
335 return(mdoc_perr(m
, ln
, pos
, EPRINT
));
340 warn_count(struct mdoc
*m
, const char *k
,
341 int want
, const char *v
, int has
)
344 return(mdoc_vwarn(m
, m
->last
->line
, m
->last
->pos
,
345 "suggests %s %s %d (has %d)", v
, k
, want
, has
));
350 err_count(struct mdoc
*m
, const char *k
,
351 int want
, const char *v
, int has
)
354 return(mdoc_verr(m
, m
->last
->line
, m
->last
->pos
,
355 "requires %s %s %d (has %d)", v
, k
, want
, has
));
360 * Build these up with macros because they're basically the same check
361 * for different inequalities. Yes, this could be done with functions,
362 * but this is reasonable for now.
365 #define CHECK_CHILD_DEFN(lvl, name, ineq) \
367 lvl##_child_##name(struct mdoc *mdoc, const char *p, int sz) \
369 if (mdoc->last->nchild ineq sz) \
371 return(lvl##_count(mdoc, #ineq, sz, p, mdoc->last->nchild)); \
374 #define CHECK_BODY_DEFN(name, lvl, func, num) \
376 b##lvl##_##name(POST_ARGS) \
378 if (MDOC_BODY != mdoc->last->type) \
380 return(func(mdoc, "multi-line arguments", (num))); \
383 #define CHECK_ELEM_DEFN(name, lvl, func, num) \
385 e##lvl##_##name(POST_ARGS) \
387 assert(MDOC_ELEM == mdoc->last->type); \
388 return(func(mdoc, "line arguments", (num))); \
391 #define CHECK_HEAD_DEFN(name, lvl, func, num) \
393 h##lvl##_##name(POST_ARGS) \
395 if (MDOC_HEAD != mdoc->last->type) \
397 return(func(mdoc, "line arguments", (num))); \
401 CHECK_CHILD_DEFN(warn
, gt
, >) /* warn_child_gt() */
402 CHECK_CHILD_DEFN(err
, gt
, >) /* err_child_gt() */
403 CHECK_CHILD_DEFN(warn
, eq
, ==) /* warn_child_eq() */
404 CHECK_CHILD_DEFN(err
, eq
, ==) /* err_child_eq() */
405 CHECK_CHILD_DEFN(err
, lt
, <) /* err_child_lt() */
406 CHECK_CHILD_DEFN(warn
, lt
, <) /* warn_child_lt() */
407 CHECK_BODY_DEFN(ge1
, warn
, warn_child_gt
, 0) /* bwarn_ge1() */
408 CHECK_BODY_DEFN(ge1
, err
, err_child_gt
, 0) /* berr_ge1() */
409 CHECK_ELEM_DEFN(ge1
, warn
, warn_child_gt
, 0) /* ewarn_gt1() */
410 CHECK_ELEM_DEFN(eq1
, err
, err_child_eq
, 1) /* eerr_eq1() */
411 CHECK_ELEM_DEFN(le1
, err
, err_child_lt
, 2) /* eerr_le1() */
412 CHECK_ELEM_DEFN(eq0
, err
, err_child_eq
, 0) /* eerr_eq0() */
413 CHECK_ELEM_DEFN(ge1
, err
, err_child_gt
, 0) /* eerr_ge1() */
414 CHECK_HEAD_DEFN(eq0
, err
, err_child_eq
, 0) /* herr_eq0() */
415 CHECK_HEAD_DEFN(le1
, warn
, warn_child_lt
, 2) /* hwarn_le1() */
416 CHECK_HEAD_DEFN(ge1
, err
, err_child_gt
, 0) /* herr_ge1() */
417 CHECK_HEAD_DEFN(eq1
, warn
, warn_child_eq
, 1) /* hwarn_eq1() */
418 CHECK_HEAD_DEFN(eq0
, warn
, warn_child_eq
, 0) /* hwarn_eq0() */
422 check_stdarg(PRE_ARGS
)
425 if (n
->args
&& 1 == n
->args
->argc
)
426 if (MDOC_Std
== n
->args
->argv
[0].arg
)
428 return(mdoc_nwarn(mdoc
, n
, EARGVAL
));
433 check_sec(PRE_ARGS
, ...)
442 sec
= (enum mdoc_sec
)va_arg(ap
, int);
443 if (SEC_CUSTOM
== sec
)
445 if (sec
!= mdoc
->lastsec
)
452 return(mdoc_nwarn(mdoc
, n
, EBADSEC
));
457 check_msec(PRE_ARGS
, ...)
465 if (0 == (msec
= va_arg(ap
, int)))
467 if (msec
!= mdoc
->meta
.msec
)
474 return(mdoc_nwarn(mdoc
, n
, EBADMSEC
));
479 check_args(struct mdoc
*m
, const struct mdoc_node
*n
)
486 assert(n
->args
->argc
);
487 for (i
= 0; i
< (int)n
->args
->argc
; i
++)
488 if ( ! check_argv(m
, n
, &n
->args
->argv
[i
]))
496 check_argv(struct mdoc
*m
, const struct mdoc_node
*n
,
497 const struct mdoc_argv
*v
)
501 for (i
= 0; i
< (int)v
->sz
; i
++)
502 if ( ! check_text(m
, v
->line
, v
->pos
, v
->value
[i
]))
505 if (MDOC_Std
== v
->arg
) {
506 /* `Nm' name must be set. */
507 if (v
->sz
|| m
->meta
.name
)
509 return(mdoc_nerr(m
, n
, ENAME
));
517 check_text(struct mdoc
*mdoc
, int line
, int pos
, const char *p
)
521 for ( ; *p
; p
++, pos
++) {
523 if ( ! (MDOC_LITERAL
& mdoc
->flags
))
524 if ( ! warn_print(mdoc
, line
, pos
))
526 } else if ( ! isprint((u_char
)*p
))
527 if ( ! warn_print(mdoc
, line
, pos
))
533 c
= mandoc_special(p
);
539 if ( ! (MDOC_IGN_ESCAPE
& mdoc
->pflags
))
540 return(mdoc_perr(mdoc
, line
, pos
, EESCAPE
));
541 if ( ! mdoc_pwarn(mdoc
, line
, pos
, EESCAPE
))
552 check_parent(PRE_ARGS
, enum mdoct tok
, enum mdoc_type t
)
556 if ((MDOC_ROOT
== t
|| tok
== n
->parent
->tok
) &&
557 (t
== n
->parent
->type
))
560 return(mdoc_verr(mdoc
, n
->line
, n
->pos
, "require parent %s",
561 MDOC_ROOT
== t
? "<root>" : mdoc_macronames
[tok
]));
567 pre_display(PRE_ARGS
)
569 struct mdoc_node
*node
;
571 /* Display elements (`Bd', `D1'...) cannot be nested. */
573 if (MDOC_BLOCK
!= n
->type
)
577 for (node
= mdoc
->last
->parent
; node
; node
= node
->parent
)
578 if (MDOC_BLOCK
== node
->type
)
579 if (MDOC_Bd
== node
->tok
)
584 return(mdoc_nerr(mdoc
, n
, ENESTDISP
));
591 int pos
, type
, width
, offset
;
593 if (MDOC_BLOCK
!= n
->type
)
596 return(mdoc_nerr(mdoc
, n
, ELISTTYPE
));
598 /* Make sure that only one type of list is specified. */
600 type
= offset
= width
= -1;
603 for (pos
= 0; pos
< (int)n
->args
->argc
; pos
++)
604 switch (n
->args
->argv
[pos
].arg
) {
627 * Note that if a duplicate is detected, we
628 * remove the duplicate instead of passing it
629 * over. If we don't do this, mdoc_action will
630 * become confused when it scans over multiple
631 * types whilst setting its bitmasks.
633 * FIXME: this should occur in mdoc_action.c.
636 if ( ! mdoc_nwarn(mdoc
, n
, EMULTILIST
))
638 mdoc_argn_free(n
->args
, pos
);
641 type
= n
->args
->argv
[pos
].arg
;
644 if (type
< 0 && ! mdoc_nwarn(mdoc
, n
, ENOTYPE
))
649 return(mdoc_nerr(mdoc
, n
, EARGREP
));
650 if (type
< 0 && ! mdoc_nwarn(mdoc
, n
, ENOTYPE
))
652 width
= n
->args
->argv
[pos
].arg
;
656 return(mdoc_nerr(mdoc
, n
, EARGREP
));
657 if (type
< 0 && ! mdoc_nwarn(mdoc
, n
, ENOTYPE
))
659 offset
= n
->args
->argv
[pos
].arg
;
666 return(mdoc_nerr(mdoc
, n
, ELISTTYPE
));
669 * Validate the width field. Some list types don't need width
670 * types and should be warned about them. Others should have it
671 * and must also be warned.
676 if (width
< 0 && ! mdoc_nwarn(mdoc
, n
, EMISSWIDTH
))
688 if (width
>= 0 && ! mdoc_nwarn(mdoc
, n
, ENOWIDTH
))
704 if (MDOC_BLOCK
!= n
->type
)
707 return(mdoc_nerr(mdoc
, n
, EDISPTYPE
));
709 /* Make sure that only one type of display is specified. */
712 for (i
= 0, err
= type
= 0; ! err
&&
713 i
< (int)n
->args
->argc
; i
++)
714 switch (n
->args
->argv
[i
].arg
) {
719 case (MDOC_Unfilled
):
726 return(mdoc_nerr(mdoc
, n
, EMULTIDISP
));
733 return(mdoc_nerr(mdoc
, n
, EDISPTYPE
));
741 if (MDOC_BLOCK
!= n
->type
)
743 return(check_parent(mdoc
, n
, MDOC_Sh
, MDOC_BODY
));
751 if (MDOC_BLOCK
!= n
->type
)
753 return(check_parent(mdoc
, n
, -1, MDOC_ROOT
));
761 if (MDOC_BLOCK
!= n
->type
)
763 return(check_parent(mdoc
, n
, MDOC_Bl
, MDOC_BODY
));
771 if (NULL
== n
->args
|| 1 == n
->args
->argc
)
773 return(mdoc_verr(mdoc
, n
->line
, n
->pos
,
774 "only one argument allowed"));
782 return(check_sec(mdoc
, n
, SEC_LIBRARY
, SEC_CUSTOM
));
790 if ( ! check_msec(mdoc
, n
, 2, 3, 0))
792 return(check_stdarg(mdoc
, n
));
800 if ( ! check_msec(mdoc
, n
, 1, 6, 8, 0))
802 return(check_stdarg(mdoc
, n
));
810 return(check_msec(mdoc
, n
, 2, 3, 9, 0));
818 return(check_msec(mdoc
, n
, 4, 0));
826 /* FIXME: make sure is capitalised. */
828 if (0 == mdoc
->meta
.date
|| mdoc
->meta
.os
)
829 if ( ! mdoc_nwarn(mdoc
, n
, EPROLOOO
))
831 if (mdoc
->meta
.title
)
832 if ( ! mdoc_nwarn(mdoc
, n
, EPROLREP
))
842 if (NULL
== mdoc
->meta
.title
|| 0 == mdoc
->meta
.date
)
843 if ( ! mdoc_nwarn(mdoc
, n
, EPROLOOO
))
846 if ( ! mdoc_nwarn(mdoc
, n
, EPROLREP
))
856 if (mdoc
->meta
.title
|| mdoc
->meta
.os
)
857 if ( ! mdoc_nwarn(mdoc
, n
, EPROLOOO
))
860 if ( ! mdoc_nwarn(mdoc
, n
, EPROLREP
))
870 struct mdoc_node
*head
;
872 if (MDOC_BLOCK
!= mdoc
->last
->type
)
875 head
= mdoc
->last
->head
;
877 if (mdoc
->last
->args
&& head
->child
)
878 return(mdoc_nerr(mdoc
, mdoc
->last
, ELINE
));
879 else if (mdoc
->last
->args
)
882 if (NULL
== head
->child
|| MDOC_TEXT
!= head
->child
->type
)
883 return(mdoc_nerr(mdoc
, mdoc
->last
, ELINE
));
885 p
= head
->child
->string
;
887 if (0 == strcmp(p
, "Em"))
889 else if (0 == strcmp(p
, "Li"))
891 else if (0 == strcmp(p
, "Sy"))
894 return(mdoc_nerr(mdoc
, head
, EFONT
));
902 if (mdoc_a2lib(mdoc
->last
->child
->string
))
904 return(mdoc_nwarn(mdoc
, mdoc
->last
, ELIB
));
911 const struct mdoc_node
*n
;
914 * The Vt macro comes in both ELEM and BLOCK form, both of which
915 * have different syntaxes (yet more context-sensitive
916 * behaviour). ELEM types must have a child; BLOCK types,
917 * specifically the BODY, should only have TEXT children.
920 if (MDOC_ELEM
== mdoc
->last
->type
)
921 return(eerr_ge1(mdoc
));
922 if (MDOC_BODY
!= mdoc
->last
->type
)
925 for (n
= mdoc
->last
->child
; n
; n
= n
->next
)
926 if (MDOC_TEXT
!= n
->type
)
927 if ( ! mdoc_nwarn(mdoc
, n
, EBADCHILD
))
938 if (mdoc
->last
->child
)
942 return(mdoc_nerr(mdoc
, mdoc
->last
, ENAME
));
950 if (NULL
== mdoc
->last
->child
)
952 if (MDOC_TEXT
!= mdoc
->last
->child
->type
)
953 return(mdoc_nerr(mdoc
, mdoc
->last
, EATT
));
954 if (mdoc_a2att(mdoc
->last
->child
->string
))
956 return(mdoc_nerr(mdoc
, mdoc
->last
, EATT
));
964 if (mdoc
->last
->args
) {
965 if (NULL
== mdoc
->last
->child
)
967 return(mdoc_nerr(mdoc
, mdoc
->last
, ENOLINE
));
970 if (mdoc
->last
->child
)
972 return(mdoc_nerr(mdoc
, mdoc
->last
, ELINE
));
980 struct mdoc_node
*n
, *c
;
982 if (MDOC_BLOCK
!= mdoc
->last
->type
)
985 n
= mdoc
->last
->parent
->parent
;
987 return(mdoc_nerr(mdoc
, mdoc
->last
, ELISTTYPE
));
989 /* Some types require block-head, some not. */
992 for (cols
= type
= -1, i
= 0; -1 == type
&&
993 i
< (int)n
->args
->argc
; i
++)
994 switch (n
->args
->argv
[i
].arg
) {
1014 type
= n
->args
->argv
[i
].arg
;
1017 type
= n
->args
->argv
[i
].arg
;
1018 cols
= (int)n
->args
->argv
[i
].sz
;
1025 return(mdoc_nerr(mdoc
, mdoc
->last
, ELISTTYPE
));
1029 if (NULL
== mdoc
->last
->head
->child
)
1030 if ( ! mdoc_nwarn(mdoc
, mdoc
->last
, ELINE
))
1040 if (NULL
== mdoc
->last
->head
->child
)
1041 if ( ! mdoc_nwarn(mdoc
, mdoc
->last
, ELINE
))
1043 if (NULL
== mdoc
->last
->body
->child
)
1044 if ( ! mdoc_nwarn(mdoc
, mdoc
->last
, EMULTILINE
))
1056 if (mdoc
->last
->head
->child
)
1057 if ( ! mdoc_nwarn(mdoc
, mdoc
->last
, ENOLINE
))
1059 if (NULL
== mdoc
->last
->body
->child
)
1060 if ( ! mdoc_nwarn(mdoc
, mdoc
->last
, EMULTILINE
))
1064 if (NULL
== mdoc
->last
->head
->child
)
1065 if ( ! mdoc_nwarn(mdoc
, mdoc
->last
, ELINE
))
1067 if (mdoc
->last
->body
->child
)
1068 if ( ! mdoc_nwarn(mdoc
, mdoc
->last
, ENOMULTILINE
))
1070 c
= mdoc
->last
->child
;
1071 for (i
= 0; c
&& MDOC_HEAD
== c
->type
; c
= c
->next
)
1074 if (i
< cols
|| i
== (cols
+ 1)) {
1075 if ( ! mdoc_vwarn(mdoc
, mdoc
->last
->line
,
1076 mdoc
->last
->pos
, "column "
1077 "mismatch: have %d, want %d",
1081 } else if (i
== cols
)
1084 return(mdoc_verr(mdoc
, mdoc
->last
->line
,
1085 mdoc
->last
->pos
, "column mismatch: "
1086 "have %d, want %d", i
, cols
));
1096 post_bl_head(POST_ARGS
)
1099 const struct mdoc_node
*n
;
1100 const struct mdoc_argv
*a
;
1102 n
= mdoc
->last
->parent
;
1105 for (i
= 0; i
< (int)n
->args
->argc
; i
++) {
1106 a
= &n
->args
->argv
[i
];
1107 if (a
->arg
== MDOC_Column
) {
1108 if (a
->sz
&& mdoc
->last
->nchild
)
1109 return(mdoc_nerr(mdoc
, n
, ECOLMIS
));
1114 if (0 == (i
= mdoc
->last
->nchild
))
1116 return(warn_count(mdoc
, "==", 0, "line arguments", i
));
1123 struct mdoc_node
*n
;
1125 if (MDOC_HEAD
== mdoc
->last
->type
)
1126 return(post_bl_head(mdoc
));
1127 if (MDOC_BODY
!= mdoc
->last
->type
)
1129 if (NULL
== mdoc
->last
->child
)
1133 * We only allow certain children of `Bl'. This is usually on
1134 * `It', but apparently `Sm' occurs here and there, so we let
1135 * that one through, too.
1139 for (n
= mdoc
->last
->child
; n
; n
= n
->next
) {
1140 if (MDOC_BLOCK
== n
->type
&& MDOC_It
== n
->tok
)
1142 if (MDOC_Sm
== n
->tok
)
1144 return(mdoc_nerr(mdoc
, n
, EBADCHILD
));
1152 ebool(struct mdoc
*mdoc
)
1154 struct mdoc_node
*n
;
1157 for (n
= mdoc
->last
->child
; n
; n
= n
->next
) {
1158 if (MDOC_TEXT
!= n
->type
)
1160 if (0 == strcmp(n
->string
, "on"))
1162 if (0 == strcmp(n
->string
, "off"))
1169 return(mdoc_nerr(mdoc
, n
, EBOOL
));
1174 post_root(POST_ARGS
)
1177 if (NULL
== mdoc
->first
->child
)
1178 return(mdoc_nerr(mdoc
, mdoc
->first
, ENODAT
));
1179 if ( ! (MDOC_PBODY
& mdoc
->flags
))
1180 return(mdoc_nerr(mdoc
, mdoc
->first
, ENOPROLOGUE
));
1182 if (MDOC_BLOCK
!= mdoc
->first
->child
->type
)
1183 return(mdoc_nerr(mdoc
, mdoc
->first
, ENODAT
));
1184 if (MDOC_Sh
!= mdoc
->first
->child
->tok
)
1185 return(mdoc_nerr(mdoc
, mdoc
->first
, ENODAT
));
1195 if (mdoc_a2st(mdoc
->last
->child
->string
))
1197 return(mdoc_nerr(mdoc
, mdoc
->last
, EBADSTAND
));
1204 struct mdoc_node
*nn
;
1206 if (MDOC_BODY
!= mdoc
->last
->type
)
1209 for (nn
= mdoc
->last
->child
; nn
; nn
= nn
->next
)
1240 return(mdoc_nerr(mdoc
, nn
, EBADCHILD
));
1251 if (MDOC_HEAD
== mdoc
->last
->type
)
1252 return(post_sh_head(mdoc
));
1253 if (MDOC_BODY
== mdoc
->last
->type
)
1254 return(post_sh_body(mdoc
));
1261 post_sh_body(POST_ARGS
)
1263 struct mdoc_node
*n
;
1265 if (SEC_NAME
!= mdoc
->lastsec
)
1269 * Warn if the NAME section doesn't contain the `Nm' and `Nd'
1270 * macros (can have multiple `Nm' and one `Nd'). Note that the
1271 * children of the BODY declaration can also be "text".
1274 if (NULL
== (n
= mdoc
->last
->child
))
1275 return(mdoc_nwarn(mdoc
, mdoc
->last
, ENAMESECINC
));
1277 for ( ; n
&& n
->next
; n
= n
->next
) {
1278 if (MDOC_ELEM
== n
->type
&& MDOC_Nm
== n
->tok
)
1280 if (MDOC_TEXT
== n
->type
)
1282 if ( ! mdoc_nwarn(mdoc
, mdoc
->last
, ENAMESECINC
))
1287 if (MDOC_BLOCK
== n
->type
&& MDOC_Nd
== n
->tok
)
1289 return(mdoc_nwarn(mdoc
, mdoc
->last
, ENAMESECINC
));
1294 post_sh_head(POST_ARGS
)
1298 const struct mdoc_node
*n
;
1301 * Process a new section. Sections are either "named" or
1302 * "custom"; custom sections are user-defined, while named ones
1303 * usually follow a conventional order and may only appear in
1304 * certain manual sections.
1309 for (n
= mdoc
->last
->child
; n
; n
= n
->next
) {
1310 /* XXX - copied from compact(). */
1311 assert(MDOC_TEXT
== n
->type
);
1313 if (strlcat(buf
, n
->string
, 64) >= 64)
1314 return(mdoc_nerr(mdoc
, n
, ETOOLONG
));
1315 if (NULL
== n
->next
)
1317 if (strlcat(buf
, " ", 64) >= 64)
1318 return(mdoc_nerr(mdoc
, n
, ETOOLONG
));
1321 sec
= mdoc_atosec(buf
);
1324 * Check: NAME should always be first, CUSTOM has no roles,
1325 * non-CUSTOM has a conventional order to be followed.
1328 if (SEC_NAME
!= sec
&& SEC_NONE
== mdoc
->lastnamed
&&
1329 ! mdoc_nwarn(mdoc
, mdoc
->last
, ESECNAME
))
1331 if (SEC_CUSTOM
== sec
)
1333 if (sec
== mdoc
->lastnamed
)
1334 if ( ! mdoc_nwarn(mdoc
, mdoc
->last
, ESECREP
))
1336 if (sec
< mdoc
->lastnamed
)
1337 if ( ! mdoc_nwarn(mdoc
, mdoc
->last
, ESECOOO
))
1341 * Check particular section/manual conventions. LIBRARY can
1342 * only occur in msec 2, 3 (TODO: are there more of these?).
1347 switch (mdoc
->meta
.msec
) {
1353 return(mdoc_nwarn(mdoc
, mdoc
->last
, EWRONGMSEC
));
1368 return(check_sec(mdoc
, n
, SEC_SYNOPSIS
, SEC_CUSTOM
));