]>
git.cameronkatri.com Git - mandoc.git/blob - mdoc_validate.c
1 /* $Id: mdoc_validate.c,v 1.7 2009/06/10 20:18:43 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
70 typedef int (*v_pre
)(PRE_ARGS
);
71 typedef int (*v_post
)(POST_ARGS
);
78 static int pwarn(struct mdoc
*, int, int, enum mwarn
);
79 static int perr(struct mdoc
*, int, int, enum merr
);
80 static int check_parent(PRE_ARGS
, int, enum mdoc_type
);
81 static int check_msec(PRE_ARGS
, ...);
82 static int check_sec(PRE_ARGS
, ...);
83 static int check_stdarg(PRE_ARGS
);
84 static int check_text(struct mdoc
*, int, int, const char *);
85 static int check_argv(struct mdoc
*,
86 const struct mdoc_node
*,
87 const struct mdoc_argv
*);
88 static int check_args(struct mdoc
*,
89 const struct mdoc_node
*);
90 static int err_child_lt(struct mdoc
*, const char *, int);
91 static int warn_child_lt(struct mdoc
*, const char *, int);
92 static int err_child_gt(struct mdoc
*, const char *, int);
93 static int warn_child_gt(struct mdoc
*, const char *, int);
94 static int err_child_eq(struct mdoc
*, const char *, int);
95 static int warn_child_eq(struct mdoc
*, const char *, int);
96 static int count_child(struct mdoc
*);
97 static int warn_print(struct mdoc
*, int, int);
98 static int warn_count(struct mdoc
*, const char *,
99 int, const char *, int);
100 static int err_count(struct mdoc
*, const char *,
101 int, const char *, int);
102 static int pre_an(PRE_ARGS
);
103 static int pre_bd(PRE_ARGS
);
104 static int pre_bl(PRE_ARGS
);
105 static int pre_cd(PRE_ARGS
);
106 static int pre_dd(PRE_ARGS
);
107 static int pre_display(PRE_ARGS
);
108 static int pre_dt(PRE_ARGS
);
109 static int pre_er(PRE_ARGS
);
110 static int pre_ex(PRE_ARGS
);
111 static int pre_fd(PRE_ARGS
);
112 static int pre_it(PRE_ARGS
);
113 static int pre_lb(PRE_ARGS
);
114 static int pre_os(PRE_ARGS
);
115 static int pre_prologue(PRE_ARGS
);
116 static int pre_rv(PRE_ARGS
);
117 static int pre_sh(PRE_ARGS
);
118 static int pre_ss(PRE_ARGS
);
119 static int herr_ge1(POST_ARGS
);
120 static int hwarn_le1(POST_ARGS
);
121 static int herr_eq0(POST_ARGS
);
122 static int eerr_eq0(POST_ARGS
);
123 static int eerr_le2(POST_ARGS
);
124 static int eerr_eq1(POST_ARGS
);
125 static int eerr_ge1(POST_ARGS
);
126 static int ewarn_eq0(POST_ARGS
);
127 static int ewarn_eq1(POST_ARGS
);
128 static int bwarn_ge1(POST_ARGS
);
129 static int hwarn_eq1(POST_ARGS
);
130 static int ewarn_ge1(POST_ARGS
);
131 static int ebool(POST_ARGS
);
132 static int post_an(POST_ARGS
);
133 static int post_args(POST_ARGS
);
134 static int post_at(POST_ARGS
);
135 static int post_bf(POST_ARGS
);
136 static int post_bl(POST_ARGS
);
137 static int post_it(POST_ARGS
);
138 static int post_nm(POST_ARGS
);
139 static int post_root(POST_ARGS
);
140 static int post_sh(POST_ARGS
);
141 static int post_sh_body(POST_ARGS
);
142 static int post_sh_head(POST_ARGS
);
143 static int post_st(POST_ARGS
);
145 #define vwarn(m, t) nwarn((m), (m)->last, (t))
146 #define verr(m, t) nerr((m), (m)->last, (t))
147 #define nwarn(m, n, t) pwarn((m), (n)->line, (n)->pos, (t))
148 #define nerr(m, n, t) perr((m), (n)->line, (n)->pos, (t))
150 static v_pre pres_an
[] = { pre_an
, NULL
};
151 static v_pre pres_bd
[] = { pre_display
, pre_bd
, NULL
};
152 static v_pre pres_bl
[] = { pre_bl
, NULL
};
153 static v_pre pres_cd
[] = { pre_cd
, NULL
};
154 static v_pre pres_dd
[] = { pre_prologue
, pre_dd
, NULL
};
155 static v_pre pres_d1
[] = { pre_display
, NULL
};
156 static v_pre pres_dt
[] = { pre_prologue
, pre_dt
, NULL
};
157 static v_pre pres_er
[] = { pre_er
, NULL
};
158 static v_pre pres_ex
[] = { pre_ex
, NULL
};
159 static v_pre pres_fd
[] = { pre_fd
, NULL
};
160 static v_pre pres_it
[] = { pre_it
, NULL
};
161 static v_pre pres_lb
[] = { pre_lb
, NULL
};
162 static v_pre pres_os
[] = { pre_prologue
, pre_os
, NULL
};
163 static v_pre pres_rv
[] = { pre_rv
, NULL
};
164 static v_pre pres_sh
[] = { pre_sh
, NULL
};
165 static v_pre pres_ss
[] = { pre_ss
, NULL
};
166 static v_post posts_bool
[] = { eerr_eq1
, ebool
, NULL
};
167 static v_post posts_bd
[] = { herr_eq0
, bwarn_ge1
, NULL
};
168 static v_post posts_text
[] = { eerr_ge1
, NULL
};
169 static v_post posts_wtext
[] = { ewarn_ge1
, NULL
};
170 static v_post posts_notext
[] = { eerr_eq0
, NULL
};
171 static v_post posts_wline
[] = { bwarn_ge1
, herr_eq0
, NULL
};
172 static v_post posts_sh
[] = { herr_ge1
, bwarn_ge1
, post_sh
, NULL
};
173 static v_post posts_bl
[] = { herr_eq0
, bwarn_ge1
, post_bl
, NULL
};
174 static v_post posts_it
[] = { post_it
, NULL
};
175 static v_post posts_in
[] = { ewarn_eq1
, NULL
};
176 static v_post posts_ss
[] = { herr_ge1
, NULL
};
177 static v_post posts_pf
[] = { eerr_eq1
, NULL
};
178 static v_post posts_lb
[] = { eerr_eq1
, NULL
};
179 static v_post posts_st
[] = { eerr_eq1
, post_st
, NULL
};
180 static v_post posts_pp
[] = { ewarn_eq0
, NULL
};
181 static v_post posts_ex
[] = { eerr_eq0
, post_args
, NULL
};
182 static v_post posts_rv
[] = { eerr_eq0
, post_args
, 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_fo
[] = { hwarn_eq1
, bwarn_ge1
, NULL
};
190 const struct valids mdoc_valids
[MDOC_MAX
] = {
191 { NULL
, NULL
}, /* \" */
192 { pres_dd
, posts_text
}, /* Dd */
193 { pres_dt
, NULL
}, /* Dt */
194 { pres_os
, NULL
}, /* Os */
195 { pres_sh
, posts_sh
}, /* Sh */
196 { pres_ss
, posts_ss
}, /* Ss */
197 { NULL
, posts_pp
}, /* Pp */
198 { pres_d1
, posts_wline
}, /* D1 */
199 { pres_d1
, posts_wline
}, /* Dl */
200 { pres_bd
, posts_bd
}, /* Bd */
201 { NULL
, NULL
}, /* Ed */
202 { pres_bl
, posts_bl
}, /* Bl */
203 { NULL
, NULL
}, /* El */
204 { pres_it
, posts_it
}, /* It */
205 { NULL
, posts_text
}, /* Ad */
206 { pres_an
, posts_an
}, /* An */
207 { NULL
, NULL
}, /* Ar */
208 { pres_cd
, posts_text
}, /* Cd */
209 { NULL
, NULL
}, /* Cm */
210 { NULL
, NULL
}, /* Dv */
211 { pres_er
, posts_text
}, /* Er */
212 { NULL
, NULL
}, /* Ev */
213 { pres_ex
, posts_ex
}, /* Ex */
214 { NULL
, NULL
}, /* Fa */
215 { pres_fd
, posts_wtext
}, /* Fd */
216 { NULL
, NULL
}, /* Fl */
217 { NULL
, posts_text
}, /* Fn */
218 { NULL
, posts_wtext
}, /* Ft */
219 { NULL
, posts_text
}, /* Ic */
220 { NULL
, posts_in
}, /* In */
221 { NULL
, NULL
}, /* Li */
222 { NULL
, posts_wtext
}, /* Nd */
223 { NULL
, posts_nm
}, /* Nm */
224 { NULL
, posts_wline
}, /* Op */
225 { NULL
, NULL
}, /* Ot */
226 { NULL
, NULL
}, /* Pa */
227 { pres_rv
, posts_rv
}, /* Rv */
228 { NULL
, posts_st
}, /* St */
229 { NULL
, NULL
}, /* Va */
230 { NULL
, posts_text
}, /* Vt */
231 { NULL
, posts_xr
}, /* Xr */
232 { NULL
, posts_text
}, /* %A */
233 { NULL
, posts_text
}, /* %B */
234 { NULL
, posts_text
}, /* %D */
235 { NULL
, posts_text
}, /* %I */
236 { NULL
, posts_text
}, /* %J */
237 { NULL
, posts_text
}, /* %N */
238 { NULL
, posts_text
}, /* %O */
239 { NULL
, posts_text
}, /* %P */
240 { NULL
, posts_text
}, /* %R */
241 { NULL
, posts_text
}, /* %T */
242 { NULL
, posts_text
}, /* %V */
243 { NULL
, NULL
}, /* Ac */
244 { NULL
, NULL
}, /* Ao */
245 { NULL
, posts_wline
}, /* Aq */
246 { NULL
, posts_at
}, /* At */
247 { NULL
, NULL
}, /* Bc */
248 { NULL
, posts_bf
}, /* Bf */
249 { NULL
, NULL
}, /* Bo */
250 { NULL
, posts_wline
}, /* Bq */
251 { NULL
, NULL
}, /* Bsx */
252 { NULL
, NULL
}, /* Bx */
253 { NULL
, posts_bool
}, /* Db */
254 { NULL
, NULL
}, /* Dc */
255 { NULL
, NULL
}, /* Do */
256 { NULL
, posts_wline
}, /* Dq */
257 { NULL
, NULL
}, /* Ec */
258 { NULL
, NULL
}, /* Ef */
259 { NULL
, NULL
}, /* Em */
260 { NULL
, NULL
}, /* Eo */
261 { NULL
, NULL
}, /* Fx */
262 { NULL
, posts_text
}, /* Ms */
263 { NULL
, posts_notext
}, /* No */
264 { NULL
, posts_notext
}, /* Ns */
265 { NULL
, NULL
}, /* Nx */
266 { NULL
, NULL
}, /* Ox */
267 { NULL
, NULL
}, /* Pc */
268 { NULL
, posts_pf
}, /* Pf */
269 { NULL
, NULL
}, /* Po */
270 { NULL
, posts_wline
}, /* Pq */
271 { NULL
, NULL
}, /* Qc */
272 { NULL
, posts_wline
}, /* Ql */
273 { NULL
, NULL
}, /* Qo */
274 { NULL
, posts_wline
}, /* Qq */
275 { NULL
, NULL
}, /* Re */
276 { NULL
, posts_wline
}, /* Rs */
277 { NULL
, NULL
}, /* Sc */
278 { NULL
, NULL
}, /* So */
279 { NULL
, posts_wline
}, /* Sq */
280 { NULL
, posts_bool
}, /* Sm */
281 { NULL
, posts_text
}, /* Sx */
282 { NULL
, posts_text
}, /* Sy */
283 { NULL
, posts_text
}, /* Tn */
284 { NULL
, NULL
}, /* Ux */
285 { NULL
, NULL
}, /* Xc */
286 { NULL
, NULL
}, /* Xo */
287 { NULL
, posts_fo
}, /* Fo */
288 { NULL
, NULL
}, /* Fc */
289 { NULL
, NULL
}, /* Oo */
290 { NULL
, NULL
}, /* Oc */
291 { NULL
, posts_wline
}, /* Bk */
292 { NULL
, NULL
}, /* Ek */
293 { NULL
, posts_notext
}, /* Bt */
294 { NULL
, NULL
}, /* Hf */
295 { NULL
, NULL
}, /* Fr */
296 { NULL
, posts_notext
}, /* Ud */
297 { pres_lb
, posts_lb
}, /* Lb */
298 { NULL
, NULL
}, /* Ap */
299 { NULL
, posts_pp
}, /* Lp */
300 { NULL
, posts_text
}, /* Lk */
301 { NULL
, posts_text
}, /* Mt */
302 { NULL
, posts_wline
}, /* Brq */
303 { NULL
, NULL
}, /* Bro */
304 { NULL
, NULL
}, /* Brc */
305 { NULL
, posts_text
}, /* %C */
306 { NULL
, NULL
}, /* Es */
307 { NULL
, NULL
}, /* En */
308 { NULL
, NULL
}, /* Dx */
309 { NULL
, posts_text
}, /* %Q */
314 extern size_t strlcat(char *, const char *, size_t);
319 mdoc_valid_pre(struct mdoc
*mdoc
,
320 const struct mdoc_node
*n
)
326 if (MDOC_TEXT
== n
->type
) {
330 return(check_text(mdoc
, line
, pos
, tp
));
333 if ( ! check_args(mdoc
, n
))
335 if (NULL
== mdoc_valids
[n
->tok
].pre
)
337 for (p
= mdoc_valids
[n
->tok
].pre
; *p
; p
++)
338 if ( ! (*p
)(mdoc
, n
))
345 mdoc_valid_post(struct mdoc
*mdoc
)
350 * This check occurs after the macro's children have been filled
351 * in: postfix validation. Since this happens when we're
352 * rewinding the scope tree, it's possible to have multiple
353 * invocations (as by design, for now), we set bit MDOC_VALID to
354 * indicate that we've validated.
357 if (MDOC_VALID
& mdoc
->last
->flags
)
359 mdoc
->last
->flags
|= MDOC_VALID
;
361 if (MDOC_TEXT
== mdoc
->last
->type
)
363 if (MDOC_ROOT
== mdoc
->last
->type
)
364 return(post_root(mdoc
));
366 if (NULL
== mdoc_valids
[mdoc
->last
->tok
].post
)
368 for (p
= mdoc_valids
[mdoc
->last
->tok
].post
; *p
; p
++)
377 perr(struct mdoc
*m
, int line
, int pos
, enum merr type
)
384 p
= "text argument too long";
387 p
= "invalid escape sequence";
390 p
= "invalid character";
393 p
= "displays may not be nested";
396 p
= "expected boolean value";
399 p
= "argument repeated";
402 p
= "multiple display types specified";
405 p
= "multiple list types specified";
408 p
= "missing list type";
411 p
= "missing display type";
414 p
= "expected line arguments";
417 p
= "document has no prologue";
420 p
= "document has no data";
423 p
= "expected valid AT&T symbol";
426 p
= "default name not yet set";
430 return(mdoc_perr(m
, line
, pos
, p
));
435 pwarn(struct mdoc
*m
, int line
, int pos
, enum mwarn type
)
444 p
= "inappropriate manual section";
448 p
= "inappropriate document section";
452 p
= "argument value suggested";
456 p
= "prologue macros repeated";
460 p
= "prologue macros out-of-order";
464 p
= "invalid character";
467 p
= "invalid escape sequence";
470 p
= "suggested no line arguments";
473 p
= "suggested line arguments";
476 p
= "suggested multi-line arguments";
479 p
= "suggested no multi-line arguments";
482 p
= "document section in wrong manual section";
486 p
= "document section out of conventional order";
489 p
= "document section repeated";
492 p
= "unknown standard";
495 p
= "NAME section contents incomplete/badly-ordered";
499 return(mdoc_pwarn(m
, line
, pos
, c
, p
));
504 warn_print(struct mdoc
*m
, int ln
, int pos
)
506 if (MDOC_IGN_CHARS
& m
->pflags
)
507 return(pwarn(m
, ln
, pos
, WPRINT
));
508 return(perr(m
, ln
, pos
, EPRINT
));
513 warn_count(struct mdoc
*m
, const char *k
,
514 int want
, const char *v
, int has
)
517 return(mdoc_warn(m
, WARN_SYNTAX
,
518 "suggests %s %s %d (has %d)", v
, k
, want
, has
));
523 err_count(struct mdoc
*m
, const char *k
,
524 int want
, const char *v
, int has
)
528 "requires %s %s %d (has %d)", v
, k
, want
, has
));
533 count_child(struct mdoc
*mdoc
)
538 for (i
= 0, n
= mdoc
->last
->child
; n
; n
= n
->next
, i
++)
546 * Build these up with macros because they're basically the same check
547 * for different inequalities. Yes, this could be done with functions,
548 * but this is reasonable for now.
551 #define CHECK_CHILD_DEFN(lvl, name, ineq) \
553 lvl##_child_##name(struct mdoc *mdoc, const char *p, int sz) \
556 if ((i = count_child(mdoc)) ineq sz) \
558 return(lvl##_count(mdoc, #ineq, sz, p, i)); \
561 #define CHECK_BODY_DEFN(name, lvl, func, num) \
563 b##lvl##_##name(POST_ARGS) \
565 if (MDOC_BODY != mdoc->last->type) \
567 return(func(mdoc, "multi-line arguments", (num))); \
570 #define CHECK_ELEM_DEFN(name, lvl, func, num) \
572 e##lvl##_##name(POST_ARGS) \
574 assert(MDOC_ELEM == mdoc->last->type); \
575 return(func(mdoc, "line arguments", (num))); \
578 #define CHECK_HEAD_DEFN(name, lvl, func, num) \
580 h##lvl##_##name(POST_ARGS) \
582 if (MDOC_HEAD != mdoc->last->type) \
584 return(func(mdoc, "line arguments", (num))); \
588 CHECK_CHILD_DEFN(warn
, gt
, >) /* warn_child_gt() */
589 CHECK_CHILD_DEFN(err
, gt
, >) /* err_child_gt() */
590 CHECK_CHILD_DEFN(warn
, eq
, ==) /* warn_child_eq() */
591 CHECK_CHILD_DEFN(err
, eq
, ==) /* err_child_eq() */
592 CHECK_CHILD_DEFN(err
, lt
, <) /* err_child_lt() */
593 CHECK_CHILD_DEFN(warn
, lt
, <) /* warn_child_lt() */
594 CHECK_BODY_DEFN(ge1
, warn
, warn_child_gt
, 0) /* bwarn_ge1() */
595 CHECK_ELEM_DEFN(eq1
, warn
, warn_child_eq
, 1) /* ewarn_eq1() */
596 CHECK_ELEM_DEFN(eq0
, warn
, warn_child_eq
, 0) /* ewarn_eq0() */
597 CHECK_ELEM_DEFN(ge1
, warn
, warn_child_gt
, 0) /* ewarn_gt1() */
598 CHECK_ELEM_DEFN(eq1
, err
, err_child_eq
, 1) /* eerr_eq1() */
599 CHECK_ELEM_DEFN(le2
, err
, err_child_lt
, 3) /* eerr_le2() */
600 CHECK_ELEM_DEFN(eq0
, err
, err_child_eq
, 0) /* eerr_eq0() */
601 CHECK_ELEM_DEFN(ge1
, err
, err_child_gt
, 0) /* eerr_ge1() */
602 CHECK_HEAD_DEFN(eq0
, err
, err_child_eq
, 0) /* herr_eq0() */
603 CHECK_HEAD_DEFN(le1
, warn
, warn_child_lt
, 2) /* hwarn_le1() */
604 CHECK_HEAD_DEFN(ge1
, err
, err_child_gt
, 0) /* herr_ge1() */
605 CHECK_HEAD_DEFN(eq1
, warn
, warn_child_eq
, 1) /* hwarn_eq1() */
609 check_stdarg(PRE_ARGS
)
612 if (n
->args
&& 1 == n
->args
->argc
)
613 if (MDOC_Std
== n
->args
->argv
[0].arg
)
615 return(nwarn(mdoc
, n
, WARGVAL
));
620 check_sec(PRE_ARGS
, ...)
629 sec
= (enum mdoc_sec
)va_arg(ap
, int);
630 if (SEC_CUSTOM
== sec
)
632 if (sec
!= mdoc
->lastsec
)
639 return(nwarn(mdoc
, n
, WBADSEC
));
644 check_msec(PRE_ARGS
, ...)
652 if (0 == (msec
= va_arg(ap
, int)))
654 if (msec
!= mdoc
->meta
.msec
)
661 return(nwarn(mdoc
, n
, WBADMSEC
));
666 check_args(struct mdoc
*m
, const struct mdoc_node
*n
)
673 assert(n
->args
->argc
);
674 for (i
= 0; i
< (int)n
->args
->argc
; i
++)
675 if ( ! check_argv(m
, n
, &n
->args
->argv
[i
]))
683 check_argv(struct mdoc
*m
, const struct mdoc_node
*n
,
684 const struct mdoc_argv
*v
)
688 for (i
= 0; i
< (int)v
->sz
; i
++)
689 if ( ! check_text(m
, v
->line
, v
->pos
, v
->value
[i
]))
692 if (MDOC_Std
== v
->arg
) {
693 /* `Nm' name must be set. */
694 if (v
->sz
|| m
->meta
.name
)
696 return(nerr(m
, n
, ENAME
));
704 check_text(struct mdoc
*mdoc
, int line
, int pos
, const char *p
)
708 /* FIXME: indicate deprecated escapes \*(xx and \*x. */
712 if ( ! (MDOC_LITERAL
& mdoc
->flags
))
713 if ( ! warn_print(mdoc
, line
, pos
))
715 } else if ( ! isprint((u_char
)*p
))
716 if ( ! warn_print(mdoc
, line
, pos
))
722 c
= mdoc_isescape(p
);
727 if ( ! (MDOC_IGN_ESCAPE
& mdoc
->pflags
))
728 return(perr(mdoc
, line
, pos
, EESCAPE
));
729 if ( ! pwarn(mdoc
, line
, pos
, WESCAPE
))
740 check_parent(PRE_ARGS
, int tok
, enum mdoc_type t
)
744 if ((MDOC_ROOT
== t
|| tok
== n
->parent
->tok
) &&
745 (t
== n
->parent
->type
))
748 return(mdoc_nerr(mdoc
, n
, "require parent %s",
749 MDOC_ROOT
== t
? "<root>" : mdoc_macronames
[tok
]));
755 pre_display(PRE_ARGS
)
757 struct mdoc_node
*node
;
759 /* Display elements (`Bd', `D1'...) cannot be nested. */
761 if (MDOC_BLOCK
!= n
->type
)
765 for (node
= mdoc
->last
->parent
; node
; node
= node
->parent
)
766 if (MDOC_BLOCK
== node
->type
)
767 if (MDOC_Bd
== node
->tok
)
772 return(nerr(mdoc
, n
, ENESTDISP
));
779 int i
, type
, width
, offset
;
781 if (MDOC_BLOCK
!= n
->type
)
784 return(nerr(mdoc
, n
, ELISTTYPE
));
786 /* Make sure that only one type of list is specified. */
788 type
= offset
= width
= -1;
791 for (i
= 0; i
< (int)n
->args
->argc
; i
++)
792 switch (n
->args
->argv
[i
].arg
) {
815 type
= n
->args
->argv
[i
].arg
;
818 return(nerr(mdoc
, n
, EMULTILIST
));
821 width
= n
->args
->argv
[i
].arg
;
824 return(nerr(mdoc
, n
, EARGREP
));
827 offset
= n
->args
->argv
[i
].arg
;
830 return(nerr(mdoc
, n
, EARGREP
));
836 return(nerr(mdoc
, n
, ELISTTYPE
));
848 return(mdoc_nwarn(mdoc
, n
, WARN_SYNTAX
,
849 "superfluous %s argument",
850 mdoc_argnames
[MDOC_Width
]));
854 return(mdoc_nwarn(mdoc
, n
, WARN_SYNTAX
,
855 "suggest %s argument",
856 mdoc_argnames
[MDOC_Width
]));
870 if (MDOC_BLOCK
!= n
->type
)
873 return(nerr(mdoc
, n
, EDISPTYPE
));
875 /* Make sure that only one type of display is specified. */
878 for (i
= 0, err
= type
= 0; ! err
&&
879 i
< (int)n
->args
->argc
; i
++)
880 switch (n
->args
->argv
[i
].arg
) {
883 case (MDOC_Unfilled
):
892 return(nerr(mdoc
, n
, EMULTIDISP
));
899 return(nerr(mdoc
, n
, EDISPTYPE
));
907 if (MDOC_BLOCK
!= n
->type
)
909 return(check_parent(mdoc
, n
, MDOC_Sh
, MDOC_BODY
));
917 if (MDOC_BLOCK
!= n
->type
)
919 return(check_parent(mdoc
, n
, -1, MDOC_ROOT
));
927 if (MDOC_BLOCK
!= n
->type
)
929 return(check_parent(mdoc
, n
, MDOC_Bl
, MDOC_BODY
));
937 if (NULL
== n
->args
|| 1 == n
->args
->argc
)
939 return(mdoc_nerr(mdoc
, n
, "only one argument allowed"));
947 return(check_sec(mdoc
, n
, SEC_LIBRARY
, SEC_CUSTOM
));
955 if ( ! check_msec(mdoc
, n
, 2, 3, 0))
957 return(check_stdarg(mdoc
, n
));
965 if ( ! check_msec(mdoc
, n
, 1, 6, 8, 0))
967 return(check_stdarg(mdoc
, n
));
975 return(check_msec(mdoc
, n
, 2, 3, 9, 0));
983 return(check_msec(mdoc
, n
, 4, 0));
988 pre_prologue(PRE_ARGS
)
991 return(check_sec(mdoc
, n
, SEC_PROLOGUE
, SEC_CUSTOM
));
999 if (0 == mdoc
->meta
.date
|| mdoc
->meta
.os
)
1000 if ( ! nwarn(mdoc
, n
, WPROLOOO
))
1002 if (mdoc
->meta
.title
)
1003 if ( ! nwarn(mdoc
, n
, WPROLREP
))
1013 if (NULL
== mdoc
->meta
.title
|| 0 == mdoc
->meta
.date
)
1014 if ( ! nwarn(mdoc
, n
, WPROLOOO
))
1017 if ( ! nwarn(mdoc
, n
, WPROLREP
))
1027 if (mdoc
->meta
.title
|| mdoc
->meta
.os
)
1028 if ( ! nwarn(mdoc
, n
, WPROLOOO
))
1030 if (mdoc
->meta
.date
)
1031 if ( ! nwarn(mdoc
, n
, WPROLREP
))
1041 struct mdoc_node
*head
;
1043 if (MDOC_BLOCK
!= mdoc
->last
->type
)
1046 head
= mdoc
->last
->head
;
1048 if (NULL
== mdoc
->last
->args
) {
1049 if (NULL
== head
->child
||
1050 MDOC_TEXT
!= head
->child
->type
)
1051 return(mdoc_err(mdoc
, "text argument expected"));
1053 p
= head
->child
->string
;
1054 if (0 == strcmp(p
, "Em"))
1056 else if (0 == strcmp(p
, "Li"))
1058 else if (0 == strcmp(p
, "Sm"))
1060 return(mdoc_nerr(mdoc
, head
->child
, "invalid font"));
1064 return(mdoc_err(mdoc
, "one argument expected"));
1074 if (mdoc
->last
->child
)
1076 if (mdoc
->meta
.name
)
1078 return(verr(mdoc
, ENAME
));
1086 if (NULL
== mdoc
->last
->child
)
1088 if (MDOC_TEXT
!= mdoc
->last
->child
->type
)
1089 return(verr(mdoc
, EATT
));
1090 if (mdoc_a2att(mdoc
->last
->child
->string
))
1092 return(verr(mdoc
, EATT
));
1100 if (mdoc
->last
->args
) {
1101 if (NULL
== mdoc
->last
->child
)
1103 return(verr(mdoc
, ELINE
));
1106 if (mdoc
->last
->child
)
1108 return(verr(mdoc
, ELINE
));
1113 post_args(POST_ARGS
)
1116 if (mdoc
->last
->args
)
1118 return(verr(mdoc
, ELINE
));
1126 struct mdoc_node
*n
, *c
;
1128 if (MDOC_BLOCK
!= mdoc
->last
->type
)
1131 n
= mdoc
->last
->parent
->parent
;
1132 if (NULL
== n
->args
)
1133 return(verr(mdoc
, ELISTTYPE
));
1135 /* Some types require block-head, some not. */
1138 for (cols
= type
= -1, i
= 0; -1 == type
&&
1139 i
< (int)n
->args
->argc
; i
++)
1140 switch (n
->args
->argv
[i
].arg
) {
1160 type
= n
->args
->argv
[i
].arg
;
1163 type
= n
->args
->argv
[i
].arg
;
1164 cols
= (int)n
->args
->argv
[i
].sz
;
1171 return(verr(mdoc
, ELISTTYPE
));
1175 if (NULL
== mdoc
->last
->head
->child
)
1176 if ( ! vwarn(mdoc
, WLINE
))
1186 if (NULL
== mdoc
->last
->head
->child
)
1187 if ( ! vwarn(mdoc
, WLINE
))
1189 if (NULL
== mdoc
->last
->body
->child
)
1190 if ( ! vwarn(mdoc
, WMULTILINE
))
1202 if (mdoc
->last
->head
->child
)
1203 if ( ! vwarn(mdoc
, WNOLINE
))
1205 if (NULL
== mdoc
->last
->body
->child
)
1206 if ( ! vwarn(mdoc
, WMULTILINE
))
1210 if (NULL
== mdoc
->last
->head
->child
)
1211 if ( ! vwarn(mdoc
, WLINE
))
1213 if (mdoc
->last
->body
->child
)
1214 if ( ! vwarn(mdoc
, WNOMULTILINE
))
1216 c
= mdoc
->last
->child
;
1217 for (i
= 0; c
&& MDOC_HEAD
== c
->type
; c
= c
->next
)
1221 return(mdoc_err(mdoc
, "column mismatch (have "
1222 "%d, want %d)", i
, cols
));
1234 struct mdoc_node
*n
;
1236 if (MDOC_BODY
!= mdoc
->last
->type
)
1238 if (NULL
== mdoc
->last
->child
)
1242 for (n
= mdoc
->last
->child
; n
; n
= n
->next
) {
1243 if (MDOC_BLOCK
== n
->type
)
1244 if (MDOC_It
== n
->tok
)
1246 return(mdoc_nerr(mdoc
, n
, "bad child of parent %s",
1247 mdoc_macronames
[mdoc
->last
->tok
]));
1255 ebool(struct mdoc
*mdoc
)
1257 struct mdoc_node
*n
;
1260 for (n
= mdoc
->last
->child
; n
; n
= n
->next
) {
1261 if (MDOC_TEXT
!= n
->type
)
1263 if (0 == strcmp(n
->string
, "on"))
1265 if (0 == strcmp(n
->string
, "off"))
1272 return(nerr(mdoc
, n
, EBOOL
));
1277 post_root(POST_ARGS
)
1280 if (NULL
== mdoc
->first
->child
)
1281 return(verr(mdoc
, ENODATA
));
1282 if (SEC_PROLOGUE
== mdoc
->lastnamed
)
1283 return(verr(mdoc
, ENOPROLOGUE
));
1285 if (MDOC_BLOCK
!= mdoc
->first
->child
->type
)
1286 return(verr(mdoc
, ENODATA
));
1287 if (MDOC_Sh
!= mdoc
->first
->child
->tok
)
1288 return(verr(mdoc
, ENODATA
));
1298 if (mdoc_a2st(mdoc
->last
->child
->string
))
1300 return(vwarn(mdoc
, WBADSTAND
));
1308 if (MDOC_HEAD
== mdoc
->last
->type
)
1309 return(post_sh_head(mdoc
));
1310 if (MDOC_BODY
== mdoc
->last
->type
)
1311 return(post_sh_body(mdoc
));
1318 post_sh_body(POST_ARGS
)
1320 struct mdoc_node
*n
;
1322 if (SEC_NAME
!= mdoc
->lastnamed
)
1326 * Warn if the NAME section doesn't contain the `Nm' and `Nd'
1327 * macros (can have multiple `Nm' and one `Nd'). Note that the
1328 * children of the BODY declaration can also be "text".
1331 if (NULL
== (n
= mdoc
->last
->child
))
1332 return(vwarn(mdoc
, WNAMESECINC
));
1334 for ( ; n
&& n
->next
; n
= n
->next
) {
1335 if (MDOC_ELEM
== n
->type
&& MDOC_Nm
== n
->tok
)
1337 if (MDOC_TEXT
== n
->type
)
1339 if ( ! vwarn(mdoc
, WNAMESECINC
))
1343 if (MDOC_ELEM
== n
->type
&& MDOC_Nd
== n
->tok
)
1345 return(vwarn(mdoc
, WNAMESECINC
));
1350 post_sh_head(POST_ARGS
)
1354 const struct mdoc_node
*n
;
1357 * Process a new section. Sections are either "named" or
1358 * "custom"; custom sections are user-defined, while named ones
1359 * usually follow a conventional order and may only appear in
1360 * certain manual sections.
1363 assert(MDOC_Sh
== mdoc
->last
->tok
);
1365 /* This is just concat() inlined, which is irritating. */
1368 for (n
= mdoc
->last
->child
; n
; n
= n
->next
) {
1369 assert(MDOC_TEXT
== n
->type
);
1370 if (strlcat(buf
, n
->string
, 64) >= 64)
1371 return(nerr(mdoc
, n
, ETOOLONG
));
1372 if (NULL
== n
->next
)
1374 if (strlcat(buf
, " ", 64) >= 64)
1375 return(nerr(mdoc
, n
, ETOOLONG
));
1378 sec
= mdoc_atosec(buf
);
1380 /* The NAME section should always be first. */
1382 if (SEC_BODY
== mdoc
->lastnamed
&& SEC_NAME
!= sec
)
1383 return(vwarn(mdoc
, WSECOOO
));
1384 if (SEC_CUSTOM
== sec
)
1387 /* Check for repeated or out-of-order sections. */
1389 if (sec
== mdoc
->lastnamed
)
1390 return(vwarn(mdoc
, WSECREP
));
1391 if (sec
< mdoc
->lastnamed
)
1392 return(vwarn(mdoc
, WSECOOO
));
1394 /* Check particular section/manual section conventions. */
1398 switch (mdoc
->meta
.msec
) {
1404 return(vwarn(mdoc
, WWRONGMSEC
));
1419 return(check_sec(mdoc
, n
, SEC_SYNOPSIS
, SEC_CUSTOM
));