]>
git.cameronkatri.com Git - mandoc.git/blob - validate.c
1 /* $Id: validate.c,v 1.73 2009/03/08 12:40:27 kristaps Exp $ */
3 * Copyright (c) 2008 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
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.
26 /* FIXME: .Bl -diag can't have non-text children in HEAD. */
27 /* TODO: ignoring Pp (it's superfluous in some invocations). */
30 * Pre- and post-validate macros as they're parsed. Pre-validation
31 * occurs when the macro has been detected and its arguments parsed.
32 * Post-validation occurs when all child macros have also been parsed.
33 * In the ELEMENT case, this is simply the parameters of the macro; in
34 * the BLOCK case, this is the HEAD, BODY, TAIL and so on.
37 #define PRE_ARGS struct mdoc *mdoc, const struct mdoc_node *n
38 #define POST_ARGS struct mdoc *mdoc
72 typedef int (*v_pre
)(PRE_ARGS
);
73 typedef int (*v_post
)(POST_ARGS
);
82 static int nwarn(struct mdoc
*,
83 const struct mdoc_node
*, enum mwarn
);
84 static int nerr(struct mdoc
*,
85 const struct mdoc_node
*, 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
*,
91 int, int, const char *);
92 static int check_argv(struct mdoc
*,
93 const struct mdoc_node
*,
94 const struct mdoc_argv
*);
95 static int check_args(struct mdoc
*,
96 const struct mdoc_node
*);
97 static int err_child_lt(struct mdoc
*, const char *, int);
98 static int warn_child_lt(struct mdoc
*, const char *, int);
99 static int err_child_gt(struct mdoc
*, const char *, int);
100 static int warn_child_gt(struct mdoc
*, const char *, int);
101 static int err_child_eq(struct mdoc
*, const char *, int);
102 static int warn_child_eq(struct mdoc
*, const char *, int);
103 static inline int count_child(struct mdoc
*);
104 static inline int warn_count(struct mdoc
*, const char *,
105 int, const char *, int);
106 static inline int err_count(struct mdoc
*, const char *,
107 int, const char *, int);
108 static int pre_an(PRE_ARGS
);
109 static int pre_bd(PRE_ARGS
);
110 static int pre_bl(PRE_ARGS
);
111 static int pre_cd(PRE_ARGS
);
112 static int pre_dd(PRE_ARGS
);
113 static int pre_display(PRE_ARGS
);
114 static int pre_dt(PRE_ARGS
);
115 static int pre_er(PRE_ARGS
);
116 static int pre_ex(PRE_ARGS
);
117 static int pre_fd(PRE_ARGS
);
118 static int pre_it(PRE_ARGS
);
119 static int pre_lb(PRE_ARGS
);
120 static int pre_os(PRE_ARGS
);
121 static int pre_prologue(PRE_ARGS
);
122 static int pre_rv(PRE_ARGS
);
123 static int pre_sh(PRE_ARGS
);
124 static int pre_ss(PRE_ARGS
);
125 static int herr_ge1(POST_ARGS
);
126 static int hwarn_le1(POST_ARGS
);
127 static int herr_eq0(POST_ARGS
);
128 static int eerr_eq0(POST_ARGS
);
129 static int eerr_le2(POST_ARGS
);
130 static int eerr_eq1(POST_ARGS
);
131 static int eerr_ge1(POST_ARGS
);
132 static int ewarn_eq0(POST_ARGS
);
133 static int ewarn_eq1(POST_ARGS
);
134 static int bwarn_ge1(POST_ARGS
);
135 static int hwarn_eq1(POST_ARGS
);
136 static int ewarn_ge1(POST_ARGS
);
137 static int ebool(POST_ARGS
);
138 static int post_an(POST_ARGS
);
139 static int post_at(POST_ARGS
);
140 static int post_bf(POST_ARGS
);
141 static int post_bl(POST_ARGS
);
142 static int post_ex(POST_ARGS
);
143 static int post_it(POST_ARGS
);
144 static int post_nm(POST_ARGS
);
145 static int post_root(POST_ARGS
);
146 static int post_sh(POST_ARGS
);
147 static int post_sh_body(POST_ARGS
);
148 static int post_sh_head(POST_ARGS
);
149 static int post_st(POST_ARGS
);
151 static v_pre pres_an
[] = { pre_an
, NULL
};
152 static v_pre pres_bd
[] = { pre_display
, pre_bd
, NULL
};
153 static v_pre pres_bl
[] = { pre_bl
, NULL
};
154 static v_pre pres_cd
[] = { pre_cd
, NULL
};
155 static v_pre pres_dd
[] = { pre_prologue
, pre_dd
, NULL
};
156 static v_pre pres_d1
[] = { pre_display
, NULL
};
157 static v_pre pres_dt
[] = { pre_prologue
, pre_dt
, NULL
};
158 static v_pre pres_er
[] = { pre_er
, NULL
};
159 static v_pre pres_ex
[] = { pre_ex
, NULL
};
160 static v_pre pres_fd
[] = { pre_fd
, NULL
};
161 static v_pre pres_it
[] = { pre_it
, NULL
};
162 static v_pre pres_lb
[] = { pre_lb
, NULL
};
163 static v_pre pres_os
[] = { pre_prologue
, pre_os
, NULL
};
164 static v_pre pres_rv
[] = { pre_rv
, NULL
};
165 static v_pre pres_sh
[] = { pre_sh
, NULL
};
166 static v_pre pres_ss
[] = { pre_ss
, NULL
};
167 static v_post posts_bool
[] = { eerr_eq1
, ebool
, NULL
};
168 static v_post posts_bd
[] = { herr_eq0
, bwarn_ge1
, NULL
};
169 static v_post posts_text
[] = { eerr_ge1
, NULL
};
170 static v_post posts_wtext
[] = { ewarn_ge1
, NULL
};
171 static v_post posts_notext
[] = { eerr_eq0
, NULL
};
172 static v_post posts_wline
[] = { bwarn_ge1
, herr_eq0
, NULL
};
173 static v_post posts_sh
[] = { herr_ge1
, bwarn_ge1
, post_sh
, NULL
};
174 static v_post posts_bl
[] = { herr_eq0
, bwarn_ge1
, post_bl
, NULL
};
175 static v_post posts_it
[] = { post_it
, NULL
};
176 static v_post posts_in
[] = { ewarn_eq1
, NULL
};
177 static v_post posts_ss
[] = { herr_ge1
, NULL
};
178 static v_post posts_pf
[] = { eerr_eq1
, NULL
};
179 static v_post posts_lb
[] = { eerr_eq1
, NULL
};
180 static v_post posts_st
[] = { eerr_eq1
, post_st
, NULL
};
181 static v_post posts_pp
[] = { ewarn_eq0
, NULL
};
182 static v_post posts_ex
[] = { eerr_eq0
, post_ex
, NULL
};
183 static v_post posts_an
[] = { post_an
, NULL
};
184 static v_post posts_at
[] = { post_at
, NULL
};
185 static v_post posts_xr
[] = { eerr_ge1
, eerr_le2
, NULL
};
186 static v_post posts_nm
[] = { post_nm
, NULL
};
187 static v_post posts_bf
[] = { hwarn_le1
, post_bf
, NULL
};
188 static v_post posts_rs
[] = { herr_eq0
, bwarn_ge1
, NULL
};
189 static v_post posts_fo
[] = { hwarn_eq1
, bwarn_ge1
, NULL
};
190 static v_post posts_bk
[] = { herr_eq0
, bwarn_ge1
, NULL
};
191 static v_post posts_fd
[] = { ewarn_ge1
, NULL
};
193 const struct valids mdoc_valids
[MDOC_MAX
] = {
194 { NULL
, NULL
}, /* \" */
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
, posts_text
}, /* Dv */
214 { pres_er
, posts_text
}, /* Er */
215 { NULL
, posts_text
}, /* Ev */
216 { pres_ex
, posts_ex
}, /* Ex */
217 { NULL
, posts_text
}, /* Fa */
218 { pres_fd
, posts_fd
}, /* 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
, posts_text
}, /* 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_notext
}, /* Rv */
231 { NULL
, posts_st
}, /* St */
232 { NULL
, posts_text
}, /* 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
, posts_text
}, /* 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_rs
}, /* 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_bk
}, /* 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 */
305 mdoc_valid_pre(struct mdoc
*mdoc
,
306 const struct mdoc_node
*node
)
312 if (MDOC_TEXT
== node
->type
) {
316 return(check_text(mdoc
, line
, pos
, tp
));
319 if ( ! check_args(mdoc
, node
))
321 if (NULL
== mdoc_valids
[node
->tok
].pre
)
323 for (p
= mdoc_valids
[node
->tok
].pre
; *p
; p
++)
324 if ( ! (*p
)(mdoc
, node
))
331 mdoc_valid_post(struct mdoc
*mdoc
)
336 * This check occurs after the macro's children have been filled
337 * in: postfix validation. Since this happens when we're
338 * rewinding the scope tree, it's possible to have multiple
339 * invocations (as by design, for now), we set bit MDOC_VALID to
340 * indicate that we've validated.
343 if (MDOC_VALID
& mdoc
->last
->flags
)
345 mdoc
->last
->flags
|= MDOC_VALID
;
347 if (MDOC_TEXT
== mdoc
->last
->type
)
349 if (MDOC_ROOT
== mdoc
->last
->type
)
350 return(post_root(mdoc
));
352 if (NULL
== mdoc_valids
[mdoc
->last
->tok
].post
)
354 for (p
= mdoc_valids
[mdoc
->last
->tok
].post
; *p
; p
++)
362 #define merr(m, t) nerr((m), (m)->last, (t))
364 nerr(struct mdoc
*m
, const struct mdoc_node
*n
, enum merr type
)
372 p
= "displays may not be nested";
375 p
= "expected boolean value";
378 p
= "argument repeated";
381 p
= "multiple display types specified";
384 p
= "multiple list types specified";
387 p
= "missing list type";
390 p
= "missing display type";
393 p
= "expected line arguments";
396 p
= "document has no prologue";
399 p
= "document has no data";
402 p
= "expected valid AT&T symbol";
405 p
= "default name not yet set";
410 return(mdoc_nerr(m
, n
, p
));
414 #define mwarn(m, t) nwarn((m), (m)->last, (t))
416 nwarn(struct mdoc
*m
, const struct mdoc_node
*n
, enum mwarn type
)
426 p
= "inappropriate manual section";
430 p
= "inappropriate document section";
434 p
= "argument value suggested";
438 p
= "prologue macros repeated";
442 p
= "prologue macros out-of-order";
446 p
= "suggested no line arguments";
449 p
= "suggested line arguments";
452 p
= "suggested multi-line arguments";
455 p
= "suggested no multi-line arguments";
458 p
= "document section in wrong manual section";
462 p
= "document section out of conventional order";
465 p
= "document section repeated";
468 p
= "unknown standard";
471 p
= "NAME section contents incomplete/badly-ordered";
475 return(mdoc_nwarn(m
, n
, c
, p
));
481 warn_count(struct mdoc
*m
, const char *k
,
482 int want
, const char *v
, int has
)
485 return(mdoc_warn(m
, WARN_SYNTAX
,
486 "suggests %s %s %d (has %d)", v
, k
, want
, has
));
491 err_count(struct mdoc
*m
, const char *k
,
492 int want
, const char *v
, int has
)
496 "requires %s %s %d (has %d)", v
, k
, want
, has
));
501 count_child(struct mdoc
*mdoc
)
506 for (i
= 0, n
= mdoc
->last
->child
; n
; n
= n
->next
, i
++)
514 * Build these up with macros because they're basically the same check
515 * for different inequalities. Yes, this could be done with functions,
516 * but this is reasonable for now.
519 #define CHECK_CHILD_DEFN(lvl, name, ineq) \
521 lvl##_child_##name(struct mdoc *mdoc, const char *p, int sz) \
524 if ((i = count_child(mdoc)) ineq sz) \
526 return(lvl##_count(mdoc, #ineq, sz, p, i)); \
529 #define CHECK_BODY_DEFN(name, lvl, func, num) \
531 b##lvl##_##name(POST_ARGS) \
533 if (MDOC_BODY != mdoc->last->type) \
535 return(func(mdoc, "multi-line arguments", (num))); \
538 #define CHECK_ELEM_DEFN(name, lvl, func, num) \
540 e##lvl##_##name(POST_ARGS) \
542 assert(MDOC_ELEM == mdoc->last->type); \
543 return(func(mdoc, "line arguments", (num))); \
546 #define CHECK_HEAD_DEFN(name, lvl, func, num) \
548 h##lvl##_##name(POST_ARGS) \
550 if (MDOC_HEAD != mdoc->last->type) \
552 return(func(mdoc, "line arguments", (num))); \
556 CHECK_CHILD_DEFN(warn
, gt
, >) /* warn_child_gt() */
557 CHECK_CHILD_DEFN(err
, gt
, >) /* err_child_gt() */
558 CHECK_CHILD_DEFN(warn
, eq
, ==) /* warn_child_eq() */
559 CHECK_CHILD_DEFN(err
, eq
, ==) /* err_child_eq() */
560 CHECK_CHILD_DEFN(err
, lt
, <) /* err_child_lt() */
561 CHECK_CHILD_DEFN(warn
, lt
, <) /* warn_child_lt() */
562 CHECK_BODY_DEFN(ge1
, warn
, warn_child_gt
, 0) /* bwarn_ge1() */
563 CHECK_ELEM_DEFN(eq1
, warn
, warn_child_eq
, 1) /* ewarn_eq1() */
564 CHECK_ELEM_DEFN(eq0
, warn
, warn_child_eq
, 0) /* ewarn_eq0() */
565 CHECK_ELEM_DEFN(ge1
, warn
, warn_child_gt
, 0) /* ewarn_gt1() */
566 CHECK_ELEM_DEFN(eq1
, err
, err_child_eq
, 1) /* eerr_eq1() */
567 CHECK_ELEM_DEFN(le2
, err
, err_child_lt
, 3) /* eerr_le2() */
568 CHECK_ELEM_DEFN(eq0
, err
, err_child_eq
, 0) /* eerr_eq0() */
569 CHECK_ELEM_DEFN(ge1
, err
, err_child_gt
, 0) /* eerr_ge1() */
570 CHECK_HEAD_DEFN(eq0
, err
, err_child_eq
, 0) /* herr_eq0() */
571 CHECK_HEAD_DEFN(le1
, warn
, warn_child_lt
, 2) /* hwarn_le1() */
572 CHECK_HEAD_DEFN(ge1
, err
, err_child_gt
, 0) /* herr_ge1() */
573 CHECK_HEAD_DEFN(eq1
, warn
, warn_child_eq
, 1) /* hwarn_eq1() */
577 check_stdarg(PRE_ARGS
)
580 if (n
->args
&& 1 == n
->args
->argc
)
581 if (MDOC_Std
== n
->args
->argv
[0].arg
)
583 return(nwarn(mdoc
, n
, WARGVAL
));
588 check_sec(PRE_ARGS
, ...)
597 sec
= (enum mdoc_sec
)va_arg(ap
, int);
598 if (SEC_CUSTOM
== sec
)
600 if (sec
!= mdoc
->lastsec
)
607 return(nwarn(mdoc
, n
, WBADSEC
));
612 check_msec(PRE_ARGS
, ...)
620 if (0 == (msec
= va_arg(ap
, int)))
622 if (msec
!= mdoc
->meta
.msec
)
629 return(nwarn(mdoc
, n
, WBADMSEC
));
634 check_args(struct mdoc
*m
, const struct mdoc_node
*n
)
641 assert(n
->args
->argc
);
642 for (i
= 0; i
< (int)n
->args
->argc
; i
++)
643 if ( ! check_argv(m
, n
, &n
->args
->argv
[i
]))
651 check_argv(struct mdoc
*m
, const struct mdoc_node
*n
,
652 const struct mdoc_argv
*v
)
656 for (i
= 0; i
< (int)v
->sz
; i
++)
657 if ( ! check_text(m
, v
->line
, v
->pos
, v
->value
[i
]))
660 if (MDOC_Std
== v
->arg
&& MDOC_Ex
== n
->tok
) {
661 /* `Nm' name must be set. */
662 if (v
->sz
|| m
->meta
.name
)
664 return(nerr(m
, n
, ENAME
));
672 check_text(struct mdoc
*mdoc
, int line
, int pos
, const char *p
)
676 /* XXX - indicate deprecated escapes \*(xx and \*x. */
679 if ( ! isprint((u_char
)*p
) && '\t' != *p
)
680 return(mdoc_perr(mdoc
, line
, pos
,
681 "invalid non-printing character"));
684 if ((c
= mdoc_isescape(p
))) {
688 return(mdoc_perr(mdoc
, line
, pos
, "invalid escape"));
698 check_parent(PRE_ARGS
, int tok
, enum mdoc_type t
)
702 if ((MDOC_ROOT
== t
|| tok
== n
->parent
->tok
) &&
703 (t
== n
->parent
->type
))
706 return(mdoc_nerr(mdoc
, n
, "require parent %s",
707 MDOC_ROOT
== t
? "<root>" : mdoc_macronames
[tok
]));
713 pre_display(PRE_ARGS
)
715 struct mdoc_node
*node
;
717 /* Display elements (`Bd', `D1'...) cannot be nested. */
719 if (MDOC_BLOCK
!= n
->type
)
723 for (node
= mdoc
->last
->parent
; node
; node
= node
->parent
)
724 if (MDOC_BLOCK
== node
->type
)
725 if (MDOC_Bd
== node
->tok
)
730 return(nerr(mdoc
, n
, ENESTDISP
));
737 int i
, type
, width
, offset
;
739 if (MDOC_BLOCK
!= n
->type
)
742 return(nerr(mdoc
, n
, ELISTTYPE
));
744 /* Make sure that only one type of list is specified. */
746 type
= offset
= width
= -1;
749 for (i
= 0; i
< (int)n
->args
->argc
; i
++)
750 switch (n
->args
->argv
[i
].arg
) {
773 type
= n
->args
->argv
[i
].arg
;
776 return(nerr(mdoc
, n
, EMULTILIST
));
779 width
= n
->args
->argv
[i
].arg
;
782 return(nerr(mdoc
, n
, EARGREP
));
785 offset
= n
->args
->argv
[i
].arg
;
788 return(nerr(mdoc
, n
, EARGREP
));
794 return(nerr(mdoc
, n
, ELISTTYPE
));
806 return(mdoc_nwarn(mdoc
, n
, WARN_SYNTAX
,
807 "superfluous %s argument",
808 mdoc_argnames
[MDOC_Width
]));
812 return(mdoc_nerr(mdoc
, n
, "missing %s argument",
813 mdoc_argnames
[MDOC_Width
]));
827 if (MDOC_BLOCK
!= n
->type
)
830 return(nerr(mdoc
, n
, EDISPTYPE
));
832 /* Make sure that only one type of display is specified. */
835 for (i
= 0, err
= type
= 0; ! err
&&
836 i
< (int)n
->args
->argc
; i
++)
837 switch (n
->args
->argv
[i
].arg
) {
840 case (MDOC_Unfilled
):
849 return(nerr(mdoc
, n
, EMULTIDISP
));
856 return(nerr(mdoc
, n
, EDISPTYPE
));
864 if (MDOC_BLOCK
!= n
->type
)
866 return(check_parent(mdoc
, n
, MDOC_Sh
, MDOC_BODY
));
874 if (MDOC_BLOCK
!= n
->type
)
876 return(check_parent(mdoc
, n
, -1, MDOC_ROOT
));
884 if (MDOC_BLOCK
!= n
->type
)
886 return(check_parent(mdoc
, n
, MDOC_Bl
, MDOC_BODY
));
894 if (NULL
== n
->args
|| 1 == n
->args
->argc
)
896 return(mdoc_nerr(mdoc
, n
, "only one argument allowed"));
904 return(check_sec(mdoc
, n
, SEC_LIBRARY
, SEC_CUSTOM
));
912 if ( ! check_msec(mdoc
, n
, 2, 3, 0))
914 return(check_stdarg(mdoc
, n
));
922 if ( ! check_msec(mdoc
, n
, 1, 6, 8, 0))
924 return(check_stdarg(mdoc
, n
));
932 return(check_msec(mdoc
, n
, 2, 0));
940 return(check_msec(mdoc
, n
, 4, 0));
945 pre_prologue(PRE_ARGS
)
948 return(check_sec(mdoc
, n
, SEC_PROLOGUE
, SEC_CUSTOM
));
956 if (0 == mdoc
->meta
.date
|| mdoc
->meta
.os
)
957 if ( ! nwarn(mdoc
, n
, WPROLOOO
))
959 if (mdoc
->meta
.title
)
960 if ( ! nwarn(mdoc
, n
, WPROLREP
))
970 if (NULL
== mdoc
->meta
.title
|| 0 == mdoc
->meta
.date
)
971 if ( ! nwarn(mdoc
, n
, WPROLOOO
))
974 if ( ! nwarn(mdoc
, n
, WPROLREP
))
984 if (mdoc
->meta
.title
|| mdoc
->meta
.os
)
985 if ( ! nwarn(mdoc
, n
, WPROLOOO
))
988 if ( ! nwarn(mdoc
, n
, WPROLREP
))
998 struct mdoc_node
*head
;
1000 if (MDOC_BLOCK
!= mdoc
->last
->type
)
1003 head
= mdoc
->last
->head
;
1005 if (NULL
== mdoc
->last
->args
) {
1006 if (NULL
== head
->child
||
1007 MDOC_TEXT
!= head
->child
->type
)
1008 return(mdoc_err(mdoc
, "text argument expected"));
1010 p
= head
->child
->string
;
1011 if (xstrcmp(p
, "Em"))
1013 else if (xstrcmp(p
, "Li"))
1015 else if (xstrcmp(p
, "Sm"))
1017 return(mdoc_nerr(mdoc
, head
->child
, "invalid font"));
1021 return(mdoc_err(mdoc
, "one argument expected"));
1031 if (mdoc
->last
->child
)
1033 if (mdoc
->meta
.name
)
1035 return(merr(mdoc
, ENAME
));
1043 if (NULL
== mdoc
->last
->child
)
1045 if (MDOC_TEXT
!= mdoc
->last
->child
->type
)
1046 return(merr(mdoc
, EATT
));
1047 if (mdoc_a2att(mdoc
->last
->child
->string
))
1049 return(merr(mdoc
, EATT
));
1057 if (mdoc
->last
->args
) {
1058 if (NULL
== mdoc
->last
->child
)
1060 return(merr(mdoc
, ELINE
));
1063 if (mdoc
->last
->child
)
1065 return(merr(mdoc
, ELINE
));
1073 if (mdoc
->last
->args
)
1075 return(merr(mdoc
, ELINE
));
1083 struct mdoc_node
*n
, *c
;
1085 if (MDOC_BLOCK
!= mdoc
->last
->type
)
1088 n
= mdoc
->last
->parent
->parent
;
1089 if (NULL
== n
->args
)
1090 return(merr(mdoc
, ELISTTYPE
));
1092 /* Some types require block-head, some not. */
1095 for (cols
= type
= -1, i
= 0; -1 == type
&&
1096 i
< (int)n
->args
->argc
; i
++)
1097 switch (n
->args
->argv
[i
].arg
) {
1117 type
= n
->args
->argv
[i
].arg
;
1120 type
= n
->args
->argv
[i
].arg
;
1121 cols
= (int)n
->args
->argv
[i
].sz
;
1128 return(merr(mdoc
, ELISTTYPE
));
1132 if (NULL
== mdoc
->last
->head
->child
)
1133 if ( ! mwarn(mdoc
, WLINE
))
1143 if (NULL
== mdoc
->last
->head
->child
)
1144 if ( ! mwarn(mdoc
, WLINE
))
1146 if (NULL
== mdoc
->last
->body
->child
)
1147 if ( ! mwarn(mdoc
, WMULTILINE
))
1159 if (mdoc
->last
->head
->child
)
1160 if ( ! mwarn(mdoc
, WNOLINE
))
1162 if (NULL
== mdoc
->last
->body
->child
)
1163 if ( ! mwarn(mdoc
, WMULTILINE
))
1167 if (NULL
== mdoc
->last
->head
->child
)
1168 if ( ! mwarn(mdoc
, WLINE
))
1170 if (mdoc
->last
->body
->child
)
1171 if ( ! mwarn(mdoc
, WNOMULTILINE
))
1173 c
= mdoc
->last
->head
->child
;
1174 for (i
= 0; c
; c
= c
->next
)
1178 if ( ! mdoc_warn(mdoc
, WARN_SYNTAX
,
1179 "column mismatch (have %d, want %d)", i
, cols
))
1193 struct mdoc_node
*n
;
1195 if (MDOC_BODY
!= mdoc
->last
->type
)
1197 if (NULL
== mdoc
->last
->child
)
1201 for (n
= mdoc
->last
->child
; n
; n
= n
->next
) {
1202 if (MDOC_BLOCK
== n
->type
)
1203 if (MDOC_It
== n
->tok
)
1205 return(mdoc_nerr(mdoc
, n
, "bad child of parent %s",
1206 mdoc_macronames
[mdoc
->last
->tok
]));
1214 ebool(struct mdoc
*mdoc
)
1216 struct mdoc_node
*n
;
1219 for (n
= mdoc
->last
->child
; n
; n
= n
->next
) {
1220 if (MDOC_TEXT
!= n
->type
)
1222 if (xstrcmp(n
->string
, "on"))
1224 if (xstrcmp(n
->string
, "off"))
1231 return(nerr(mdoc
, n
, EBOOL
));
1236 post_root(POST_ARGS
)
1239 if (NULL
== mdoc
->first
->child
)
1240 return(merr(mdoc
, ENODATA
));
1241 if (SEC_PROLOGUE
== mdoc
->lastnamed
)
1242 return(merr(mdoc
, ENOPROLOGUE
));
1244 if (MDOC_BLOCK
!= mdoc
->first
->child
->type
)
1245 return(merr(mdoc
, ENODATA
));
1246 if (MDOC_Sh
!= mdoc
->first
->child
->tok
)
1247 return(merr(mdoc
, ENODATA
));
1257 if (mdoc_a2st(mdoc
->last
->child
->string
))
1259 return(mwarn(mdoc
, WBADSTAND
));
1267 if (MDOC_HEAD
== mdoc
->last
->type
)
1268 return(post_sh_head(mdoc
));
1269 if (MDOC_BODY
== mdoc
->last
->type
)
1270 return(post_sh_body(mdoc
));
1277 post_sh_body(POST_ARGS
)
1279 struct mdoc_node
*n
;
1281 if (SEC_NAME
!= mdoc
->lastnamed
)
1285 * Warn if the NAME section doesn't contain the `Nm' and `Nd'
1286 * macros (can have multiple `Nm' and one `Nd'). Note that the
1287 * children of the BODY declaration can also be "text".
1290 if (NULL
== (n
= mdoc
->last
->child
))
1291 return(mwarn(mdoc
, WNAMESECINC
));
1293 for ( ; n
&& n
->next
; n
= n
->next
) {
1294 if (MDOC_ELEM
== n
->type
&& MDOC_Nm
== n
->tok
)
1296 if (MDOC_TEXT
== n
->type
)
1298 if ( ! mwarn(mdoc
, WNAMESECINC
))
1302 if (MDOC_ELEM
== n
->type
&& MDOC_Nd
== n
->tok
)
1304 return(mwarn(mdoc
, WNAMESECINC
));
1309 post_sh_head(POST_ARGS
)
1315 * Process a new section. Sections are either "named" or
1316 * "custom"; custom sections are user-defined, while named ones
1317 * usually follow a conventional order and may only appear in
1318 * certain manual sections.
1321 assert(MDOC_Sh
== mdoc
->last
->tok
);
1323 (void)xstrlcpys(buf
, mdoc
->last
->child
, sizeof(buf
));
1325 sec
= mdoc_atosec(buf
);
1327 /* The NAME section should always be first. */
1329 if (SEC_BODY
== mdoc
->lastnamed
&& SEC_NAME
!= sec
)
1330 return(mwarn(mdoc
, WSECOOO
));
1331 if (SEC_CUSTOM
== sec
)
1334 /* Check for repeated or out-of-order sections. */
1336 if (sec
== mdoc
->lastnamed
)
1337 return(mwarn(mdoc
, WSECREP
));
1338 if (sec
< mdoc
->lastnamed
)
1339 return(mwarn(mdoc
, WSECOOO
));
1341 /* Check particular section/manual section conventions. */
1345 switch (mdoc
->meta
.msec
) {
1351 return(mwarn(mdoc
, WWRONGMSEC
));
1366 return(check_sec(mdoc
, n
, SEC_SYNOPSIS
, SEC_CUSTOM
));