]>
git.cameronkatri.com Git - mandoc.git/blob - mdoc_validate.c
1 /* $Id: mdoc_validate.c,v 1.9 2009/06/12 09:18:00 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
73 typedef int (*v_pre
)(PRE_ARGS
);
74 typedef int (*v_post
)(POST_ARGS
);
81 static int pwarn(struct mdoc
*, int, int, enum mwarn
);
82 static int perr(struct mdoc
*, int, int, enum merr
);
83 static int check_parent(PRE_ARGS
, int, enum mdoc_type
);
84 static int check_msec(PRE_ARGS
, ...);
85 static int check_sec(PRE_ARGS
, ...);
86 static int check_stdarg(PRE_ARGS
);
87 static int check_text(struct mdoc
*, int, int, const char *);
88 static int check_argv(struct mdoc
*,
89 const struct mdoc_node
*,
90 const struct mdoc_argv
*);
91 static int check_args(struct mdoc
*,
92 const struct mdoc_node
*);
93 static int err_child_lt(struct mdoc
*, const char *, int);
94 static int warn_child_lt(struct mdoc
*, const char *, int);
95 static int err_child_gt(struct mdoc
*, const char *, int);
96 static int warn_child_gt(struct mdoc
*, const char *, int);
97 static int err_child_eq(struct mdoc
*, const char *, int);
98 static int warn_child_eq(struct mdoc
*, const char *, int);
99 static int count_child(struct mdoc
*);
100 static int warn_print(struct mdoc
*, int, int);
101 static int warn_count(struct mdoc
*, const char *,
102 int, const char *, int);
103 static int err_count(struct mdoc
*, const char *,
104 int, const char *, int);
105 static int pre_an(PRE_ARGS
);
106 static int pre_bd(PRE_ARGS
);
107 static int pre_bl(PRE_ARGS
);
108 static int pre_cd(PRE_ARGS
);
109 static int pre_dd(PRE_ARGS
);
110 static int pre_display(PRE_ARGS
);
111 static int pre_dt(PRE_ARGS
);
112 static int pre_er(PRE_ARGS
);
113 static int pre_ex(PRE_ARGS
);
114 static int pre_fd(PRE_ARGS
);
115 static int pre_it(PRE_ARGS
);
116 static int pre_lb(PRE_ARGS
);
117 static int pre_os(PRE_ARGS
);
118 static int pre_prologue(PRE_ARGS
);
119 static int pre_rv(PRE_ARGS
);
120 static int pre_sh(PRE_ARGS
);
121 static int pre_ss(PRE_ARGS
);
122 static int herr_ge1(POST_ARGS
);
123 static int hwarn_le1(POST_ARGS
);
124 static int herr_eq0(POST_ARGS
);
125 static int eerr_eq0(POST_ARGS
);
126 static int eerr_le2(POST_ARGS
);
127 static int eerr_eq1(POST_ARGS
);
128 static int eerr_ge1(POST_ARGS
);
129 static int ewarn_eq0(POST_ARGS
);
130 static int ewarn_eq1(POST_ARGS
);
131 static int bwarn_ge1(POST_ARGS
);
132 static int hwarn_eq1(POST_ARGS
);
133 static int ewarn_ge1(POST_ARGS
);
134 static int ebool(POST_ARGS
);
135 static int post_an(POST_ARGS
);
136 static int post_args(POST_ARGS
);
137 static int post_at(POST_ARGS
);
138 static int post_bf(POST_ARGS
);
139 static int post_bl(POST_ARGS
);
140 static int post_it(POST_ARGS
);
141 static int post_nm(POST_ARGS
);
142 static int post_root(POST_ARGS
);
143 static int post_sh(POST_ARGS
);
144 static int post_sh_body(POST_ARGS
);
145 static int post_sh_head(POST_ARGS
);
146 static int post_st(POST_ARGS
);
148 #define vwarn(m, t) nwarn((m), (m)->last, (t))
149 #define verr(m, t) nerr((m), (m)->last, (t))
150 #define nwarn(m, n, t) pwarn((m), (n)->line, (n)->pos, (t))
151 #define nerr(m, n, t) perr((m), (n)->line, (n)->pos, (t))
153 static v_pre pres_an
[] = { pre_an
, NULL
};
154 static v_pre pres_bd
[] = { pre_display
, pre_bd
, NULL
};
155 static v_pre pres_bl
[] = { pre_bl
, NULL
};
156 static v_pre pres_cd
[] = { pre_cd
, NULL
};
157 static v_pre pres_dd
[] = { pre_prologue
, pre_dd
, NULL
};
158 static v_pre pres_d1
[] = { pre_display
, NULL
};
159 static v_pre pres_dt
[] = { pre_prologue
, pre_dt
, NULL
};
160 static v_pre pres_er
[] = { pre_er
, NULL
};
161 static v_pre pres_ex
[] = { pre_ex
, NULL
};
162 static v_pre pres_fd
[] = { pre_fd
, NULL
};
163 static v_pre pres_it
[] = { pre_it
, NULL
};
164 static v_pre pres_lb
[] = { pre_lb
, NULL
};
165 static v_pre pres_os
[] = { pre_prologue
, pre_os
, NULL
};
166 static v_pre pres_rv
[] = { pre_rv
, NULL
};
167 static v_pre pres_sh
[] = { pre_sh
, NULL
};
168 static v_pre pres_ss
[] = { pre_ss
, NULL
};
169 static v_post posts_bool
[] = { eerr_eq1
, ebool
, NULL
};
170 static v_post posts_bd
[] = { herr_eq0
, bwarn_ge1
, NULL
};
171 static v_post posts_text
[] = { eerr_ge1
, NULL
};
172 static v_post posts_wtext
[] = { ewarn_ge1
, NULL
};
173 static v_post posts_notext
[] = { eerr_eq0
, NULL
};
174 static v_post posts_wline
[] = { bwarn_ge1
, herr_eq0
, NULL
};
175 static v_post posts_sh
[] = { herr_ge1
, bwarn_ge1
, post_sh
, NULL
};
176 static v_post posts_bl
[] = { herr_eq0
, bwarn_ge1
, post_bl
, NULL
};
177 static v_post posts_it
[] = { post_it
, NULL
};
178 static v_post posts_in
[] = { ewarn_eq1
, NULL
};
179 static v_post posts_ss
[] = { herr_ge1
, NULL
};
180 static v_post posts_pf
[] = { eerr_eq1
, NULL
};
181 static v_post posts_lb
[] = { eerr_eq1
, NULL
};
182 static v_post posts_st
[] = { eerr_eq1
, post_st
, NULL
};
183 static v_post posts_pp
[] = { ewarn_eq0
, NULL
};
184 static v_post posts_ex
[] = { eerr_eq0
, post_args
, NULL
};
185 static v_post posts_rv
[] = { eerr_eq0
, post_args
, NULL
};
186 static v_post posts_an
[] = { post_an
, NULL
};
187 static v_post posts_at
[] = { post_at
, NULL
};
188 static v_post posts_xr
[] = { eerr_ge1
, eerr_le2
, NULL
};
189 static v_post posts_nm
[] = { post_nm
, NULL
};
190 static v_post posts_bf
[] = { hwarn_le1
, post_bf
, NULL
};
191 static v_post posts_fo
[] = { hwarn_eq1
, bwarn_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
, NULL
}, /* Dv */
214 { pres_er
, posts_text
}, /* Er */
215 { NULL
, NULL
}, /* Ev */
216 { pres_ex
, posts_ex
}, /* Ex */
217 { NULL
, NULL
}, /* Fa */
218 { pres_fd
, posts_wtext
}, /* 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
, NULL
}, /* 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_rv
}, /* Rv */
231 { NULL
, posts_st
}, /* St */
232 { NULL
, NULL
}, /* 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
, NULL
}, /* 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_wline
}, /* 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_wline
}, /* 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 */
301 { NULL
, NULL
}, /* Ap */
302 { NULL
, posts_pp
}, /* Lp */
303 { NULL
, NULL
}, /* Lk */
304 { NULL
, posts_text
}, /* Mt */
305 { NULL
, posts_wline
}, /* Brq */
306 { NULL
, NULL
}, /* Bro */
307 { NULL
, NULL
}, /* Brc */
308 { NULL
, posts_text
}, /* %C */
309 { NULL
, NULL
}, /* Es */
310 { NULL
, NULL
}, /* En */
311 { NULL
, NULL
}, /* Dx */
312 { NULL
, posts_text
}, /* %Q */
317 extern size_t strlcat(char *, const char *, size_t);
322 mdoc_valid_pre(struct mdoc
*mdoc
,
323 const struct mdoc_node
*n
)
329 if (MDOC_TEXT
== n
->type
) {
333 return(check_text(mdoc
, line
, pos
, tp
));
336 if ( ! check_args(mdoc
, n
))
338 if (NULL
== mdoc_valids
[n
->tok
].pre
)
340 for (p
= mdoc_valids
[n
->tok
].pre
; *p
; p
++)
341 if ( ! (*p
)(mdoc
, n
))
348 mdoc_valid_post(struct mdoc
*mdoc
)
353 * This check occurs after the macro's children have been filled
354 * in: postfix validation. Since this happens when we're
355 * rewinding the scope tree, it's possible to have multiple
356 * invocations (as by design, for now), we set bit MDOC_VALID to
357 * indicate that we've validated.
360 if (MDOC_VALID
& mdoc
->last
->flags
)
362 mdoc
->last
->flags
|= MDOC_VALID
;
364 if (MDOC_TEXT
== mdoc
->last
->type
)
366 if (MDOC_ROOT
== mdoc
->last
->type
)
367 return(post_root(mdoc
));
369 if (NULL
== mdoc_valids
[mdoc
->last
->tok
].post
)
371 for (p
= mdoc_valids
[mdoc
->last
->tok
].post
; *p
; p
++)
380 perr(struct mdoc
*m
, int line
, int pos
, enum merr type
)
387 p
= "text argument too long";
390 p
= "invalid escape sequence";
393 p
= "invalid character";
396 p
= "displays may not be nested";
399 p
= "expected boolean value";
402 p
= "argument repeated";
405 p
= "multiple display types specified";
408 p
= "multiple list types specified";
411 p
= "missing list type";
414 p
= "missing display type";
417 p
= "expected line arguments";
420 p
= "document has no prologue";
423 p
= "document has no data";
426 p
= "expected valid AT&T symbol";
429 p
= "default name not yet set";
433 return(mdoc_perr(m
, line
, pos
, p
));
438 pwarn(struct mdoc
*m
, int line
, int pos
, enum mwarn type
)
447 p
= "inappropriate manual section";
451 p
= "inappropriate document section";
455 p
= "argument value suggested";
459 p
= "prologue macros repeated";
463 p
= "prologue macros out-of-order";
467 p
= "deprecated column argument syntax";
471 p
= "superfluous width argument";
474 p
= "missing width argument";
477 p
= "invalid character";
480 p
= "invalid escape sequence";
483 p
= "suggested no line arguments";
486 p
= "suggested line arguments";
489 p
= "suggested multi-line arguments";
492 p
= "suggested no multi-line arguments";
495 p
= "document section in wrong manual section";
499 p
= "document section out of conventional order";
502 p
= "document section repeated";
505 p
= "unknown standard";
508 p
= "NAME section contents incomplete/badly-ordered";
512 return(mdoc_pwarn(m
, line
, pos
, c
, p
));
517 warn_print(struct mdoc
*m
, int ln
, int pos
)
519 if (MDOC_IGN_CHARS
& m
->pflags
)
520 return(pwarn(m
, ln
, pos
, WPRINT
));
521 return(perr(m
, ln
, pos
, EPRINT
));
526 warn_count(struct mdoc
*m
, const char *k
,
527 int want
, const char *v
, int has
)
530 return(mdoc_warn(m
, WARN_SYNTAX
,
531 "suggests %s %s %d (has %d)", v
, k
, want
, has
));
536 err_count(struct mdoc
*m
, const char *k
,
537 int want
, const char *v
, int has
)
541 "requires %s %s %d (has %d)", v
, k
, want
, has
));
546 count_child(struct mdoc
*mdoc
)
551 for (i
= 0, n
= mdoc
->last
->child
; n
; n
= n
->next
, i
++)
559 * Build these up with macros because they're basically the same check
560 * for different inequalities. Yes, this could be done with functions,
561 * but this is reasonable for now.
564 #define CHECK_CHILD_DEFN(lvl, name, ineq) \
566 lvl##_child_##name(struct mdoc *mdoc, const char *p, int sz) \
569 if ((i = count_child(mdoc)) ineq sz) \
571 return(lvl##_count(mdoc, #ineq, sz, p, i)); \
574 #define CHECK_BODY_DEFN(name, lvl, func, num) \
576 b##lvl##_##name(POST_ARGS) \
578 if (MDOC_BODY != mdoc->last->type) \
580 return(func(mdoc, "multi-line arguments", (num))); \
583 #define CHECK_ELEM_DEFN(name, lvl, func, num) \
585 e##lvl##_##name(POST_ARGS) \
587 assert(MDOC_ELEM == mdoc->last->type); \
588 return(func(mdoc, "line arguments", (num))); \
591 #define CHECK_HEAD_DEFN(name, lvl, func, num) \
593 h##lvl##_##name(POST_ARGS) \
595 if (MDOC_HEAD != mdoc->last->type) \
597 return(func(mdoc, "line arguments", (num))); \
601 CHECK_CHILD_DEFN(warn
, gt
, >) /* warn_child_gt() */
602 CHECK_CHILD_DEFN(err
, gt
, >) /* err_child_gt() */
603 CHECK_CHILD_DEFN(warn
, eq
, ==) /* warn_child_eq() */
604 CHECK_CHILD_DEFN(err
, eq
, ==) /* err_child_eq() */
605 CHECK_CHILD_DEFN(err
, lt
, <) /* err_child_lt() */
606 CHECK_CHILD_DEFN(warn
, lt
, <) /* warn_child_lt() */
607 CHECK_BODY_DEFN(ge1
, warn
, warn_child_gt
, 0) /* bwarn_ge1() */
608 CHECK_ELEM_DEFN(eq1
, warn
, warn_child_eq
, 1) /* ewarn_eq1() */
609 CHECK_ELEM_DEFN(eq0
, warn
, warn_child_eq
, 0) /* ewarn_eq0() */
610 CHECK_ELEM_DEFN(ge1
, warn
, warn_child_gt
, 0) /* ewarn_gt1() */
611 CHECK_ELEM_DEFN(eq1
, err
, err_child_eq
, 1) /* eerr_eq1() */
612 CHECK_ELEM_DEFN(le2
, err
, err_child_lt
, 3) /* eerr_le2() */
613 CHECK_ELEM_DEFN(eq0
, err
, err_child_eq
, 0) /* eerr_eq0() */
614 CHECK_ELEM_DEFN(ge1
, err
, err_child_gt
, 0) /* eerr_ge1() */
615 CHECK_HEAD_DEFN(eq0
, err
, err_child_eq
, 0) /* herr_eq0() */
616 CHECK_HEAD_DEFN(le1
, warn
, warn_child_lt
, 2) /* hwarn_le1() */
617 CHECK_HEAD_DEFN(ge1
, err
, err_child_gt
, 0) /* herr_ge1() */
618 CHECK_HEAD_DEFN(eq1
, warn
, warn_child_eq
, 1) /* hwarn_eq1() */
622 check_stdarg(PRE_ARGS
)
625 if (n
->args
&& 1 == n
->args
->argc
)
626 if (MDOC_Std
== n
->args
->argv
[0].arg
)
628 return(nwarn(mdoc
, n
, WARGVAL
));
633 check_sec(PRE_ARGS
, ...)
642 sec
= (enum mdoc_sec
)va_arg(ap
, int);
643 if (SEC_CUSTOM
== sec
)
645 if (sec
!= mdoc
->lastsec
)
652 return(nwarn(mdoc
, n
, WBADSEC
));
657 check_msec(PRE_ARGS
, ...)
665 if (0 == (msec
= va_arg(ap
, int)))
667 if (msec
!= mdoc
->meta
.msec
)
674 return(nwarn(mdoc
, n
, WBADMSEC
));
679 check_args(struct mdoc
*m
, const struct mdoc_node
*n
)
686 assert(n
->args
->argc
);
687 for (i
= 0; i
< (int)n
->args
->argc
; i
++)
688 if ( ! check_argv(m
, n
, &n
->args
->argv
[i
]))
696 check_argv(struct mdoc
*m
, const struct mdoc_node
*n
,
697 const struct mdoc_argv
*v
)
701 for (i
= 0; i
< (int)v
->sz
; i
++)
702 if ( ! check_text(m
, v
->line
, v
->pos
, v
->value
[i
]))
705 if (MDOC_Std
== v
->arg
) {
706 /* `Nm' name must be set. */
707 if (v
->sz
|| m
->meta
.name
)
709 return(nerr(m
, n
, ENAME
));
717 check_text(struct mdoc
*mdoc
, int line
, int pos
, const char *p
)
721 /* FIXME: indicate deprecated escapes \*(xx and \*x. */
725 if ( ! (MDOC_LITERAL
& mdoc
->flags
))
726 if ( ! warn_print(mdoc
, line
, pos
))
728 } else if ( ! isprint((u_char
)*p
))
729 if ( ! warn_print(mdoc
, line
, pos
))
735 c
= mdoc_isescape(p
);
740 if ( ! (MDOC_IGN_ESCAPE
& mdoc
->pflags
))
741 return(perr(mdoc
, line
, pos
, EESCAPE
));
742 if ( ! pwarn(mdoc
, line
, pos
, WESCAPE
))
753 check_parent(PRE_ARGS
, int tok
, enum mdoc_type t
)
757 if ((MDOC_ROOT
== t
|| tok
== n
->parent
->tok
) &&
758 (t
== n
->parent
->type
))
761 return(mdoc_nerr(mdoc
, n
, "require parent %s",
762 MDOC_ROOT
== t
? "<root>" : mdoc_macronames
[tok
]));
768 pre_display(PRE_ARGS
)
770 struct mdoc_node
*node
;
772 /* Display elements (`Bd', `D1'...) cannot be nested. */
774 if (MDOC_BLOCK
!= n
->type
)
778 for (node
= mdoc
->last
->parent
; node
; node
= node
->parent
)
779 if (MDOC_BLOCK
== node
->type
)
780 if (MDOC_Bd
== node
->tok
)
785 return(nerr(mdoc
, n
, ENESTDISP
));
792 int pos
, col
, type
, width
, offset
;
794 if (MDOC_BLOCK
!= n
->type
)
797 return(nerr(mdoc
, n
, ELISTTYPE
));
799 /* Make sure that only one type of list is specified. */
801 type
= offset
= width
= col
= -1;
804 for (pos
= 0; pos
< (int)n
->args
->argc
; pos
++)
805 switch (n
->args
->argv
[pos
].arg
) {
828 return(nerr(mdoc
, n
, EMULTILIST
));
829 type
= n
->args
->argv
[pos
].arg
;
834 return(nerr(mdoc
, n
, EARGREP
));
835 width
= n
->args
->argv
[pos
].arg
;
839 return(nerr(mdoc
, n
, EARGREP
));
840 offset
= n
->args
->argv
[pos
].arg
;
847 return(nerr(mdoc
, n
, ELISTTYPE
));
850 * Validate the width field. Some list types don't need width
851 * types and should be warned about them. Others should have it
852 * and must also be warned.
857 if (-1 == width
&& ! nwarn(mdoc
, n
, WMISSWIDTH
))
867 if (-1 != width
&& ! nwarn(mdoc
, n
, WNOWIDTH
))
875 * General validation of fields.
881 if (0 == n
->args
->argv
[col
].sz
)
883 if ( ! nwarn(mdoc
, n
, WDEPCOL
))
899 if (MDOC_BLOCK
!= n
->type
)
902 return(nerr(mdoc
, n
, EDISPTYPE
));
904 /* Make sure that only one type of display is specified. */
907 for (i
= 0, err
= type
= 0; ! err
&&
908 i
< (int)n
->args
->argc
; i
++)
909 switch (n
->args
->argv
[i
].arg
) {
912 case (MDOC_Unfilled
):
921 return(nerr(mdoc
, n
, EMULTIDISP
));
928 return(nerr(mdoc
, n
, EDISPTYPE
));
936 if (MDOC_BLOCK
!= n
->type
)
938 return(check_parent(mdoc
, n
, MDOC_Sh
, MDOC_BODY
));
946 if (MDOC_BLOCK
!= n
->type
)
948 return(check_parent(mdoc
, n
, -1, MDOC_ROOT
));
956 if (MDOC_BLOCK
!= n
->type
)
958 return(check_parent(mdoc
, n
, MDOC_Bl
, MDOC_BODY
));
966 if (NULL
== n
->args
|| 1 == n
->args
->argc
)
968 return(mdoc_nerr(mdoc
, n
, "only one argument allowed"));
976 return(check_sec(mdoc
, n
, SEC_LIBRARY
, SEC_CUSTOM
));
984 if ( ! check_msec(mdoc
, n
, 2, 3, 0))
986 return(check_stdarg(mdoc
, n
));
994 if ( ! check_msec(mdoc
, n
, 1, 6, 8, 0))
996 return(check_stdarg(mdoc
, n
));
1004 return(check_msec(mdoc
, n
, 2, 3, 9, 0));
1012 return(check_msec(mdoc
, n
, 4, 0));
1017 pre_prologue(PRE_ARGS
)
1020 return(check_sec(mdoc
, n
, SEC_PROLOGUE
, SEC_CUSTOM
));
1028 if (0 == mdoc
->meta
.date
|| mdoc
->meta
.os
)
1029 if ( ! nwarn(mdoc
, n
, WPROLOOO
))
1031 if (mdoc
->meta
.title
)
1032 if ( ! nwarn(mdoc
, n
, WPROLREP
))
1042 if (NULL
== mdoc
->meta
.title
|| 0 == mdoc
->meta
.date
)
1043 if ( ! nwarn(mdoc
, n
, WPROLOOO
))
1046 if ( ! nwarn(mdoc
, n
, WPROLREP
))
1056 if (mdoc
->meta
.title
|| mdoc
->meta
.os
)
1057 if ( ! nwarn(mdoc
, n
, WPROLOOO
))
1059 if (mdoc
->meta
.date
)
1060 if ( ! nwarn(mdoc
, n
, WPROLREP
))
1070 struct mdoc_node
*head
;
1072 if (MDOC_BLOCK
!= mdoc
->last
->type
)
1075 head
= mdoc
->last
->head
;
1077 if (NULL
== mdoc
->last
->args
) {
1078 if (NULL
== head
->child
||
1079 MDOC_TEXT
!= head
->child
->type
)
1080 return(mdoc_err(mdoc
, "text argument expected"));
1082 p
= head
->child
->string
;
1083 if (0 == strcmp(p
, "Em"))
1085 else if (0 == strcmp(p
, "Li"))
1087 else if (0 == strcmp(p
, "Sm"))
1089 return(mdoc_nerr(mdoc
, head
->child
, "invalid font"));
1093 return(mdoc_err(mdoc
, "one argument expected"));
1103 if (mdoc
->last
->child
)
1105 if (mdoc
->meta
.name
)
1107 return(verr(mdoc
, ENAME
));
1115 if (NULL
== mdoc
->last
->child
)
1117 if (MDOC_TEXT
!= mdoc
->last
->child
->type
)
1118 return(verr(mdoc
, EATT
));
1119 if (mdoc_a2att(mdoc
->last
->child
->string
))
1121 return(verr(mdoc
, EATT
));
1129 if (mdoc
->last
->args
) {
1130 if (NULL
== mdoc
->last
->child
)
1132 return(verr(mdoc
, ELINE
));
1135 if (mdoc
->last
->child
)
1137 return(verr(mdoc
, ELINE
));
1142 post_args(POST_ARGS
)
1145 if (mdoc
->last
->args
)
1147 return(verr(mdoc
, ELINE
));
1155 struct mdoc_node
*n
, *c
;
1157 if (MDOC_BLOCK
!= mdoc
->last
->type
)
1160 n
= mdoc
->last
->parent
->parent
;
1161 if (NULL
== n
->args
)
1162 return(verr(mdoc
, ELISTTYPE
));
1164 /* Some types require block-head, some not. */
1167 for (cols
= type
= -1, i
= 0; -1 == type
&&
1168 i
< (int)n
->args
->argc
; i
++)
1169 switch (n
->args
->argv
[i
].arg
) {
1189 type
= n
->args
->argv
[i
].arg
;
1192 type
= n
->args
->argv
[i
].arg
;
1193 cols
= (int)n
->args
->argv
[i
].sz
;
1200 return(verr(mdoc
, ELISTTYPE
));
1204 if (NULL
== mdoc
->last
->head
->child
)
1205 if ( ! vwarn(mdoc
, WLINE
))
1215 if (NULL
== mdoc
->last
->head
->child
)
1216 if ( ! vwarn(mdoc
, WLINE
))
1218 if (NULL
== mdoc
->last
->body
->child
)
1219 if ( ! vwarn(mdoc
, WMULTILINE
))
1231 if (mdoc
->last
->head
->child
)
1232 if ( ! vwarn(mdoc
, WNOLINE
))
1234 if (NULL
== mdoc
->last
->body
->child
)
1235 if ( ! vwarn(mdoc
, WMULTILINE
))
1239 if (NULL
== mdoc
->last
->head
->child
)
1240 if ( ! vwarn(mdoc
, WLINE
))
1242 if (mdoc
->last
->body
->child
)
1243 if ( ! vwarn(mdoc
, WNOMULTILINE
))
1245 c
= mdoc
->last
->child
;
1246 for (i
= 0; c
&& MDOC_HEAD
== c
->type
; c
= c
->next
)
1250 return(mdoc_err(mdoc
, "column mismatch (have "
1251 "%d, want %d)", i
, cols
));
1263 struct mdoc_node
*n
;
1265 if (MDOC_BODY
!= mdoc
->last
->type
)
1267 if (NULL
== mdoc
->last
->child
)
1271 for (n
= mdoc
->last
->child
; n
; n
= n
->next
) {
1272 if (MDOC_BLOCK
== n
->type
)
1273 if (MDOC_It
== n
->tok
)
1275 return(mdoc_nerr(mdoc
, n
, "bad child of parent %s",
1276 mdoc_macronames
[mdoc
->last
->tok
]));
1284 ebool(struct mdoc
*mdoc
)
1286 struct mdoc_node
*n
;
1289 for (n
= mdoc
->last
->child
; n
; n
= n
->next
) {
1290 if (MDOC_TEXT
!= n
->type
)
1292 if (0 == strcmp(n
->string
, "on"))
1294 if (0 == strcmp(n
->string
, "off"))
1301 return(nerr(mdoc
, n
, EBOOL
));
1306 post_root(POST_ARGS
)
1309 if (NULL
== mdoc
->first
->child
)
1310 return(verr(mdoc
, ENODATA
));
1311 if (SEC_PROLOGUE
== mdoc
->lastnamed
)
1312 return(verr(mdoc
, ENOPROLOGUE
));
1314 if (MDOC_BLOCK
!= mdoc
->first
->child
->type
)
1315 return(verr(mdoc
, ENODATA
));
1316 if (MDOC_Sh
!= mdoc
->first
->child
->tok
)
1317 return(verr(mdoc
, ENODATA
));
1327 if (mdoc_a2st(mdoc
->last
->child
->string
))
1329 return(vwarn(mdoc
, WBADSTAND
));
1337 if (MDOC_HEAD
== mdoc
->last
->type
)
1338 return(post_sh_head(mdoc
));
1339 if (MDOC_BODY
== mdoc
->last
->type
)
1340 return(post_sh_body(mdoc
));
1347 post_sh_body(POST_ARGS
)
1349 struct mdoc_node
*n
;
1351 if (SEC_NAME
!= mdoc
->lastnamed
)
1355 * Warn if the NAME section doesn't contain the `Nm' and `Nd'
1356 * macros (can have multiple `Nm' and one `Nd'). Note that the
1357 * children of the BODY declaration can also be "text".
1360 if (NULL
== (n
= mdoc
->last
->child
))
1361 return(vwarn(mdoc
, WNAMESECINC
));
1363 for ( ; n
&& n
->next
; n
= n
->next
) {
1364 if (MDOC_ELEM
== n
->type
&& MDOC_Nm
== n
->tok
)
1366 if (MDOC_TEXT
== n
->type
)
1368 if ( ! vwarn(mdoc
, WNAMESECINC
))
1372 if (MDOC_ELEM
== n
->type
&& MDOC_Nd
== n
->tok
)
1374 return(vwarn(mdoc
, WNAMESECINC
));
1379 post_sh_head(POST_ARGS
)
1383 const struct mdoc_node
*n
;
1386 * Process a new section. Sections are either "named" or
1387 * "custom"; custom sections are user-defined, while named ones
1388 * usually follow a conventional order and may only appear in
1389 * certain manual sections.
1392 assert(MDOC_Sh
== mdoc
->last
->tok
);
1394 /* This is just concat() inlined, which is irritating. */
1397 for (n
= mdoc
->last
->child
; n
; n
= n
->next
) {
1398 assert(MDOC_TEXT
== n
->type
);
1399 if (strlcat(buf
, n
->string
, 64) >= 64)
1400 return(nerr(mdoc
, n
, ETOOLONG
));
1401 if (NULL
== n
->next
)
1403 if (strlcat(buf
, " ", 64) >= 64)
1404 return(nerr(mdoc
, n
, ETOOLONG
));
1407 sec
= mdoc_atosec(buf
);
1409 /* The NAME section should always be first. */
1411 if (SEC_BODY
== mdoc
->lastnamed
&& SEC_NAME
!= sec
)
1412 return(vwarn(mdoc
, WSECOOO
));
1413 if (SEC_CUSTOM
== sec
)
1416 /* Check for repeated or out-of-order sections. */
1418 if (sec
== mdoc
->lastnamed
)
1419 return(vwarn(mdoc
, WSECREP
));
1420 if (sec
< mdoc
->lastnamed
)
1421 return(vwarn(mdoc
, WSECOOO
));
1423 /* Check particular section/manual section conventions. */
1427 switch (mdoc
->meta
.msec
) {
1433 return(vwarn(mdoc
, WWRONGMSEC
));
1448 return(check_sec(mdoc
, n
, SEC_SYNOPSIS
, SEC_CUSTOM
));