]>
git.cameronkatri.com Git - mandoc.git/blob - validate.c
1 /* $Id: validate.c,v 1.92 2009/03/21 21:09:00 kristaps Exp $ */
3 * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@openbsd.org>
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the
7 * above copyright notice and this permission notice appear in all
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
11 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
12 * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
13 * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
14 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
15 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
16 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17 * PERFORMANCE OF THIS SOFTWARE.
19 #include <sys/types.h>
28 /* FIXME: .Bl -diag can't have non-text children in HEAD. */
29 /* TODO: ignoring Pp (it's superfluous in some invocations). */
32 * Pre- and post-validate macros as they're parsed. Pre-validation
33 * occurs when the macro has been detected and its arguments parsed.
34 * Post-validation occurs when all child macros have also been parsed.
35 * In the ELEMENT case, this is simply the parameters of the macro; in
36 * the BLOCK case, this is the HEAD, BODY, TAIL and so on.
39 #define PRE_ARGS struct mdoc *mdoc, const struct mdoc_node *n
40 #define POST_ARGS struct mdoc *mdoc
77 typedef int (*v_pre
)(PRE_ARGS
);
78 typedef int (*v_post
)(POST_ARGS
);
87 static int pwarn(struct mdoc
*, int, int, enum mwarn
);
88 static int perr(struct mdoc
*, int, int, enum merr
);
89 static int check_parent(PRE_ARGS
, int, enum mdoc_type
);
90 static int check_msec(PRE_ARGS
, ...);
91 static int check_sec(PRE_ARGS
, ...);
92 static int check_stdarg(PRE_ARGS
);
93 static int check_text(struct mdoc
*, int, int, const char *);
94 static int check_argv(struct mdoc
*,
95 const struct mdoc_node
*,
96 const struct mdoc_argv
*);
97 static int check_args(struct mdoc
*,
98 const struct mdoc_node
*);
99 static int err_child_lt(struct mdoc
*, const char *, int);
100 static int warn_child_lt(struct mdoc
*, const char *, int);
101 static int err_child_gt(struct mdoc
*, const char *, int);
102 static int warn_child_gt(struct mdoc
*, const char *, int);
103 static int err_child_eq(struct mdoc
*, const char *, int);
104 static int warn_child_eq(struct mdoc
*, const char *, int);
105 static inline int count_child(struct mdoc
*);
106 static inline int warn_count(struct mdoc
*, const char *,
107 int, const char *, int);
108 static inline int err_count(struct mdoc
*, const char *,
109 int, const char *, int);
110 static int pre_an(PRE_ARGS
);
111 static int pre_bd(PRE_ARGS
);
112 static int pre_bl(PRE_ARGS
);
113 static int pre_cd(PRE_ARGS
);
114 static int pre_dd(PRE_ARGS
);
115 static int pre_display(PRE_ARGS
);
116 static int pre_dt(PRE_ARGS
);
117 static int pre_er(PRE_ARGS
);
118 static int pre_ex(PRE_ARGS
);
119 static int pre_fd(PRE_ARGS
);
120 static int pre_it(PRE_ARGS
);
121 static int pre_lb(PRE_ARGS
);
122 static int pre_os(PRE_ARGS
);
123 static int pre_prologue(PRE_ARGS
);
124 static int pre_rv(PRE_ARGS
);
125 static int pre_sh(PRE_ARGS
);
126 static int pre_ss(PRE_ARGS
);
127 static int herr_ge1(POST_ARGS
);
128 static int hwarn_le1(POST_ARGS
);
129 static int herr_eq0(POST_ARGS
);
130 static int eerr_eq0(POST_ARGS
);
131 static int eerr_le2(POST_ARGS
);
132 static int eerr_eq1(POST_ARGS
);
133 static int eerr_ge1(POST_ARGS
);
134 static int ewarn_eq0(POST_ARGS
);
135 static int ewarn_eq1(POST_ARGS
);
136 static int bwarn_ge1(POST_ARGS
);
137 static int hwarn_eq1(POST_ARGS
);
138 static int ewarn_ge1(POST_ARGS
);
139 static int ebool(POST_ARGS
);
141 static int post_an(POST_ARGS
);
142 static int post_args(POST_ARGS
);
143 static int post_at(POST_ARGS
);
144 static int post_bf(POST_ARGS
);
145 static int post_bl(POST_ARGS
);
146 static int post_it(POST_ARGS
);
147 static int post_nm(POST_ARGS
);
148 static int post_root(POST_ARGS
);
149 static int post_sh(POST_ARGS
);
150 static int post_sh_body(POST_ARGS
);
151 static int post_sh_head(POST_ARGS
);
152 static int post_st(POST_ARGS
);
154 #define mwarn(m, t) nwarn((m), (m)->last, (t))
155 #define merr(m, t) nerr((m), (m)->last, (t))
156 #define nwarn(m, n, t) pwarn((m), (n)->line, (n)->pos, (t))
157 #define nerr(m, n, t) perr((m), (n)->line, (n)->pos, (t))
159 static v_pre pres_an
[] = { pre_an
, NULL
};
160 static v_pre pres_bd
[] = { pre_display
, pre_bd
, NULL
};
161 static v_pre pres_bl
[] = { pre_bl
, NULL
};
162 static v_pre pres_cd
[] = { pre_cd
, NULL
};
163 static v_pre pres_dd
[] = { pre_prologue
, pre_dd
, NULL
};
164 static v_pre pres_d1
[] = { pre_display
, NULL
};
165 static v_pre pres_dt
[] = { pre_prologue
, pre_dt
, NULL
};
166 static v_pre pres_er
[] = { pre_er
, NULL
};
167 static v_pre pres_ex
[] = { pre_ex
, NULL
};
168 static v_pre pres_fd
[] = { pre_fd
, NULL
};
169 static v_pre pres_it
[] = { pre_it
, NULL
};
170 static v_pre pres_lb
[] = { pre_lb
, NULL
};
171 static v_pre pres_os
[] = { pre_prologue
, pre_os
, NULL
};
172 static v_pre pres_rv
[] = { pre_rv
, NULL
};
173 static v_pre pres_sh
[] = { pre_sh
, NULL
};
174 static v_pre pres_ss
[] = { pre_ss
, NULL
};
175 static v_post posts_bool
[] = { eerr_eq1
, ebool
, NULL
};
176 static v_post posts_bd
[] = { herr_eq0
, bwarn_ge1
, NULL
};
177 static v_post posts_text
[] = { eerr_ge1
, NULL
};
178 static v_post posts_wtext
[] = { ewarn_ge1
, NULL
};
179 static v_post posts_notext
[] = { eerr_eq0
, NULL
};
180 static v_post posts_wline
[] = { bwarn_ge1
, herr_eq0
, NULL
};
181 static v_post posts_sh
[] = { herr_ge1
, bwarn_ge1
, post_sh
, NULL
};
182 static v_post posts_bl
[] = { herr_eq0
, bwarn_ge1
, post_bl
, NULL
};
183 static v_post posts_it
[] = { post_it
, NULL
};
184 static v_post posts_in
[] = { ewarn_eq1
, NULL
};
185 static v_post posts_ss
[] = { herr_ge1
, NULL
};
186 static v_post posts_pf
[] = { eerr_eq1
, NULL
};
187 static v_post posts_lb
[] = { eerr_eq1
, NULL
};
188 static v_post posts_st
[] = { eerr_eq1
, post_st
, NULL
};
189 static v_post posts_pp
[] = { ewarn_eq0
, NULL
};
190 static v_post posts_ex
[] = { eerr_eq0
, post_args
, NULL
};
191 static v_post posts_rv
[] = { eerr_eq0
, post_args
, NULL
};
192 static v_post posts_an
[] = { post_an
, NULL
};
193 static v_post posts_at
[] = { post_at
, NULL
};
194 static v_post posts_xr
[] = { eerr_ge1
, eerr_le2
, NULL
};
195 static v_post posts_nm
[] = { post_nm
, NULL
};
196 static v_post posts_bf
[] = { hwarn_le1
, post_bf
, NULL
};
197 static v_post posts_fo
[] = { hwarn_eq1
, bwarn_ge1
, NULL
};
199 const struct valids mdoc_valids
[MDOC_MAX
] = {
200 { NULL
, NULL
}, /* \" */
201 { pres_dd
, posts_text
}, /* Dd */
202 { pres_dt
, NULL
}, /* Dt */
203 { pres_os
, NULL
}, /* Os */
204 { pres_sh
, posts_sh
}, /* Sh */
205 { pres_ss
, posts_ss
}, /* Ss */
206 { NULL
, posts_pp
}, /* Pp */
207 { pres_d1
, posts_wline
}, /* D1 */
208 { pres_d1
, posts_wline
}, /* Dl */
209 { pres_bd
, posts_bd
}, /* Bd */
210 { NULL
, NULL
}, /* Ed */
211 { pres_bl
, posts_bl
}, /* Bl */
212 { NULL
, NULL
}, /* El */
213 { pres_it
, posts_it
}, /* It */
214 { NULL
, posts_text
}, /* Ad */
215 { pres_an
, posts_an
}, /* An */
216 { NULL
, NULL
}, /* Ar */
217 { pres_cd
, posts_text
}, /* Cd */
218 { NULL
, NULL
}, /* Cm */
219 { NULL
, NULL
}, /* Dv */
220 { pres_er
, posts_text
}, /* Er */
221 { NULL
, NULL
}, /* Ev */
222 { pres_ex
, posts_ex
}, /* Ex */
223 { NULL
, NULL
}, /* Fa */
224 { pres_fd
, posts_wtext
}, /* Fd */
225 { NULL
, NULL
}, /* Fl */
226 { NULL
, posts_text
}, /* Fn */
227 { NULL
, posts_wtext
}, /* Ft */
228 { NULL
, posts_text
}, /* Ic */
229 { NULL
, posts_in
}, /* In */
230 { NULL
, NULL
}, /* Li */
231 { NULL
, posts_wtext
}, /* Nd */
232 { NULL
, posts_nm
}, /* Nm */
233 { NULL
, posts_wline
}, /* Op */
234 { NULL
, NULL
}, /* Ot */
235 { NULL
, NULL
}, /* Pa */
236 { pres_rv
, posts_rv
}, /* Rv */
237 { NULL
, posts_st
}, /* St */
238 { NULL
, NULL
}, /* Va */
239 { NULL
, posts_text
}, /* Vt */
240 { NULL
, posts_xr
}, /* Xr */
241 { NULL
, posts_text
}, /* %A */
242 { NULL
, posts_text
}, /* %B */
243 { NULL
, posts_text
}, /* %D */
244 { NULL
, posts_text
}, /* %I */
245 { NULL
, posts_text
}, /* %J */
246 { NULL
, posts_text
}, /* %N */
247 { NULL
, posts_text
}, /* %O */
248 { NULL
, posts_text
}, /* %P */
249 { NULL
, posts_text
}, /* %R */
250 { NULL
, posts_text
}, /* %T */
251 { NULL
, posts_text
}, /* %V */
252 { NULL
, NULL
}, /* Ac */
253 { NULL
, NULL
}, /* Ao */
254 { NULL
, posts_wline
}, /* Aq */
255 { NULL
, posts_at
}, /* At */
256 { NULL
, NULL
}, /* Bc */
257 { NULL
, posts_bf
}, /* Bf */
258 { NULL
, NULL
}, /* Bo */
259 { NULL
, posts_wline
}, /* Bq */
260 { NULL
, NULL
}, /* Bsx */
261 { NULL
, NULL
}, /* Bx */
262 { NULL
, posts_bool
}, /* Db */
263 { NULL
, NULL
}, /* Dc */
264 { NULL
, NULL
}, /* Do */
265 { NULL
, posts_wline
}, /* Dq */
266 { NULL
, NULL
}, /* Ec */
267 { NULL
, NULL
}, /* Ef */
268 { NULL
, NULL
}, /* Em */
269 { NULL
, NULL
}, /* Eo */
270 { NULL
, NULL
}, /* Fx */
271 { NULL
, posts_text
}, /* Ms */
272 { NULL
, posts_notext
}, /* No */
273 { NULL
, posts_notext
}, /* Ns */
274 { NULL
, NULL
}, /* Nx */
275 { NULL
, NULL
}, /* Ox */
276 { NULL
, NULL
}, /* Pc */
277 { NULL
, posts_pf
}, /* Pf */
278 { NULL
, NULL
}, /* Po */
279 { NULL
, posts_wline
}, /* Pq */
280 { NULL
, NULL
}, /* Qc */
281 { NULL
, posts_wline
}, /* Ql */
282 { NULL
, NULL
}, /* Qo */
283 { NULL
, posts_wline
}, /* Qq */
284 { NULL
, NULL
}, /* Re */
285 { NULL
, posts_wline
}, /* Rs */
286 { NULL
, NULL
}, /* Sc */
287 { NULL
, NULL
}, /* So */
288 { NULL
, posts_wline
}, /* Sq */
289 { NULL
, posts_bool
}, /* Sm */
290 { NULL
, posts_text
}, /* Sx */
291 { NULL
, posts_text
}, /* Sy */
292 { NULL
, posts_text
}, /* Tn */
293 { NULL
, NULL
}, /* Ux */
294 { NULL
, NULL
}, /* Xc */
295 { NULL
, NULL
}, /* Xo */
296 { NULL
, posts_fo
}, /* Fo */
297 { NULL
, NULL
}, /* Fc */
298 { NULL
, NULL
}, /* Oo */
299 { NULL
, NULL
}, /* Oc */
300 { NULL
, posts_wline
}, /* Bk */
301 { NULL
, NULL
}, /* Ek */
302 { NULL
, posts_notext
}, /* Bt */
303 { NULL
, NULL
}, /* Hf */
304 { NULL
, NULL
}, /* Fr */
305 { NULL
, posts_notext
}, /* Ud */
306 { pres_lb
, posts_lb
}, /* Lb */
307 { NULL
, NULL
}, /* Ap */
308 { NULL
, posts_pp
}, /* Lp */
309 { NULL
, posts_text
}, /* Lk */
310 { NULL
, posts_text
}, /* Mt */
311 { NULL
, posts_wline
}, /* Brq */
312 { NULL
, NULL
}, /* Bro */
313 { NULL
, NULL
}, /* Brc */
314 { NULL
, posts_text
}, /* %C */
315 { NULL
, NULL
}, /* Es */
316 { NULL
, NULL
}, /* En */
317 { NULL
, NULL
}, /* Dx */
318 { NULL
, posts_text
}, /* %Q */
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
= "invalid escape sequence";
391 p
= "invalid character";
394 p
= "displays may not be nested";
397 p
= "expected boolean value";
400 p
= "argument repeated";
403 p
= "multiple display types specified";
406 p
= "multiple list types specified";
409 p
= "missing list type";
412 p
= "missing display type";
415 p
= "expected line arguments";
418 p
= "document has no prologue";
421 p
= "document has no data";
424 p
= "expected valid AT&T symbol";
427 p
= "default name not yet set";
431 return(mdoc_perr(m
, line
, pos
, p
));
436 pwarn(struct mdoc
*m
, int line
, int pos
, enum mwarn type
)
445 p
= "inappropriate manual section";
449 p
= "inappropriate document section";
453 p
= "argument value suggested";
457 p
= "prologue macros repeated";
461 p
= "prologue macros out-of-order";
465 p
= "invalid escape sequence";
468 p
= "suggested no line arguments";
471 p
= "suggested line arguments";
474 p
= "suggested multi-line arguments";
477 p
= "suggested no multi-line arguments";
480 p
= "document section in wrong manual section";
484 p
= "document section out of conventional order";
487 p
= "document section repeated";
490 p
= "unknown standard";
493 p
= "NAME section contents incomplete/badly-ordered";
497 return(mdoc_pwarn(m
, line
, pos
, c
, p
));
503 warn_count(struct mdoc
*m
, const char *k
,
504 int want
, const char *v
, int has
)
507 return(mdoc_warn(m
, WARN_SYNTAX
,
508 "suggests %s %s %d (has %d)", v
, k
, want
, has
));
513 err_count(struct mdoc
*m
, const char *k
,
514 int want
, const char *v
, int has
)
518 "requires %s %s %d (has %d)", v
, k
, want
, has
));
523 count_child(struct mdoc
*mdoc
)
528 for (i
= 0, n
= mdoc
->last
->child
; n
; n
= n
->next
, i
++)
536 * Build these up with macros because they're basically the same check
537 * for different inequalities. Yes, this could be done with functions,
538 * but this is reasonable for now.
541 #define CHECK_CHILD_DEFN(lvl, name, ineq) \
543 lvl##_child_##name(struct mdoc *mdoc, const char *p, int sz) \
546 if ((i = count_child(mdoc)) ineq sz) \
548 return(lvl##_count(mdoc, #ineq, sz, p, i)); \
551 #define CHECK_BODY_DEFN(name, lvl, func, num) \
553 b##lvl##_##name(POST_ARGS) \
555 if (MDOC_BODY != mdoc->last->type) \
557 return(func(mdoc, "multi-line arguments", (num))); \
560 #define CHECK_ELEM_DEFN(name, lvl, func, num) \
562 e##lvl##_##name(POST_ARGS) \
564 assert(MDOC_ELEM == mdoc->last->type); \
565 return(func(mdoc, "line arguments", (num))); \
568 #define CHECK_HEAD_DEFN(name, lvl, func, num) \
570 h##lvl##_##name(POST_ARGS) \
572 if (MDOC_HEAD != mdoc->last->type) \
574 return(func(mdoc, "line arguments", (num))); \
578 CHECK_CHILD_DEFN(warn
, gt
, >) /* warn_child_gt() */
579 CHECK_CHILD_DEFN(err
, gt
, >) /* err_child_gt() */
580 CHECK_CHILD_DEFN(warn
, eq
, ==) /* warn_child_eq() */
581 CHECK_CHILD_DEFN(err
, eq
, ==) /* err_child_eq() */
582 CHECK_CHILD_DEFN(err
, lt
, <) /* err_child_lt() */
583 CHECK_CHILD_DEFN(warn
, lt
, <) /* warn_child_lt() */
584 CHECK_BODY_DEFN(ge1
, warn
, warn_child_gt
, 0) /* bwarn_ge1() */
585 CHECK_ELEM_DEFN(eq1
, warn
, warn_child_eq
, 1) /* ewarn_eq1() */
586 CHECK_ELEM_DEFN(eq0
, warn
, warn_child_eq
, 0) /* ewarn_eq0() */
587 CHECK_ELEM_DEFN(ge1
, warn
, warn_child_gt
, 0) /* ewarn_gt1() */
588 CHECK_ELEM_DEFN(eq1
, err
, err_child_eq
, 1) /* eerr_eq1() */
589 CHECK_ELEM_DEFN(le2
, err
, err_child_lt
, 3) /* eerr_le2() */
590 CHECK_ELEM_DEFN(eq0
, err
, err_child_eq
, 0) /* eerr_eq0() */
591 CHECK_ELEM_DEFN(ge1
, err
, err_child_gt
, 0) /* eerr_ge1() */
592 CHECK_HEAD_DEFN(eq0
, err
, err_child_eq
, 0) /* herr_eq0() */
593 CHECK_HEAD_DEFN(le1
, warn
, warn_child_lt
, 2) /* hwarn_le1() */
594 CHECK_HEAD_DEFN(ge1
, err
, err_child_gt
, 0) /* herr_ge1() */
595 CHECK_HEAD_DEFN(eq1
, warn
, warn_child_eq
, 1) /* hwarn_eq1() */
599 check_stdarg(PRE_ARGS
)
602 if (n
->args
&& 1 == n
->args
->argc
)
603 if (MDOC_Std
== n
->args
->argv
[0].arg
)
605 return(nwarn(mdoc
, n
, WARGVAL
));
610 check_sec(PRE_ARGS
, ...)
619 sec
= (enum mdoc_sec
)va_arg(ap
, int);
620 if (SEC_CUSTOM
== sec
)
622 if (sec
!= mdoc
->lastsec
)
629 return(nwarn(mdoc
, n
, WBADSEC
));
634 check_msec(PRE_ARGS
, ...)
642 if (0 == (msec
= va_arg(ap
, int)))
644 if (msec
!= mdoc
->meta
.msec
)
651 return(nwarn(mdoc
, n
, WBADMSEC
));
656 check_args(struct mdoc
*m
, const struct mdoc_node
*n
)
663 assert(n
->args
->argc
);
664 for (i
= 0; i
< (int)n
->args
->argc
; i
++)
665 if ( ! check_argv(m
, n
, &n
->args
->argv
[i
]))
673 check_argv(struct mdoc
*m
, const struct mdoc_node
*n
,
674 const struct mdoc_argv
*v
)
678 for (i
= 0; i
< (int)v
->sz
; i
++)
679 if ( ! check_text(m
, v
->line
, v
->pos
, v
->value
[i
]))
682 if (MDOC_Std
== v
->arg
) {
683 /* `Nm' name must be set. */
684 if (v
->sz
|| m
->meta
.name
)
686 return(nerr(m
, n
, ENAME
));
694 check_text(struct mdoc
*mdoc
, int line
, int pos
, const char *p
)
698 /* FIXME: indicate deprecated escapes \*(xx and \*x. */
702 if ( ! (MDOC_LITERAL
& mdoc
->flags
))
703 return(perr(mdoc
, line
, pos
, EPRINT
));
704 } else if ( ! isprint((u_char
)*p
))
705 return(perr(mdoc
, line
, pos
, EPRINT
));
710 c
= mdoc_isescape(p
);
715 if ( ! (MDOC_IGN_ESCAPE
& mdoc
->pflags
))
716 return(perr(mdoc
, line
, pos
, EESCAPE
));
717 if ( ! pwarn(mdoc
, line
, pos
, WESCAPE
))
728 check_parent(PRE_ARGS
, int tok
, enum mdoc_type t
)
732 if ((MDOC_ROOT
== t
|| tok
== n
->parent
->tok
) &&
733 (t
== n
->parent
->type
))
736 return(mdoc_nerr(mdoc
, n
, "require parent %s",
737 MDOC_ROOT
== t
? "<root>" : mdoc_macronames
[tok
]));
743 pre_display(PRE_ARGS
)
745 struct mdoc_node
*node
;
747 /* Display elements (`Bd', `D1'...) cannot be nested. */
749 if (MDOC_BLOCK
!= n
->type
)
753 for (node
= mdoc
->last
->parent
; node
; node
= node
->parent
)
754 if (MDOC_BLOCK
== node
->type
)
755 if (MDOC_Bd
== node
->tok
)
760 return(nerr(mdoc
, n
, ENESTDISP
));
767 int i
, type
, width
, offset
;
769 if (MDOC_BLOCK
!= n
->type
)
772 return(nerr(mdoc
, n
, ELISTTYPE
));
774 /* Make sure that only one type of list is specified. */
776 type
= offset
= width
= -1;
779 for (i
= 0; i
< (int)n
->args
->argc
; i
++)
780 switch (n
->args
->argv
[i
].arg
) {
803 type
= n
->args
->argv
[i
].arg
;
806 return(nerr(mdoc
, n
, EMULTILIST
));
809 width
= n
->args
->argv
[i
].arg
;
812 return(nerr(mdoc
, n
, EARGREP
));
815 offset
= n
->args
->argv
[i
].arg
;
818 return(nerr(mdoc
, n
, EARGREP
));
824 return(nerr(mdoc
, n
, ELISTTYPE
));
836 return(mdoc_nwarn(mdoc
, n
, WARN_SYNTAX
,
837 "superfluous %s argument",
838 mdoc_argnames
[MDOC_Width
]));
842 return(mdoc_nwarn(mdoc
, n
, WARN_SYNTAX
,
843 "suggest %s argument",
844 mdoc_argnames
[MDOC_Width
]));
858 if (MDOC_BLOCK
!= n
->type
)
861 return(nerr(mdoc
, n
, EDISPTYPE
));
863 /* Make sure that only one type of display is specified. */
866 for (i
= 0, err
= type
= 0; ! err
&&
867 i
< (int)n
->args
->argc
; i
++)
868 switch (n
->args
->argv
[i
].arg
) {
871 case (MDOC_Unfilled
):
880 return(nerr(mdoc
, n
, EMULTIDISP
));
887 return(nerr(mdoc
, n
, EDISPTYPE
));
895 if (MDOC_BLOCK
!= n
->type
)
897 return(check_parent(mdoc
, n
, MDOC_Sh
, MDOC_BODY
));
905 if (MDOC_BLOCK
!= n
->type
)
907 return(check_parent(mdoc
, n
, -1, MDOC_ROOT
));
915 if (MDOC_BLOCK
!= n
->type
)
917 return(check_parent(mdoc
, n
, MDOC_Bl
, MDOC_BODY
));
925 if (NULL
== n
->args
|| 1 == n
->args
->argc
)
927 return(mdoc_nerr(mdoc
, n
, "only one argument allowed"));
935 return(check_sec(mdoc
, n
, SEC_LIBRARY
, SEC_CUSTOM
));
943 if ( ! check_msec(mdoc
, n
, 2, 3, 0))
945 return(check_stdarg(mdoc
, n
));
953 if ( ! check_msec(mdoc
, n
, 1, 6, 8, 0))
955 return(check_stdarg(mdoc
, n
));
963 return(check_msec(mdoc
, n
, 2, 0));
971 return(check_msec(mdoc
, n
, 4, 0));
976 pre_prologue(PRE_ARGS
)
979 return(check_sec(mdoc
, n
, SEC_PROLOGUE
, SEC_CUSTOM
));
987 if (0 == mdoc
->meta
.date
|| mdoc
->meta
.os
)
988 if ( ! nwarn(mdoc
, n
, WPROLOOO
))
990 if (mdoc
->meta
.title
)
991 if ( ! nwarn(mdoc
, n
, WPROLREP
))
1001 if (NULL
== mdoc
->meta
.title
|| 0 == mdoc
->meta
.date
)
1002 if ( ! nwarn(mdoc
, n
, WPROLOOO
))
1005 if ( ! nwarn(mdoc
, n
, WPROLREP
))
1015 if (mdoc
->meta
.title
|| mdoc
->meta
.os
)
1016 if ( ! nwarn(mdoc
, n
, WPROLOOO
))
1018 if (mdoc
->meta
.date
)
1019 if ( ! nwarn(mdoc
, n
, WPROLREP
))
1029 struct mdoc_node
*head
;
1031 if (MDOC_BLOCK
!= mdoc
->last
->type
)
1034 head
= mdoc
->last
->head
;
1036 if (NULL
== mdoc
->last
->args
) {
1037 if (NULL
== head
->child
||
1038 MDOC_TEXT
!= head
->child
->type
)
1039 return(mdoc_err(mdoc
, "text argument expected"));
1041 p
= head
->child
->string
;
1042 if (xstrcmp(p
, "Em"))
1044 else if (xstrcmp(p
, "Li"))
1046 else if (xstrcmp(p
, "Sm"))
1048 return(mdoc_nerr(mdoc
, head
->child
, "invalid font"));
1052 return(mdoc_err(mdoc
, "one argument expected"));
1062 if (mdoc
->last
->child
)
1064 if (mdoc
->meta
.name
)
1066 return(merr(mdoc
, ENAME
));
1074 if (NULL
== mdoc
->last
->child
)
1076 if (MDOC_TEXT
!= mdoc
->last
->child
->type
)
1077 return(merr(mdoc
, EATT
));
1078 if (mdoc_a2att(mdoc
->last
->child
->string
))
1080 return(merr(mdoc
, EATT
));
1088 if (mdoc
->last
->args
) {
1089 if (NULL
== mdoc
->last
->child
)
1091 return(merr(mdoc
, ELINE
));
1094 if (mdoc
->last
->child
)
1096 return(merr(mdoc
, ELINE
));
1101 post_args(POST_ARGS
)
1104 if (mdoc
->last
->args
)
1106 return(merr(mdoc
, ELINE
));
1114 struct mdoc_node
*n
, *c
;
1116 if (MDOC_BLOCK
!= mdoc
->last
->type
)
1119 n
= mdoc
->last
->parent
->parent
;
1120 if (NULL
== n
->args
)
1121 return(merr(mdoc
, ELISTTYPE
));
1123 /* Some types require block-head, some not. */
1126 for (cols
= type
= -1, i
= 0; -1 == type
&&
1127 i
< (int)n
->args
->argc
; i
++)
1128 switch (n
->args
->argv
[i
].arg
) {
1148 type
= n
->args
->argv
[i
].arg
;
1151 type
= n
->args
->argv
[i
].arg
;
1152 cols
= (int)n
->args
->argv
[i
].sz
;
1159 return(merr(mdoc
, ELISTTYPE
));
1163 if (NULL
== mdoc
->last
->head
->child
)
1164 if ( ! mwarn(mdoc
, WLINE
))
1174 if (NULL
== mdoc
->last
->head
->child
)
1175 if ( ! mwarn(mdoc
, WLINE
))
1177 if (NULL
== mdoc
->last
->body
->child
)
1178 if ( ! mwarn(mdoc
, WMULTILINE
))
1190 if (mdoc
->last
->head
->child
)
1191 if ( ! mwarn(mdoc
, WNOLINE
))
1193 if (NULL
== mdoc
->last
->body
->child
)
1194 if ( ! mwarn(mdoc
, WMULTILINE
))
1198 if (NULL
== mdoc
->last
->head
->child
)
1199 if ( ! mwarn(mdoc
, WLINE
))
1201 if (mdoc
->last
->body
->child
)
1202 if ( ! mwarn(mdoc
, WNOMULTILINE
))
1204 c
= mdoc
->last
->child
;
1205 for (i
= 0; c
&& MDOC_HEAD
== c
->type
; c
= c
->next
)
1209 return(mdoc_err(mdoc
, "column mismatch (have "
1210 "%d, want %d)", i
, cols
));
1222 struct mdoc_node
*n
;
1224 if (MDOC_BODY
!= mdoc
->last
->type
)
1226 if (NULL
== mdoc
->last
->child
)
1230 for (n
= mdoc
->last
->child
; n
; n
= n
->next
) {
1231 if (MDOC_BLOCK
== n
->type
)
1232 if (MDOC_It
== n
->tok
)
1234 return(mdoc_nerr(mdoc
, n
, "bad child of parent %s",
1235 mdoc_macronames
[mdoc
->last
->tok
]));
1243 ebool(struct mdoc
*mdoc
)
1245 struct mdoc_node
*n
;
1248 for (n
= mdoc
->last
->child
; n
; n
= n
->next
) {
1249 if (MDOC_TEXT
!= n
->type
)
1251 if (xstrcmp(n
->string
, "on"))
1253 if (xstrcmp(n
->string
, "off"))
1260 return(nerr(mdoc
, n
, EBOOL
));
1265 post_root(POST_ARGS
)
1268 if (NULL
== mdoc
->first
->child
)
1269 return(merr(mdoc
, ENODATA
));
1270 if (SEC_PROLOGUE
== mdoc
->lastnamed
)
1271 return(merr(mdoc
, ENOPROLOGUE
));
1273 if (MDOC_BLOCK
!= mdoc
->first
->child
->type
)
1274 return(merr(mdoc
, ENODATA
));
1275 if (MDOC_Sh
!= mdoc
->first
->child
->tok
)
1276 return(merr(mdoc
, ENODATA
));
1286 if (mdoc_a2st(mdoc
->last
->child
->string
))
1288 return(mwarn(mdoc
, WBADSTAND
));
1296 if (MDOC_HEAD
== mdoc
->last
->type
)
1297 return(post_sh_head(mdoc
));
1298 if (MDOC_BODY
== mdoc
->last
->type
)
1299 return(post_sh_body(mdoc
));
1306 post_sh_body(POST_ARGS
)
1308 struct mdoc_node
*n
;
1310 if (SEC_NAME
!= mdoc
->lastnamed
)
1314 * Warn if the NAME section doesn't contain the `Nm' and `Nd'
1315 * macros (can have multiple `Nm' and one `Nd'). Note that the
1316 * children of the BODY declaration can also be "text".
1319 if (NULL
== (n
= mdoc
->last
->child
))
1320 return(mwarn(mdoc
, WNAMESECINC
));
1322 for ( ; n
&& n
->next
; n
= n
->next
) {
1323 if (MDOC_ELEM
== n
->type
&& MDOC_Nm
== n
->tok
)
1325 if (MDOC_TEXT
== n
->type
)
1327 if ( ! mwarn(mdoc
, WNAMESECINC
))
1331 if (MDOC_ELEM
== n
->type
&& MDOC_Nd
== n
->tok
)
1333 return(mwarn(mdoc
, WNAMESECINC
));
1338 post_sh_head(POST_ARGS
)
1344 * Process a new section. Sections are either "named" or
1345 * "custom"; custom sections are user-defined, while named ones
1346 * usually follow a conventional order and may only appear in
1347 * certain manual sections.
1350 assert(MDOC_Sh
== mdoc
->last
->tok
);
1352 (void)xstrlcpys(buf
, mdoc
->last
->child
, sizeof(buf
));
1354 sec
= mdoc_atosec(buf
);
1356 /* The NAME section should always be first. */
1358 if (SEC_BODY
== mdoc
->lastnamed
&& SEC_NAME
!= sec
)
1359 return(mwarn(mdoc
, WSECOOO
));
1360 if (SEC_CUSTOM
== sec
)
1363 /* Check for repeated or out-of-order sections. */
1365 if (sec
== mdoc
->lastnamed
)
1366 return(mwarn(mdoc
, WSECREP
));
1367 if (sec
< mdoc
->lastnamed
)
1368 return(mwarn(mdoc
, WSECOOO
));
1370 /* Check particular section/manual section conventions. */
1374 switch (mdoc
->meta
.msec
) {
1380 return(mwarn(mdoc
, WWRONGMSEC
));
1395 return(check_sec(mdoc
, n
, SEC_SYNOPSIS
, SEC_CUSTOM
));