]>
git.cameronkatri.com Git - mandoc.git/blob - mdoc_validate.c
3111f8642dffc7c04eb5c4c359a886875eb65c5b
1 /* $Id: mdoc_validate.c,v 1.14 2009/06/17 14:08:47 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 count_child(struct mdoc
*);
103 static int warn_print(struct mdoc
*, int, int);
104 static int warn_count(struct mdoc
*, const char *,
105 int, const char *, int);
106 static 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_rv(PRE_ARGS
);
122 static int pre_sh(PRE_ARGS
);
123 static int pre_ss(PRE_ARGS
);
124 static int herr_ge1(POST_ARGS
);
125 static int hwarn_le1(POST_ARGS
);
126 static int herr_eq0(POST_ARGS
);
127 static int eerr_eq0(POST_ARGS
);
128 static int eerr_le2(POST_ARGS
);
129 static int eerr_eq1(POST_ARGS
);
130 static int eerr_ge1(POST_ARGS
);
131 static int ewarn_eq0(POST_ARGS
);
132 static int ewarn_eq1(POST_ARGS
);
133 static int bwarn_ge1(POST_ARGS
);
134 static int hwarn_eq1(POST_ARGS
);
135 static int ewarn_ge1(POST_ARGS
);
136 static int ebool(POST_ARGS
);
137 static int post_an(POST_ARGS
);
138 static int post_args(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_bl_head(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 #define vwarn(m, t) nwarn((m), (m)->last, (t))
152 #define verr(m, t) nerr((m), (m)->last, (t))
153 #define nwarn(m, n, t) pwarn((m), (n)->line, (n)->pos, (t))
154 #define nerr(m, n, t) perr((m), (n)->line, (n)->pos, (t))
156 static v_pre pres_an
[] = { pre_an
, NULL
};
157 static v_pre pres_bd
[] = { pre_display
, pre_bd
, NULL
};
158 static v_pre pres_bl
[] = { pre_bl
, NULL
};
159 static v_pre pres_cd
[] = { pre_cd
, NULL
};
160 static v_pre pres_dd
[] = { pre_dd
, NULL
};
161 static v_pre pres_d1
[] = { pre_display
, NULL
};
162 static v_pre pres_dt
[] = { pre_dt
, NULL
};
163 static v_pre pres_er
[] = { pre_er
, NULL
};
164 static v_pre pres_ex
[] = { pre_ex
, NULL
};
165 static v_pre pres_fd
[] = { pre_fd
, NULL
};
166 static v_pre pres_it
[] = { pre_it
, NULL
};
167 static v_pre pres_lb
[] = { pre_lb
, NULL
};
168 static v_pre pres_os
[] = { pre_os
, NULL
};
169 static v_pre pres_rv
[] = { pre_rv
, NULL
};
170 static v_pre pres_sh
[] = { pre_sh
, NULL
};
171 static v_pre pres_ss
[] = { pre_ss
, NULL
};
172 static v_post posts_bool
[] = { eerr_eq1
, ebool
, NULL
};
173 static v_post posts_bd
[] = { herr_eq0
, bwarn_ge1
, NULL
};
174 static v_post posts_text
[] = { eerr_ge1
, NULL
};
175 static v_post posts_wtext
[] = { ewarn_ge1
, NULL
};
176 static v_post posts_notext
[] = { eerr_eq0
, NULL
};
177 static v_post posts_wline
[] = { bwarn_ge1
, herr_eq0
, NULL
};
178 static v_post posts_sh
[] = { herr_ge1
, bwarn_ge1
, post_sh
, NULL
};
179 static v_post posts_bl
[] = { bwarn_ge1
, post_bl
, NULL
};
180 static v_post posts_it
[] = { post_it
, NULL
};
181 static v_post posts_in
[] = { ewarn_eq1
, NULL
};
182 static v_post posts_ss
[] = { herr_ge1
, NULL
};
183 static v_post posts_pf
[] = { eerr_eq1
, NULL
};
184 static v_post posts_lb
[] = { eerr_eq1
, NULL
};
185 static v_post posts_st
[] = { eerr_eq1
, post_st
, NULL
};
186 static v_post posts_pp
[] = { ewarn_eq0
, NULL
};
187 static v_post posts_ex
[] = { eerr_eq0
, post_args
, NULL
};
188 static v_post posts_rv
[] = { eerr_eq0
, post_args
, NULL
};
189 static v_post posts_an
[] = { post_an
, NULL
};
190 static v_post posts_at
[] = { post_at
, NULL
};
191 static v_post posts_xr
[] = { eerr_ge1
, eerr_le2
, NULL
};
192 static v_post posts_nm
[] = { post_nm
, NULL
};
193 static v_post posts_bf
[] = { hwarn_le1
, post_bf
, NULL
};
194 static v_post posts_fo
[] = { hwarn_eq1
, bwarn_ge1
, NULL
};
196 const struct valids mdoc_valids
[MDOC_MAX
] = {
197 { NULL
, NULL
}, /* Ap */
198 { pres_dd
, posts_text
}, /* Dd */
199 { pres_dt
, NULL
}, /* Dt */
200 { pres_os
, NULL
}, /* Os */
201 { pres_sh
, posts_sh
}, /* Sh */
202 { pres_ss
, posts_ss
}, /* Ss */
203 { NULL
, posts_pp
}, /* Pp */
204 { pres_d1
, posts_wline
}, /* D1 */
205 { pres_d1
, posts_wline
}, /* Dl */
206 { pres_bd
, posts_bd
}, /* Bd */
207 { NULL
, NULL
}, /* Ed */
208 { pres_bl
, posts_bl
}, /* Bl */
209 { NULL
, NULL
}, /* El */
210 { pres_it
, posts_it
}, /* It */
211 { NULL
, posts_text
}, /* Ad */
212 { pres_an
, posts_an
}, /* An */
213 { NULL
, NULL
}, /* Ar */
214 { pres_cd
, posts_text
}, /* Cd */
215 { NULL
, NULL
}, /* Cm */
216 { NULL
, NULL
}, /* Dv */
217 { pres_er
, posts_text
}, /* Er */
218 { NULL
, NULL
}, /* Ev */
219 { pres_ex
, posts_ex
}, /* Ex */
220 { NULL
, NULL
}, /* Fa */
221 { pres_fd
, posts_wtext
}, /* Fd */
222 { NULL
, NULL
}, /* Fl */
223 { NULL
, posts_text
}, /* Fn */
224 { NULL
, posts_wtext
}, /* Ft */
225 { NULL
, posts_text
}, /* Ic */
226 { NULL
, posts_in
}, /* In */
227 { NULL
, NULL
}, /* Li */
228 { NULL
, posts_wtext
}, /* Nd */
229 { NULL
, posts_nm
}, /* Nm */
230 { NULL
, posts_wline
}, /* Op */
231 { NULL
, NULL
}, /* Ot */
232 { NULL
, NULL
}, /* Pa */
233 { pres_rv
, posts_rv
}, /* Rv */
234 { NULL
, posts_st
}, /* St */
235 { NULL
, NULL
}, /* Va */
236 { NULL
, posts_text
}, /* Vt */
237 { NULL
, posts_xr
}, /* Xr */
238 { NULL
, posts_text
}, /* %A */
239 { NULL
, posts_text
}, /* %B */
240 { NULL
, posts_text
}, /* %D */
241 { NULL
, posts_text
}, /* %I */
242 { NULL
, posts_text
}, /* %J */
243 { NULL
, posts_text
}, /* %N */
244 { NULL
, posts_text
}, /* %O */
245 { NULL
, posts_text
}, /* %P */
246 { NULL
, posts_text
}, /* %R */
247 { NULL
, posts_text
}, /* %T */
248 { NULL
, posts_text
}, /* %V */
249 { NULL
, NULL
}, /* Ac */
250 { NULL
, NULL
}, /* Ao */
251 { NULL
, posts_wline
}, /* Aq */
252 { NULL
, posts_at
}, /* At */
253 { NULL
, NULL
}, /* Bc */
254 { NULL
, posts_bf
}, /* Bf */
255 { NULL
, NULL
}, /* Bo */
256 { NULL
, posts_wline
}, /* Bq */
257 { NULL
, NULL
}, /* Bsx */
258 { NULL
, NULL
}, /* Bx */
259 { NULL
, posts_bool
}, /* Db */
260 { NULL
, NULL
}, /* Dc */
261 { NULL
, NULL
}, /* Do */
262 { NULL
, posts_wline
}, /* Dq */
263 { NULL
, NULL
}, /* Ec */
264 { NULL
, NULL
}, /* Ef */
265 { NULL
, NULL
}, /* Em */
266 { NULL
, NULL
}, /* Eo */
267 { NULL
, NULL
}, /* Fx */
268 { NULL
, posts_text
}, /* Ms */
269 { NULL
, posts_notext
}, /* No */
270 { NULL
, posts_notext
}, /* Ns */
271 { NULL
, NULL
}, /* Nx */
272 { NULL
, NULL
}, /* Ox */
273 { NULL
, NULL
}, /* Pc */
274 { NULL
, posts_pf
}, /* Pf */
275 { NULL
, NULL
}, /* Po */
276 { NULL
, posts_wline
}, /* Pq */
277 { NULL
, NULL
}, /* Qc */
278 { NULL
, posts_wline
}, /* Ql */
279 { NULL
, NULL
}, /* Qo */
280 { NULL
, posts_wline
}, /* Qq */
281 { NULL
, NULL
}, /* Re */
282 { NULL
, posts_wline
}, /* Rs */
283 { NULL
, NULL
}, /* Sc */
284 { NULL
, NULL
}, /* So */
285 { NULL
, posts_wline
}, /* Sq */
286 { NULL
, posts_bool
}, /* Sm */
287 { NULL
, posts_text
}, /* Sx */
288 { NULL
, posts_text
}, /* Sy */
289 { NULL
, posts_text
}, /* Tn */
290 { NULL
, NULL
}, /* Ux */
291 { NULL
, NULL
}, /* Xc */
292 { NULL
, NULL
}, /* Xo */
293 { NULL
, posts_fo
}, /* Fo */
294 { NULL
, NULL
}, /* Fc */
295 { NULL
, NULL
}, /* Oo */
296 { NULL
, NULL
}, /* Oc */
297 { NULL
, posts_wline
}, /* Bk */
298 { NULL
, NULL
}, /* Ek */
299 { NULL
, posts_notext
}, /* Bt */
300 { NULL
, NULL
}, /* Hf */
301 { NULL
, NULL
}, /* Fr */
302 { NULL
, posts_notext
}, /* Ud */
303 { pres_lb
, posts_lb
}, /* Lb */
304 { NULL
, posts_pp
}, /* Lp */
305 { NULL
, NULL
}, /* Lk */
306 { NULL
, posts_text
}, /* Mt */
307 { NULL
, posts_wline
}, /* Brq */
308 { NULL
, NULL
}, /* Bro */
309 { NULL
, NULL
}, /* Brc */
310 { NULL
, posts_text
}, /* %C */
311 { NULL
, NULL
}, /* Es */
312 { NULL
, NULL
}, /* En */
313 { NULL
, NULL
}, /* Dx */
314 { NULL
, posts_text
}, /* %Q */
319 extern size_t strlcat(char *, const char *, size_t);
324 mdoc_valid_pre(struct mdoc
*mdoc
,
325 const struct mdoc_node
*n
)
331 if (MDOC_TEXT
== n
->type
) {
335 return(check_text(mdoc
, line
, pos
, tp
));
338 if ( ! check_args(mdoc
, n
))
340 if (NULL
== mdoc_valids
[n
->tok
].pre
)
342 for (p
= mdoc_valids
[n
->tok
].pre
; *p
; p
++)
343 if ( ! (*p
)(mdoc
, n
))
350 mdoc_valid_post(struct mdoc
*mdoc
)
355 * This check occurs after the macro's children have been filled
356 * in: postfix validation. Since this happens when we're
357 * rewinding the scope tree, it's possible to have multiple
358 * invocations (as by design, for now), we set bit MDOC_VALID to
359 * indicate that we've validated.
362 if (MDOC_VALID
& mdoc
->last
->flags
)
364 mdoc
->last
->flags
|= MDOC_VALID
;
366 if (MDOC_TEXT
== mdoc
->last
->type
)
368 if (MDOC_ROOT
== mdoc
->last
->type
)
369 return(post_root(mdoc
));
371 if (NULL
== mdoc_valids
[mdoc
->last
->tok
].post
)
373 for (p
= mdoc_valids
[mdoc
->last
->tok
].post
; *p
; p
++)
382 perr(struct mdoc
*m
, int line
, int pos
, enum merr type
)
389 p
= "text argument too long";
392 p
= "invalid escape sequence";
395 p
= "invalid character";
398 p
= "displays may not be nested";
401 p
= "expected boolean value";
404 p
= "argument repeated";
407 p
= "multiple display types specified";
410 p
= "multiple list types specified";
413 p
= "missing list type";
416 p
= "missing display type";
419 p
= "the NAME section must come first";
422 p
= "expected line arguments";
425 p
= "document has no prologue";
428 p
= "document has no data";
431 p
= "column syntax style mismatch";
434 p
= "expected valid AT&T symbol";
437 p
= "default name not yet set";
441 return(mdoc_perr(m
, line
, pos
, p
));
446 pwarn(struct mdoc
*m
, int line
, int pos
, enum mwarn type
)
455 p
= "inappropriate manual section";
459 p
= "inappropriate document section";
463 p
= "argument value suggested";
467 p
= "prologue macros repeated";
471 p
= "prologue macros out-of-order";
475 p
= "deprecated column argument syntax";
479 p
= "superfluous width argument";
482 p
= "missing width argument";
485 p
= "invalid character";
488 p
= "invalid escape sequence";
491 p
= "deprecated special-character escape";
494 p
= "suggested no line arguments";
497 p
= "suggested line arguments";
500 p
= "suggested multi-line arguments";
503 p
= "suggested no multi-line arguments";
506 p
= "document section in wrong manual section";
510 p
= "document section out of conventional order";
513 p
= "document section repeated";
516 p
= "unknown standard";
519 p
= "NAME section contents incomplete/badly-ordered";
523 return(mdoc_pwarn(m
, line
, pos
, c
, p
));
528 warn_print(struct mdoc
*m
, int ln
, int pos
)
530 if (MDOC_IGN_CHARS
& m
->pflags
)
531 return(pwarn(m
, ln
, pos
, WPRINT
));
532 return(perr(m
, ln
, pos
, EPRINT
));
537 warn_count(struct mdoc
*m
, const char *k
,
538 int want
, const char *v
, int has
)
541 return(mdoc_warn(m
, WARN_SYNTAX
,
542 "suggests %s %s %d (has %d)", v
, k
, want
, has
));
547 err_count(struct mdoc
*m
, const char *k
,
548 int want
, const char *v
, int has
)
552 "requires %s %s %d (has %d)", v
, k
, want
, has
));
557 count_child(struct mdoc
*mdoc
)
562 for (i
= 0, n
= mdoc
->last
->child
; n
; n
= n
->next
, i
++)
570 * Build these up with macros because they're basically the same check
571 * for different inequalities. Yes, this could be done with functions,
572 * but this is reasonable for now.
575 #define CHECK_CHILD_DEFN(lvl, name, ineq) \
577 lvl##_child_##name(struct mdoc *mdoc, const char *p, int sz) \
580 if ((i = count_child(mdoc)) ineq sz) \
582 return(lvl##_count(mdoc, #ineq, sz, p, i)); \
585 #define CHECK_BODY_DEFN(name, lvl, func, num) \
587 b##lvl##_##name(POST_ARGS) \
589 if (MDOC_BODY != mdoc->last->type) \
591 return(func(mdoc, "multi-line arguments", (num))); \
594 #define CHECK_ELEM_DEFN(name, lvl, func, num) \
596 e##lvl##_##name(POST_ARGS) \
598 assert(MDOC_ELEM == mdoc->last->type); \
599 return(func(mdoc, "line arguments", (num))); \
602 #define CHECK_HEAD_DEFN(name, lvl, func, num) \
604 h##lvl##_##name(POST_ARGS) \
606 if (MDOC_HEAD != mdoc->last->type) \
608 return(func(mdoc, "line arguments", (num))); \
612 CHECK_CHILD_DEFN(warn
, gt
, >) /* warn_child_gt() */
613 CHECK_CHILD_DEFN(err
, gt
, >) /* err_child_gt() */
614 CHECK_CHILD_DEFN(warn
, eq
, ==) /* warn_child_eq() */
615 CHECK_CHILD_DEFN(err
, eq
, ==) /* err_child_eq() */
616 CHECK_CHILD_DEFN(err
, lt
, <) /* err_child_lt() */
617 CHECK_CHILD_DEFN(warn
, lt
, <) /* warn_child_lt() */
618 CHECK_BODY_DEFN(ge1
, warn
, warn_child_gt
, 0) /* bwarn_ge1() */
619 CHECK_ELEM_DEFN(eq1
, warn
, warn_child_eq
, 1) /* ewarn_eq1() */
620 CHECK_ELEM_DEFN(eq0
, warn
, warn_child_eq
, 0) /* ewarn_eq0() */
621 CHECK_ELEM_DEFN(ge1
, warn
, warn_child_gt
, 0) /* ewarn_gt1() */
622 CHECK_ELEM_DEFN(eq1
, err
, err_child_eq
, 1) /* eerr_eq1() */
623 CHECK_ELEM_DEFN(le2
, err
, err_child_lt
, 3) /* eerr_le2() */
624 CHECK_ELEM_DEFN(eq0
, err
, err_child_eq
, 0) /* eerr_eq0() */
625 CHECK_ELEM_DEFN(ge1
, err
, err_child_gt
, 0) /* eerr_ge1() */
626 CHECK_HEAD_DEFN(eq0
, err
, err_child_eq
, 0) /* herr_eq0() */
627 CHECK_HEAD_DEFN(le1
, warn
, warn_child_lt
, 2) /* hwarn_le1() */
628 CHECK_HEAD_DEFN(ge1
, err
, err_child_gt
, 0) /* herr_ge1() */
629 CHECK_HEAD_DEFN(eq1
, warn
, warn_child_eq
, 1) /* hwarn_eq1() */
633 check_stdarg(PRE_ARGS
)
636 if (n
->args
&& 1 == n
->args
->argc
)
637 if (MDOC_Std
== n
->args
->argv
[0].arg
)
639 return(nwarn(mdoc
, n
, WARGVAL
));
644 check_sec(PRE_ARGS
, ...)
653 sec
= (enum mdoc_sec
)va_arg(ap
, int);
654 if (SEC_CUSTOM
== sec
)
656 if (sec
!= mdoc
->lastsec
)
663 return(nwarn(mdoc
, n
, WBADSEC
));
668 check_msec(PRE_ARGS
, ...)
676 if (0 == (msec
= va_arg(ap
, int)))
678 if (msec
!= mdoc
->meta
.msec
)
685 return(nwarn(mdoc
, n
, WBADMSEC
));
690 check_args(struct mdoc
*m
, const struct mdoc_node
*n
)
697 assert(n
->args
->argc
);
698 for (i
= 0; i
< (int)n
->args
->argc
; i
++)
699 if ( ! check_argv(m
, n
, &n
->args
->argv
[i
]))
707 check_argv(struct mdoc
*m
, const struct mdoc_node
*n
,
708 const struct mdoc_argv
*v
)
712 for (i
= 0; i
< (int)v
->sz
; i
++)
713 if ( ! check_text(m
, v
->line
, v
->pos
, v
->value
[i
]))
716 if (MDOC_Std
== v
->arg
) {
717 /* `Nm' name must be set. */
718 if (v
->sz
|| m
->meta
.name
)
720 return(nerr(m
, n
, ENAME
));
728 check_text(struct mdoc
*mdoc
, int line
, int pos
, const char *p
)
734 if ( ! (MDOC_LITERAL
& mdoc
->flags
))
735 if ( ! warn_print(mdoc
, line
, pos
))
737 } else if ( ! isprint((u_char
)*p
))
738 if ( ! warn_print(mdoc
, line
, pos
))
744 c
= mdoc_isescape(p
);
746 /* See if form is deprecated. */
748 if ( ! pwarn(mdoc
, line
, pos
, WDEPESC
))
753 if ( ! (MDOC_IGN_ESCAPE
& mdoc
->pflags
))
754 return(perr(mdoc
, line
, pos
, EESCAPE
));
755 if ( ! pwarn(mdoc
, line
, pos
, WESCAPE
))
766 check_parent(PRE_ARGS
, int tok
, enum mdoc_type t
)
770 if ((MDOC_ROOT
== t
|| tok
== n
->parent
->tok
) &&
771 (t
== n
->parent
->type
))
774 return(mdoc_nerr(mdoc
, n
, "require parent %s",
775 MDOC_ROOT
== t
? "<root>" : mdoc_macronames
[tok
]));
781 pre_display(PRE_ARGS
)
783 struct mdoc_node
*node
;
785 /* Display elements (`Bd', `D1'...) cannot be nested. */
787 if (MDOC_BLOCK
!= n
->type
)
791 for (node
= mdoc
->last
->parent
; node
; node
= node
->parent
)
792 if (MDOC_BLOCK
== node
->type
)
793 if (MDOC_Bd
== node
->tok
)
798 return(nerr(mdoc
, n
, ENESTDISP
));
805 int pos
, col
, type
, width
, offset
;
807 if (MDOC_BLOCK
!= n
->type
)
810 return(nerr(mdoc
, n
, ELISTTYPE
));
812 /* Make sure that only one type of list is specified. */
814 type
= offset
= width
= col
= -1;
817 for (pos
= 0; pos
< (int)n
->args
->argc
; pos
++)
818 switch (n
->args
->argv
[pos
].arg
) {
841 return(nerr(mdoc
, n
, EMULTILIST
));
842 type
= n
->args
->argv
[pos
].arg
;
847 return(nerr(mdoc
, n
, EARGREP
));
848 width
= n
->args
->argv
[pos
].arg
;
852 return(nerr(mdoc
, n
, EARGREP
));
853 offset
= n
->args
->argv
[pos
].arg
;
860 return(nerr(mdoc
, n
, ELISTTYPE
));
863 * Validate the width field. Some list types don't need width
864 * types and should be warned about them. Others should have it
865 * and must also be warned.
870 if (-1 == width
&& ! nwarn(mdoc
, n
, WMISSWIDTH
))
880 if (-1 != width
&& ! nwarn(mdoc
, n
, WNOWIDTH
))
896 if (MDOC_BLOCK
!= n
->type
)
899 return(nerr(mdoc
, n
, EDISPTYPE
));
901 /* Make sure that only one type of display is specified. */
904 for (i
= 0, err
= type
= 0; ! err
&&
905 i
< (int)n
->args
->argc
; i
++)
906 switch (n
->args
->argv
[i
].arg
) {
909 case (MDOC_Unfilled
):
918 return(nerr(mdoc
, n
, EMULTIDISP
));
925 return(nerr(mdoc
, n
, EDISPTYPE
));
933 if (MDOC_BLOCK
!= n
->type
)
935 return(check_parent(mdoc
, n
, MDOC_Sh
, MDOC_BODY
));
943 if (MDOC_BLOCK
!= n
->type
)
945 return(check_parent(mdoc
, n
, -1, MDOC_ROOT
));
953 if (MDOC_BLOCK
!= n
->type
)
955 return(check_parent(mdoc
, n
, MDOC_Bl
, MDOC_BODY
));
963 if (NULL
== n
->args
|| 1 == n
->args
->argc
)
965 return(mdoc_nerr(mdoc
, n
, "only one argument allowed"));
973 return(check_sec(mdoc
, n
, SEC_LIBRARY
, SEC_CUSTOM
));
981 if ( ! check_msec(mdoc
, n
, 2, 3, 0))
983 return(check_stdarg(mdoc
, n
));
991 if ( ! check_msec(mdoc
, n
, 1, 6, 8, 0))
993 return(check_stdarg(mdoc
, n
));
1001 return(check_msec(mdoc
, n
, 2, 3, 9, 0));
1009 return(check_msec(mdoc
, n
, 4, 0));
1017 if (0 == mdoc
->meta
.date
|| mdoc
->meta
.os
)
1018 if ( ! nwarn(mdoc
, n
, WPROLOOO
))
1020 if (mdoc
->meta
.title
)
1021 if ( ! nwarn(mdoc
, n
, WPROLREP
))
1031 if (NULL
== mdoc
->meta
.title
|| 0 == mdoc
->meta
.date
)
1032 if ( ! nwarn(mdoc
, n
, WPROLOOO
))
1035 if ( ! nwarn(mdoc
, n
, WPROLREP
))
1045 if (mdoc
->meta
.title
|| mdoc
->meta
.os
)
1046 if ( ! nwarn(mdoc
, n
, WPROLOOO
))
1048 if (mdoc
->meta
.date
)
1049 if ( ! nwarn(mdoc
, n
, WPROLREP
))
1059 struct mdoc_node
*head
;
1061 if (MDOC_BLOCK
!= mdoc
->last
->type
)
1064 head
= mdoc
->last
->head
;
1066 if (mdoc
->last
->args
&& head
->child
)
1067 return(mdoc_err(mdoc
, "one argument expected"));
1068 else if (mdoc
->last
->args
)
1071 if (NULL
== head
->child
|| MDOC_TEXT
!= head
->child
->type
)
1072 return(mdoc_err(mdoc
, "text argument expected"));
1074 p
= head
->child
->string
;
1076 if (0 == strcmp(p
, "Em"))
1078 else if (0 == strcmp(p
, "Li"))
1080 else if (0 == strcmp(p
, "Sm"))
1083 return(mdoc_nerr(mdoc
, head
->child
, "invalid font mode"));
1091 if (mdoc
->last
->child
)
1093 if (mdoc
->meta
.name
)
1095 return(verr(mdoc
, ENAME
));
1103 if (NULL
== mdoc
->last
->child
)
1105 if (MDOC_TEXT
!= mdoc
->last
->child
->type
)
1106 return(verr(mdoc
, EATT
));
1107 if (mdoc_a2att(mdoc
->last
->child
->string
))
1109 return(verr(mdoc
, EATT
));
1117 if (mdoc
->last
->args
) {
1118 if (NULL
== mdoc
->last
->child
)
1120 return(verr(mdoc
, ELINE
));
1123 if (mdoc
->last
->child
)
1125 return(verr(mdoc
, ELINE
));
1130 post_args(POST_ARGS
)
1133 if (mdoc
->last
->args
)
1135 return(verr(mdoc
, ELINE
));
1143 struct mdoc_node
*n
, *c
;
1145 if (MDOC_BLOCK
!= mdoc
->last
->type
)
1148 n
= mdoc
->last
->parent
->parent
;
1149 if (NULL
== n
->args
)
1150 return(verr(mdoc
, ELISTTYPE
));
1152 /* Some types require block-head, some not. */
1155 for (cols
= type
= -1, i
= 0; -1 == type
&&
1156 i
< (int)n
->args
->argc
; i
++)
1157 switch (n
->args
->argv
[i
].arg
) {
1177 type
= n
->args
->argv
[i
].arg
;
1180 type
= n
->args
->argv
[i
].arg
;
1181 cols
= (int)n
->args
->argv
[i
].sz
;
1188 return(verr(mdoc
, ELISTTYPE
));
1192 if (NULL
== mdoc
->last
->head
->child
)
1193 if ( ! vwarn(mdoc
, WLINE
))
1203 if (NULL
== mdoc
->last
->head
->child
)
1204 if ( ! vwarn(mdoc
, WLINE
))
1206 if (NULL
== mdoc
->last
->body
->child
)
1207 if ( ! vwarn(mdoc
, WMULTILINE
))
1219 if (mdoc
->last
->head
->child
)
1220 if ( ! vwarn(mdoc
, WNOLINE
))
1222 if (NULL
== mdoc
->last
->body
->child
)
1223 if ( ! vwarn(mdoc
, WMULTILINE
))
1227 if (NULL
== mdoc
->last
->head
->child
)
1228 if ( ! vwarn(mdoc
, WLINE
))
1230 if (mdoc
->last
->body
->child
)
1231 if ( ! vwarn(mdoc
, WNOMULTILINE
))
1233 c
= mdoc
->last
->child
;
1234 for (i
= 0; c
&& MDOC_HEAD
== c
->type
; c
= c
->next
)
1238 return(mdoc_err(mdoc
, "column mismatch (have "
1239 "%d, want %d)", i
, cols
));
1249 post_bl_head(POST_ARGS
)
1252 const struct mdoc_node
*n
;
1254 n
= mdoc
->last
->parent
;
1257 for (i
= 0; i
< (int)n
->args
->argc
; i
++)
1258 if (n
->args
->argv
[i
].arg
== MDOC_Column
)
1261 if (i
== (int)n
->args
->argc
)
1264 if (n
->args
->argv
[i
].sz
&& mdoc
->last
->child
)
1265 return(nerr(mdoc
, n
, ECOLMIS
));
1274 struct mdoc_node
*n
;
1276 if (MDOC_HEAD
== mdoc
->last
->type
)
1277 return(post_bl_head(mdoc
));
1278 if (MDOC_BODY
!= mdoc
->last
->type
)
1280 if (NULL
== mdoc
->last
->child
)
1284 for (n
= mdoc
->last
->child
; n
; n
= n
->next
) {
1285 if (MDOC_BLOCK
== n
->type
)
1286 if (MDOC_It
== n
->tok
)
1288 return(mdoc_nerr(mdoc
, n
, "bad child of parent %s",
1289 mdoc_macronames
[mdoc
->last
->tok
]));
1297 ebool(struct mdoc
*mdoc
)
1299 struct mdoc_node
*n
;
1302 for (n
= mdoc
->last
->child
; n
; n
= n
->next
) {
1303 if (MDOC_TEXT
!= n
->type
)
1305 if (0 == strcmp(n
->string
, "on"))
1307 if (0 == strcmp(n
->string
, "off"))
1314 return(nerr(mdoc
, n
, EBOOL
));
1319 post_root(POST_ARGS
)
1322 if (NULL
== mdoc
->first
->child
)
1323 return(verr(mdoc
, ENODATA
));
1324 if ( ! (MDOC_PBODY
& mdoc
->flags
))
1325 return(verr(mdoc
, ENOPROLOGUE
));
1327 if (MDOC_BLOCK
!= mdoc
->first
->child
->type
)
1328 return(verr(mdoc
, ENODATA
));
1329 if (MDOC_Sh
!= mdoc
->first
->child
->tok
)
1330 return(verr(mdoc
, ENODATA
));
1340 if (mdoc_a2st(mdoc
->last
->child
->string
))
1342 return(vwarn(mdoc
, WBADSTAND
));
1350 if (MDOC_HEAD
== mdoc
->last
->type
)
1351 return(post_sh_head(mdoc
));
1352 if (MDOC_BODY
== mdoc
->last
->type
)
1353 return(post_sh_body(mdoc
));
1360 post_sh_body(POST_ARGS
)
1362 struct mdoc_node
*n
;
1364 if (SEC_NAME
!= mdoc
->lastnamed
)
1368 * Warn if the NAME section doesn't contain the `Nm' and `Nd'
1369 * macros (can have multiple `Nm' and one `Nd'). Note that the
1370 * children of the BODY declaration can also be "text".
1373 if (NULL
== (n
= mdoc
->last
->child
))
1374 return(vwarn(mdoc
, WNAMESECINC
));
1376 for ( ; n
&& n
->next
; n
= n
->next
) {
1377 if (MDOC_ELEM
== n
->type
&& MDOC_Nm
== n
->tok
)
1379 if (MDOC_TEXT
== n
->type
)
1381 if ( ! vwarn(mdoc
, WNAMESECINC
))
1385 if (MDOC_ELEM
== n
->type
&& MDOC_Nd
== n
->tok
)
1387 return(vwarn(mdoc
, WNAMESECINC
));
1392 post_sh_head(POST_ARGS
)
1396 const struct mdoc_node
*n
;
1399 * Process a new section. Sections are either "named" or
1400 * "custom"; custom sections are user-defined, while named ones
1401 * usually follow a conventional order and may only appear in
1402 * certain manual sections.
1407 for (n
= mdoc
->last
->child
; n
; n
= n
->next
) {
1408 /* XXX - copied from compact(). */
1409 assert(MDOC_TEXT
== n
->type
);
1411 if (strlcat(buf
, n
->string
, 64) >= 64)
1412 return(nerr(mdoc
, n
, ETOOLONG
));
1413 if (NULL
== n
->next
)
1415 if (strlcat(buf
, " ", 64) >= 64)
1416 return(nerr(mdoc
, n
, ETOOLONG
));
1419 sec
= mdoc_atosec(buf
);
1422 * Check: NAME should always be first, CUSTOM has no roles,
1423 * non-CUSTOM has a conventional order to be followed.
1426 if (SEC_NAME
!= sec
&& SEC_NONE
== mdoc
->lastnamed
)
1427 return(verr(mdoc
, ESECNAME
));
1428 if (SEC_CUSTOM
== sec
)
1430 if (sec
== mdoc
->lastnamed
)
1431 return(vwarn(mdoc
, WSECREP
));
1432 if (sec
< mdoc
->lastnamed
)
1433 return(vwarn(mdoc
, WSECOOO
));
1436 * Check particular section/manual conventions. LIBRARY can
1437 * only occur in msec 2, 3 (TODO: are there more of these?).
1442 switch (mdoc
->meta
.msec
) {
1448 return(vwarn(mdoc
, WWRONGMSEC
));
1463 return(check_sec(mdoc
, n
, SEC_SYNOPSIS
, SEC_CUSTOM
));