]>
git.cameronkatri.com Git - mandoc.git/blob - mdoc_validate.c
1 /* $Id: mdoc_validate.c,v 1.10 2009/06/16 19:45:51 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 /* FIXME: .Bl -diag can't have non-text children in HEAD. */
28 /* TODO: ignoring Pp (it's superfluous in some invocations). */
30 #define PRE_ARGS struct mdoc *mdoc, const struct mdoc_node *n
31 #define POST_ARGS struct mdoc *mdoc
73 typedef int (*v_pre
)(PRE_ARGS
);
74 typedef int (*v_post
)(POST_ARGS
);
81 static int pwarn(struct mdoc
*, int, int, enum mwarn
);
82 static int perr(struct mdoc
*, int, int, enum merr
);
83 static int check_parent(PRE_ARGS
, int, enum mdoc_type
);
84 static int check_msec(PRE_ARGS
, ...);
85 static int check_sec(PRE_ARGS
, ...);
86 static int check_stdarg(PRE_ARGS
);
87 static int check_text(struct mdoc
*, int, int, const char *);
88 static int check_argv(struct mdoc
*,
89 const struct mdoc_node
*,
90 const struct mdoc_argv
*);
91 static int check_args(struct mdoc
*,
92 const struct mdoc_node
*);
93 static int err_child_lt(struct mdoc
*, const char *, int);
94 static int warn_child_lt(struct mdoc
*, const char *, int);
95 static int err_child_gt(struct mdoc
*, const char *, int);
96 static int warn_child_gt(struct mdoc
*, const char *, int);
97 static int err_child_eq(struct mdoc
*, const char *, int);
98 static int warn_child_eq(struct mdoc
*, const char *, int);
99 static int count_child(struct mdoc
*);
100 static int warn_print(struct mdoc
*, int, int);
101 static int warn_count(struct mdoc
*, const char *,
102 int, const char *, int);
103 static int err_count(struct mdoc
*, const char *,
104 int, const char *, int);
105 static int pre_an(PRE_ARGS
);
106 static int pre_bd(PRE_ARGS
);
107 static int pre_bl(PRE_ARGS
);
108 static int pre_cd(PRE_ARGS
);
109 static int pre_dd(PRE_ARGS
);
110 static int pre_display(PRE_ARGS
);
111 static int pre_dt(PRE_ARGS
);
112 static int pre_er(PRE_ARGS
);
113 static int pre_ex(PRE_ARGS
);
114 static int pre_fd(PRE_ARGS
);
115 static int pre_it(PRE_ARGS
);
116 static int pre_lb(PRE_ARGS
);
117 static int pre_os(PRE_ARGS
);
118 static int pre_prologue(PRE_ARGS
);
119 static int pre_rv(PRE_ARGS
);
120 static int pre_sh(PRE_ARGS
);
121 static int pre_ss(PRE_ARGS
);
122 static int herr_ge1(POST_ARGS
);
123 static int hwarn_le1(POST_ARGS
);
124 static int herr_eq0(POST_ARGS
);
125 static int eerr_eq0(POST_ARGS
);
126 static int eerr_le2(POST_ARGS
);
127 static int eerr_eq1(POST_ARGS
);
128 static int eerr_ge1(POST_ARGS
);
129 static int ewarn_eq0(POST_ARGS
);
130 static int ewarn_eq1(POST_ARGS
);
131 static int bwarn_ge1(POST_ARGS
);
132 static int hwarn_eq1(POST_ARGS
);
133 static int ewarn_ge1(POST_ARGS
);
134 static int ebool(POST_ARGS
);
135 static int post_an(POST_ARGS
);
136 static int post_args(POST_ARGS
);
137 static int post_at(POST_ARGS
);
138 static int post_bf(POST_ARGS
);
139 static int post_bl(POST_ARGS
);
140 static int post_it(POST_ARGS
);
141 static int post_nm(POST_ARGS
);
142 static int post_root(POST_ARGS
);
143 static int post_sh(POST_ARGS
);
144 static int post_sh_body(POST_ARGS
);
145 static int post_sh_head(POST_ARGS
);
146 static int post_st(POST_ARGS
);
148 #define vwarn(m, t) nwarn((m), (m)->last, (t))
149 #define verr(m, t) nerr((m), (m)->last, (t))
150 #define nwarn(m, n, t) pwarn((m), (n)->line, (n)->pos, (t))
151 #define nerr(m, n, t) perr((m), (n)->line, (n)->pos, (t))
153 static v_pre pres_an
[] = { pre_an
, NULL
};
154 static v_pre pres_bd
[] = { pre_display
, pre_bd
, NULL
};
155 static v_pre pres_bl
[] = { pre_bl
, NULL
};
156 static v_pre pres_cd
[] = { pre_cd
, NULL
};
157 static v_pre pres_dd
[] = { pre_prologue
, pre_dd
, NULL
};
158 static v_pre pres_d1
[] = { pre_display
, NULL
};
159 static v_pre pres_dt
[] = { pre_prologue
, pre_dt
, NULL
};
160 static v_pre pres_er
[] = { pre_er
, NULL
};
161 static v_pre pres_ex
[] = { pre_ex
, NULL
};
162 static v_pre pres_fd
[] = { pre_fd
, NULL
};
163 static v_pre pres_it
[] = { pre_it
, NULL
};
164 static v_pre pres_lb
[] = { pre_lb
, NULL
};
165 static v_pre pres_os
[] = { pre_prologue
, pre_os
, NULL
};
166 static v_pre pres_rv
[] = { pre_rv
, NULL
};
167 static v_pre pres_sh
[] = { pre_sh
, NULL
};
168 static v_pre pres_ss
[] = { pre_ss
, NULL
};
169 static v_post posts_bool
[] = { eerr_eq1
, ebool
, NULL
};
170 static v_post posts_bd
[] = { herr_eq0
, bwarn_ge1
, NULL
};
171 static v_post posts_text
[] = { eerr_ge1
, NULL
};
172 static v_post posts_wtext
[] = { ewarn_ge1
, NULL
};
173 static v_post posts_notext
[] = { eerr_eq0
, NULL
};
174 static v_post posts_wline
[] = { bwarn_ge1
, herr_eq0
, NULL
};
175 static v_post posts_sh
[] = { herr_ge1
, bwarn_ge1
, post_sh
, NULL
};
176 static v_post posts_bl
[] = { herr_eq0
, bwarn_ge1
, post_bl
, NULL
};
177 static v_post posts_it
[] = { post_it
, NULL
};
178 static v_post posts_in
[] = { ewarn_eq1
, NULL
};
179 static v_post posts_ss
[] = { herr_ge1
, NULL
};
180 static v_post posts_pf
[] = { eerr_eq1
, NULL
};
181 static v_post posts_lb
[] = { eerr_eq1
, NULL
};
182 static v_post posts_st
[] = { eerr_eq1
, post_st
, NULL
};
183 static v_post posts_pp
[] = { ewarn_eq0
, NULL
};
184 static v_post posts_ex
[] = { eerr_eq0
, post_args
, NULL
};
185 static v_post posts_rv
[] = { eerr_eq0
, post_args
, NULL
};
186 static v_post posts_an
[] = { post_an
, NULL
};
187 static v_post posts_at
[] = { post_at
, NULL
};
188 static v_post posts_xr
[] = { eerr_ge1
, eerr_le2
, NULL
};
189 static v_post posts_nm
[] = { post_nm
, NULL
};
190 static v_post posts_bf
[] = { hwarn_le1
, post_bf
, NULL
};
191 static v_post posts_fo
[] = { hwarn_eq1
, bwarn_ge1
, NULL
};
193 const struct valids mdoc_valids
[MDOC_MAX
] = {
194 { NULL
, NULL
}, /* Ap */
195 { pres_dd
, posts_text
}, /* Dd */
196 { pres_dt
, NULL
}, /* Dt */
197 { pres_os
, NULL
}, /* Os */
198 { pres_sh
, posts_sh
}, /* Sh */
199 { pres_ss
, posts_ss
}, /* Ss */
200 { NULL
, posts_pp
}, /* Pp */
201 { pres_d1
, posts_wline
}, /* D1 */
202 { pres_d1
, posts_wline
}, /* Dl */
203 { pres_bd
, posts_bd
}, /* Bd */
204 { NULL
, NULL
}, /* Ed */
205 { pres_bl
, posts_bl
}, /* Bl */
206 { NULL
, NULL
}, /* El */
207 { pres_it
, posts_it
}, /* It */
208 { NULL
, posts_text
}, /* Ad */
209 { pres_an
, posts_an
}, /* An */
210 { NULL
, NULL
}, /* Ar */
211 { pres_cd
, posts_text
}, /* Cd */
212 { NULL
, NULL
}, /* Cm */
213 { NULL
, NULL
}, /* Dv */
214 { pres_er
, posts_text
}, /* Er */
215 { NULL
, NULL
}, /* Ev */
216 { pres_ex
, posts_ex
}, /* Ex */
217 { NULL
, NULL
}, /* Fa */
218 { pres_fd
, posts_wtext
}, /* Fd */
219 { NULL
, NULL
}, /* Fl */
220 { NULL
, posts_text
}, /* Fn */
221 { NULL
, posts_wtext
}, /* Ft */
222 { NULL
, posts_text
}, /* Ic */
223 { NULL
, posts_in
}, /* In */
224 { NULL
, NULL
}, /* Li */
225 { NULL
, posts_wtext
}, /* Nd */
226 { NULL
, posts_nm
}, /* Nm */
227 { NULL
, posts_wline
}, /* Op */
228 { NULL
, NULL
}, /* Ot */
229 { NULL
, NULL
}, /* Pa */
230 { pres_rv
, posts_rv
}, /* Rv */
231 { NULL
, posts_st
}, /* St */
232 { NULL
, NULL
}, /* Va */
233 { NULL
, posts_text
}, /* Vt */
234 { NULL
, posts_xr
}, /* Xr */
235 { NULL
, posts_text
}, /* %A */
236 { NULL
, posts_text
}, /* %B */
237 { NULL
, posts_text
}, /* %D */
238 { NULL
, posts_text
}, /* %I */
239 { NULL
, posts_text
}, /* %J */
240 { NULL
, posts_text
}, /* %N */
241 { NULL
, posts_text
}, /* %O */
242 { NULL
, posts_text
}, /* %P */
243 { NULL
, posts_text
}, /* %R */
244 { NULL
, posts_text
}, /* %T */
245 { NULL
, posts_text
}, /* %V */
246 { NULL
, NULL
}, /* Ac */
247 { NULL
, NULL
}, /* Ao */
248 { NULL
, posts_wline
}, /* Aq */
249 { NULL
, posts_at
}, /* At */
250 { NULL
, NULL
}, /* Bc */
251 { NULL
, posts_bf
}, /* Bf */
252 { NULL
, NULL
}, /* Bo */
253 { NULL
, posts_wline
}, /* Bq */
254 { NULL
, NULL
}, /* Bsx */
255 { NULL
, NULL
}, /* Bx */
256 { NULL
, posts_bool
}, /* Db */
257 { NULL
, NULL
}, /* Dc */
258 { NULL
, NULL
}, /* Do */
259 { NULL
, posts_wline
}, /* Dq */
260 { NULL
, NULL
}, /* Ec */
261 { NULL
, NULL
}, /* Ef */
262 { NULL
, NULL
}, /* Em */
263 { NULL
, NULL
}, /* Eo */
264 { NULL
, NULL
}, /* Fx */
265 { NULL
, posts_text
}, /* Ms */
266 { NULL
, posts_notext
}, /* No */
267 { NULL
, posts_notext
}, /* Ns */
268 { NULL
, NULL
}, /* Nx */
269 { NULL
, NULL
}, /* Ox */
270 { NULL
, NULL
}, /* Pc */
271 { NULL
, posts_pf
}, /* Pf */
272 { NULL
, NULL
}, /* Po */
273 { NULL
, posts_wline
}, /* Pq */
274 { NULL
, NULL
}, /* Qc */
275 { NULL
, posts_wline
}, /* Ql */
276 { NULL
, NULL
}, /* Qo */
277 { NULL
, posts_wline
}, /* Qq */
278 { NULL
, NULL
}, /* Re */
279 { NULL
, posts_wline
}, /* Rs */
280 { NULL
, NULL
}, /* Sc */
281 { NULL
, NULL
}, /* So */
282 { NULL
, posts_wline
}, /* Sq */
283 { NULL
, posts_bool
}, /* Sm */
284 { NULL
, posts_text
}, /* Sx */
285 { NULL
, posts_text
}, /* Sy */
286 { NULL
, posts_text
}, /* Tn */
287 { NULL
, NULL
}, /* Ux */
288 { NULL
, NULL
}, /* Xc */
289 { NULL
, NULL
}, /* Xo */
290 { NULL
, posts_fo
}, /* Fo */
291 { NULL
, NULL
}, /* Fc */
292 { NULL
, NULL
}, /* Oo */
293 { NULL
, NULL
}, /* Oc */
294 { NULL
, posts_wline
}, /* Bk */
295 { NULL
, NULL
}, /* Ek */
296 { NULL
, posts_notext
}, /* Bt */
297 { NULL
, NULL
}, /* Hf */
298 { NULL
, NULL
}, /* Fr */
299 { NULL
, posts_notext
}, /* Ud */
300 { pres_lb
, posts_lb
}, /* Lb */
301 { NULL
, posts_pp
}, /* Lp */
302 { NULL
, NULL
}, /* Lk */
303 { NULL
, posts_text
}, /* Mt */
304 { NULL
, posts_wline
}, /* Brq */
305 { NULL
, NULL
}, /* Bro */
306 { NULL
, NULL
}, /* Brc */
307 { NULL
, posts_text
}, /* %C */
308 { NULL
, NULL
}, /* Es */
309 { NULL
, NULL
}, /* En */
310 { NULL
, NULL
}, /* Dx */
311 { NULL
, posts_text
}, /* %Q */
316 extern size_t strlcat(char *, const char *, size_t);
321 mdoc_valid_pre(struct mdoc
*mdoc
,
322 const struct mdoc_node
*n
)
328 if (MDOC_TEXT
== n
->type
) {
332 return(check_text(mdoc
, line
, pos
, tp
));
335 if ( ! check_args(mdoc
, n
))
337 if (NULL
== mdoc_valids
[n
->tok
].pre
)
339 for (p
= mdoc_valids
[n
->tok
].pre
; *p
; p
++)
340 if ( ! (*p
)(mdoc
, n
))
347 mdoc_valid_post(struct mdoc
*mdoc
)
352 * This check occurs after the macro's children have been filled
353 * in: postfix validation. Since this happens when we're
354 * rewinding the scope tree, it's possible to have multiple
355 * invocations (as by design, for now), we set bit MDOC_VALID to
356 * indicate that we've validated.
359 if (MDOC_VALID
& mdoc
->last
->flags
)
361 mdoc
->last
->flags
|= MDOC_VALID
;
363 if (MDOC_TEXT
== mdoc
->last
->type
)
365 if (MDOC_ROOT
== mdoc
->last
->type
)
366 return(post_root(mdoc
));
368 if (NULL
== mdoc_valids
[mdoc
->last
->tok
].post
)
370 for (p
= mdoc_valids
[mdoc
->last
->tok
].post
; *p
; p
++)
379 perr(struct mdoc
*m
, int line
, int pos
, enum merr type
)
386 p
= "text argument too long";
389 p
= "invalid escape sequence";
392 p
= "invalid character";
395 p
= "displays may not be nested";
398 p
= "expected boolean value";
401 p
= "argument repeated";
404 p
= "multiple display types specified";
407 p
= "multiple list types specified";
410 p
= "missing list type";
413 p
= "missing display type";
416 p
= "expected line arguments";
419 p
= "document has no prologue";
422 p
= "document has no data";
425 p
= "expected valid AT&T symbol";
428 p
= "default name not yet set";
432 return(mdoc_perr(m
, line
, pos
, p
));
437 pwarn(struct mdoc
*m
, int line
, int pos
, enum mwarn type
)
446 p
= "inappropriate manual section";
450 p
= "inappropriate document section";
454 p
= "argument value suggested";
458 p
= "prologue macros repeated";
462 p
= "prologue macros out-of-order";
466 p
= "deprecated column argument syntax";
470 p
= "superfluous width argument";
473 p
= "missing width argument";
476 p
= "invalid character";
479 p
= "invalid escape sequence";
482 p
= "suggested no line arguments";
485 p
= "suggested line arguments";
488 p
= "suggested multi-line arguments";
491 p
= "suggested no multi-line arguments";
494 p
= "document section in wrong manual section";
498 p
= "document section out of conventional order";
501 p
= "document section repeated";
504 p
= "unknown standard";
507 p
= "NAME section contents incomplete/badly-ordered";
511 return(mdoc_pwarn(m
, line
, pos
, c
, p
));
516 warn_print(struct mdoc
*m
, int ln
, int pos
)
518 if (MDOC_IGN_CHARS
& m
->pflags
)
519 return(pwarn(m
, ln
, pos
, WPRINT
));
520 return(perr(m
, ln
, pos
, EPRINT
));
525 warn_count(struct mdoc
*m
, const char *k
,
526 int want
, const char *v
, int has
)
529 return(mdoc_warn(m
, WARN_SYNTAX
,
530 "suggests %s %s %d (has %d)", v
, k
, want
, has
));
535 err_count(struct mdoc
*m
, const char *k
,
536 int want
, const char *v
, int has
)
540 "requires %s %s %d (has %d)", v
, k
, want
, has
));
545 count_child(struct mdoc
*mdoc
)
550 for (i
= 0, n
= mdoc
->last
->child
; n
; n
= n
->next
, i
++)
558 * Build these up with macros because they're basically the same check
559 * for different inequalities. Yes, this could be done with functions,
560 * but this is reasonable for now.
563 #define CHECK_CHILD_DEFN(lvl, name, ineq) \
565 lvl##_child_##name(struct mdoc *mdoc, const char *p, int sz) \
568 if ((i = count_child(mdoc)) ineq sz) \
570 return(lvl##_count(mdoc, #ineq, sz, p, i)); \
573 #define CHECK_BODY_DEFN(name, lvl, func, num) \
575 b##lvl##_##name(POST_ARGS) \
577 if (MDOC_BODY != mdoc->last->type) \
579 return(func(mdoc, "multi-line arguments", (num))); \
582 #define CHECK_ELEM_DEFN(name, lvl, func, num) \
584 e##lvl##_##name(POST_ARGS) \
586 assert(MDOC_ELEM == mdoc->last->type); \
587 return(func(mdoc, "line arguments", (num))); \
590 #define CHECK_HEAD_DEFN(name, lvl, func, num) \
592 h##lvl##_##name(POST_ARGS) \
594 if (MDOC_HEAD != mdoc->last->type) \
596 return(func(mdoc, "line arguments", (num))); \
600 CHECK_CHILD_DEFN(warn
, gt
, >) /* warn_child_gt() */
601 CHECK_CHILD_DEFN(err
, gt
, >) /* err_child_gt() */
602 CHECK_CHILD_DEFN(warn
, eq
, ==) /* warn_child_eq() */
603 CHECK_CHILD_DEFN(err
, eq
, ==) /* err_child_eq() */
604 CHECK_CHILD_DEFN(err
, lt
, <) /* err_child_lt() */
605 CHECK_CHILD_DEFN(warn
, lt
, <) /* warn_child_lt() */
606 CHECK_BODY_DEFN(ge1
, warn
, warn_child_gt
, 0) /* bwarn_ge1() */
607 CHECK_ELEM_DEFN(eq1
, warn
, warn_child_eq
, 1) /* ewarn_eq1() */
608 CHECK_ELEM_DEFN(eq0
, warn
, warn_child_eq
, 0) /* ewarn_eq0() */
609 CHECK_ELEM_DEFN(ge1
, warn
, warn_child_gt
, 0) /* ewarn_gt1() */
610 CHECK_ELEM_DEFN(eq1
, err
, err_child_eq
, 1) /* eerr_eq1() */
611 CHECK_ELEM_DEFN(le2
, err
, err_child_lt
, 3) /* eerr_le2() */
612 CHECK_ELEM_DEFN(eq0
, err
, err_child_eq
, 0) /* eerr_eq0() */
613 CHECK_ELEM_DEFN(ge1
, err
, err_child_gt
, 0) /* eerr_ge1() */
614 CHECK_HEAD_DEFN(eq0
, err
, err_child_eq
, 0) /* herr_eq0() */
615 CHECK_HEAD_DEFN(le1
, warn
, warn_child_lt
, 2) /* hwarn_le1() */
616 CHECK_HEAD_DEFN(ge1
, err
, err_child_gt
, 0) /* herr_ge1() */
617 CHECK_HEAD_DEFN(eq1
, warn
, warn_child_eq
, 1) /* hwarn_eq1() */
621 check_stdarg(PRE_ARGS
)
624 if (n
->args
&& 1 == n
->args
->argc
)
625 if (MDOC_Std
== n
->args
->argv
[0].arg
)
627 return(nwarn(mdoc
, n
, WARGVAL
));
632 check_sec(PRE_ARGS
, ...)
641 sec
= (enum mdoc_sec
)va_arg(ap
, int);
642 if (SEC_CUSTOM
== sec
)
644 if (sec
!= mdoc
->lastsec
)
651 return(nwarn(mdoc
, n
, WBADSEC
));
656 check_msec(PRE_ARGS
, ...)
664 if (0 == (msec
= va_arg(ap
, int)))
666 if (msec
!= mdoc
->meta
.msec
)
673 return(nwarn(mdoc
, n
, WBADMSEC
));
678 check_args(struct mdoc
*m
, const struct mdoc_node
*n
)
685 assert(n
->args
->argc
);
686 for (i
= 0; i
< (int)n
->args
->argc
; i
++)
687 if ( ! check_argv(m
, n
, &n
->args
->argv
[i
]))
695 check_argv(struct mdoc
*m
, const struct mdoc_node
*n
,
696 const struct mdoc_argv
*v
)
700 for (i
= 0; i
< (int)v
->sz
; i
++)
701 if ( ! check_text(m
, v
->line
, v
->pos
, v
->value
[i
]))
704 if (MDOC_Std
== v
->arg
) {
705 /* `Nm' name must be set. */
706 if (v
->sz
|| m
->meta
.name
)
708 return(nerr(m
, n
, ENAME
));
716 check_text(struct mdoc
*mdoc
, int line
, int pos
, const char *p
)
720 /* FIXME: indicate deprecated escapes \*(xx and \*x. */
724 if ( ! (MDOC_LITERAL
& mdoc
->flags
))
725 if ( ! warn_print(mdoc
, line
, pos
))
727 } else if ( ! isprint((u_char
)*p
))
728 if ( ! warn_print(mdoc
, line
, pos
))
734 c
= mdoc_isescape(p
);
739 if ( ! (MDOC_IGN_ESCAPE
& mdoc
->pflags
))
740 return(perr(mdoc
, line
, pos
, EESCAPE
));
741 if ( ! pwarn(mdoc
, line
, pos
, WESCAPE
))
752 check_parent(PRE_ARGS
, int tok
, enum mdoc_type t
)
756 if ((MDOC_ROOT
== t
|| tok
== n
->parent
->tok
) &&
757 (t
== n
->parent
->type
))
760 return(mdoc_nerr(mdoc
, n
, "require parent %s",
761 MDOC_ROOT
== t
? "<root>" : mdoc_macronames
[tok
]));
767 pre_display(PRE_ARGS
)
769 struct mdoc_node
*node
;
771 /* Display elements (`Bd', `D1'...) cannot be nested. */
773 if (MDOC_BLOCK
!= n
->type
)
777 for (node
= mdoc
->last
->parent
; node
; node
= node
->parent
)
778 if (MDOC_BLOCK
== node
->type
)
779 if (MDOC_Bd
== node
->tok
)
784 return(nerr(mdoc
, n
, ENESTDISP
));
791 int pos
, col
, type
, width
, offset
;
793 if (MDOC_BLOCK
!= n
->type
)
796 return(nerr(mdoc
, n
, ELISTTYPE
));
798 /* Make sure that only one type of list is specified. */
800 type
= offset
= width
= col
= -1;
803 for (pos
= 0; pos
< (int)n
->args
->argc
; pos
++)
804 switch (n
->args
->argv
[pos
].arg
) {
827 return(nerr(mdoc
, n
, EMULTILIST
));
828 type
= n
->args
->argv
[pos
].arg
;
833 return(nerr(mdoc
, n
, EARGREP
));
834 width
= n
->args
->argv
[pos
].arg
;
838 return(nerr(mdoc
, n
, EARGREP
));
839 offset
= n
->args
->argv
[pos
].arg
;
846 return(nerr(mdoc
, n
, ELISTTYPE
));
849 * Validate the width field. Some list types don't need width
850 * types and should be warned about them. Others should have it
851 * and must also be warned.
856 if (-1 == width
&& ! nwarn(mdoc
, n
, WMISSWIDTH
))
866 if (-1 != width
&& ! nwarn(mdoc
, n
, WNOWIDTH
))
874 * General validation of fields.
880 if (0 == n
->args
->argv
[col
].sz
)
882 if ( ! nwarn(mdoc
, n
, WDEPCOL
))
898 if (MDOC_BLOCK
!= n
->type
)
901 return(nerr(mdoc
, n
, EDISPTYPE
));
903 /* Make sure that only one type of display is specified. */
906 for (i
= 0, err
= type
= 0; ! err
&&
907 i
< (int)n
->args
->argc
; i
++)
908 switch (n
->args
->argv
[i
].arg
) {
911 case (MDOC_Unfilled
):
920 return(nerr(mdoc
, n
, EMULTIDISP
));
927 return(nerr(mdoc
, n
, EDISPTYPE
));
935 if (MDOC_BLOCK
!= n
->type
)
937 return(check_parent(mdoc
, n
, MDOC_Sh
, MDOC_BODY
));
945 if (MDOC_BLOCK
!= n
->type
)
947 return(check_parent(mdoc
, n
, -1, MDOC_ROOT
));
955 if (MDOC_BLOCK
!= n
->type
)
957 return(check_parent(mdoc
, n
, MDOC_Bl
, MDOC_BODY
));
965 if (NULL
== n
->args
|| 1 == n
->args
->argc
)
967 return(mdoc_nerr(mdoc
, n
, "only one argument allowed"));
975 return(check_sec(mdoc
, n
, SEC_LIBRARY
, SEC_CUSTOM
));
983 if ( ! check_msec(mdoc
, n
, 2, 3, 0))
985 return(check_stdarg(mdoc
, n
));
993 if ( ! check_msec(mdoc
, n
, 1, 6, 8, 0))
995 return(check_stdarg(mdoc
, n
));
1003 return(check_msec(mdoc
, n
, 2, 3, 9, 0));
1011 return(check_msec(mdoc
, n
, 4, 0));
1016 pre_prologue(PRE_ARGS
)
1019 return(check_sec(mdoc
, n
, SEC_PROLOGUE
, SEC_CUSTOM
));
1027 if (0 == mdoc
->meta
.date
|| mdoc
->meta
.os
)
1028 if ( ! nwarn(mdoc
, n
, WPROLOOO
))
1030 if (mdoc
->meta
.title
)
1031 if ( ! nwarn(mdoc
, n
, WPROLREP
))
1041 if (NULL
== mdoc
->meta
.title
|| 0 == mdoc
->meta
.date
)
1042 if ( ! nwarn(mdoc
, n
, WPROLOOO
))
1045 if ( ! nwarn(mdoc
, n
, WPROLREP
))
1055 if (mdoc
->meta
.title
|| mdoc
->meta
.os
)
1056 if ( ! nwarn(mdoc
, n
, WPROLOOO
))
1058 if (mdoc
->meta
.date
)
1059 if ( ! nwarn(mdoc
, n
, WPROLREP
))
1069 struct mdoc_node
*head
;
1071 if (MDOC_BLOCK
!= mdoc
->last
->type
)
1074 head
= mdoc
->last
->head
;
1076 if (NULL
== mdoc
->last
->args
) {
1077 if (NULL
== head
->child
||
1078 MDOC_TEXT
!= head
->child
->type
)
1079 return(mdoc_err(mdoc
, "text argument expected"));
1081 p
= head
->child
->string
;
1082 if (0 == strcmp(p
, "Em"))
1084 else if (0 == strcmp(p
, "Li"))
1086 else if (0 == strcmp(p
, "Sm"))
1088 return(mdoc_nerr(mdoc
, head
->child
, "invalid font"));
1092 return(mdoc_err(mdoc
, "one argument expected"));
1102 if (mdoc
->last
->child
)
1104 if (mdoc
->meta
.name
)
1106 return(verr(mdoc
, ENAME
));
1114 if (NULL
== mdoc
->last
->child
)
1116 if (MDOC_TEXT
!= mdoc
->last
->child
->type
)
1117 return(verr(mdoc
, EATT
));
1118 if (mdoc_a2att(mdoc
->last
->child
->string
))
1120 return(verr(mdoc
, EATT
));
1128 if (mdoc
->last
->args
) {
1129 if (NULL
== mdoc
->last
->child
)
1131 return(verr(mdoc
, ELINE
));
1134 if (mdoc
->last
->child
)
1136 return(verr(mdoc
, ELINE
));
1141 post_args(POST_ARGS
)
1144 if (mdoc
->last
->args
)
1146 return(verr(mdoc
, ELINE
));
1154 struct mdoc_node
*n
, *c
;
1156 if (MDOC_BLOCK
!= mdoc
->last
->type
)
1159 n
= mdoc
->last
->parent
->parent
;
1160 if (NULL
== n
->args
)
1161 return(verr(mdoc
, ELISTTYPE
));
1163 /* Some types require block-head, some not. */
1166 for (cols
= type
= -1, i
= 0; -1 == type
&&
1167 i
< (int)n
->args
->argc
; i
++)
1168 switch (n
->args
->argv
[i
].arg
) {
1188 type
= n
->args
->argv
[i
].arg
;
1191 type
= n
->args
->argv
[i
].arg
;
1192 cols
= (int)n
->args
->argv
[i
].sz
;
1199 return(verr(mdoc
, ELISTTYPE
));
1203 if (NULL
== mdoc
->last
->head
->child
)
1204 if ( ! vwarn(mdoc
, WLINE
))
1214 if (NULL
== mdoc
->last
->head
->child
)
1215 if ( ! vwarn(mdoc
, WLINE
))
1217 if (NULL
== mdoc
->last
->body
->child
)
1218 if ( ! vwarn(mdoc
, WMULTILINE
))
1230 if (mdoc
->last
->head
->child
)
1231 if ( ! vwarn(mdoc
, WNOLINE
))
1233 if (NULL
== mdoc
->last
->body
->child
)
1234 if ( ! vwarn(mdoc
, WMULTILINE
))
1238 if (NULL
== mdoc
->last
->head
->child
)
1239 if ( ! vwarn(mdoc
, WLINE
))
1241 if (mdoc
->last
->body
->child
)
1242 if ( ! vwarn(mdoc
, WNOMULTILINE
))
1244 c
= mdoc
->last
->child
;
1245 for (i
= 0; c
&& MDOC_HEAD
== c
->type
; c
= c
->next
)
1249 return(mdoc_err(mdoc
, "column mismatch (have "
1250 "%d, want %d)", i
, cols
));
1262 struct mdoc_node
*n
;
1264 if (MDOC_BODY
!= mdoc
->last
->type
)
1266 if (NULL
== mdoc
->last
->child
)
1270 for (n
= mdoc
->last
->child
; n
; n
= n
->next
) {
1271 if (MDOC_BLOCK
== n
->type
)
1272 if (MDOC_It
== n
->tok
)
1274 return(mdoc_nerr(mdoc
, n
, "bad child of parent %s",
1275 mdoc_macronames
[mdoc
->last
->tok
]));
1283 ebool(struct mdoc
*mdoc
)
1285 struct mdoc_node
*n
;
1288 for (n
= mdoc
->last
->child
; n
; n
= n
->next
) {
1289 if (MDOC_TEXT
!= n
->type
)
1291 if (0 == strcmp(n
->string
, "on"))
1293 if (0 == strcmp(n
->string
, "off"))
1300 return(nerr(mdoc
, n
, EBOOL
));
1305 post_root(POST_ARGS
)
1308 if (NULL
== mdoc
->first
->child
)
1309 return(verr(mdoc
, ENODATA
));
1310 if (SEC_PROLOGUE
== mdoc
->lastnamed
)
1311 return(verr(mdoc
, ENOPROLOGUE
));
1313 if (MDOC_BLOCK
!= mdoc
->first
->child
->type
)
1314 return(verr(mdoc
, ENODATA
));
1315 if (MDOC_Sh
!= mdoc
->first
->child
->tok
)
1316 return(verr(mdoc
, ENODATA
));
1326 if (mdoc_a2st(mdoc
->last
->child
->string
))
1328 return(vwarn(mdoc
, WBADSTAND
));
1336 if (MDOC_HEAD
== mdoc
->last
->type
)
1337 return(post_sh_head(mdoc
));
1338 if (MDOC_BODY
== mdoc
->last
->type
)
1339 return(post_sh_body(mdoc
));
1346 post_sh_body(POST_ARGS
)
1348 struct mdoc_node
*n
;
1350 if (SEC_NAME
!= mdoc
->lastnamed
)
1354 * Warn if the NAME section doesn't contain the `Nm' and `Nd'
1355 * macros (can have multiple `Nm' and one `Nd'). Note that the
1356 * children of the BODY declaration can also be "text".
1359 if (NULL
== (n
= mdoc
->last
->child
))
1360 return(vwarn(mdoc
, WNAMESECINC
));
1362 for ( ; n
&& n
->next
; n
= n
->next
) {
1363 if (MDOC_ELEM
== n
->type
&& MDOC_Nm
== n
->tok
)
1365 if (MDOC_TEXT
== n
->type
)
1367 if ( ! vwarn(mdoc
, WNAMESECINC
))
1371 if (MDOC_ELEM
== n
->type
&& MDOC_Nd
== n
->tok
)
1373 return(vwarn(mdoc
, WNAMESECINC
));
1378 post_sh_head(POST_ARGS
)
1382 const struct mdoc_node
*n
;
1385 * Process a new section. Sections are either "named" or
1386 * "custom"; custom sections are user-defined, while named ones
1387 * usually follow a conventional order and may only appear in
1388 * certain manual sections.
1391 assert(MDOC_Sh
== mdoc
->last
->tok
);
1393 /* This is just concat() inlined, which is irritating. */
1396 for (n
= mdoc
->last
->child
; n
; n
= n
->next
) {
1397 assert(MDOC_TEXT
== n
->type
);
1398 if (strlcat(buf
, n
->string
, 64) >= 64)
1399 return(nerr(mdoc
, n
, ETOOLONG
));
1400 if (NULL
== n
->next
)
1402 if (strlcat(buf
, " ", 64) >= 64)
1403 return(nerr(mdoc
, n
, ETOOLONG
));
1406 sec
= mdoc_atosec(buf
);
1408 /* The NAME section should always be first. */
1410 if (SEC_BODY
== mdoc
->lastnamed
&& SEC_NAME
!= sec
)
1411 return(vwarn(mdoc
, WSECOOO
));
1412 if (SEC_CUSTOM
== sec
)
1415 /* Check for repeated or out-of-order sections. */
1417 if (sec
== mdoc
->lastnamed
)
1418 return(vwarn(mdoc
, WSECREP
));
1419 if (sec
< mdoc
->lastnamed
)
1420 return(vwarn(mdoc
, WSECOOO
));
1422 /* Check particular section/manual section conventions. */
1426 switch (mdoc
->meta
.msec
) {
1432 return(vwarn(mdoc
, WWRONGMSEC
));
1447 return(check_sec(mdoc
, n
, SEC_SYNOPSIS
, SEC_CUSTOM
));