]>
git.cameronkatri.com Git - mandoc.git/blob - mdoc_validate.c
48e000c7ddb70b0f0e05bbd8497c5a8b2a11b7ad
1 /* $Id: mdoc_validate.c,v 1.90 2010/05/31 23:40:25 kristaps Exp $ */
3 * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@bsd.lv>
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.
21 #include <sys/types.h>
32 #include "libmandoc.h"
34 /* FIXME: .Bl -diag can't have non-text children in HEAD. */
35 /* TODO: ignoring Pp (it's superfluous in some invocations). */
37 #define PRE_ARGS struct mdoc *mdoc, const struct mdoc_node *n
38 #define POST_ARGS struct mdoc *mdoc
40 typedef int (*v_pre
)(PRE_ARGS
);
41 typedef int (*v_post
)(POST_ARGS
);
48 static int check_parent(PRE_ARGS
, enum mdoct
, enum mdoc_type
);
49 static int check_stdarg(PRE_ARGS
);
50 static int check_text(struct mdoc
*, int, int, const char *);
51 static int check_argv(struct mdoc
*,
52 const struct mdoc_node
*,
53 const struct mdoc_argv
*);
54 static int check_args(struct mdoc
*,
55 const struct mdoc_node
*);
56 static int err_child_lt(struct mdoc
*, const char *, int);
57 static int warn_child_lt(struct mdoc
*, const char *, int);
58 static int err_child_gt(struct mdoc
*, const char *, int);
59 static int warn_child_gt(struct mdoc
*, const char *, int);
60 static int err_child_eq(struct mdoc
*, const char *, int);
61 static int warn_child_eq(struct mdoc
*, const char *, int);
62 static int warn_count(struct mdoc
*, const char *,
63 int, const char *, int);
64 static int err_count(struct mdoc
*, const char *,
65 int, const char *, int);
67 static int berr_ge1(POST_ARGS
);
68 static int bwarn_ge1(POST_ARGS
);
69 static int ebool(POST_ARGS
);
70 static int eerr_eq0(POST_ARGS
);
71 static int eerr_eq1(POST_ARGS
);
72 static int eerr_ge1(POST_ARGS
);
73 static int eerr_le1(POST_ARGS
);
74 static int ewarn_ge1(POST_ARGS
);
75 static int herr_eq0(POST_ARGS
);
76 static int herr_ge1(POST_ARGS
);
77 static int hwarn_eq1(POST_ARGS
);
78 static int hwarn_eq0(POST_ARGS
);
79 static int hwarn_le1(POST_ARGS
);
81 static int post_an(POST_ARGS
);
82 static int post_at(POST_ARGS
);
83 static int post_bf(POST_ARGS
);
84 static int post_bl(POST_ARGS
);
85 static int post_bl_head(POST_ARGS
);
86 static int post_dt(POST_ARGS
);
87 static int post_it(POST_ARGS
);
88 static int post_lb(POST_ARGS
);
89 static int post_nm(POST_ARGS
);
90 static int post_root(POST_ARGS
);
91 static int post_rs(POST_ARGS
);
92 static int post_sh(POST_ARGS
);
93 static int post_sh_body(POST_ARGS
);
94 static int post_sh_head(POST_ARGS
);
95 static int post_st(POST_ARGS
);
96 static int post_eoln(POST_ARGS
);
97 static int post_vt(POST_ARGS
);
98 static int pre_an(PRE_ARGS
);
99 static int pre_bd(PRE_ARGS
);
100 static int pre_bl(PRE_ARGS
);
101 static int pre_dd(PRE_ARGS
);
102 static int pre_display(PRE_ARGS
);
103 static int pre_dt(PRE_ARGS
);
104 static int pre_it(PRE_ARGS
);
105 static int pre_os(PRE_ARGS
);
106 static int pre_rv(PRE_ARGS
);
107 static int pre_sh(PRE_ARGS
);
108 static int pre_ss(PRE_ARGS
);
110 static v_post posts_an
[] = { post_an
, NULL
};
111 static v_post posts_at
[] = { post_at
, NULL
};
112 static v_post posts_bd
[] = { hwarn_eq0
, bwarn_ge1
, NULL
};
113 static v_post posts_bf
[] = { hwarn_le1
, post_bf
, NULL
};
114 static v_post posts_bl
[] = { bwarn_ge1
, post_bl
, NULL
};
115 static v_post posts_bool
[] = { eerr_eq1
, ebool
, NULL
};
116 static v_post posts_eoln
[] = { post_eoln
, NULL
};
117 static v_post posts_dt
[] = { post_dt
, NULL
};
118 static v_post posts_fo
[] = { hwarn_eq1
, bwarn_ge1
, NULL
};
119 static v_post posts_it
[] = { post_it
, NULL
};
120 static v_post posts_lb
[] = { eerr_eq1
, post_lb
, NULL
};
121 static v_post posts_nd
[] = { berr_ge1
, NULL
};
122 static v_post posts_nm
[] = { post_nm
, NULL
};
123 static v_post posts_notext
[] = { eerr_eq0
, NULL
};
124 static v_post posts_rs
[] = { berr_ge1
, herr_eq0
, post_rs
, NULL
};
125 static v_post posts_sh
[] = { herr_ge1
, bwarn_ge1
, post_sh
, NULL
};
126 static v_post posts_sp
[] = { eerr_le1
, NULL
};
127 static v_post posts_ss
[] = { herr_ge1
, NULL
};
128 static v_post posts_st
[] = { eerr_eq1
, post_st
, NULL
};
129 static v_post posts_text
[] = { eerr_ge1
, NULL
};
130 static v_post posts_text1
[] = { eerr_eq1
, NULL
};
131 static v_post posts_vt
[] = { post_vt
, NULL
};
132 static v_post posts_wline
[] = { bwarn_ge1
, herr_eq0
, NULL
};
133 static v_post posts_wtext
[] = { ewarn_ge1
, NULL
};
134 static v_pre pres_an
[] = { pre_an
, NULL
};
135 static v_pre pres_bd
[] = { pre_display
, pre_bd
, NULL
};
136 static v_pre pres_bl
[] = { pre_bl
, NULL
};
137 static v_pre pres_d1
[] = { pre_display
, NULL
};
138 static v_pre pres_dd
[] = { pre_dd
, NULL
};
139 static v_pre pres_dt
[] = { pre_dt
, NULL
};
140 static v_pre pres_er
[] = { NULL
, NULL
};
141 static v_pre pres_ex
[] = { NULL
, NULL
};
142 static v_pre pres_fd
[] = { NULL
, NULL
};
143 static v_pre pres_it
[] = { pre_it
, NULL
};
144 static v_pre pres_os
[] = { pre_os
, NULL
};
145 static v_pre pres_rv
[] = { pre_rv
, NULL
};
146 static v_pre pres_sh
[] = { pre_sh
, NULL
};
147 static v_pre pres_ss
[] = { pre_ss
, NULL
};
149 const struct valids mdoc_valids
[MDOC_MAX
] = {
150 { NULL
, NULL
}, /* Ap */
151 { pres_dd
, posts_text
}, /* Dd */
152 { pres_dt
, posts_dt
}, /* Dt */
153 { pres_os
, NULL
}, /* Os */
154 { pres_sh
, posts_sh
}, /* Sh */
155 { pres_ss
, posts_ss
}, /* Ss */
156 { NULL
, posts_notext
}, /* Pp */
157 { pres_d1
, posts_wline
}, /* D1 */
158 { pres_d1
, posts_wline
}, /* Dl */
159 { pres_bd
, posts_bd
}, /* Bd */
160 { NULL
, NULL
}, /* Ed */
161 { pres_bl
, posts_bl
}, /* Bl */
162 { NULL
, NULL
}, /* El */
163 { pres_it
, posts_it
}, /* It */
164 { NULL
, posts_text
}, /* Ad */
165 { pres_an
, posts_an
}, /* An */
166 { NULL
, NULL
}, /* Ar */
167 { NULL
, posts_text
}, /* Cd */
168 { NULL
, NULL
}, /* Cm */
169 { NULL
, NULL
}, /* Dv */
170 { pres_er
, posts_text
}, /* Er */
171 { NULL
, NULL
}, /* Ev */
172 { pres_ex
, NULL
}, /* Ex */
173 { NULL
, NULL
}, /* Fa */
174 { pres_fd
, posts_wtext
}, /* Fd */
175 { NULL
, NULL
}, /* Fl */
176 { NULL
, posts_text
}, /* Fn */
177 { NULL
, posts_wtext
}, /* Ft */
178 { NULL
, posts_text
}, /* Ic */
179 { NULL
, posts_text1
}, /* In */
180 { NULL
, NULL
}, /* Li */
181 { NULL
, posts_nd
}, /* Nd */
182 { NULL
, posts_nm
}, /* Nm */
183 { NULL
, posts_wline
}, /* Op */
184 { NULL
, NULL
}, /* Ot */
185 { NULL
, NULL
}, /* Pa */
186 { pres_rv
, NULL
}, /* Rv */
187 { NULL
, posts_st
}, /* St */
188 { NULL
, NULL
}, /* Va */
189 { NULL
, posts_vt
}, /* Vt */
190 { NULL
, posts_wtext
}, /* Xr */
191 { NULL
, posts_text
}, /* %A */
192 { NULL
, posts_text
}, /* %B */ /* FIXME: can be used outside Rs/Re. */
193 { NULL
, posts_text
}, /* %D */ /* FIXME: check date with mandoc_a2time(). */
194 { NULL
, posts_text
}, /* %I */
195 { NULL
, posts_text
}, /* %J */
196 { NULL
, posts_text
}, /* %N */
197 { NULL
, posts_text
}, /* %O */
198 { NULL
, posts_text
}, /* %P */
199 { NULL
, posts_text
}, /* %R */
200 { NULL
, posts_text
}, /* %T */ /* FIXME: can be used outside Rs/Re. */
201 { NULL
, posts_text
}, /* %V */
202 { NULL
, NULL
}, /* Ac */
203 { NULL
, NULL
}, /* Ao */
204 { NULL
, posts_wline
}, /* Aq */
205 { NULL
, posts_at
}, /* At */
206 { NULL
, NULL
}, /* Bc */
207 { NULL
, posts_bf
}, /* Bf */
208 { NULL
, NULL
}, /* Bo */
209 { NULL
, posts_wline
}, /* Bq */
210 { NULL
, NULL
}, /* Bsx */
211 { NULL
, NULL
}, /* Bx */
212 { NULL
, posts_bool
}, /* Db */
213 { NULL
, NULL
}, /* Dc */
214 { NULL
, NULL
}, /* Do */
215 { NULL
, posts_wline
}, /* Dq */
216 { NULL
, NULL
}, /* Ec */
217 { NULL
, NULL
}, /* Ef */
218 { NULL
, NULL
}, /* Em */
219 { NULL
, NULL
}, /* Eo */
220 { NULL
, NULL
}, /* Fx */
221 { NULL
, posts_text
}, /* Ms */
222 { NULL
, posts_notext
}, /* No */
223 { NULL
, posts_notext
}, /* Ns */
224 { NULL
, NULL
}, /* Nx */
225 { NULL
, NULL
}, /* Ox */
226 { NULL
, NULL
}, /* Pc */
227 { NULL
, posts_text1
}, /* Pf */
228 { NULL
, NULL
}, /* Po */
229 { NULL
, posts_wline
}, /* Pq */
230 { NULL
, NULL
}, /* Qc */
231 { NULL
, posts_wline
}, /* Ql */
232 { NULL
, NULL
}, /* Qo */
233 { NULL
, posts_wline
}, /* Qq */
234 { NULL
, NULL
}, /* Re */
235 { NULL
, posts_rs
}, /* Rs */
236 { NULL
, NULL
}, /* Sc */
237 { NULL
, NULL
}, /* So */
238 { NULL
, posts_wline
}, /* Sq */
239 { NULL
, posts_bool
}, /* Sm */
240 { NULL
, posts_text
}, /* Sx */
241 { NULL
, posts_text
}, /* Sy */
242 { NULL
, posts_text
}, /* Tn */
243 { NULL
, NULL
}, /* Ux */
244 { NULL
, NULL
}, /* Xc */
245 { NULL
, NULL
}, /* Xo */
246 { NULL
, posts_fo
}, /* Fo */
247 { NULL
, NULL
}, /* Fc */
248 { NULL
, NULL
}, /* Oo */
249 { NULL
, NULL
}, /* Oc */
250 { NULL
, posts_wline
}, /* Bk */
251 { NULL
, NULL
}, /* Ek */
252 { NULL
, posts_eoln
}, /* Bt */
253 { NULL
, NULL
}, /* Hf */
254 { NULL
, NULL
}, /* Fr */
255 { NULL
, posts_eoln
}, /* Ud */
256 { NULL
, posts_lb
}, /* Lb */
257 { NULL
, posts_notext
}, /* Lp */
258 { NULL
, posts_text
}, /* Lk */
259 { NULL
, posts_text
}, /* Mt */
260 { NULL
, posts_wline
}, /* Brq */
261 { NULL
, NULL
}, /* Bro */
262 { NULL
, NULL
}, /* Brc */
263 { NULL
, posts_text
}, /* %C */
264 { NULL
, NULL
}, /* Es */
265 { NULL
, NULL
}, /* En */
266 { NULL
, NULL
}, /* Dx */
267 { NULL
, posts_text
}, /* %Q */
268 { NULL
, posts_notext
}, /* br */
269 { NULL
, posts_sp
}, /* sp */
270 { NULL
, posts_text1
}, /* %U */
271 { NULL
, NULL
}, /* Ta */
276 mdoc_valid_pre(struct mdoc
*mdoc
, const struct mdoc_node
*n
)
282 if (MDOC_TEXT
== n
->type
) {
286 return(check_text(mdoc
, line
, pos
, tp
));
289 if ( ! check_args(mdoc
, n
))
291 if (NULL
== mdoc_valids
[n
->tok
].pre
)
293 for (p
= mdoc_valids
[n
->tok
].pre
; *p
; p
++)
294 if ( ! (*p
)(mdoc
, n
))
301 mdoc_valid_post(struct mdoc
*mdoc
)
305 if (MDOC_VALID
& mdoc
->last
->flags
)
307 mdoc
->last
->flags
|= MDOC_VALID
;
309 if (MDOC_TEXT
== mdoc
->last
->type
)
311 if (MDOC_ROOT
== mdoc
->last
->type
)
312 return(post_root(mdoc
));
314 if (NULL
== mdoc_valids
[mdoc
->last
->tok
].post
)
316 for (p
= mdoc_valids
[mdoc
->last
->tok
].post
; *p
; p
++)
325 warn_count(struct mdoc
*m
, const char *k
,
326 int want
, const char *v
, int has
)
329 return(mdoc_vmsg(m
, MANDOCERR_ARGCOUNT
,
330 m
->last
->line
, m
->last
->pos
,
331 "%s %s %d (have %d)", v
, k
, want
, has
));
336 err_count(struct mdoc
*m
, const char *k
,
337 int want
, const char *v
, int has
)
340 mdoc_vmsg(m
, MANDOCERR_SYNTARGCOUNT
,
341 m
->last
->line
, m
->last
->pos
,
342 "%s %s %d (have %d)",
349 * Build these up with macros because they're basically the same check
350 * for different inequalities. Yes, this could be done with functions,
351 * but this is reasonable for now.
354 #define CHECK_CHILD_DEFN(lvl, name, ineq) \
356 lvl##_child_##name(struct mdoc *mdoc, const char *p, int sz) \
358 if (mdoc->last->nchild ineq sz) \
360 return(lvl##_count(mdoc, #ineq, sz, p, mdoc->last->nchild)); \
363 #define CHECK_BODY_DEFN(name, lvl, func, num) \
365 b##lvl##_##name(POST_ARGS) \
367 if (MDOC_BODY != mdoc->last->type) \
369 return(func(mdoc, "multi-line arguments", (num))); \
372 #define CHECK_ELEM_DEFN(name, lvl, func, num) \
374 e##lvl##_##name(POST_ARGS) \
376 assert(MDOC_ELEM == mdoc->last->type); \
377 return(func(mdoc, "line arguments", (num))); \
380 #define CHECK_HEAD_DEFN(name, lvl, func, num) \
382 h##lvl##_##name(POST_ARGS) \
384 if (MDOC_HEAD != mdoc->last->type) \
386 return(func(mdoc, "line arguments", (num))); \
390 CHECK_CHILD_DEFN(warn
, gt
, >) /* warn_child_gt() */
391 CHECK_CHILD_DEFN(err
, gt
, >) /* err_child_gt() */
392 CHECK_CHILD_DEFN(warn
, eq
, ==) /* warn_child_eq() */
393 CHECK_CHILD_DEFN(err
, eq
, ==) /* err_child_eq() */
394 CHECK_CHILD_DEFN(err
, lt
, <) /* err_child_lt() */
395 CHECK_CHILD_DEFN(warn
, lt
, <) /* warn_child_lt() */
396 CHECK_BODY_DEFN(ge1
, warn
, warn_child_gt
, 0) /* bwarn_ge1() */
397 CHECK_BODY_DEFN(ge1
, err
, err_child_gt
, 0) /* berr_ge1() */
398 CHECK_ELEM_DEFN(ge1
, warn
, warn_child_gt
, 0) /* ewarn_ge1() */
399 CHECK_ELEM_DEFN(eq1
, err
, err_child_eq
, 1) /* eerr_eq1() */
400 CHECK_ELEM_DEFN(le1
, err
, err_child_lt
, 2) /* eerr_le1() */
401 CHECK_ELEM_DEFN(eq0
, err
, err_child_eq
, 0) /* eerr_eq0() */
402 CHECK_ELEM_DEFN(ge1
, err
, err_child_gt
, 0) /* eerr_ge1() */
403 CHECK_HEAD_DEFN(eq0
, err
, err_child_eq
, 0) /* herr_eq0() */
404 CHECK_HEAD_DEFN(le1
, warn
, warn_child_lt
, 2) /* hwarn_le1() */
405 CHECK_HEAD_DEFN(ge1
, err
, err_child_gt
, 0) /* herr_ge1() */
406 CHECK_HEAD_DEFN(eq1
, warn
, warn_child_eq
, 1) /* hwarn_eq1() */
407 CHECK_HEAD_DEFN(eq0
, warn
, warn_child_eq
, 0) /* hwarn_eq0() */
411 check_stdarg(PRE_ARGS
)
414 if (n
->args
&& 1 == n
->args
->argc
)
415 if (MDOC_Std
== n
->args
->argv
[0].arg
)
417 return(mdoc_nmsg(mdoc
, n
, MANDOCERR_NOARGV
));
422 check_args(struct mdoc
*m
, const struct mdoc_node
*n
)
429 assert(n
->args
->argc
);
430 for (i
= 0; i
< (int)n
->args
->argc
; i
++)
431 if ( ! check_argv(m
, n
, &n
->args
->argv
[i
]))
439 check_argv(struct mdoc
*m
, const struct mdoc_node
*n
,
440 const struct mdoc_argv
*v
)
444 for (i
= 0; i
< (int)v
->sz
; i
++)
445 if ( ! check_text(m
, v
->line
, v
->pos
, v
->value
[i
]))
448 if (MDOC_Std
== v
->arg
) {
449 if (v
->sz
|| m
->meta
.name
)
451 if ( ! mdoc_nmsg(m
, n
, MANDOCERR_NONAME
))
460 check_text(struct mdoc
*mdoc
, int line
, int pos
, const char *p
)
464 for ( ; *p
; p
++, pos
++) {
466 if ( ! (MDOC_LITERAL
& mdoc
->flags
))
467 if ( ! mdoc_pmsg(mdoc
, line
, pos
, MANDOCERR_BADCHAR
))
469 } else if ( ! isprint((u_char
)*p
) && ASCII_HYPH
!= *p
)
470 if ( ! mdoc_pmsg(mdoc
, line
, pos
, MANDOCERR_BADCHAR
))
476 c
= mandoc_special(p
);
483 c
= mdoc_pmsg(mdoc
, line
, pos
, MANDOCERR_BADESCAPE
);
484 if ( ! (MDOC_IGN_ESCAPE
& mdoc
->pflags
) && ! c
)
495 check_parent(PRE_ARGS
, enum mdoct tok
, enum mdoc_type t
)
499 if ((MDOC_ROOT
== t
|| tok
== n
->parent
->tok
) &&
500 (t
== n
->parent
->type
))
503 mdoc_vmsg(mdoc
, MANDOCERR_SYNTCHILD
,
504 n
->line
, n
->pos
, "want parent %s",
505 MDOC_ROOT
== t
? "<root>" :
506 mdoc_macronames
[tok
]);
513 pre_display(PRE_ARGS
)
515 struct mdoc_node
*node
;
517 /* Display elements (`Bd', `D1'...) cannot be nested. */
519 if (MDOC_BLOCK
!= n
->type
)
523 for (node
= mdoc
->last
->parent
; node
; node
= node
->parent
)
524 if (MDOC_BLOCK
== node
->type
)
525 if (MDOC_Bd
== node
->tok
)
530 mdoc_nmsg(mdoc
, n
, MANDOCERR_NESTEDDISP
);
538 int pos
, type
, width
, offset
;
540 if (MDOC_BLOCK
!= n
->type
)
542 if (NULL
== n
->args
) {
543 mdoc_nmsg(mdoc
, n
, MANDOCERR_LISTTYPE
);
547 /* Make sure that only one type of list is specified. */
549 type
= offset
= width
= -1;
552 for (pos
= 0; pos
< (int)n
->args
->argc
; pos
++)
553 switch (n
->args
->argv
[pos
].arg
) {
576 type
= n
->args
->argv
[pos
].arg
;
579 if (mdoc_nmsg(mdoc
, n
, MANDOCERR_LISTREP
))
585 if (mdoc_nmsg(mdoc
, n
, MANDOCERR_LISTFIRST
))
590 if ( ! mdoc_nmsg(mdoc
, n
, MANDOCERR_ARGVREP
))
592 if (type
< 0 && ! mdoc_nmsg(mdoc
, n
, MANDOCERR_LISTFIRST
))
594 width
= n
->args
->argv
[pos
].arg
;
598 if ( ! mdoc_nmsg(mdoc
, n
, MANDOCERR_ARGVREP
))
600 if (type
< 0 && ! mdoc_nmsg(mdoc
, n
, MANDOCERR_LISTFIRST
))
602 offset
= n
->args
->argv
[pos
].arg
;
609 mdoc_nmsg(mdoc
, n
, MANDOCERR_LISTTYPE
);
614 * Validate the width field. Some list types don't need width
615 * types and should be warned about them. Others should have it
616 * and must also be warned.
621 if (width
< 0 && ! mdoc_nmsg(mdoc
, n
, MANDOCERR_NOWIDTHARG
))
633 if (width
>= 0 && ! mdoc_nmsg(mdoc
, n
, MANDOCERR_WIDTHARG
))
649 if (MDOC_BLOCK
!= n
->type
)
651 if (NULL
== n
->args
) {
652 mdoc_nmsg(mdoc
, n
, MANDOCERR_DISPTYPE
);
656 /* Make sure that only one type of display is specified. */
659 for (i
= 0, err
= type
= 0; ! err
&&
660 i
< (int)n
->args
->argc
; i
++)
661 switch (n
->args
->argv
[i
].arg
) {
666 case (MDOC_Unfilled
):
673 if ( ! mdoc_nmsg(mdoc
, n
, MANDOCERR_DISPREP
))
682 mdoc_nmsg(mdoc
, n
, MANDOCERR_DISPTYPE
);
691 if (MDOC_BLOCK
!= n
->type
)
693 return(check_parent(mdoc
, n
, MDOC_Sh
, MDOC_BODY
));
701 if (MDOC_BLOCK
!= n
->type
)
703 return(check_parent(mdoc
, n
, MDOC_MAX
, MDOC_ROOT
));
711 if (MDOC_BLOCK
!= n
->type
)
714 * FIXME: this can probably be lifted if we make the It into
715 * something else on-the-fly?
717 return(check_parent(mdoc
, n
, MDOC_Bl
, MDOC_BODY
));
725 if (NULL
== n
->args
|| 1 == n
->args
->argc
)
727 mdoc_vmsg(mdoc
, MANDOCERR_SYNTARGCOUNT
,
729 "line arguments == 1 (have %d)",
739 return(check_stdarg(mdoc
, n
));
746 const struct mdoc_node
*nn
;
749 if (NULL
!= (nn
= mdoc
->last
->child
))
750 for (p
= nn
->string
; *p
; p
++) {
751 if (toupper((u_char
)*p
) == *p
)
753 if ( ! mdoc_nmsg(mdoc
, nn
, MANDOCERR_UPPERCASE
))
766 if (0 == mdoc
->meta
.date
|| mdoc
->meta
.os
)
767 if ( ! mdoc_nmsg(mdoc
, n
, MANDOCERR_PROLOGOOO
))
769 if (mdoc
->meta
.title
)
770 if ( ! mdoc_nmsg(mdoc
, n
, MANDOCERR_PROLOGREP
))
780 if (NULL
== mdoc
->meta
.title
|| 0 == mdoc
->meta
.date
)
781 if ( ! mdoc_nmsg(mdoc
, n
, MANDOCERR_PROLOGOOO
))
784 if ( ! mdoc_nmsg(mdoc
, n
, MANDOCERR_PROLOGREP
))
794 if (mdoc
->meta
.title
|| mdoc
->meta
.os
)
795 if ( ! mdoc_nmsg(mdoc
, n
, MANDOCERR_PROLOGOOO
))
798 if ( ! mdoc_nmsg(mdoc
, n
, MANDOCERR_PROLOGREP
))
808 struct mdoc_node
*head
;
810 if (MDOC_BLOCK
!= mdoc
->last
->type
)
813 head
= mdoc
->last
->head
;
815 if (mdoc
->last
->args
&& head
->child
) {
816 /* FIXME: this should provide a default. */
817 mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_SYNTARGVCOUNT
);
819 } else if (mdoc
->last
->args
)
822 if (NULL
== head
->child
|| MDOC_TEXT
!= head
->child
->type
) {
823 /* FIXME: this should provide a default. */
824 mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_SYNTARGVCOUNT
);
828 p
= head
->child
->string
;
830 if (0 == strcmp(p
, "Em"))
832 else if (0 == strcmp(p
, "Li"))
834 else if (0 == strcmp(p
, "Sy"))
837 mdoc_nmsg(mdoc
, head
, MANDOCERR_FONTTYPE
);
846 if (mdoc_a2lib(mdoc
->last
->child
->string
))
848 return(mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_BADLIB
));
856 if (NULL
== mdoc
->last
->child
)
858 return(mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_ARGSLOST
));
865 const struct mdoc_node
*n
;
868 * The Vt macro comes in both ELEM and BLOCK form, both of which
869 * have different syntaxes (yet more context-sensitive
870 * behaviour). ELEM types must have a child; BLOCK types,
871 * specifically the BODY, should only have TEXT children.
874 if (MDOC_ELEM
== mdoc
->last
->type
)
875 return(eerr_ge1(mdoc
));
876 if (MDOC_BODY
!= mdoc
->last
->type
)
879 for (n
= mdoc
->last
->child
; n
; n
= n
->next
)
880 if (MDOC_TEXT
!= n
->type
)
881 if ( ! mdoc_nmsg(mdoc
, n
, MANDOCERR_CHILD
))
892 if (mdoc
->last
->child
)
896 return(mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_NONAME
));
904 if (NULL
== mdoc
->last
->child
)
906 assert(MDOC_TEXT
== mdoc
->last
->child
->type
);
907 if (mdoc_a2att(mdoc
->last
->child
->string
))
909 return(mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_BADATT
));
917 if (mdoc
->last
->args
) {
918 if (NULL
== mdoc
->last
->child
)
920 return(mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_ARGCOUNT
));
923 if (mdoc
->last
->child
)
925 return(mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_NOARGS
));
934 struct mdoc_node
*n
, *c
;
937 if (MDOC_BLOCK
!= mdoc
->last
->type
)
940 n
= mdoc
->last
->parent
->parent
;
943 if (LIST__NONE
== lt
) {
944 mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_LISTTYPE
);
950 if (mdoc
->last
->head
->child
)
952 /* FIXME: give this a dummy value. */
953 if ( ! mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_NOARGS
))
963 if (NULL
== mdoc
->last
->head
->child
)
964 if ( ! mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_NOARGS
))
966 if (NULL
== mdoc
->last
->body
->child
)
967 if ( ! mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_NOBODY
))
979 if (mdoc
->last
->head
->child
)
980 if ( ! mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_ARGSLOST
))
982 if (NULL
== mdoc
->last
->body
->child
)
983 if ( ! mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_NOBODY
))
988 for (i
= 0; i
< (int)n
->args
->argc
; i
++)
989 if (MDOC_Column
== n
->args
->argv
[i
].arg
) {
990 cols
= (int)n
->args
->argv
[i
].sz
;
995 assert(NULL
== mdoc
->last
->head
->child
);
997 if (NULL
== mdoc
->last
->body
->child
)
998 if ( ! mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_NOBODY
))
1001 for (i
= 0, c
= mdoc
->last
->child
; c
; c
= c
->next
)
1002 if (MDOC_BODY
== c
->type
)
1006 er
= MANDOCERR_ARGCOUNT
;
1007 else if (i
== cols
|| i
== cols
+ 1)
1010 er
= MANDOCERR_SYNTARGCOUNT
;
1012 rc
= mdoc_vmsg(mdoc
, er
,
1013 mdoc
->last
->line
, mdoc
->last
->pos
,
1014 "columns == %d (have %d)", cols
, i
);
1025 post_bl_head(POST_ARGS
)
1028 struct mdoc_node
*n
;
1030 assert(mdoc
->last
->parent
);
1031 n
= mdoc
->last
->parent
;
1033 if (LIST_column
== n
->data
.list
) {
1034 for (i
= 0; i
< (int)n
->args
->argc
; i
++)
1035 if (MDOC_Column
== n
->args
->argv
[i
].arg
)
1037 assert(i
< (int)n
->args
->argc
);
1039 if (n
->args
->argv
[i
].sz
&& mdoc
->last
->nchild
) {
1040 mdoc_nmsg(mdoc
, n
, MANDOCERR_COLUMNS
);
1046 if (0 == (i
= mdoc
->last
->nchild
))
1048 return(warn_count(mdoc
, "==", 0, "line arguments", i
));
1055 struct mdoc_node
*n
;
1057 if (MDOC_HEAD
== mdoc
->last
->type
)
1058 return(post_bl_head(mdoc
));
1059 if (MDOC_BODY
!= mdoc
->last
->type
)
1061 if (NULL
== mdoc
->last
->child
)
1065 * We only allow certain children of `Bl'. This is usually on
1066 * `It', but apparently `Sm' occurs here and there, so we let
1067 * that one through, too.
1071 for (n
= mdoc
->last
->child
; n
; n
= n
->next
) {
1072 if (MDOC_BLOCK
== n
->type
&& MDOC_It
== n
->tok
)
1074 if (MDOC_Sm
== n
->tok
)
1076 mdoc_nmsg(mdoc
, n
, MANDOCERR_SYNTCHILD
);
1085 ebool(struct mdoc
*mdoc
)
1087 struct mdoc_node
*n
;
1090 for (n
= mdoc
->last
->child
; n
; n
= n
->next
) {
1091 if (MDOC_TEXT
!= n
->type
)
1093 if (0 == strcmp(n
->string
, "on"))
1095 if (0 == strcmp(n
->string
, "off"))
1102 return(mdoc_nmsg(mdoc
, n
, MANDOCERR_BADBOOL
));
1107 post_root(POST_ARGS
)
1110 if (NULL
== mdoc
->first
->child
)
1111 mdoc_nmsg(mdoc
, mdoc
->first
, MANDOCERR_NODOCBODY
);
1112 else if ( ! (MDOC_PBODY
& mdoc
->flags
))
1113 mdoc_nmsg(mdoc
, mdoc
->first
, MANDOCERR_NODOCPROLOG
);
1114 else if (MDOC_BLOCK
!= mdoc
->first
->child
->type
)
1115 mdoc_nmsg(mdoc
, mdoc
->first
, MANDOCERR_NODOCBODY
);
1116 else if (MDOC_Sh
!= mdoc
->first
->child
->tok
)
1117 mdoc_nmsg(mdoc
, mdoc
->first
, MANDOCERR_NODOCBODY
);
1129 if (mdoc_a2st(mdoc
->last
->child
->string
))
1131 return(mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_BADSTANDARD
));
1138 struct mdoc_node
*nn
;
1140 if (MDOC_BODY
!= mdoc
->last
->type
)
1143 for (nn
= mdoc
->last
->child
; nn
; nn
= nn
->next
)
1174 mdoc_nmsg(mdoc
, nn
, MANDOCERR_SYNTCHILD
);
1186 if (MDOC_HEAD
== mdoc
->last
->type
)
1187 return(post_sh_head(mdoc
));
1188 if (MDOC_BODY
== mdoc
->last
->type
)
1189 return(post_sh_body(mdoc
));
1196 post_sh_body(POST_ARGS
)
1198 struct mdoc_node
*n
;
1200 if (SEC_NAME
!= mdoc
->lastsec
)
1204 * Warn if the NAME section doesn't contain the `Nm' and `Nd'
1205 * macros (can have multiple `Nm' and one `Nd'). Note that the
1206 * children of the BODY declaration can also be "text".
1209 if (NULL
== (n
= mdoc
->last
->child
))
1210 return(mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_BADNAMESEC
));
1212 for ( ; n
&& n
->next
; n
= n
->next
) {
1213 if (MDOC_ELEM
== n
->type
&& MDOC_Nm
== n
->tok
)
1215 if (MDOC_TEXT
== n
->type
)
1217 if ( ! mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_BADNAMESEC
))
1222 if (MDOC_BLOCK
== n
->type
&& MDOC_Nd
== n
->tok
)
1224 return(mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_BADNAMESEC
));
1229 post_sh_head(POST_ARGS
)
1233 const struct mdoc_node
*n
;
1236 * Process a new section. Sections are either "named" or
1237 * "custom"; custom sections are user-defined, while named ones
1238 * usually follow a conventional order and may only appear in
1239 * certain manual sections.
1245 * FIXME: yes, these can use a dynamic buffer, but I don't do so
1246 * in the interests of simplicity.
1249 for (n
= mdoc
->last
->child
; n
; n
= n
->next
) {
1250 /* XXX - copied from compact(). */
1251 assert(MDOC_TEXT
== n
->type
);
1253 if (strlcat(buf
, n
->string
, BUFSIZ
) >= BUFSIZ
) {
1254 mdoc_nmsg(mdoc
, n
, MANDOCERR_MEM
);
1257 if (NULL
== n
->next
)
1259 if (strlcat(buf
, " ", BUFSIZ
) >= BUFSIZ
) {
1260 mdoc_nmsg(mdoc
, n
, MANDOCERR_MEM
);
1265 sec
= mdoc_str2sec(buf
);
1268 * Check: NAME should always be first, CUSTOM has no roles,
1269 * non-CUSTOM has a conventional order to be followed.
1272 if (SEC_NAME
!= sec
&& SEC_NONE
== mdoc
->lastnamed
)
1273 if ( ! mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_NAMESECFIRST
))
1276 if (SEC_CUSTOM
== sec
)
1279 if (sec
== mdoc
->lastnamed
)
1280 if ( ! mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_SECREP
))
1283 if (sec
< mdoc
->lastnamed
)
1284 if ( ! mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_SECOOO
))
1288 * Check particular section/manual conventions. LIBRARY can
1289 * only occur in manual section 2, 3, and 9.
1294 assert(mdoc
->meta
.msec
);
1295 if (*mdoc
->meta
.msec
== '2')
1297 if (*mdoc
->meta
.msec
== '3')
1299 if (*mdoc
->meta
.msec
== '9')
1301 return(mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_SECMSEC
));