]>
git.cameronkatri.com Git - mandoc.git/blob - validate.c
1 /* $Id: validate.c,v 1.46 2009/01/21 11:35:26 kristaps Exp $ */
3 * Copyright (c) 2008 Kristaps Dzonsons <kristaps@kth.se>
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the
7 * above copyright notice and this permission notice appear in all
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
11 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
12 * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
13 * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
14 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
15 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
16 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17 * PERFORMANCE OF THIS SOFTWARE.
25 * Pre- and post-validate macros as they're parsed. Pre-validation
26 * occurs when the macro has been detected and its arguments parsed.
27 * Post-validation occurs when all child macros have also been parsed.
28 * In the ELEMENT case, this is simply the parameters of the macro; in
29 * the BLOCK case, this is the HEAD, BODY, TAIL and so on.
32 typedef int (*v_pre
)(struct mdoc
*, struct mdoc_node
*);
33 typedef int (*v_post
)(struct mdoc
*);
35 /* FIXME: some sections should only occur in specific msecs. */
36 /* FIXME: ignoring Pp. */
37 /* FIXME: math symbols. */
38 /* FIXME: valid character-escape checks. */
39 /* FIXME: make sure required sections are included (NAME, ...). */
48 static int pre_check_parent(struct mdoc
*, struct mdoc_node
*,
50 static int pre_check_msecs(struct mdoc
*, struct mdoc_node
*,
51 int, enum mdoc_msec
*);
52 static int pre_check_stdarg(struct mdoc
*, struct mdoc_node
*);
53 static int post_check_children_count(struct mdoc
*);
54 static int post_check_children_lt(struct mdoc
*, const char *, int);
55 static int post_check_children_gt(struct mdoc
*, const char *, int);
56 static int post_check_children_wgt(struct mdoc
*, const char *, int);
57 static int post_check_children_eq(struct mdoc
*, const char *, int);
58 static int post_check_children_weq(struct mdoc
*, const char *, int);
60 /* Specific pre-child-parse routines. */
62 static int pre_display(struct mdoc
*, struct mdoc_node
*);
63 static int pre_sh(struct mdoc
*, struct mdoc_node
*);
64 static int pre_ss(struct mdoc
*, struct mdoc_node
*);
65 static int pre_bd(struct mdoc
*, struct mdoc_node
*);
66 static int pre_bl(struct mdoc
*, struct mdoc_node
*);
67 static int pre_it(struct mdoc
*, struct mdoc_node
*);
68 static int pre_cd(struct mdoc
*, struct mdoc_node
*);
69 static int pre_er(struct mdoc
*, struct mdoc_node
*);
70 static int pre_ex(struct mdoc
*, struct mdoc_node
*);
71 static int pre_rv(struct mdoc
*, struct mdoc_node
*);
72 static int pre_an(struct mdoc
*, struct mdoc_node
*);
73 static int pre_st(struct mdoc
*, struct mdoc_node
*);
74 static int pre_prologue(struct mdoc
*, struct mdoc_node
*);
75 static int pre_prologue(struct mdoc
*, struct mdoc_node
*);
76 static int pre_prologue(struct mdoc
*, struct mdoc_node
*);
78 /* Specific post-child-parse routines. */
80 static int herr_ge1(struct mdoc
*);
81 static int herr_le1(struct mdoc
*);
82 static int herr_eq0(struct mdoc
*);
83 static int eerr_eq0(struct mdoc
*);
84 static int eerr_le1(struct mdoc
*);
85 static int eerr_le2(struct mdoc
*);
86 static int eerr_eq1(struct mdoc
*);
87 static int eerr_ge1(struct mdoc
*);
88 static int ewarn_eq0(struct mdoc
*);
89 static int ewarn_eq1(struct mdoc
*);
90 static int bwarn_ge1(struct mdoc
*);
91 static int ewarn_ge1(struct mdoc
*);
92 static int ebool(struct mdoc
*);
93 static int post_sh(struct mdoc
*);
94 static int post_sh_body(struct mdoc
*);
95 static int post_sh_head(struct mdoc
*);
96 static int post_bl(struct mdoc
*);
97 static int post_it(struct mdoc
*);
98 static int post_ex(struct mdoc
*);
99 static int post_an(struct mdoc
*);
100 static int post_at(struct mdoc
*);
101 static int post_xr(struct mdoc
*);
102 static int post_nm(struct mdoc
*);
103 static int post_bf(struct mdoc
*);
104 static int post_root(struct mdoc
*);
106 /* Collections of pre-child-parse routines. */
108 static v_pre pres_prologue
[] = { pre_prologue
, NULL
};
109 static v_pre pres_d1
[] = { pre_display
, NULL
};
110 static v_pre pres_bd
[] = { pre_display
, pre_bd
, NULL
};
111 static v_pre pres_bl
[] = { pre_bl
, NULL
};
112 static v_pre pres_it
[] = { pre_it
, NULL
};
113 static v_pre pres_ss
[] = { pre_ss
, NULL
};
114 static v_pre pres_sh
[] = { pre_sh
, NULL
};
115 static v_pre pres_cd
[] = { pre_cd
, NULL
};
116 static v_pre pres_er
[] = { pre_er
, NULL
};
117 static v_pre pres_ex
[] = { pre_ex
, NULL
};
118 static v_pre pres_rv
[] = { pre_rv
, NULL
};
119 static v_pre pres_an
[] = { pre_an
, NULL
};
120 static v_pre pres_st
[] = { pre_st
, NULL
};
122 /* Collections of post-child-parse routines. */
124 static v_post posts_bool
[] = { eerr_eq1
, ebool
, NULL
};
125 static v_post posts_bd
[] = { herr_eq0
, bwarn_ge1
, NULL
};
126 static v_post posts_text
[] = { eerr_ge1
, NULL
};
127 static v_post posts_wtext
[] = { ewarn_ge1
, NULL
};
128 static v_post posts_notext
[] = { eerr_eq0
, NULL
};
129 static v_post posts_wline
[] = { bwarn_ge1
, herr_eq0
, NULL
};
130 static v_post posts_sh
[] = { herr_ge1
, bwarn_ge1
, post_sh
, NULL
};
131 static v_post posts_bl
[] = { herr_eq0
, bwarn_ge1
, post_bl
, NULL
};
132 static v_post posts_it
[] = { post_it
, NULL
};
133 static v_post posts_in
[] = { ewarn_eq1
, NULL
};
134 static v_post posts_ss
[] = { herr_ge1
, NULL
};
135 static v_post posts_pp
[] = { ewarn_eq0
, NULL
};
136 static v_post posts_ex
[] = { eerr_le1
, post_ex
, NULL
};
137 static v_post posts_an
[] = { post_an
, NULL
};
138 static v_post posts_at
[] = { post_at
, NULL
};
139 static v_post posts_xr
[] = { eerr_ge1
, eerr_le2
, post_xr
, NULL
};
140 static v_post posts_nm
[] = { post_nm
, NULL
};
141 static v_post posts_bf
[] = { herr_le1
, post_bf
, NULL
};
142 static v_post posts_rs
[] = { herr_eq0
, bwarn_ge1
, NULL
};
143 static v_post posts_fo
[] = { bwarn_ge1
, NULL
};
144 static v_post posts_bk
[] = { herr_eq0
, bwarn_ge1
, NULL
};
146 /* Per-macro pre- and post-child-check routine collections. */
148 const struct valids mdoc_valids
[MDOC_MAX
] = {
149 { NULL
, NULL
}, /* \" */
150 { pres_prologue
, posts_text
}, /* Dd */
151 { pres_prologue
, NULL
}, /* Dt */
152 { pres_prologue
, NULL
}, /* Os */
153 /* FIXME: NAME section internal ordering. */
154 { pres_sh
, posts_sh
}, /* Sh */
155 { pres_ss
, posts_ss
}, /* Ss */
156 { NULL
, posts_pp
}, /* 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 { pres_cd
, posts_text
}, /* Cd */
168 { NULL
, NULL
}, /* Cm */
169 { NULL
, posts_text
}, /* Dv */
170 { pres_er
, posts_text
}, /* Er */
171 { NULL
, posts_text
}, /* Ev */
172 { pres_ex
, posts_ex
}, /* Ex */
173 { NULL
, posts_text
}, /* Fa */
174 /* FIXME: only in SYNOPSIS section. */
175 { NULL
, posts_wtext
}, /* Fd */
176 { NULL
, NULL
}, /* Fl */
177 { NULL
, posts_text
}, /* Fn */
178 { NULL
, posts_wtext
}, /* Ft */
179 { NULL
, posts_text
}, /* Ic */
180 { NULL
, posts_in
}, /* In */
181 { NULL
, posts_text
}, /* Li */
182 { NULL
, posts_wtext
}, /* Nd */
183 { NULL
, posts_nm
}, /* Nm */
184 { NULL
, posts_wline
}, /* Op */
185 { NULL
, NULL
}, /* Ot */
186 { NULL
, NULL
}, /* Pa */
187 { pres_rv
, posts_notext
}, /* Rv */
188 { pres_st
, posts_notext
}, /* St */
189 { NULL
, posts_text
}, /* Va */
190 { NULL
, posts_text
}, /* Vt */
191 { NULL
, posts_xr
}, /* Xr */
192 { NULL
, posts_text
}, /* %A */
193 { NULL
, posts_text
}, /* %B */
194 { NULL
, posts_text
}, /* %D */
195 { NULL
, posts_text
}, /* %I */
196 { NULL
, posts_text
}, /* %J */
197 { NULL
, posts_text
}, /* %N */
198 { NULL
, posts_text
}, /* %O */
199 { NULL
, posts_text
}, /* %P */
200 { NULL
, posts_text
}, /* %R */
201 { NULL
, posts_text
}, /* %T */
202 { NULL
, posts_text
}, /* %V */
203 { NULL
, NULL
}, /* Ac */
204 { NULL
, NULL
}, /* Ao */
205 { NULL
, posts_wline
}, /* Aq */
206 { NULL
, posts_at
}, /* At */
207 { NULL
, NULL
}, /* Bc */
208 { NULL
, posts_bf
}, /* Bf */
209 { NULL
, NULL
}, /* Bo */
210 { NULL
, posts_wline
}, /* Bq */
211 { NULL
, NULL
}, /* Bsx */
212 { NULL
, NULL
}, /* Bx */
213 { NULL
, posts_bool
}, /* Db */
214 { NULL
, NULL
}, /* Dc */
215 { NULL
, NULL
}, /* Do */
216 { NULL
, posts_wline
}, /* Dq */
217 { NULL
, NULL
}, /* Ec */
218 { NULL
, NULL
}, /* Ef */ /* -symbolic, etc. */
219 { NULL
, posts_text
}, /* Em */
220 { NULL
, NULL
}, /* Eo */
221 { NULL
, NULL
}, /* Fx */
222 { NULL
, posts_text
}, /* Ms */
223 { NULL
, posts_notext
}, /* No */
224 { NULL
, posts_notext
}, /* Ns */
225 { NULL
, NULL
}, /* Nx */
226 { NULL
, NULL
}, /* Ox */
227 { NULL
, NULL
}, /* Pc */
228 { NULL
, NULL
}, /* Pf */
229 { NULL
, NULL
}, /* Po */
230 { NULL
, posts_wline
}, /* Pq */
231 { NULL
, NULL
}, /* Qc */
232 { NULL
, posts_wline
}, /* Ql */
233 { NULL
, NULL
}, /* Qo */
234 { NULL
, posts_wline
}, /* Qq */
235 { NULL
, NULL
}, /* Re */
236 { NULL
, posts_rs
}, /* Rs */
237 { NULL
, NULL
}, /* Sc */
238 { NULL
, NULL
}, /* So */
239 { NULL
, posts_wline
}, /* Sq */
240 { NULL
, posts_bool
}, /* Sm */
241 { NULL
, posts_text
}, /* Sx */
242 { NULL
, posts_text
}, /* Sy */
243 { NULL
, posts_text
}, /* Tn */
244 { NULL
, NULL
}, /* Ux */
245 { NULL
, NULL
}, /* Xc */
246 { NULL
, NULL
}, /* Xo */
247 { NULL
, posts_fo
}, /* Fo */
248 { NULL
, NULL
}, /* Fc */
249 { NULL
, NULL
}, /* Oo */
250 { NULL
, NULL
}, /* Oc */
251 { NULL
, posts_bk
}, /* Bk */
252 { NULL
, NULL
}, /* Ek */
253 { NULL
, posts_notext
}, /* Bt */
254 { NULL
, NULL
}, /* Hf */
255 { NULL
, NULL
}, /* Fr */
256 { NULL
, posts_notext
}, /* Ud */
261 post_check_children_count(struct mdoc
*mdoc
)
266 for (i
= 0, n
= mdoc
->last
->child
; n
; n
= n
->next
, i
++)
273 post_check_children_wgt(struct mdoc
*mdoc
, const char *p
, int sz
)
277 if ((i
= post_check_children_count(mdoc
)) > sz
)
279 return(mdoc_warn(mdoc
, WARN_SYNTAX
, "macro suggests more "
280 "than %d %s (has %d)", sz
, p
, i
));
285 post_check_children_gt(struct mdoc
*mdoc
, const char *p
, int sz
)
289 if ((i
= post_check_children_count(mdoc
)) > sz
)
291 return(mdoc_err(mdoc
, "macro requires more than %d "
292 "%s (has %d)", sz
, p
, i
));
297 post_check_children_weq(struct mdoc
*mdoc
, const char *p
, int sz
)
301 if ((i
= post_check_children_count(mdoc
)) == sz
)
303 return(mdoc_warn(mdoc
, WARN_SYNTAX
, "macro suggests %d "
304 "%s (has %d)", sz
, p
, i
));
309 post_check_children_eq(struct mdoc
*mdoc
, const char *p
, int sz
)
313 if ((i
= post_check_children_count(mdoc
)) == sz
)
315 return(mdoc_err(mdoc
, "macro requires %d %s "
316 "(have %d)", sz
, p
, i
));
321 post_check_children_lt(struct mdoc
*mdoc
, const char *p
, int sz
)
325 if ((i
= post_check_children_count(mdoc
)) < sz
)
327 return(mdoc_err(mdoc
, "macro requires less than %d "
328 "%s (have %d)", sz
, p
, i
));
333 pre_check_stdarg(struct mdoc
*mdoc
, struct mdoc_node
*node
)
336 if (1 == node
->data
.elem
.argc
&&
337 MDOC_Std
== node
->data
.elem
.argv
[0].arg
)
339 return(mdoc_nwarn(mdoc
, node
, WARN_COMPAT
,
340 "macro suggests single `%s' argument",
341 mdoc_argnames
[MDOC_Std
]));
346 pre_check_msecs(struct mdoc
*mdoc
, struct mdoc_node
*node
,
347 int sz
, enum mdoc_msec
*msecs
)
351 for (i
= 0; i
< sz
; i
++)
352 if (msecs
[i
] == mdoc
->meta
.msec
)
354 return(mdoc_nwarn(mdoc
, node
, WARN_COMPAT
, "macro not "
355 "appropriate for manual section"));
360 pre_check_parent(struct mdoc
*mdoc
, struct mdoc_node
*node
,
361 int tok
, enum mdoc_type type
)
364 if (type
!= node
->parent
->type
)
365 return(mdoc_nerr(mdoc
, node
, "invalid macro parent class %s, expected %s",
366 mdoc_type2a(node
->parent
->type
),
368 if (MDOC_ROOT
!= type
&& tok
!= node
->parent
->tok
)
369 return(mdoc_nerr(mdoc
, node
, "invalid macro parent `%s', expected `%s'",
370 mdoc_macronames
[node
->parent
->tok
],
371 mdoc_macronames
[tok
]));
377 bwarn_ge1(struct mdoc
*mdoc
)
380 if (MDOC_BODY
!= mdoc
->last
->type
)
382 return(post_check_children_wgt(mdoc
, "body children", 0));
387 ewarn_eq1(struct mdoc
*mdoc
)
390 assert(MDOC_ELEM
== mdoc
->last
->type
);
391 return(post_check_children_weq(mdoc
, "parameters", 1));
396 ewarn_eq0(struct mdoc
*mdoc
)
399 assert(MDOC_ELEM
== mdoc
->last
->type
);
400 return(post_check_children_weq(mdoc
, "parameters", 0));
405 ewarn_ge1(struct mdoc
*mdoc
)
408 assert(MDOC_ELEM
== mdoc
->last
->type
);
409 return(post_check_children_wgt(mdoc
, "parameters", 0));
414 eerr_eq1(struct mdoc
*mdoc
)
417 assert(MDOC_ELEM
== mdoc
->last
->type
);
418 return(post_check_children_eq(mdoc
, "parameters", 1));
423 eerr_le2(struct mdoc
*mdoc
)
426 assert(MDOC_ELEM
== mdoc
->last
->type
);
427 return(post_check_children_lt(mdoc
, "parameters", 3));
432 eerr_le1(struct mdoc
*mdoc
)
435 assert(MDOC_ELEM
== mdoc
->last
->type
);
436 return(post_check_children_lt(mdoc
, "parameters", 2));
441 eerr_eq0(struct mdoc
*mdoc
)
444 assert(MDOC_ELEM
== mdoc
->last
->type
);
445 return(post_check_children_eq(mdoc
, "parameters", 0));
450 eerr_ge1(struct mdoc
*mdoc
)
453 assert(MDOC_ELEM
== mdoc
->last
->type
);
454 return(post_check_children_gt(mdoc
, "parameters", 0));
459 herr_eq0(struct mdoc
*mdoc
)
462 if (MDOC_HEAD
!= mdoc
->last
->type
)
464 return(post_check_children_eq(mdoc
, "parameters", 0));
469 herr_le1(struct mdoc
*mdoc
)
471 if (MDOC_HEAD
!= mdoc
->last
->type
)
473 return(post_check_children_lt(mdoc
, "parameters", 2));
478 herr_ge1(struct mdoc
*mdoc
)
481 if (MDOC_HEAD
!= mdoc
->last
->type
)
483 return(post_check_children_gt(mdoc
, "parameters", 0));
488 pre_display(struct mdoc
*mdoc
, struct mdoc_node
*node
)
492 if (MDOC_BLOCK
!= node
->type
)
497 for (n
= mdoc
->last
->parent
; n
; n
= n
->parent
)
498 if (MDOC_BLOCK
== n
->type
)
499 if (MDOC_Bd
== n
->tok
)
503 return(mdoc_nerr(mdoc
, node
, "displays may not be nested"));
508 pre_bl(struct mdoc
*mdoc
, struct mdoc_node
*node
)
511 struct mdoc_arg
*argv
;
514 if (MDOC_BLOCK
!= node
->type
)
516 assert(MDOC_Bl
== node
->tok
);
519 argc
= node
->data
.block
.argc
;
522 for (i
= type
= err
= 0; i
< argc
; i
++) {
523 argv
= &node
->data
.block
.argv
[(int)i
];
556 return(mdoc_err(mdoc
, "no list type specified"));
560 return(mdoc_perr(mdoc
, argv
->line
,
561 argv
->pos
, "only one list type possible"));
566 pre_bd(struct mdoc
*mdoc
, struct mdoc_node
*node
)
569 struct mdoc_arg
*argv
;
572 if (MDOC_BLOCK
!= node
->type
)
574 assert(MDOC_Bd
== node
->tok
);
577 argc
= node
->data
.block
.argc
;
580 for (err
= i
= type
= 0; 0 == err
&& i
< argc
; i
++) {
581 argv
= &node
->data
.block
.argv
[(int)i
];
586 case (MDOC_Unfilled
):
602 return(mdoc_err(mdoc
, "no display type specified"));
606 return(mdoc_perr(mdoc
, argv
->line
,
607 argv
->pos
, "only one display type possible"));
612 pre_ss(struct mdoc
*mdoc
, struct mdoc_node
*node
)
615 if (MDOC_BLOCK
!= node
->type
)
617 return(pre_check_parent(mdoc
, node
, MDOC_Sh
, MDOC_BODY
));
622 pre_sh(struct mdoc
*mdoc
, struct mdoc_node
*node
)
625 if (MDOC_BLOCK
!= node
->type
)
627 return(pre_check_parent(mdoc
, node
, -1, MDOC_ROOT
));
632 pre_st(struct mdoc
*mdoc
, struct mdoc_node
*node
)
635 assert(MDOC_ELEM
== node
->type
);
636 assert(MDOC_St
== node
->tok
);
637 if (1 == node
->data
.elem
.argc
)
639 return(mdoc_nerr(mdoc
, node
, "macro must have one argument"));
644 pre_an(struct mdoc
*mdoc
, struct mdoc_node
*node
)
647 assert(MDOC_ELEM
== node
->type
);
648 assert(MDOC_An
== node
->tok
);
649 if (1 >= node
->data
.elem
.argc
)
651 return(mdoc_nerr(mdoc
, node
, "macro may only have one argument"));
656 pre_rv(struct mdoc
*mdoc
, struct mdoc_node
*node
)
658 enum mdoc_msec msecs
[2];
660 assert(MDOC_ELEM
== node
->type
);
661 assert(MDOC_Rv
== node
->tok
);
665 if ( ! pre_check_msecs(mdoc
, node
, 2, msecs
))
667 return(pre_check_stdarg(mdoc
, node
));
672 pre_ex(struct mdoc
*mdoc
, struct mdoc_node
*node
)
674 enum mdoc_msec msecs
[3];
676 assert(MDOC_ELEM
== node
->type
);
677 assert(MDOC_Ex
== node
->tok
);
682 if ( ! pre_check_msecs(mdoc
, node
, 3, msecs
))
684 return(pre_check_stdarg(mdoc
, node
));
689 pre_er(struct mdoc
*mdoc
, struct mdoc_node
*node
)
691 enum mdoc_msec msecs
[1];
694 return(pre_check_msecs(mdoc
, node
, 1, msecs
));
699 pre_cd(struct mdoc
*mdoc
, struct mdoc_node
*node
)
701 enum mdoc_msec msecs
[1];
704 return(pre_check_msecs(mdoc
, node
, 1, msecs
));
709 pre_it(struct mdoc
*mdoc
, struct mdoc_node
*node
)
712 if (MDOC_BLOCK
!= node
->type
)
714 return(pre_check_parent(mdoc
, node
, MDOC_Bl
, MDOC_BODY
));
719 pre_prologue(struct mdoc
*mdoc
, struct mdoc_node
*node
)
722 if (SEC_PROLOGUE
!= mdoc
->lastnamed
)
723 return(mdoc_nerr(mdoc
, node
, "macro may only be invoked in the prologue"));
724 assert(MDOC_ELEM
== node
->type
);
726 /* Check for ordering. */
730 if (mdoc
->meta
.title
&& mdoc
->meta
.date
)
732 return(mdoc_nerr(mdoc
, node
, "prologue macro out-of-order"));
734 if (NULL
== mdoc
->meta
.title
&& mdoc
->meta
.date
)
736 return(mdoc_nerr(mdoc
, node
, "prologue macro out-of-order"));
738 if (NULL
== mdoc
->meta
.title
&& 0 == mdoc
->meta
.date
)
740 return(mdoc_nerr(mdoc
, node
, "prologue macro out-of-order"));
746 /* Check for repetition. */
750 if (NULL
== mdoc
->meta
.os
)
754 if (0 == mdoc
->meta
.date
)
758 if (NULL
== mdoc
->meta
.title
)
766 return(mdoc_nerr(mdoc
, node
, "prologue macro repeated"));
771 post_bf(struct mdoc
*mdoc
)
774 struct mdoc_node
*head
;
776 if (MDOC_BLOCK
!= mdoc
->last
->type
)
778 assert(MDOC_Bf
== mdoc
->last
->tok
);
779 head
= mdoc
->last
->data
.block
.head
;
782 if (0 == mdoc
->last
->data
.block
.argc
) {
784 assert(MDOC_TEXT
== head
->child
->type
);
785 p
= head
->child
->data
.text
.string
;
786 if (xstrcmp(p
, "Em"))
788 else if (xstrcmp(p
, "Li"))
790 else if (xstrcmp(p
, "Sm"))
792 return(mdoc_nerr(mdoc
, head
->child
, "invalid font mode"));
794 return(mdoc_err(mdoc
, "macro expects an argument or parameter"));
797 return(mdoc_err(mdoc
, "macro expects an argument or parameter"));
798 if (1 == mdoc
->last
->data
.block
.argc
)
800 return(mdoc_err(mdoc
, "macro expects an argument or parameter"));
805 post_nm(struct mdoc
*mdoc
)
808 assert(MDOC_ELEM
== mdoc
->last
->type
);
809 assert(MDOC_Nm
== mdoc
->last
->tok
);
810 if (mdoc
->last
->child
)
814 return(mdoc_err(mdoc
, "macro `%s' has not been invoked with a name",
815 mdoc_macronames
[MDOC_Nm
]));
820 post_xr(struct mdoc
*mdoc
)
824 assert(MDOC_ELEM
== mdoc
->last
->type
);
825 assert(MDOC_Xr
== mdoc
->last
->tok
);
826 assert(mdoc
->last
->child
);
827 assert(MDOC_TEXT
== mdoc
->last
->child
->type
);
829 if (NULL
== (n
= mdoc
->last
->child
->next
))
831 assert(MDOC_TEXT
== n
->type
);
832 if (MSEC_DEFAULT
!= mdoc_atomsec(n
->data
.text
.string
))
834 return(mdoc_nerr(mdoc
, n
, "invalid manual section"));
839 post_at(struct mdoc
*mdoc
)
842 assert(MDOC_ELEM
== mdoc
->last
->type
);
843 assert(MDOC_At
== mdoc
->last
->tok
);
845 if (NULL
== mdoc
->last
->child
)
847 assert(MDOC_TEXT
== mdoc
->last
->child
->type
);
849 if (ATT_DEFAULT
!= mdoc_atoatt(mdoc
->last
->child
->data
.text
.string
))
851 return(mdoc_err(mdoc
, "macro expects a valid AT&T version symbol"));
856 post_an(struct mdoc
*mdoc
)
859 assert(MDOC_ELEM
== mdoc
->last
->type
);
860 assert(MDOC_An
== mdoc
->last
->tok
);
862 if (0 != mdoc
->last
->data
.elem
.argc
) {
863 if (NULL
== mdoc
->last
->child
)
865 return(mdoc_err(mdoc
, "macro expects either argument or parameters"));
868 if (mdoc
->last
->child
)
870 return(mdoc_err(mdoc
, "macro expects either argument or parameters"));
875 post_ex(struct mdoc
*mdoc
)
878 assert(MDOC_ELEM
== mdoc
->last
->type
);
879 assert(MDOC_Ex
== mdoc
->last
->tok
);
881 if (0 == mdoc
->last
->data
.elem
.argc
) {
882 if (mdoc
->last
->child
)
884 return(mdoc_err(mdoc
, "macro expects `%s' or a single child",
885 mdoc_argnames
[MDOC_Std
]));
887 if (mdoc
->last
->child
)
888 return(mdoc_err(mdoc
, "macro expects `%s' or a single child",
889 mdoc_argnames
[MDOC_Std
]));
890 if (1 != mdoc
->last
->data
.elem
.argc
)
891 return(mdoc_err(mdoc
, "macro expects `%s' or a single child",
892 mdoc_argnames
[MDOC_Std
]));
893 if (MDOC_Std
!= mdoc
->last
->data
.elem
.argv
[0].arg
)
894 return(mdoc_err(mdoc
, "macro expects `%s' or a single child",
895 mdoc_argnames
[MDOC_Std
]));
900 /* Warn if `Bl' type-specific syntax isn't reflected in items. */
902 post_it(struct mdoc
*mdoc
)
905 #define TYPE_NONE (0)
906 #define TYPE_BODY (1)
907 #define TYPE_HEAD (2)
911 if (MDOC_BLOCK
!= mdoc
->last
->type
)
914 assert(MDOC_It
== mdoc
->last
->tok
);
916 n
= mdoc
->last
->parent
;
918 assert(MDOC_Bl
== n
->tok
);
921 assert(MDOC_BLOCK
== n
->type
);
922 assert(MDOC_Bl
== n
->tok
);
924 argc
= n
->data
.block
.argc
;
928 /* Some types require block-head, some not. */
931 for (i
= 0; TYPE_NONE
== type
&& i
< argc
; i
++)
932 switch (n
->data
.block
.argv
[(int)i
].arg
) {
943 sv
= n
->data
.block
.argv
[(int)i
].arg
;
957 sv
= n
->data
.block
.argv
[(int)i
].arg
;
963 assert(TYPE_NONE
!= type
);
965 if (TYPE_HEAD
== type
) {
966 n
= mdoc
->last
->data
.block
.head
;
968 if (NULL
== n
->child
)
969 if ( ! mdoc_warn(mdoc
, WARN_SYNTAX
, "macro suggests line parameters"))
972 n
= mdoc
->last
->data
.block
.body
;
974 if (NULL
== n
->child
)
975 if ( ! mdoc_warn(mdoc
, WARN_SYNTAX
, "macro suggests body children"))
981 assert(TYPE_BODY
== type
);
982 assert(mdoc
->last
->data
.block
.head
);
984 n
= mdoc
->last
->data
.block
.head
;
987 if ( ! mdoc_warn(mdoc
, WARN_SYNTAX
, "macro suggests no line parameters"))
990 n
= mdoc
->last
->data
.block
.body
;
992 if (NULL
== n
->child
)
993 if ( ! mdoc_warn(mdoc
, WARN_SYNTAX
, "macro suggests body children"))
997 if (MDOC_Column
!= sv
)
1000 /* Make sure the number of columns is sane. */
1002 argc
= mdoc
->last
->parent
->parent
->data
.block
.argv
->sz
;
1003 n
= mdoc
->last
->data
.block
.head
->child
;
1005 for (i
= 0; n
; n
= n
->next
)
1010 return(mdoc_err(mdoc
, "expected %zu list columns, have %zu", argc
, i
));
1018 post_bl(struct mdoc
*mdoc
)
1020 struct mdoc_node
*n
;
1022 if (MDOC_BODY
!= mdoc
->last
->type
)
1024 assert(MDOC_Bl
== mdoc
->last
->tok
);
1027 for (n
= mdoc
->last
->child
; n
; n
= n
->next
) {
1028 if (MDOC_BLOCK
== n
->type
)
1029 if (MDOC_It
== n
->tok
)
1035 return(mdoc_nerr(mdoc
, n
, "invalid child of parent macro `Bl'"));
1040 ebool(struct mdoc
*mdoc
)
1042 struct mdoc_node
*n
;
1044 assert(MDOC_ELEM
== mdoc
->last
->type
);
1046 for (n
= mdoc
->last
->child
; n
; n
= n
->next
) {
1047 if (MDOC_TEXT
!= n
->type
)
1049 if (xstrcmp(n
->data
.text
.string
, "on"))
1051 if (xstrcmp(n
->data
.text
.string
, "off"))
1057 return(mdoc_nerr(mdoc
, n
, "expected boolean value"));
1062 post_root(struct mdoc
*mdoc
)
1065 if (NULL
== mdoc
->first
->child
)
1066 return(mdoc_err(mdoc
, "document has no data"));
1067 if (SEC_PROLOGUE
== mdoc
->lastnamed
)
1068 return(mdoc_err(mdoc
, "document has incomplete prologue"));
1069 if (MDOC_BLOCK
!= mdoc
->first
->child
->type
)
1070 return(mdoc_err(mdoc
, "document expects `%s' macro after prologue", mdoc_macronames
[MDOC_Sh
]));
1071 if (MDOC_Sh
!= mdoc
->first
->child
->tok
)
1072 return(mdoc_err(mdoc
, "document expects `%s' macro after prologue", mdoc_macronames
[MDOC_Sh
]));
1078 post_sh(struct mdoc
*mdoc
)
1081 if (MDOC_HEAD
== mdoc
->last
->type
)
1082 return(post_sh_head(mdoc
));
1083 if (MDOC_BODY
== mdoc
->last
->type
)
1084 return(post_sh_body(mdoc
));
1090 post_sh_body(struct mdoc
*mdoc
)
1092 struct mdoc_node
*n
;
1094 assert(MDOC_Sh
== mdoc
->last
->tok
);
1095 if (SEC_NAME
!= mdoc
->lastnamed
)
1098 if (NULL
== (n
= mdoc
->last
->child
))
1099 return(mdoc_err(mdoc
, "section NAME must contain %s as the first body child", mdoc_macronames
[MDOC_Nm
]));
1100 if (MDOC_ELEM
!= n
->type
|| MDOC_Nm
!= n
->tok
)
1101 return(mdoc_err(mdoc
, "section NAME must contain %s as the first body child", mdoc_macronames
[MDOC_Nm
]));
1102 if (NULL
== (n
= n
->next
))
1103 return(mdoc_err(mdoc
, "section NAME must contain %s as the second body child", mdoc_macronames
[MDOC_Nd
]));
1104 if (MDOC_ELEM
!= n
->type
|| MDOC_Nd
!= n
->tok
)
1105 return(mdoc_err(mdoc
, "section NAME must contain %s as the second body child", mdoc_macronames
[MDOC_Nd
]));
1106 if (NULL
== (n
= n
->next
))
1109 return(mdoc_warn(mdoc
, WARN_SYNTAX
, "section NAME usually limited to %s and %s body children",
1110 mdoc_macronames
[MDOC_Nm
], mdoc_macronames
[MDOC_Nd
]));
1115 post_sh_head(struct mdoc
*mdoc
)
1120 assert(MDOC_Sh
== mdoc
->last
->tok
);
1122 if ( ! xstrlcats(buf
, mdoc
->last
->child
, 64))
1123 return(mdoc_err(mdoc
, "macro parameters too long"));
1125 sec
= mdoc_atosec(buf
);
1127 if (SEC_BODY
== mdoc
->lastnamed
&& SEC_NAME
!= sec
)
1128 return(mdoc_err(mdoc
, "section NAME must be first"));
1129 if (SEC_CUSTOM
== sec
)
1131 if (sec
== mdoc
->lastnamed
)
1132 return(mdoc_warn(mdoc
, WARN_SYNTAX
, "section repeated"));
1133 if (sec
< mdoc
->lastnamed
)
1134 return(mdoc_warn(mdoc
, WARN_SYNTAX
, "section out of conventional order"));
1141 mdoc_valid_pre(struct mdoc
*mdoc
, struct mdoc_node
*node
)
1145 if (MDOC_TEXT
== node
->type
)
1147 assert(MDOC_ROOT
!= node
->type
);
1149 if (NULL
== mdoc_valids
[node
->tok
].pre
)
1151 for (p
= mdoc_valids
[node
->tok
].pre
; *p
; p
++)
1152 if ( ! (*p
)(mdoc
, node
))
1159 mdoc_valid_post(struct mdoc
*mdoc
)
1163 if (MDOC_VALID
& mdoc
->last
->flags
)
1165 mdoc
->last
->flags
|= MDOC_VALID
;
1167 if (MDOC_TEXT
== mdoc
->last
->type
)
1169 if (MDOC_ROOT
== mdoc
->last
->type
)
1170 return(post_root(mdoc
));
1172 if (NULL
== mdoc_valids
[mdoc
->last
->tok
].post
)
1174 for (p
= mdoc_valids
[mdoc
->last
->tok
].post
; *p
; p
++)