1 /* $Vendor-Id: mdoc_validate.c,v 1.54 2009/11/02 06:22:46 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.
17 #include <sys/types.h>
27 #include "libmandoc.h"
29 /* FIXME: .Bl -diag can't have non-text children in HEAD. */
30 /* TODO: ignoring Pp (it's superfluous in some invocations). */
32 #define PRE_ARGS struct mdoc *mdoc, const struct mdoc_node *n
33 #define POST_ARGS struct mdoc *mdoc
35 typedef int (*v_pre
)(PRE_ARGS
);
36 typedef int (*v_post
)(POST_ARGS
);
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);
59 static int warn_print(struct mdoc
*, int, int);
60 static int warn_count(struct mdoc
*, const char *,
61 int, const char *, int);
62 static int err_count(struct mdoc
*, const char *,
63 int, const char *, int);
66 extern size_t strlcat(char *, const char *, size_t);
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_le2(POST_ARGS
);
76 static int eerr_le1(POST_ARGS
);
77 static int ewarn_ge1(POST_ARGS
);
78 static int herr_eq0(POST_ARGS
);
79 static int herr_ge1(POST_ARGS
);
80 static int hwarn_eq1(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 pre_an(PRE_ARGS
);
98 static int pre_bd(PRE_ARGS
);
99 static int pre_bl(PRE_ARGS
);
100 static int pre_cd(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_er(PRE_ARGS
);
105 static int pre_ex(PRE_ARGS
);
106 static int pre_fd(PRE_ARGS
);
107 static int pre_it(PRE_ARGS
);
108 static int pre_lb(PRE_ARGS
);
109 static int pre_os(PRE_ARGS
);
110 static int pre_rv(PRE_ARGS
);
111 static int pre_sh(PRE_ARGS
);
112 static int pre_ss(PRE_ARGS
);
114 static v_post posts_an
[] = { post_an
, NULL
};
115 static v_post posts_at
[] = { post_at
, NULL
};
116 static v_post posts_bd
[] = { herr_eq0
, bwarn_ge1
, NULL
};
117 static v_post posts_bf
[] = { hwarn_le1
, post_bf
, NULL
};
118 static v_post posts_bl
[] = { bwarn_ge1
, post_bl
, NULL
};
119 static v_post posts_bool
[] = { eerr_eq1
, ebool
, NULL
};
120 static v_post posts_fo
[] = { hwarn_eq1
, bwarn_ge1
, NULL
};
121 static v_post posts_it
[] = { post_it
, NULL
};
122 static v_post posts_lb
[] = { eerr_eq1
, post_lb
, NULL
};
123 static v_post posts_nd
[] = { berr_ge1
, NULL
};
124 static v_post posts_nm
[] = { post_nm
, NULL
};
125 static v_post posts_notext
[] = { eerr_eq0
, NULL
};
126 static v_post posts_rs
[] = { berr_ge1
, herr_eq0
, post_rs
, NULL
};
127 static v_post posts_sh
[] = { herr_ge1
, bwarn_ge1
, post_sh
, NULL
};
128 static v_post posts_sp
[] = { eerr_le1
, NULL
};
129 static v_post posts_ss
[] = { herr_ge1
, NULL
};
130 static v_post posts_st
[] = { eerr_eq1
, post_st
, NULL
};
131 static v_post posts_text
[] = { eerr_ge1
, NULL
};
132 static v_post posts_text1
[] = { eerr_eq1
, NULL
};
133 static v_post posts_wline
[] = { bwarn_ge1
, herr_eq0
, NULL
};
134 static v_post posts_wtext
[] = { ewarn_ge1
, NULL
};
135 static v_post posts_xr
[] = { eerr_ge1
, eerr_le2
, NULL
};
136 static v_pre pres_an
[] = { pre_an
, NULL
};
137 static v_pre pres_bd
[] = { pre_display
, pre_bd
, NULL
};
138 static v_pre pres_bl
[] = { pre_bl
, NULL
};
139 static v_pre pres_cd
[] = { pre_cd
, NULL
};
140 static v_pre pres_d1
[] = { pre_display
, NULL
};
141 static v_pre pres_dd
[] = { pre_dd
, NULL
};
142 static v_pre pres_dt
[] = { pre_dt
, NULL
};
143 static v_pre pres_er
[] = { pre_er
, NULL
};
144 static v_pre pres_ex
[] = { pre_ex
, NULL
};
145 static v_pre pres_fd
[] = { pre_fd
, NULL
};
146 static v_pre pres_it
[] = { pre_it
, NULL
};
147 static v_pre pres_lb
[] = { pre_lb
, NULL
};
148 static v_pre pres_os
[] = { pre_os
, NULL
};
149 static v_pre pres_rv
[] = { pre_rv
, NULL
};
150 static v_pre pres_sh
[] = { pre_sh
, NULL
};
151 static v_pre pres_ss
[] = { pre_ss
, NULL
};
153 const struct valids mdoc_valids
[MDOC_MAX
] = {
154 { NULL
, NULL
}, /* Ap */
155 { pres_dd
, posts_text
}, /* Dd */
156 { pres_dt
, NULL
}, /* Dt */
157 { pres_os
, NULL
}, /* Os */
158 { pres_sh
, posts_sh
}, /* Sh */
159 { pres_ss
, posts_ss
}, /* Ss */
160 { NULL
, posts_notext
}, /* Pp */
161 { pres_d1
, posts_wline
}, /* D1 */
162 { pres_d1
, posts_wline
}, /* Dl */
163 { pres_bd
, posts_bd
}, /* Bd */
164 { NULL
, NULL
}, /* Ed */
165 { pres_bl
, posts_bl
}, /* Bl */
166 { NULL
, NULL
}, /* El */
167 { pres_it
, posts_it
}, /* It */
168 { NULL
, posts_text
}, /* Ad */
169 { pres_an
, posts_an
}, /* An */
170 { NULL
, NULL
}, /* Ar */
171 { pres_cd
, posts_text
}, /* Cd */
172 { NULL
, NULL
}, /* Cm */
173 { NULL
, NULL
}, /* Dv */
174 { pres_er
, posts_text
}, /* Er */
175 { NULL
, NULL
}, /* Ev */
176 { pres_ex
, NULL
}, /* Ex */
177 { NULL
, NULL
}, /* Fa */
178 { pres_fd
, posts_wtext
}, /* Fd */
179 { NULL
, NULL
}, /* Fl */
180 { NULL
, posts_text
}, /* Fn */
181 { NULL
, posts_wtext
}, /* Ft */
182 { NULL
, posts_text
}, /* Ic */
183 { NULL
, posts_text1
}, /* In */
184 { NULL
, NULL
}, /* Li */
185 { NULL
, posts_nd
}, /* Nd */
186 { NULL
, posts_nm
}, /* Nm */
187 { NULL
, posts_wline
}, /* Op */
188 { NULL
, NULL
}, /* Ot */
189 { NULL
, NULL
}, /* Pa */
190 { pres_rv
, NULL
}, /* Rv */
191 { NULL
, posts_st
}, /* St */
192 { NULL
, NULL
}, /* Va */
193 { NULL
, posts_text
}, /* Vt */
194 { NULL
, posts_xr
}, /* Xr */
195 { NULL
, posts_text
}, /* %A */
196 { NULL
, posts_text
}, /* %B */ /* FIXME: can be used outside Rs/Re. */
197 { NULL
, posts_text
}, /* %D */ /* FIXME: check date with mandoc_a2time(). */
198 { NULL
, posts_text
}, /* %I */
199 { NULL
, posts_text
}, /* %J */
200 { NULL
, posts_text
}, /* %N */
201 { NULL
, posts_text
}, /* %O */
202 { NULL
, posts_text
}, /* %P */
203 { NULL
, posts_text
}, /* %R */
204 { NULL
, posts_text
}, /* %T */ /* FIXME: can be used outside Rs/Re. */
205 { NULL
, posts_text
}, /* %V */
206 { NULL
, NULL
}, /* Ac */
207 { NULL
, NULL
}, /* Ao */
208 { NULL
, posts_wline
}, /* Aq */
209 { NULL
, posts_at
}, /* At */
210 { NULL
, NULL
}, /* Bc */
211 { NULL
, posts_bf
}, /* Bf */
212 { NULL
, NULL
}, /* Bo */
213 { NULL
, posts_wline
}, /* Bq */
214 { NULL
, NULL
}, /* Bsx */
215 { NULL
, NULL
}, /* Bx */
216 { NULL
, posts_bool
}, /* Db */
217 { NULL
, NULL
}, /* Dc */
218 { NULL
, NULL
}, /* Do */
219 { NULL
, posts_wline
}, /* Dq */
220 { NULL
, NULL
}, /* Ec */
221 { NULL
, NULL
}, /* Ef */
222 { NULL
, NULL
}, /* Em */
223 { NULL
, NULL
}, /* Eo */
224 { NULL
, NULL
}, /* Fx */
225 { NULL
, posts_text
}, /* Ms */
226 { NULL
, posts_notext
}, /* No */
227 { NULL
, posts_notext
}, /* Ns */
228 { NULL
, NULL
}, /* Nx */
229 { NULL
, NULL
}, /* Ox */
230 { NULL
, NULL
}, /* Pc */
231 { NULL
, posts_text1
}, /* Pf */
232 { NULL
, NULL
}, /* Po */
233 { NULL
, posts_wline
}, /* Pq */
234 { NULL
, NULL
}, /* Qc */
235 { NULL
, posts_wline
}, /* Ql */
236 { NULL
, NULL
}, /* Qo */
237 { NULL
, posts_wline
}, /* Qq */
238 { NULL
, NULL
}, /* Re */
239 { NULL
, posts_rs
}, /* Rs */
240 { NULL
, NULL
}, /* Sc */
241 { NULL
, NULL
}, /* So */
242 { NULL
, posts_wline
}, /* Sq */
243 { NULL
, posts_bool
}, /* Sm */
244 { NULL
, posts_text
}, /* Sx */
245 { NULL
, posts_text
}, /* Sy */
246 { NULL
, posts_text
}, /* Tn */
247 { NULL
, NULL
}, /* Ux */
248 { NULL
, NULL
}, /* Xc */
249 { NULL
, NULL
}, /* Xo */
250 { NULL
, posts_fo
}, /* Fo */
251 { NULL
, NULL
}, /* Fc */
252 { NULL
, NULL
}, /* Oo */
253 { NULL
, NULL
}, /* Oc */
254 { NULL
, posts_wline
}, /* Bk */
255 { NULL
, NULL
}, /* Ek */
256 { NULL
, posts_notext
}, /* Bt */
257 { NULL
, NULL
}, /* Hf */
258 { NULL
, NULL
}, /* Fr */
259 { NULL
, posts_notext
}, /* Ud */
260 { pres_lb
, posts_lb
}, /* Lb */
261 { NULL
, posts_notext
}, /* Lp */
262 { NULL
, posts_text
}, /* Lk */
263 { NULL
, posts_text
}, /* Mt */
264 { NULL
, posts_wline
}, /* Brq */
265 { NULL
, NULL
}, /* Bro */
266 { NULL
, NULL
}, /* Brc */
267 { NULL
, posts_text
}, /* %C */
268 { NULL
, NULL
}, /* Es */
269 { NULL
, NULL
}, /* En */
270 { NULL
, NULL
}, /* Dx */
271 { NULL
, posts_text
}, /* %Q */
272 { NULL
, posts_notext
}, /* br */
273 { NULL
, posts_sp
}, /* sp */
274 { NULL
, posts_text1
}, /* %U */
279 mdoc_valid_pre(struct mdoc
*mdoc
, const struct mdoc_node
*n
)
285 if (MDOC_TEXT
== n
->type
) {
289 return(check_text(mdoc
, line
, pos
, tp
));
292 if ( ! check_args(mdoc
, n
))
294 if (NULL
== mdoc_valids
[n
->tok
].pre
)
296 for (p
= mdoc_valids
[n
->tok
].pre
; *p
; p
++)
297 if ( ! (*p
)(mdoc
, n
))
304 mdoc_valid_post(struct mdoc
*mdoc
)
308 if (MDOC_VALID
& mdoc
->last
->flags
)
310 mdoc
->last
->flags
|= MDOC_VALID
;
312 if (MDOC_TEXT
== mdoc
->last
->type
)
314 if (MDOC_ROOT
== mdoc
->last
->type
)
315 return(post_root(mdoc
));
317 if (NULL
== mdoc_valids
[mdoc
->last
->tok
].post
)
319 for (p
= mdoc_valids
[mdoc
->last
->tok
].post
; *p
; p
++)
328 warn_print(struct mdoc
*m
, int ln
, int pos
)
331 if (MDOC_IGN_CHARS
& m
->pflags
)
332 return(mdoc_pwarn(m
, ln
, pos
, EPRINT
));
333 return(mdoc_perr(m
, ln
, pos
, EPRINT
));
338 warn_count(struct mdoc
*m
, const char *k
,
339 int want
, const char *v
, int has
)
342 return(mdoc_vwarn(m
, m
->last
->line
, m
->last
->pos
,
343 "suggests %s %s %d (has %d)", v
, k
, want
, has
));
348 err_count(struct mdoc
*m
, const char *k
,
349 int want
, const char *v
, int has
)
352 return(mdoc_verr(m
, m
->last
->line
, m
->last
->pos
,
353 "requires %s %s %d (has %d)", v
, k
, want
, has
));
358 * Build these up with macros because they're basically the same check
359 * for different inequalities. Yes, this could be done with functions,
360 * but this is reasonable for now.
363 #define CHECK_CHILD_DEFN(lvl, name, ineq) \
365 lvl##_child_##name(struct mdoc *mdoc, const char *p, int sz) \
367 if (mdoc->last->nchild ineq sz) \
369 return(lvl##_count(mdoc, #ineq, sz, p, mdoc->last->nchild)); \
372 #define CHECK_BODY_DEFN(name, lvl, func, num) \
374 b##lvl##_##name(POST_ARGS) \
376 if (MDOC_BODY != mdoc->last->type) \
378 return(func(mdoc, "multi-line arguments", (num))); \
381 #define CHECK_ELEM_DEFN(name, lvl, func, num) \
383 e##lvl##_##name(POST_ARGS) \
385 assert(MDOC_ELEM == mdoc->last->type); \
386 return(func(mdoc, "line arguments", (num))); \
389 #define CHECK_HEAD_DEFN(name, lvl, func, num) \
391 h##lvl##_##name(POST_ARGS) \
393 if (MDOC_HEAD != mdoc->last->type) \
395 return(func(mdoc, "line arguments", (num))); \
399 CHECK_CHILD_DEFN(warn
, gt
, >) /* warn_child_gt() */
400 CHECK_CHILD_DEFN(err
, gt
, >) /* err_child_gt() */
401 CHECK_CHILD_DEFN(warn
, eq
, ==) /* warn_child_eq() */
402 CHECK_CHILD_DEFN(err
, eq
, ==) /* err_child_eq() */
403 CHECK_CHILD_DEFN(err
, lt
, <) /* err_child_lt() */
404 CHECK_CHILD_DEFN(warn
, lt
, <) /* warn_child_lt() */
405 CHECK_BODY_DEFN(ge1
, warn
, warn_child_gt
, 0) /* bwarn_ge1() */
406 CHECK_BODY_DEFN(ge1
, err
, err_child_gt
, 0) /* berr_ge1() */
407 CHECK_ELEM_DEFN(ge1
, warn
, warn_child_gt
, 0) /* ewarn_gt1() */
408 CHECK_ELEM_DEFN(eq1
, err
, err_child_eq
, 1) /* eerr_eq1() */
409 CHECK_ELEM_DEFN(le2
, err
, err_child_lt
, 3) /* eerr_le2() */
410 CHECK_ELEM_DEFN(le1
, err
, err_child_lt
, 2) /* eerr_le1() */
411 CHECK_ELEM_DEFN(eq0
, err
, err_child_eq
, 0) /* eerr_eq0() */
412 CHECK_ELEM_DEFN(ge1
, err
, err_child_gt
, 0) /* eerr_ge1() */
413 CHECK_HEAD_DEFN(eq0
, err
, err_child_eq
, 0) /* herr_eq0() */
414 CHECK_HEAD_DEFN(le1
, warn
, warn_child_lt
, 2) /* hwarn_le1() */
415 CHECK_HEAD_DEFN(ge1
, err
, err_child_gt
, 0) /* herr_ge1() */
416 CHECK_HEAD_DEFN(eq1
, warn
, warn_child_eq
, 1) /* hwarn_eq1() */
420 check_stdarg(PRE_ARGS
)
423 if (n
->args
&& 1 == n
->args
->argc
)
424 if (MDOC_Std
== n
->args
->argv
[0].arg
)
426 return(mdoc_nwarn(mdoc
, n
, EARGVAL
));
431 check_sec(PRE_ARGS
, ...)
440 sec
= (enum mdoc_sec
)va_arg(ap
, int);
441 if (SEC_CUSTOM
== sec
)
443 if (sec
!= mdoc
->lastsec
)
450 return(mdoc_nwarn(mdoc
, n
, EBADSEC
));
455 check_msec(PRE_ARGS
, ...)
463 if (0 == (msec
= va_arg(ap
, int)))
465 if (msec
!= mdoc
->meta
.msec
)
472 return(mdoc_nwarn(mdoc
, n
, EBADMSEC
));
477 check_args(struct mdoc
*m
, const struct mdoc_node
*n
)
484 assert(n
->args
->argc
);
485 for (i
= 0; i
< (int)n
->args
->argc
; i
++)
486 if ( ! check_argv(m
, n
, &n
->args
->argv
[i
]))
494 check_argv(struct mdoc
*m
, const struct mdoc_node
*n
,
495 const struct mdoc_argv
*v
)
499 for (i
= 0; i
< (int)v
->sz
; i
++)
500 if ( ! check_text(m
, v
->line
, v
->pos
, v
->value
[i
]))
503 if (MDOC_Std
== v
->arg
) {
504 /* `Nm' name must be set. */
505 if (v
->sz
|| m
->meta
.name
)
507 return(mdoc_nerr(m
, n
, ENAME
));
515 check_text(struct mdoc
*mdoc
, int line
, int pos
, const char *p
)
519 for ( ; *p
; p
++, pos
++) {
521 if ( ! (MDOC_LITERAL
& mdoc
->flags
))
522 if ( ! warn_print(mdoc
, line
, pos
))
524 } else if ( ! isprint((u_char
)*p
))
525 if ( ! warn_print(mdoc
, line
, pos
))
531 c
= mandoc_special(p
);
537 if ( ! (MDOC_IGN_ESCAPE
& mdoc
->pflags
))
538 return(mdoc_perr(mdoc
, line
, pos
, EESCAPE
));
539 if ( ! mdoc_pwarn(mdoc
, line
, pos
, EESCAPE
))
550 check_parent(PRE_ARGS
, int tok
, enum mdoc_type t
)
554 if ((MDOC_ROOT
== t
|| tok
== n
->parent
->tok
) &&
555 (t
== n
->parent
->type
))
558 return(mdoc_verr(mdoc
, n
->line
, n
->pos
, "require parent %s",
559 MDOC_ROOT
== t
? "<root>" : mdoc_macronames
[tok
]));
565 pre_display(PRE_ARGS
)
567 struct mdoc_node
*node
;
569 /* Display elements (`Bd', `D1'...) cannot be nested. */
571 if (MDOC_BLOCK
!= n
->type
)
575 for (node
= mdoc
->last
->parent
; node
; node
= node
->parent
)
576 if (MDOC_BLOCK
== node
->type
)
577 if (MDOC_Bd
== node
->tok
)
582 return(mdoc_nerr(mdoc
, n
, ENESTDISP
));
589 int pos
, type
, width
, offset
;
591 if (MDOC_BLOCK
!= n
->type
)
594 return(mdoc_nerr(mdoc
, n
, ELISTTYPE
));
596 /* Make sure that only one type of list is specified. */
598 type
= offset
= width
= -1;
601 for (pos
= 0; pos
< (int)n
->args
->argc
; pos
++)
602 switch (n
->args
->argv
[pos
].arg
) {
625 return(mdoc_nerr(mdoc
, n
, EMULTILIST
));
626 type
= n
->args
->argv
[pos
].arg
;
629 if (type
< 0 && ! mdoc_nwarn(mdoc
, n
, ENOTYPE
))
634 return(mdoc_nerr(mdoc
, n
, EARGREP
));
635 if (type
< 0 && ! mdoc_nwarn(mdoc
, n
, ENOTYPE
))
637 width
= n
->args
->argv
[pos
].arg
;
641 return(mdoc_nerr(mdoc
, n
, EARGREP
));
642 if (type
< 0 && ! mdoc_nwarn(mdoc
, n
, ENOTYPE
))
644 offset
= n
->args
->argv
[pos
].arg
;
651 return(mdoc_nerr(mdoc
, n
, ELISTTYPE
));
654 * Validate the width field. Some list types don't need width
655 * types and should be warned about them. Others should have it
656 * and must also be warned.
661 if (width
< 0 && ! mdoc_nwarn(mdoc
, n
, EMISSWIDTH
))
673 if (width
>= 0 && ! mdoc_nwarn(mdoc
, n
, ENOWIDTH
))
689 if (MDOC_BLOCK
!= n
->type
)
692 return(mdoc_nerr(mdoc
, n
, EDISPTYPE
));
694 /* Make sure that only one type of display is specified. */
697 for (i
= 0, err
= type
= 0; ! err
&&
698 i
< (int)n
->args
->argc
; i
++)
699 switch (n
->args
->argv
[i
].arg
) {
704 case (MDOC_Unfilled
):
711 return(mdoc_nerr(mdoc
, n
, EMULTIDISP
));
718 return(mdoc_nerr(mdoc
, n
, EDISPTYPE
));
726 if (MDOC_BLOCK
!= n
->type
)
728 return(check_parent(mdoc
, n
, MDOC_Sh
, MDOC_BODY
));
736 if (MDOC_BLOCK
!= n
->type
)
738 return(check_parent(mdoc
, n
, -1, MDOC_ROOT
));
746 if (MDOC_BLOCK
!= n
->type
)
748 return(check_parent(mdoc
, n
, MDOC_Bl
, MDOC_BODY
));
756 if (NULL
== n
->args
|| 1 == n
->args
->argc
)
758 return(mdoc_verr(mdoc
, n
->line
, n
->pos
,
759 "only one argument allowed"));
767 return(check_sec(mdoc
, n
, SEC_LIBRARY
, SEC_CUSTOM
));
775 if ( ! check_msec(mdoc
, n
, 2, 3, 0))
777 return(check_stdarg(mdoc
, n
));
785 if ( ! check_msec(mdoc
, n
, 1, 6, 8, 0))
787 return(check_stdarg(mdoc
, n
));
795 return(check_msec(mdoc
, n
, 2, 3, 9, 0));
803 return(check_msec(mdoc
, n
, 4, 0));
811 /* FIXME: make sure is capitalised. */
813 if (0 == mdoc
->meta
.date
|| mdoc
->meta
.os
)
814 if ( ! mdoc_nwarn(mdoc
, n
, EPROLOOO
))
816 if (mdoc
->meta
.title
)
817 if ( ! mdoc_nwarn(mdoc
, n
, EPROLREP
))
827 if (NULL
== mdoc
->meta
.title
|| 0 == mdoc
->meta
.date
)
828 if ( ! mdoc_nwarn(mdoc
, n
, EPROLOOO
))
831 if ( ! mdoc_nwarn(mdoc
, n
, EPROLREP
))
841 if (mdoc
->meta
.title
|| mdoc
->meta
.os
)
842 if ( ! mdoc_nwarn(mdoc
, n
, EPROLOOO
))
845 if ( ! mdoc_nwarn(mdoc
, n
, EPROLREP
))
855 struct mdoc_node
*head
;
857 if (MDOC_BLOCK
!= mdoc
->last
->type
)
860 head
= mdoc
->last
->head
;
862 if (mdoc
->last
->args
&& head
->child
)
863 return(mdoc_nerr(mdoc
, mdoc
->last
, ELINE
));
864 else if (mdoc
->last
->args
)
867 if (NULL
== head
->child
|| MDOC_TEXT
!= head
->child
->type
)
868 return(mdoc_nerr(mdoc
, mdoc
->last
, ELINE
));
870 p
= head
->child
->string
;
872 if (0 == strcmp(p
, "Em"))
874 else if (0 == strcmp(p
, "Li"))
876 else if (0 == strcmp(p
, "Sy"))
879 return(mdoc_nerr(mdoc
, head
, EFONT
));
887 if (mdoc_a2lib(mdoc
->last
->child
->string
))
889 return(mdoc_nwarn(mdoc
, mdoc
->last
, ELIB
));
897 if (mdoc
->last
->child
)
901 return(mdoc_nerr(mdoc
, mdoc
->last
, ENAME
));
909 if (NULL
== mdoc
->last
->child
)
911 if (MDOC_TEXT
!= mdoc
->last
->child
->type
)
912 return(mdoc_nerr(mdoc
, mdoc
->last
, EATT
));
913 if (mdoc_a2att(mdoc
->last
->child
->string
))
915 return(mdoc_nerr(mdoc
, mdoc
->last
, EATT
));
923 if (mdoc
->last
->args
) {
924 if (NULL
== mdoc
->last
->child
)
926 return(mdoc_nerr(mdoc
, mdoc
->last
, ENOLINE
));
929 if (mdoc
->last
->child
)
931 return(mdoc_nerr(mdoc
, mdoc
->last
, ELINE
));
939 struct mdoc_node
*n
, *c
;
941 if (MDOC_BLOCK
!= mdoc
->last
->type
)
944 n
= mdoc
->last
->parent
->parent
;
946 return(mdoc_nerr(mdoc
, mdoc
->last
, ELISTTYPE
));
948 /* Some types require block-head, some not. */
951 for (cols
= type
= -1, i
= 0; -1 == type
&&
952 i
< (int)n
->args
->argc
; i
++)
953 switch (n
->args
->argv
[i
].arg
) {
973 type
= n
->args
->argv
[i
].arg
;
976 type
= n
->args
->argv
[i
].arg
;
977 cols
= (int)n
->args
->argv
[i
].sz
;
984 return(mdoc_nerr(mdoc
, mdoc
->last
, ELISTTYPE
));
988 if (NULL
== mdoc
->last
->head
->child
)
989 if ( ! mdoc_nwarn(mdoc
, mdoc
->last
, ELINE
))
999 if (NULL
== mdoc
->last
->head
->child
)
1000 if ( ! mdoc_nwarn(mdoc
, mdoc
->last
, ELINE
))
1002 if (NULL
== mdoc
->last
->body
->child
)
1003 if ( ! mdoc_nwarn(mdoc
, mdoc
->last
, EMULTILINE
))
1015 if (mdoc
->last
->head
->child
)
1016 if ( ! mdoc_nwarn(mdoc
, mdoc
->last
, ENOLINE
))
1018 if (NULL
== mdoc
->last
->body
->child
)
1019 if ( ! mdoc_nwarn(mdoc
, mdoc
->last
, EMULTILINE
))
1023 if (NULL
== mdoc
->last
->head
->child
)
1024 if ( ! mdoc_nwarn(mdoc
, mdoc
->last
, ELINE
))
1026 if (mdoc
->last
->body
->child
)
1027 if ( ! mdoc_nwarn(mdoc
, mdoc
->last
, ENOMULTILINE
))
1029 c
= mdoc
->last
->child
;
1030 for (i
= 0; c
&& MDOC_HEAD
== c
->type
; c
= c
->next
)
1033 if (i
< cols
|| i
== (cols
+ 1)) {
1034 if ( ! mdoc_vwarn(mdoc
, mdoc
->last
->line
,
1035 mdoc
->last
->pos
, "column "
1036 "mismatch: have %d, want %d",
1040 } else if (i
== cols
)
1043 return(mdoc_verr(mdoc
, mdoc
->last
->line
,
1044 mdoc
->last
->pos
, "column mismatch: "
1045 "have %d, want %d", i
, cols
));
1055 post_bl_head(POST_ARGS
)
1058 const struct mdoc_node
*n
;
1060 n
= mdoc
->last
->parent
;
1063 for (i
= 0; i
< (int)n
->args
->argc
; i
++)
1064 if (n
->args
->argv
[i
].arg
== MDOC_Column
)
1067 if (i
== (int)n
->args
->argc
)
1070 if (n
->args
->argv
[i
].sz
&& mdoc
->last
->child
)
1071 return(mdoc_nerr(mdoc
, n
, ECOLMIS
));
1080 struct mdoc_node
*n
;
1082 if (MDOC_HEAD
== mdoc
->last
->type
)
1083 return(post_bl_head(mdoc
));
1084 if (MDOC_BODY
!= mdoc
->last
->type
)
1086 if (NULL
== mdoc
->last
->child
)
1090 for (n
= mdoc
->last
->child
; n
; n
= n
->next
) {
1091 if (MDOC_BLOCK
== n
->type
)
1092 if (MDOC_It
== n
->tok
)
1094 return(mdoc_nerr(mdoc
, n
, EBADCHILD
));
1102 ebool(struct mdoc
*mdoc
)
1104 struct mdoc_node
*n
;
1107 for (n
= mdoc
->last
->child
; n
; n
= n
->next
) {
1108 if (MDOC_TEXT
!= n
->type
)
1110 if (0 == strcmp(n
->string
, "on"))
1112 if (0 == strcmp(n
->string
, "off"))
1119 return(mdoc_nerr(mdoc
, n
, EBOOL
));
1124 post_root(POST_ARGS
)
1127 if (NULL
== mdoc
->first
->child
)
1128 return(mdoc_nerr(mdoc
, mdoc
->first
, ENODAT
));
1129 if ( ! (MDOC_PBODY
& mdoc
->flags
))
1130 return(mdoc_nerr(mdoc
, mdoc
->first
, ENOPROLOGUE
));
1132 if (MDOC_BLOCK
!= mdoc
->first
->child
->type
)
1133 return(mdoc_nerr(mdoc
, mdoc
->first
, ENODAT
));
1134 if (MDOC_Sh
!= mdoc
->first
->child
->tok
)
1135 return(mdoc_nerr(mdoc
, mdoc
->first
, ENODAT
));
1145 if (mdoc_a2st(mdoc
->last
->child
->string
))
1147 return(mdoc_nerr(mdoc
, mdoc
->last
, EBADSTAND
));
1154 struct mdoc_node
*nn
;
1156 if (MDOC_BODY
!= mdoc
->last
->type
)
1159 for (nn
= mdoc
->last
->child
; nn
; nn
= nn
->next
)
1190 return(mdoc_nerr(mdoc
, nn
, EBADCHILD
));
1201 if (MDOC_HEAD
== mdoc
->last
->type
)
1202 return(post_sh_head(mdoc
));
1203 if (MDOC_BODY
== mdoc
->last
->type
)
1204 return(post_sh_body(mdoc
));
1211 post_sh_body(POST_ARGS
)
1213 struct mdoc_node
*n
;
1215 if (SEC_NAME
!= mdoc
->lastsec
)
1219 * Warn if the NAME section doesn't contain the `Nm' and `Nd'
1220 * macros (can have multiple `Nm' and one `Nd'). Note that the
1221 * children of the BODY declaration can also be "text".
1224 if (NULL
== (n
= mdoc
->last
->child
))
1225 return(mdoc_nwarn(mdoc
, mdoc
->last
, ENAMESECINC
));
1227 for ( ; n
&& n
->next
; n
= n
->next
) {
1228 if (MDOC_ELEM
== n
->type
&& MDOC_Nm
== n
->tok
)
1230 if (MDOC_TEXT
== n
->type
)
1232 if ( ! mdoc_nwarn(mdoc
, mdoc
->last
, ENAMESECINC
))
1237 if (MDOC_BLOCK
== n
->type
&& MDOC_Nd
== n
->tok
)
1239 return(mdoc_nwarn(mdoc
, mdoc
->last
, ENAMESECINC
));
1244 post_sh_head(POST_ARGS
)
1248 const struct mdoc_node
*n
;
1251 * Process a new section. Sections are either "named" or
1252 * "custom"; custom sections are user-defined, while named ones
1253 * usually follow a conventional order and may only appear in
1254 * certain manual sections.
1259 for (n
= mdoc
->last
->child
; n
; n
= n
->next
) {
1260 /* XXX - copied from compact(). */
1261 assert(MDOC_TEXT
== n
->type
);
1263 if (strlcat(buf
, n
->string
, 64) >= 64)
1264 return(mdoc_nerr(mdoc
, n
, ETOOLONG
));
1265 if (NULL
== n
->next
)
1267 if (strlcat(buf
, " ", 64) >= 64)
1268 return(mdoc_nerr(mdoc
, n
, ETOOLONG
));
1271 sec
= mdoc_atosec(buf
);
1274 * Check: NAME should always be first, CUSTOM has no roles,
1275 * non-CUSTOM has a conventional order to be followed.
1278 if (SEC_NAME
!= sec
&& SEC_NONE
== mdoc
->lastnamed
)
1279 return(mdoc_nerr(mdoc
, mdoc
->last
, ESECNAME
));
1280 if (SEC_CUSTOM
== sec
)
1282 if (sec
== mdoc
->lastnamed
)
1283 if ( ! mdoc_nwarn(mdoc
, mdoc
->last
, ESECREP
))
1285 if (sec
< mdoc
->lastnamed
)
1286 if ( ! mdoc_nwarn(mdoc
, mdoc
->last
, ESECOOO
))
1290 * Check particular section/manual conventions. LIBRARY can
1291 * only occur in msec 2, 3 (TODO: are there more of these?).
1296 switch (mdoc
->meta
.msec
) {
1302 return(mdoc_nwarn(mdoc
, mdoc
->last
, EWRONGMSEC
));
1317 return(check_sec(mdoc
, n
, SEC_SYNOPSIS
, SEC_CUSTOM
));