]>
git.cameronkatri.com Git - mandoc.git/blob - mdoc_validate.c
1 /* $Id: mdoc_validate.c,v 1.16 2009/06/18 20:46:19 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
76 typedef int (*v_pre
)(PRE_ARGS
);
77 typedef int (*v_post
)(POST_ARGS
);
84 static int pwarn(struct mdoc
*, int, int, enum mwarn
);
85 static int perr(struct mdoc
*, int, int, enum merr
);
86 static int check_parent(PRE_ARGS
, int, enum mdoc_type
);
87 static int check_msec(PRE_ARGS
, ...);
88 static int check_sec(PRE_ARGS
, ...);
89 static int check_stdarg(PRE_ARGS
);
90 static int check_text(struct mdoc
*, int, int, const char *);
91 static int check_argv(struct mdoc
*,
92 const struct mdoc_node
*,
93 const struct mdoc_argv
*);
94 static int check_args(struct mdoc
*,
95 const struct mdoc_node
*);
96 static int err_child_lt(struct mdoc
*, const char *, int);
97 static int warn_child_lt(struct mdoc
*, const char *, int);
98 static int err_child_gt(struct mdoc
*, const char *, int);
99 static int warn_child_gt(struct mdoc
*, const char *, int);
100 static int err_child_eq(struct mdoc
*, const char *, int);
101 static int warn_child_eq(struct mdoc
*, const char *, int);
102 static int warn_print(struct mdoc
*, int, int);
103 static int warn_count(struct mdoc
*, const char *,
104 int, const char *, int);
105 static int err_count(struct mdoc
*, const char *,
106 int, const char *, int);
107 static int pre_an(PRE_ARGS
);
108 static int pre_bd(PRE_ARGS
);
109 static int pre_bl(PRE_ARGS
);
110 static int pre_cd(PRE_ARGS
);
111 static int pre_dd(PRE_ARGS
);
112 static int pre_display(PRE_ARGS
);
113 static int pre_dt(PRE_ARGS
);
114 static int pre_er(PRE_ARGS
);
115 static int pre_ex(PRE_ARGS
);
116 static int pre_fd(PRE_ARGS
);
117 static int pre_it(PRE_ARGS
);
118 static int pre_lb(PRE_ARGS
);
119 static int pre_os(PRE_ARGS
);
120 static int pre_rv(PRE_ARGS
);
121 static int pre_sh(PRE_ARGS
);
122 static int pre_ss(PRE_ARGS
);
123 static int herr_ge1(POST_ARGS
);
124 static int hwarn_le1(POST_ARGS
);
125 static int herr_eq0(POST_ARGS
);
126 static int eerr_eq0(POST_ARGS
);
127 static int eerr_le2(POST_ARGS
);
128 static int eerr_eq1(POST_ARGS
);
129 static int eerr_ge1(POST_ARGS
);
130 static int ewarn_eq0(POST_ARGS
);
131 static int ewarn_eq1(POST_ARGS
);
132 static int bwarn_ge1(POST_ARGS
);
133 static int hwarn_eq1(POST_ARGS
);
134 static int ewarn_ge1(POST_ARGS
);
135 static int ebool(POST_ARGS
);
136 static int post_an(POST_ARGS
);
137 static int post_args(POST_ARGS
);
138 static int post_at(POST_ARGS
);
139 static int post_bf(POST_ARGS
);
140 static int post_bl(POST_ARGS
);
141 static int post_bl_head(POST_ARGS
);
142 static int post_it(POST_ARGS
);
143 static int post_nm(POST_ARGS
);
144 static int post_root(POST_ARGS
);
145 static int post_sh(POST_ARGS
);
146 static int post_sh_body(POST_ARGS
);
147 static int post_sh_head(POST_ARGS
);
148 static int post_st(POST_ARGS
);
150 #define vwarn(m, t) nwarn((m), (m)->last, (t))
151 #define verr(m, t) nerr((m), (m)->last, (t))
152 #define nwarn(m, n, t) pwarn((m), (n)->line, (n)->pos, (t))
153 #define nerr(m, n, t) perr((m), (n)->line, (n)->pos, (t))
155 static v_pre pres_an
[] = { pre_an
, NULL
};
156 static v_pre pres_bd
[] = { pre_display
, pre_bd
, NULL
};
157 static v_pre pres_bl
[] = { pre_bl
, NULL
};
158 static v_pre pres_cd
[] = { pre_cd
, NULL
};
159 static v_pre pres_dd
[] = { pre_dd
, NULL
};
160 static v_pre pres_d1
[] = { pre_display
, NULL
};
161 static v_pre pres_dt
[] = { pre_dt
, NULL
};
162 static v_pre pres_er
[] = { pre_er
, NULL
};
163 static v_pre pres_ex
[] = { pre_ex
, NULL
};
164 static v_pre pres_fd
[] = { pre_fd
, NULL
};
165 static v_pre pres_it
[] = { pre_it
, NULL
};
166 static v_pre pres_lb
[] = { pre_lb
, NULL
};
167 static v_pre pres_os
[] = { pre_os
, NULL
};
168 static v_pre pres_rv
[] = { pre_rv
, NULL
};
169 static v_pre pres_sh
[] = { pre_sh
, NULL
};
170 static v_pre pres_ss
[] = { pre_ss
, NULL
};
171 static v_post posts_bool
[] = { eerr_eq1
, ebool
, NULL
};
172 static v_post posts_bd
[] = { herr_eq0
, bwarn_ge1
, NULL
};
173 static v_post posts_text
[] = { eerr_ge1
, NULL
};
174 static v_post posts_wtext
[] = { ewarn_ge1
, NULL
};
175 static v_post posts_notext
[] = { eerr_eq0
, NULL
};
176 static v_post posts_wline
[] = { bwarn_ge1
, herr_eq0
, NULL
};
177 static v_post posts_sh
[] = { herr_ge1
, bwarn_ge1
, post_sh
, NULL
};
178 static v_post posts_bl
[] = { bwarn_ge1
, post_bl
, NULL
};
179 static v_post posts_it
[] = { post_it
, NULL
};
180 static v_post posts_in
[] = { ewarn_eq1
, NULL
};
181 static v_post posts_ss
[] = { herr_ge1
, NULL
};
182 static v_post posts_pf
[] = { eerr_eq1
, NULL
};
183 static v_post posts_lb
[] = { eerr_eq1
, NULL
};
184 static v_post posts_st
[] = { eerr_eq1
, post_st
, NULL
};
185 static v_post posts_pp
[] = { ewarn_eq0
, NULL
};
186 static v_post posts_ex
[] = { eerr_eq0
, post_args
, NULL
};
187 static v_post posts_rv
[] = { eerr_eq0
, post_args
, NULL
};
188 static v_post posts_an
[] = { post_an
, NULL
};
189 static v_post posts_at
[] = { post_at
, NULL
};
190 static v_post posts_xr
[] = { eerr_ge1
, eerr_le2
, NULL
};
191 static v_post posts_nm
[] = { post_nm
, NULL
};
192 static v_post posts_bf
[] = { hwarn_le1
, post_bf
, NULL
};
193 static v_post posts_fo
[] = { hwarn_eq1
, bwarn_ge1
, NULL
};
195 const struct valids mdoc_valids
[MDOC_MAX
] = {
196 { NULL
, NULL
}, /* Ap */
197 { pres_dd
, posts_text
}, /* Dd */
198 { pres_dt
, NULL
}, /* Dt */
199 { pres_os
, NULL
}, /* Os */
200 { pres_sh
, posts_sh
}, /* Sh */
201 { pres_ss
, posts_ss
}, /* Ss */
202 { NULL
, posts_pp
}, /* Pp */
203 { pres_d1
, posts_wline
}, /* D1 */
204 { pres_d1
, posts_wline
}, /* Dl */
205 { pres_bd
, posts_bd
}, /* Bd */
206 { NULL
, NULL
}, /* Ed */
207 { pres_bl
, posts_bl
}, /* Bl */
208 { NULL
, NULL
}, /* El */
209 { pres_it
, posts_it
}, /* It */
210 { NULL
, posts_text
}, /* Ad */
211 { pres_an
, posts_an
}, /* An */
212 { NULL
, NULL
}, /* Ar */
213 { pres_cd
, posts_text
}, /* Cd */
214 { NULL
, NULL
}, /* Cm */
215 { NULL
, NULL
}, /* Dv */
216 { pres_er
, posts_text
}, /* Er */
217 { NULL
, NULL
}, /* Ev */
218 { pres_ex
, posts_ex
}, /* Ex */
219 { NULL
, NULL
}, /* Fa */
220 { pres_fd
, posts_wtext
}, /* Fd */
221 { NULL
, NULL
}, /* Fl */
222 { NULL
, posts_text
}, /* Fn */
223 { NULL
, posts_wtext
}, /* Ft */
224 { NULL
, posts_text
}, /* Ic */
225 { NULL
, posts_in
}, /* In */
226 { NULL
, NULL
}, /* Li */
227 { NULL
, posts_wtext
}, /* Nd */
228 { NULL
, posts_nm
}, /* Nm */
229 { NULL
, posts_wline
}, /* Op */
230 { NULL
, NULL
}, /* Ot */
231 { NULL
, NULL
}, /* Pa */
232 { pres_rv
, posts_rv
}, /* Rv */
233 { NULL
, posts_st
}, /* St */
234 { NULL
, NULL
}, /* Va */
235 { NULL
, posts_text
}, /* Vt */
236 { NULL
, posts_xr
}, /* Xr */
237 { NULL
, posts_text
}, /* %A */
238 { NULL
, posts_text
}, /* %B */
239 { NULL
, posts_text
}, /* %D */
240 { NULL
, posts_text
}, /* %I */
241 { NULL
, posts_text
}, /* %J */
242 { NULL
, posts_text
}, /* %N */
243 { NULL
, posts_text
}, /* %O */
244 { NULL
, posts_text
}, /* %P */
245 { NULL
, posts_text
}, /* %R */
246 { NULL
, posts_text
}, /* %T */
247 { NULL
, posts_text
}, /* %V */
248 { NULL
, NULL
}, /* Ac */
249 { NULL
, NULL
}, /* Ao */
250 { NULL
, posts_wline
}, /* Aq */
251 { NULL
, posts_at
}, /* At */
252 { NULL
, NULL
}, /* Bc */
253 { NULL
, posts_bf
}, /* Bf */
254 { NULL
, NULL
}, /* Bo */
255 { NULL
, posts_wline
}, /* Bq */
256 { NULL
, NULL
}, /* Bsx */
257 { NULL
, NULL
}, /* Bx */
258 { NULL
, posts_bool
}, /* Db */
259 { NULL
, NULL
}, /* Dc */
260 { NULL
, NULL
}, /* Do */
261 { NULL
, posts_wline
}, /* Dq */
262 { NULL
, NULL
}, /* Ec */
263 { NULL
, NULL
}, /* Ef */
264 { NULL
, NULL
}, /* Em */
265 { NULL
, NULL
}, /* Eo */
266 { NULL
, NULL
}, /* Fx */
267 { NULL
, posts_text
}, /* Ms */
268 { NULL
, posts_notext
}, /* No */
269 { NULL
, posts_notext
}, /* Ns */
270 { NULL
, NULL
}, /* Nx */
271 { NULL
, NULL
}, /* Ox */
272 { NULL
, NULL
}, /* Pc */
273 { NULL
, posts_pf
}, /* Pf */
274 { NULL
, NULL
}, /* Po */
275 { NULL
, posts_wline
}, /* Pq */
276 { NULL
, NULL
}, /* Qc */
277 { NULL
, posts_wline
}, /* Ql */
278 { NULL
, NULL
}, /* Qo */
279 { NULL
, posts_wline
}, /* Qq */
280 { NULL
, NULL
}, /* Re */
281 { NULL
, posts_wline
}, /* Rs */
282 { NULL
, NULL
}, /* Sc */
283 { NULL
, NULL
}, /* So */
284 { NULL
, posts_wline
}, /* Sq */
285 { NULL
, posts_bool
}, /* Sm */
286 { NULL
, posts_text
}, /* Sx */
287 { NULL
, posts_text
}, /* Sy */
288 { NULL
, posts_text
}, /* Tn */
289 { NULL
, NULL
}, /* Ux */
290 { NULL
, NULL
}, /* Xc */
291 { NULL
, NULL
}, /* Xo */
292 { NULL
, posts_fo
}, /* Fo */
293 { NULL
, NULL
}, /* Fc */
294 { NULL
, NULL
}, /* Oo */
295 { NULL
, NULL
}, /* Oc */
296 { NULL
, posts_wline
}, /* Bk */
297 { NULL
, NULL
}, /* Ek */
298 { NULL
, posts_notext
}, /* Bt */
299 { NULL
, NULL
}, /* Hf */
300 { NULL
, NULL
}, /* Fr */
301 { NULL
, posts_notext
}, /* Ud */
302 { pres_lb
, posts_lb
}, /* Lb */
303 { NULL
, posts_pp
}, /* Lp */
304 { NULL
, NULL
}, /* Lk */
305 { NULL
, posts_text
}, /* Mt */
306 { NULL
, posts_wline
}, /* Brq */
307 { NULL
, NULL
}, /* Bro */
308 { NULL
, NULL
}, /* Brc */
309 { NULL
, posts_text
}, /* %C */
310 { NULL
, NULL
}, /* Es */
311 { NULL
, NULL
}, /* En */
312 { NULL
, NULL
}, /* Dx */
313 { NULL
, posts_text
}, /* %Q */
318 extern size_t strlcat(char *, const char *, size_t);
323 mdoc_valid_pre(struct mdoc
*mdoc
,
324 const struct mdoc_node
*n
)
330 if (MDOC_TEXT
== n
->type
) {
334 return(check_text(mdoc
, line
, pos
, tp
));
337 if ( ! check_args(mdoc
, n
))
339 if (NULL
== mdoc_valids
[n
->tok
].pre
)
341 for (p
= mdoc_valids
[n
->tok
].pre
; *p
; p
++)
342 if ( ! (*p
)(mdoc
, n
))
349 mdoc_valid_post(struct mdoc
*mdoc
)
354 * This check occurs after the macro's children have been filled
355 * in: postfix validation. Since this happens when we're
356 * rewinding the scope tree, it's possible to have multiple
357 * invocations (as by design, for now), we set bit MDOC_VALID to
358 * indicate that we've validated.
361 if (MDOC_VALID
& mdoc
->last
->flags
)
363 mdoc
->last
->flags
|= MDOC_VALID
;
365 if (MDOC_TEXT
== mdoc
->last
->type
)
367 if (MDOC_ROOT
== mdoc
->last
->type
)
368 return(post_root(mdoc
));
370 if (NULL
== mdoc_valids
[mdoc
->last
->tok
].post
)
372 for (p
= mdoc_valids
[mdoc
->last
->tok
].post
; *p
; p
++)
381 perr(struct mdoc
*m
, int line
, int pos
, enum merr type
)
388 p
= "text argument too long";
391 p
= "invalid escape sequence";
394 p
= "invalid character";
397 p
= "displays may not be nested";
400 p
= "expected boolean value";
403 p
= "argument repeated";
406 p
= "multiple display types specified";
409 p
= "multiple list types specified";
412 p
= "missing list type";
415 p
= "missing display type";
418 p
= "the NAME section must come first";
421 p
= "expected line arguments";
424 p
= "document has no prologue";
427 p
= "document has no data";
430 p
= "column syntax style mismatch";
433 p
= "expected valid AT&T symbol";
436 p
= "default name not yet set";
440 return(mdoc_perr(m
, line
, pos
, p
));
445 pwarn(struct mdoc
*m
, int line
, int pos
, enum mwarn type
)
454 p
= "inappropriate manual section";
458 p
= "inappropriate document section";
462 p
= "argument value suggested";
466 p
= "prologue macros repeated";
470 p
= "prologue macros out-of-order";
474 p
= "deprecated column argument syntax";
478 p
= "superfluous width argument";
481 p
= "missing width argument";
484 p
= "invalid character";
487 p
= "invalid escape sequence";
490 p
= "deprecated special-character escape";
493 p
= "suggested no line arguments";
496 p
= "suggested line arguments";
499 p
= "suggested multi-line arguments";
502 p
= "suggested no multi-line arguments";
505 p
= "document section in wrong manual section";
509 p
= "document section out of conventional order";
512 p
= "document section repeated";
515 p
= "unknown standard";
518 p
= "NAME section contents incomplete/badly-ordered";
522 return(mdoc_pwarn(m
, line
, pos
, c
, p
));
527 warn_print(struct mdoc
*m
, int ln
, int pos
)
529 if (MDOC_IGN_CHARS
& m
->pflags
)
530 return(pwarn(m
, ln
, pos
, WPRINT
));
531 return(perr(m
, ln
, pos
, EPRINT
));
536 warn_count(struct mdoc
*m
, const char *k
,
537 int want
, const char *v
, int has
)
540 return(mdoc_warn(m
, WARN_SYNTAX
,
541 "suggests %s %s %d (has %d)", v
, k
, want
, has
));
546 err_count(struct mdoc
*m
, const char *k
,
547 int want
, const char *v
, int has
)
551 "requires %s %s %d (has %d)", v
, k
, want
, has
));
556 * Build these up with macros because they're basically the same check
557 * for different inequalities. Yes, this could be done with functions,
558 * but this is reasonable for now.
561 #define CHECK_CHILD_DEFN(lvl, name, ineq) \
563 lvl##_child_##name(struct mdoc *mdoc, const char *p, int sz) \
565 if (mdoc->last->nchild ineq sz) \
567 return(lvl##_count(mdoc, #ineq, sz, p, mdoc->last->nchild)); \
570 #define CHECK_BODY_DEFN(name, lvl, func, num) \
572 b##lvl##_##name(POST_ARGS) \
574 if (MDOC_BODY != mdoc->last->type) \
576 return(func(mdoc, "multi-line arguments", (num))); \
579 #define CHECK_ELEM_DEFN(name, lvl, func, num) \
581 e##lvl##_##name(POST_ARGS) \
583 assert(MDOC_ELEM == mdoc->last->type); \
584 return(func(mdoc, "line arguments", (num))); \
587 #define CHECK_HEAD_DEFN(name, lvl, func, num) \
589 h##lvl##_##name(POST_ARGS) \
591 if (MDOC_HEAD != mdoc->last->type) \
593 return(func(mdoc, "line arguments", (num))); \
597 CHECK_CHILD_DEFN(warn
, gt
, >) /* warn_child_gt() */
598 CHECK_CHILD_DEFN(err
, gt
, >) /* err_child_gt() */
599 CHECK_CHILD_DEFN(warn
, eq
, ==) /* warn_child_eq() */
600 CHECK_CHILD_DEFN(err
, eq
, ==) /* err_child_eq() */
601 CHECK_CHILD_DEFN(err
, lt
, <) /* err_child_lt() */
602 CHECK_CHILD_DEFN(warn
, lt
, <) /* warn_child_lt() */
603 CHECK_BODY_DEFN(ge1
, warn
, warn_child_gt
, 0) /* bwarn_ge1() */
604 CHECK_ELEM_DEFN(eq1
, warn
, warn_child_eq
, 1) /* ewarn_eq1() */
605 CHECK_ELEM_DEFN(eq0
, warn
, warn_child_eq
, 0) /* ewarn_eq0() */
606 CHECK_ELEM_DEFN(ge1
, warn
, warn_child_gt
, 0) /* ewarn_gt1() */
607 CHECK_ELEM_DEFN(eq1
, err
, err_child_eq
, 1) /* eerr_eq1() */
608 CHECK_ELEM_DEFN(le2
, err
, err_child_lt
, 3) /* eerr_le2() */
609 CHECK_ELEM_DEFN(eq0
, err
, err_child_eq
, 0) /* eerr_eq0() */
610 CHECK_ELEM_DEFN(ge1
, err
, err_child_gt
, 0) /* eerr_ge1() */
611 CHECK_HEAD_DEFN(eq0
, err
, err_child_eq
, 0) /* herr_eq0() */
612 CHECK_HEAD_DEFN(le1
, warn
, warn_child_lt
, 2) /* hwarn_le1() */
613 CHECK_HEAD_DEFN(ge1
, err
, err_child_gt
, 0) /* herr_ge1() */
614 CHECK_HEAD_DEFN(eq1
, warn
, warn_child_eq
, 1) /* hwarn_eq1() */
618 check_stdarg(PRE_ARGS
)
621 if (n
->args
&& 1 == n
->args
->argc
)
622 if (MDOC_Std
== n
->args
->argv
[0].arg
)
624 return(nwarn(mdoc
, n
, WARGVAL
));
629 check_sec(PRE_ARGS
, ...)
638 sec
= (enum mdoc_sec
)va_arg(ap
, int);
639 if (SEC_CUSTOM
== sec
)
641 if (sec
!= mdoc
->lastsec
)
648 return(nwarn(mdoc
, n
, WBADSEC
));
653 check_msec(PRE_ARGS
, ...)
661 if (0 == (msec
= va_arg(ap
, int)))
663 if (msec
!= mdoc
->meta
.msec
)
670 return(nwarn(mdoc
, n
, WBADMSEC
));
675 check_args(struct mdoc
*m
, const struct mdoc_node
*n
)
682 assert(n
->args
->argc
);
683 for (i
= 0; i
< (int)n
->args
->argc
; i
++)
684 if ( ! check_argv(m
, n
, &n
->args
->argv
[i
]))
692 check_argv(struct mdoc
*m
, const struct mdoc_node
*n
,
693 const struct mdoc_argv
*v
)
697 for (i
= 0; i
< (int)v
->sz
; i
++)
698 if ( ! check_text(m
, v
->line
, v
->pos
, v
->value
[i
]))
701 if (MDOC_Std
== v
->arg
) {
702 /* `Nm' name must be set. */
703 if (v
->sz
|| m
->meta
.name
)
705 return(nerr(m
, n
, ENAME
));
713 check_text(struct mdoc
*mdoc
, int line
, int pos
, const char *p
)
719 if ( ! (MDOC_LITERAL
& mdoc
->flags
))
720 if ( ! warn_print(mdoc
, line
, pos
))
722 } else if ( ! isprint((u_char
)*p
))
723 if ( ! warn_print(mdoc
, line
, pos
))
729 c
= mdoc_isescape(p
);
731 /* See if form is deprecated. */
733 if ( ! pwarn(mdoc
, line
, pos
, WDEPESC
))
738 if ( ! (MDOC_IGN_ESCAPE
& mdoc
->pflags
))
739 return(perr(mdoc
, line
, pos
, EESCAPE
));
740 if ( ! pwarn(mdoc
, line
, pos
, WESCAPE
))
751 check_parent(PRE_ARGS
, int tok
, enum mdoc_type t
)
755 if ((MDOC_ROOT
== t
|| tok
== n
->parent
->tok
) &&
756 (t
== n
->parent
->type
))
759 return(mdoc_nerr(mdoc
, n
, "require parent %s",
760 MDOC_ROOT
== t
? "<root>" : mdoc_macronames
[tok
]));
766 pre_display(PRE_ARGS
)
768 struct mdoc_node
*node
;
770 /* Display elements (`Bd', `D1'...) cannot be nested. */
772 if (MDOC_BLOCK
!= n
->type
)
776 for (node
= mdoc
->last
->parent
; node
; node
= node
->parent
)
777 if (MDOC_BLOCK
== node
->type
)
778 if (MDOC_Bd
== node
->tok
)
783 return(nerr(mdoc
, n
, ENESTDISP
));
790 int pos
, type
, width
, offset
;
792 if (MDOC_BLOCK
!= n
->type
)
795 return(nerr(mdoc
, n
, ELISTTYPE
));
797 /* Make sure that only one type of list is specified. */
799 type
= offset
= width
= -1;
802 for (pos
= 0; pos
< (int)n
->args
->argc
; pos
++)
803 switch (n
->args
->argv
[pos
].arg
) {
826 return(nerr(mdoc
, n
, EMULTILIST
));
827 type
= n
->args
->argv
[pos
].arg
;
831 return(nerr(mdoc
, n
, EARGREP
));
832 width
= n
->args
->argv
[pos
].arg
;
836 return(nerr(mdoc
, n
, EARGREP
));
837 offset
= n
->args
->argv
[pos
].arg
;
844 return(nerr(mdoc
, n
, ELISTTYPE
));
847 * Validate the width field. Some list types don't need width
848 * types and should be warned about them. Others should have it
849 * and must also be warned.
854 if (-1 == width
&& ! nwarn(mdoc
, n
, WMISSWIDTH
))
864 if (-1 != width
&& ! nwarn(mdoc
, n
, WNOWIDTH
))
880 if (MDOC_BLOCK
!= n
->type
)
883 return(nerr(mdoc
, n
, EDISPTYPE
));
885 /* Make sure that only one type of display is specified. */
888 for (i
= 0, err
= type
= 0; ! err
&&
889 i
< (int)n
->args
->argc
; i
++)
890 switch (n
->args
->argv
[i
].arg
) {
893 case (MDOC_Unfilled
):
902 return(nerr(mdoc
, n
, EMULTIDISP
));
909 return(nerr(mdoc
, n
, EDISPTYPE
));
917 if (MDOC_BLOCK
!= n
->type
)
919 return(check_parent(mdoc
, n
, MDOC_Sh
, MDOC_BODY
));
927 if (MDOC_BLOCK
!= n
->type
)
929 return(check_parent(mdoc
, n
, -1, MDOC_ROOT
));
937 if (MDOC_BLOCK
!= n
->type
)
939 return(check_parent(mdoc
, n
, MDOC_Bl
, MDOC_BODY
));
947 if (NULL
== n
->args
|| 1 == n
->args
->argc
)
949 return(mdoc_nerr(mdoc
, n
, "only one argument allowed"));
957 return(check_sec(mdoc
, n
, SEC_LIBRARY
, SEC_CUSTOM
));
965 if ( ! check_msec(mdoc
, n
, 2, 3, 0))
967 return(check_stdarg(mdoc
, n
));
975 if ( ! check_msec(mdoc
, n
, 1, 6, 8, 0))
977 return(check_stdarg(mdoc
, n
));
985 return(check_msec(mdoc
, n
, 2, 3, 9, 0));
993 return(check_msec(mdoc
, n
, 4, 0));
1001 if (0 == mdoc
->meta
.date
|| mdoc
->meta
.os
)
1002 if ( ! nwarn(mdoc
, n
, WPROLOOO
))
1004 if (mdoc
->meta
.title
)
1005 if ( ! nwarn(mdoc
, n
, WPROLREP
))
1015 if (NULL
== mdoc
->meta
.title
|| 0 == mdoc
->meta
.date
)
1016 if ( ! nwarn(mdoc
, n
, WPROLOOO
))
1019 if ( ! nwarn(mdoc
, n
, WPROLREP
))
1029 if (mdoc
->meta
.title
|| mdoc
->meta
.os
)
1030 if ( ! nwarn(mdoc
, n
, WPROLOOO
))
1032 if (mdoc
->meta
.date
)
1033 if ( ! nwarn(mdoc
, n
, WPROLREP
))
1043 struct mdoc_node
*head
;
1045 if (MDOC_BLOCK
!= mdoc
->last
->type
)
1048 head
= mdoc
->last
->head
;
1050 if (mdoc
->last
->args
&& head
->child
)
1051 return(mdoc_err(mdoc
, "one argument expected"));
1052 else if (mdoc
->last
->args
)
1055 if (NULL
== head
->child
|| MDOC_TEXT
!= head
->child
->type
)
1056 return(mdoc_err(mdoc
, "text argument expected"));
1058 p
= head
->child
->string
;
1060 if (0 == strcmp(p
, "Em"))
1062 else if (0 == strcmp(p
, "Li"))
1064 else if (0 == strcmp(p
, "Sm"))
1067 return(mdoc_nerr(mdoc
, head
->child
, "invalid font mode"));
1075 if (mdoc
->last
->child
)
1077 if (mdoc
->meta
.name
)
1079 return(verr(mdoc
, ENAME
));
1087 if (NULL
== mdoc
->last
->child
)
1089 if (MDOC_TEXT
!= mdoc
->last
->child
->type
)
1090 return(verr(mdoc
, EATT
));
1091 if (mdoc_a2att(mdoc
->last
->child
->string
))
1093 return(verr(mdoc
, EATT
));
1101 if (mdoc
->last
->args
) {
1102 if (NULL
== mdoc
->last
->child
)
1104 return(verr(mdoc
, ELINE
));
1107 if (mdoc
->last
->child
)
1109 return(verr(mdoc
, ELINE
));
1114 post_args(POST_ARGS
)
1117 if (mdoc
->last
->args
)
1119 return(verr(mdoc
, ELINE
));
1127 struct mdoc_node
*n
, *c
;
1129 if (MDOC_BLOCK
!= mdoc
->last
->type
)
1132 n
= mdoc
->last
->parent
->parent
;
1133 if (NULL
== n
->args
)
1134 return(verr(mdoc
, ELISTTYPE
));
1136 /* Some types require block-head, some not. */
1139 for (cols
= type
= -1, i
= 0; -1 == type
&&
1140 i
< (int)n
->args
->argc
; i
++)
1141 switch (n
->args
->argv
[i
].arg
) {
1161 type
= n
->args
->argv
[i
].arg
;
1164 type
= n
->args
->argv
[i
].arg
;
1165 cols
= (int)n
->args
->argv
[i
].sz
;
1172 return(verr(mdoc
, ELISTTYPE
));
1176 if (NULL
== mdoc
->last
->head
->child
)
1177 if ( ! vwarn(mdoc
, WLINE
))
1187 if (NULL
== mdoc
->last
->head
->child
)
1188 if ( ! vwarn(mdoc
, WLINE
))
1190 if (NULL
== mdoc
->last
->body
->child
)
1191 if ( ! vwarn(mdoc
, WMULTILINE
))
1203 if (mdoc
->last
->head
->child
)
1204 if ( ! vwarn(mdoc
, WNOLINE
))
1206 if (NULL
== mdoc
->last
->body
->child
)
1207 if ( ! vwarn(mdoc
, WMULTILINE
))
1211 if (NULL
== mdoc
->last
->head
->child
)
1212 if ( ! vwarn(mdoc
, WLINE
))
1214 if (mdoc
->last
->body
->child
)
1215 if ( ! vwarn(mdoc
, WNOMULTILINE
))
1217 c
= mdoc
->last
->child
;
1218 for (i
= 0; c
&& MDOC_HEAD
== c
->type
; c
= c
->next
)
1222 return(mdoc_err(mdoc
, "column mismatch (have "
1223 "%d, want %d)", i
, cols
));
1233 post_bl_head(POST_ARGS
)
1236 const struct mdoc_node
*n
;
1238 n
= mdoc
->last
->parent
;
1241 for (i
= 0; i
< (int)n
->args
->argc
; i
++)
1242 if (n
->args
->argv
[i
].arg
== MDOC_Column
)
1245 if (i
== (int)n
->args
->argc
)
1248 if (n
->args
->argv
[i
].sz
&& mdoc
->last
->child
)
1249 return(nerr(mdoc
, n
, ECOLMIS
));
1258 struct mdoc_node
*n
;
1260 if (MDOC_HEAD
== mdoc
->last
->type
)
1261 return(post_bl_head(mdoc
));
1262 if (MDOC_BODY
!= mdoc
->last
->type
)
1264 if (NULL
== mdoc
->last
->child
)
1268 for (n
= mdoc
->last
->child
; n
; n
= n
->next
) {
1269 if (MDOC_BLOCK
== n
->type
)
1270 if (MDOC_It
== n
->tok
)
1272 return(mdoc_nerr(mdoc
, n
, "bad child of parent %s",
1273 mdoc_macronames
[mdoc
->last
->tok
]));
1281 ebool(struct mdoc
*mdoc
)
1283 struct mdoc_node
*n
;
1286 for (n
= mdoc
->last
->child
; n
; n
= n
->next
) {
1287 if (MDOC_TEXT
!= n
->type
)
1289 if (0 == strcmp(n
->string
, "on"))
1291 if (0 == strcmp(n
->string
, "off"))
1298 return(nerr(mdoc
, n
, EBOOL
));
1303 post_root(POST_ARGS
)
1306 if (NULL
== mdoc
->first
->child
)
1307 return(verr(mdoc
, ENODATA
));
1308 if ( ! (MDOC_PBODY
& mdoc
->flags
))
1309 return(verr(mdoc
, ENOPROLOGUE
));
1311 if (MDOC_BLOCK
!= mdoc
->first
->child
->type
)
1312 return(verr(mdoc
, ENODATA
));
1313 if (MDOC_Sh
!= mdoc
->first
->child
->tok
)
1314 return(verr(mdoc
, ENODATA
));
1324 if (mdoc_a2st(mdoc
->last
->child
->string
))
1326 return(vwarn(mdoc
, WBADSTAND
));
1334 if (MDOC_HEAD
== mdoc
->last
->type
)
1335 return(post_sh_head(mdoc
));
1336 if (MDOC_BODY
== mdoc
->last
->type
)
1337 return(post_sh_body(mdoc
));
1344 post_sh_body(POST_ARGS
)
1346 struct mdoc_node
*n
;
1348 if (SEC_NAME
!= mdoc
->lastnamed
)
1352 * Warn if the NAME section doesn't contain the `Nm' and `Nd'
1353 * macros (can have multiple `Nm' and one `Nd'). Note that the
1354 * children of the BODY declaration can also be "text".
1357 if (NULL
== (n
= mdoc
->last
->child
))
1358 return(vwarn(mdoc
, WNAMESECINC
));
1360 for ( ; n
&& n
->next
; n
= n
->next
) {
1361 if (MDOC_ELEM
== n
->type
&& MDOC_Nm
== n
->tok
)
1363 if (MDOC_TEXT
== n
->type
)
1365 if ( ! vwarn(mdoc
, WNAMESECINC
))
1369 if (MDOC_ELEM
== n
->type
&& MDOC_Nd
== n
->tok
)
1371 return(vwarn(mdoc
, WNAMESECINC
));
1376 post_sh_head(POST_ARGS
)
1380 const struct mdoc_node
*n
;
1383 * Process a new section. Sections are either "named" or
1384 * "custom"; custom sections are user-defined, while named ones
1385 * usually follow a conventional order and may only appear in
1386 * certain manual sections.
1391 for (n
= mdoc
->last
->child
; n
; n
= n
->next
) {
1392 /* XXX - copied from compact(). */
1393 assert(MDOC_TEXT
== n
->type
);
1395 if (strlcat(buf
, n
->string
, 64) >= 64)
1396 return(nerr(mdoc
, n
, ETOOLONG
));
1397 if (NULL
== n
->next
)
1399 if (strlcat(buf
, " ", 64) >= 64)
1400 return(nerr(mdoc
, n
, ETOOLONG
));
1403 sec
= mdoc_atosec(buf
);
1406 * Check: NAME should always be first, CUSTOM has no roles,
1407 * non-CUSTOM has a conventional order to be followed.
1410 if (SEC_NAME
!= sec
&& SEC_NONE
== mdoc
->lastnamed
)
1411 return(verr(mdoc
, ESECNAME
));
1412 if (SEC_CUSTOM
== sec
)
1414 if (sec
== mdoc
->lastnamed
)
1415 return(vwarn(mdoc
, WSECREP
));
1416 if (sec
< mdoc
->lastnamed
)
1417 return(vwarn(mdoc
, WSECOOO
));
1420 * Check particular section/manual conventions. LIBRARY can
1421 * only occur in msec 2, 3 (TODO: are there more of these?).
1426 switch (mdoc
->meta
.msec
) {
1432 return(vwarn(mdoc
, WWRONGMSEC
));
1447 return(check_sec(mdoc
, n
, SEC_SYNOPSIS
, SEC_CUSTOM
));