]>
git.cameronkatri.com Git - mandoc.git/blob - validate.c
1 /* $Id: validate.c,v 1.43 2009/01/20 12:51:28 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.
24 typedef int (*v_pre
)(struct mdoc
*, struct mdoc_node
*);
25 typedef int (*v_post
)(struct mdoc
*);
27 /* FIXME: some sections should only occur in specific msecs. */
28 /* FIXME: ignoring Pp. */
29 /* FIXME: math symbols. */
30 /* FIXME: valid character-escape checks. */
31 /* FIXME: make sure required sections are included (NAME, ...). */
40 static int pre_check_parent(struct mdoc
*, struct mdoc_node
*,
42 static int pre_check_msecs(struct mdoc
*, struct mdoc_node
*,
43 int, enum mdoc_msec
*);
44 static int pre_check_stdarg(struct mdoc
*, struct mdoc_node
*);
45 static int post_check_children_count(struct mdoc
*);
46 static int post_check_children_lt(struct mdoc
*, const char *, int);
47 static int post_check_children_gt(struct mdoc
*, const char *, int);
48 static int post_check_children_wgt(struct mdoc
*, const char *, int);
49 static int post_check_children_eq(struct mdoc
*, const char *, int);
50 static int post_check_children_weq(struct mdoc
*, const char *, int);
52 /* Specific pre-child-parse routines. */
54 static int pre_display(struct mdoc
*, struct mdoc_node
*);
55 static int pre_sh(struct mdoc
*, struct mdoc_node
*);
56 static int pre_ss(struct mdoc
*, struct mdoc_node
*);
57 static int pre_bd(struct mdoc
*, struct mdoc_node
*);
58 static int pre_bl(struct mdoc
*, struct mdoc_node
*);
59 static int pre_it(struct mdoc
*, struct mdoc_node
*);
60 static int pre_cd(struct mdoc
*, struct mdoc_node
*);
61 static int pre_er(struct mdoc
*, struct mdoc_node
*);
62 static int pre_ex(struct mdoc
*, struct mdoc_node
*);
63 static int pre_rv(struct mdoc
*, struct mdoc_node
*);
64 static int pre_an(struct mdoc
*, struct mdoc_node
*);
65 static int pre_st(struct mdoc
*, struct mdoc_node
*);
66 static int pre_prologue(struct mdoc
*, struct mdoc_node
*);
67 static int pre_prologue(struct mdoc
*, struct mdoc_node
*);
68 static int pre_prologue(struct mdoc
*, struct mdoc_node
*);
70 /* Specific post-child-parse routines. */
72 static int herr_ge1(struct mdoc
*);
73 static int herr_le1(struct mdoc
*);
74 static int herr_eq0(struct mdoc
*);
75 static int eerr_eq0(struct mdoc
*);
76 static int eerr_le1(struct mdoc
*);
77 static int eerr_le2(struct mdoc
*);
78 static int eerr_eq1(struct mdoc
*);
79 static int eerr_ge1(struct mdoc
*);
80 static int ewarn_eq0(struct mdoc
*);
81 static int ewarn_eq1(struct mdoc
*);
82 static int bwarn_ge1(struct mdoc
*);
83 static int ewarn_ge1(struct mdoc
*);
84 static int ebool(struct mdoc
*);
85 static int post_sh(struct mdoc
*);
86 static int post_bl(struct mdoc
*);
87 static int post_it(struct mdoc
*);
88 static int post_ex(struct mdoc
*);
89 static int post_an(struct mdoc
*);
90 static int post_at(struct mdoc
*);
91 static int post_xr(struct mdoc
*);
92 static int post_nm(struct mdoc
*);
93 static int post_bf(struct mdoc
*);
94 static int post_root(struct mdoc
*);
96 /* Collections of pre-child-parse routines. */
98 static v_pre pres_prologue
[] = { pre_prologue
, NULL
};
99 static v_pre pres_d1
[] = { pre_display
, NULL
};
100 static v_pre pres_bd
[] = { pre_display
, pre_bd
, NULL
};
101 static v_pre pres_bl
[] = { pre_bl
, NULL
};
102 static v_pre pres_it
[] = { pre_it
, NULL
};
103 static v_pre pres_ss
[] = { pre_ss
, NULL
};
104 static v_pre pres_sh
[] = { pre_sh
, NULL
};
105 static v_pre pres_cd
[] = { pre_cd
, NULL
};
106 static v_pre pres_er
[] = { pre_er
, NULL
};
107 static v_pre pres_ex
[] = { pre_ex
, NULL
};
108 static v_pre pres_rv
[] = { pre_rv
, NULL
};
109 static v_pre pres_an
[] = { pre_an
, NULL
};
110 static v_pre pres_st
[] = { pre_st
, NULL
};
112 /* Collections of post-child-parse routines. */
114 static v_post posts_bool
[] = { eerr_eq1
, ebool
, NULL
};
115 static v_post posts_bd
[] = { herr_eq0
, bwarn_ge1
, NULL
};
116 static v_post posts_text
[] = { eerr_ge1
, NULL
};
117 static v_post posts_wtext
[] = { ewarn_ge1
, NULL
};
118 static v_post posts_notext
[] = { eerr_eq0
, NULL
};
119 static v_post posts_wline
[] = { bwarn_ge1
, herr_eq0
, NULL
};
120 static v_post posts_sh
[] = { herr_ge1
, bwarn_ge1
, post_sh
, NULL
};
121 static v_post posts_bl
[] = { herr_eq0
, bwarn_ge1
, post_bl
, NULL
};
122 static v_post posts_it
[] = { post_it
, NULL
};
123 static v_post posts_in
[] = { ewarn_eq1
, NULL
};
124 static v_post posts_ss
[] = { herr_ge1
, NULL
};
125 static v_post posts_pp
[] = { ewarn_eq0
, NULL
};
126 static v_post posts_ex
[] = { eerr_le1
, post_ex
, NULL
};
127 static v_post posts_an
[] = { post_an
, NULL
};
128 static v_post posts_at
[] = { post_at
, NULL
};
129 static v_post posts_xr
[] = { eerr_ge1
, eerr_le2
, post_xr
, NULL
};
130 static v_post posts_nm
[] = { post_nm
, NULL
};
131 static v_post posts_bf
[] = { herr_le1
, post_bf
, NULL
};
133 /* Per-macro pre- and post-child-check routine collections. */
135 const struct valids mdoc_valids
[MDOC_MAX
] = {
136 { NULL
, NULL
}, /* \" */
137 { pres_prologue
, posts_text
}, /* Dd */
138 { pres_prologue
, NULL
}, /* Dt */
139 { pres_prologue
, NULL
}, /* Os */
140 /* FIXME: NAME section internal ordering. */
141 { pres_sh
, posts_sh
}, /* Sh */
142 { pres_ss
, posts_ss
}, /* Ss */
143 { NULL
, posts_pp
}, /* Pp */
144 { pres_d1
, posts_wline
}, /* D1 */
145 { pres_d1
, posts_wline
}, /* Dl */
146 { pres_bd
, posts_bd
}, /* Bd */
147 { NULL
, NULL
}, /* Ed */
148 { pres_bl
, posts_bl
}, /* Bl */
149 { NULL
, NULL
}, /* El */
150 { pres_it
, posts_it
}, /* It */
151 { NULL
, posts_text
}, /* Ad */
152 { pres_an
, posts_an
}, /* An */
153 { NULL
, NULL
}, /* Ar */
154 { pres_cd
, posts_text
}, /* Cd */
155 { NULL
, NULL
}, /* Cm */
156 { NULL
, posts_text
}, /* Dv */
157 { pres_er
, posts_text
}, /* Er */
158 { NULL
, posts_text
}, /* Ev */
159 { pres_ex
, posts_ex
}, /* Ex */
160 { NULL
, posts_text
}, /* Fa */
161 /* FIXME: only in SYNOPSIS section. */
162 { NULL
, posts_wtext
}, /* Fd */
163 { NULL
, NULL
}, /* Fl */
164 { NULL
, posts_text
}, /* Fn */
165 { NULL
, posts_wtext
}, /* Ft */
166 { NULL
, posts_text
}, /* Ic */
167 { NULL
, posts_in
}, /* In */
168 { NULL
, posts_text
}, /* Li */
169 { NULL
, posts_wtext
}, /* Nd */
170 { NULL
, posts_nm
}, /* Nm */
171 { NULL
, posts_wline
}, /* Op */
172 { NULL
, NULL
}, /* Ot */
173 { NULL
, NULL
}, /* Pa */
174 { pres_rv
, posts_notext
}, /* Rv */
175 { pres_st
, posts_notext
}, /* St */
176 { NULL
, posts_text
}, /* Va */
177 { NULL
, posts_text
}, /* Vt */
178 { NULL
, posts_xr
}, /* Xr */
179 { NULL
, posts_text
}, /* %A */
180 { NULL
, posts_text
}, /* %B */
181 { NULL
, posts_text
}, /* %D */
182 { NULL
, posts_text
}, /* %I */
183 { NULL
, posts_text
}, /* %J */
184 { NULL
, posts_text
}, /* %N */
185 { NULL
, posts_text
}, /* %O */
186 { NULL
, posts_text
}, /* %P */
187 { NULL
, posts_text
}, /* %R */
188 { NULL
, posts_text
}, /* %T */
189 { NULL
, posts_text
}, /* %V */
190 { NULL
, NULL
}, /* Ac */
191 { NULL
, NULL
}, /* Ao */
192 { NULL
, posts_wline
}, /* Aq */
193 { NULL
, posts_at
}, /* At */
194 { NULL
, NULL
}, /* Bc */
195 { NULL
, posts_bf
}, /* Bf */
196 { NULL
, NULL
}, /* Bo */
197 { NULL
, posts_wline
}, /* Bq */
198 { NULL
, NULL
}, /* Bsx */
199 { NULL
, NULL
}, /* Bx */
200 { NULL
, posts_bool
}, /* Db */
201 { NULL
, NULL
}, /* Dc */
202 { NULL
, NULL
}, /* Do */
203 { NULL
, posts_wline
}, /* Dq */
204 { NULL
, NULL
}, /* Ec */
205 { NULL
, NULL
}, /* Ef */ /* -symbolic, etc. */
206 { NULL
, posts_text
}, /* Em */
207 { NULL
, NULL
}, /* Eo */
208 { NULL
, NULL
}, /* Fx */
209 { NULL
, posts_text
}, /* Ms */
210 { NULL
, posts_notext
}, /* No */
211 { NULL
, posts_notext
}, /* Ns */
212 { NULL
, NULL
}, /* Nx */
213 { NULL
, NULL
}, /* Ox */
214 { NULL
, NULL
}, /* Pc */
215 { NULL
, NULL
}, /* Pf */
216 { NULL
, NULL
}, /* Po */
217 { NULL
, posts_wline
}, /* Pq */
218 { NULL
, NULL
}, /* Qc */
219 { NULL
, posts_wline
}, /* Ql */
220 { NULL
, NULL
}, /* Qo */
221 { NULL
, posts_wline
}, /* Qq */
222 { NULL
, NULL
}, /* Re */
223 { NULL
, NULL
}, /* Rs */
224 { NULL
, NULL
}, /* Sc */
225 { NULL
, NULL
}, /* So */
226 { NULL
, posts_wline
}, /* Sq */
227 { NULL
, posts_bool
}, /* Sm */
228 { NULL
, posts_text
}, /* Sx */
229 { NULL
, posts_text
}, /* Sy */
230 { NULL
, posts_text
}, /* Tn */
231 { NULL
, NULL
}, /* Ux */
232 { NULL
, NULL
}, /* Xc */
233 { NULL
, NULL
}, /* Xo */
234 { NULL
, NULL
}, /* Fo */
235 { NULL
, NULL
}, /* Fc */
236 { NULL
, NULL
}, /* Oo */
237 { NULL
, NULL
}, /* Oc */
238 { NULL
, NULL
}, /* Bk */
239 { NULL
, NULL
}, /* Ek */
240 { NULL
, posts_notext
}, /* Bt */
241 { NULL
, NULL
}, /* Hf */
242 { NULL
, NULL
}, /* Fr */
243 { NULL
, posts_notext
}, /* Ud */
248 post_check_children_count(struct mdoc
*mdoc
)
253 for (i
= 0, n
= mdoc
->last
->child
; n
; n
= n
->next
, i
++)
260 post_check_children_wgt(struct mdoc
*mdoc
, const char *p
, int sz
)
264 if ((i
= post_check_children_count(mdoc
)) > sz
)
266 return(mdoc_warn(mdoc
, WARN_SYNTAX
, "macro suggests more "
267 "than %d %s (has %d)", sz
, p
, i
));
272 post_check_children_gt(struct mdoc
*mdoc
, const char *p
, int sz
)
276 if ((i
= post_check_children_count(mdoc
)) > sz
)
278 return(mdoc_err(mdoc
, "macro requires more than %d "
279 "%s (has %d)", sz
, p
, i
));
284 post_check_children_weq(struct mdoc
*mdoc
, const char *p
, int sz
)
288 if ((i
= post_check_children_count(mdoc
)) == sz
)
290 return(mdoc_warn(mdoc
, WARN_SYNTAX
, "macro suggests %d "
291 "%s (has %d)", sz
, p
, i
));
296 post_check_children_eq(struct mdoc
*mdoc
, const char *p
, int sz
)
300 if ((i
= post_check_children_count(mdoc
)) == sz
)
302 return(mdoc_err(mdoc
, "macro requires %d %s "
303 "(have %d)", sz
, p
, i
));
308 post_check_children_lt(struct mdoc
*mdoc
, const char *p
, int sz
)
312 if ((i
= post_check_children_count(mdoc
)) < sz
)
314 return(mdoc_err(mdoc
, "macro requires less than %d "
315 "%s (have %d)", sz
, p
, i
));
320 pre_check_stdarg(struct mdoc
*mdoc
, struct mdoc_node
*node
)
323 if (1 == node
->data
.elem
.argc
&&
324 MDOC_Std
== node
->data
.elem
.argv
[0].arg
)
326 return(mdoc_nwarn(mdoc
, node
, WARN_COMPAT
,
327 "macro suggests single `%s' argument",
328 mdoc_argnames
[MDOC_Std
]));
333 pre_check_msecs(struct mdoc
*mdoc
, struct mdoc_node
*node
,
334 int sz
, enum mdoc_msec
*msecs
)
338 for (i
= 0; i
< sz
; i
++)
339 if (msecs
[i
] == mdoc
->meta
.msec
)
341 return(mdoc_nwarn(mdoc
, node
, WARN_COMPAT
, "macro not "
342 "appropriate for manual section"));
347 pre_check_parent(struct mdoc
*mdoc
, struct mdoc_node
*node
,
348 int tok
, enum mdoc_type type
)
351 if (type
!= node
->parent
->type
)
352 return(mdoc_nerr(mdoc
, node
, "invalid macro parent class %s, expected %s",
353 mdoc_type2a(node
->parent
->type
),
355 if (MDOC_ROOT
!= type
&& tok
!= node
->parent
->tok
)
356 return(mdoc_nerr(mdoc
, node
, "invalid macro parent `%s', expected `%s'",
357 mdoc_macronames
[node
->parent
->tok
],
358 mdoc_macronames
[tok
]));
364 bwarn_ge1(struct mdoc
*mdoc
)
367 if (MDOC_BODY
!= mdoc
->last
->type
)
369 return(post_check_children_wgt(mdoc
, "body children", 0));
374 ewarn_eq1(struct mdoc
*mdoc
)
377 assert(MDOC_ELEM
== mdoc
->last
->type
);
378 return(post_check_children_weq(mdoc
, "parameters", 1));
383 ewarn_eq0(struct mdoc
*mdoc
)
386 assert(MDOC_ELEM
== mdoc
->last
->type
);
387 return(post_check_children_weq(mdoc
, "parameters", 0));
392 ewarn_ge1(struct mdoc
*mdoc
)
395 assert(MDOC_ELEM
== mdoc
->last
->type
);
396 return(post_check_children_wgt(mdoc
, "parameters", 0));
401 eerr_eq1(struct mdoc
*mdoc
)
404 assert(MDOC_ELEM
== mdoc
->last
->type
);
405 return(post_check_children_eq(mdoc
, "parameters", 1));
410 eerr_le2(struct mdoc
*mdoc
)
413 assert(MDOC_ELEM
== mdoc
->last
->type
);
414 return(post_check_children_lt(mdoc
, "parameters", 3));
419 eerr_le1(struct mdoc
*mdoc
)
422 assert(MDOC_ELEM
== mdoc
->last
->type
);
423 return(post_check_children_lt(mdoc
, "parameters", 2));
428 eerr_eq0(struct mdoc
*mdoc
)
431 assert(MDOC_ELEM
== mdoc
->last
->type
);
432 return(post_check_children_eq(mdoc
, "parameters", 0));
437 eerr_ge1(struct mdoc
*mdoc
)
440 assert(MDOC_ELEM
== mdoc
->last
->type
);
441 return(post_check_children_gt(mdoc
, "parameters", 0));
446 herr_eq0(struct mdoc
*mdoc
)
449 if (MDOC_HEAD
!= mdoc
->last
->type
)
451 return(post_check_children_eq(mdoc
, "parameters", 0));
456 herr_le1(struct mdoc
*mdoc
)
458 if (MDOC_HEAD
!= mdoc
->last
->type
)
460 return(post_check_children_lt(mdoc
, "parameters", 2));
465 herr_ge1(struct mdoc
*mdoc
)
468 if (MDOC_HEAD
!= mdoc
->last
->type
)
470 return(post_check_children_gt(mdoc
, "parameters", 0));
475 pre_display(struct mdoc
*mdoc
, struct mdoc_node
*node
)
479 if (MDOC_BLOCK
!= node
->type
)
484 for (n
= mdoc
->last
->parent
; n
; n
= n
->parent
)
485 if (MDOC_BLOCK
== n
->type
)
486 if (MDOC_Bd
== n
->tok
)
490 return(mdoc_nerr(mdoc
, node
, "displays may not be nested"));
495 pre_bl(struct mdoc
*mdoc
, struct mdoc_node
*node
)
498 struct mdoc_arg
*argv
;
501 if (MDOC_BLOCK
!= node
->type
)
503 assert(MDOC_Bl
== node
->tok
);
506 argc
= node
->data
.block
.argc
;
509 for (i
= type
= err
= 0; i
< argc
; i
++) {
510 argv
= &node
->data
.block
.argv
[(int)i
];
543 return(mdoc_err(mdoc
, "no list type specified"));
547 return(mdoc_perr(mdoc
, argv
->line
,
548 argv
->pos
, "only one list type possible"));
553 pre_bd(struct mdoc
*mdoc
, struct mdoc_node
*node
)
556 struct mdoc_arg
*argv
;
559 if (MDOC_BLOCK
!= node
->type
)
561 assert(MDOC_Bd
== node
->tok
);
564 argc
= node
->data
.block
.argc
;
567 for (err
= i
= type
= 0; 0 == err
&& i
< argc
; i
++) {
568 argv
= &node
->data
.block
.argv
[(int)i
];
573 case (MDOC_Unfilled
):
589 return(mdoc_err(mdoc
, "no display type specified"));
593 return(mdoc_perr(mdoc
, argv
->line
,
594 argv
->pos
, "only one display type possible"));
599 pre_ss(struct mdoc
*mdoc
, struct mdoc_node
*node
)
602 if (MDOC_BLOCK
!= node
->type
)
604 return(pre_check_parent(mdoc
, node
, MDOC_Sh
, MDOC_BODY
));
609 pre_sh(struct mdoc
*mdoc
, struct mdoc_node
*node
)
612 if (MDOC_BLOCK
!= node
->type
)
614 return(pre_check_parent(mdoc
, node
, -1, MDOC_ROOT
));
619 pre_st(struct mdoc
*mdoc
, struct mdoc_node
*node
)
622 assert(MDOC_ELEM
== node
->type
);
623 assert(MDOC_St
== node
->tok
);
624 if (1 == node
->data
.elem
.argc
)
626 return(mdoc_nerr(mdoc
, node
, "macro must have one argument"));
631 pre_an(struct mdoc
*mdoc
, struct mdoc_node
*node
)
634 assert(MDOC_ELEM
== node
->type
);
635 assert(MDOC_An
== node
->tok
);
636 if (1 >= node
->data
.elem
.argc
)
638 return(mdoc_nerr(mdoc
, node
, "macro may only have one argument"));
643 pre_rv(struct mdoc
*mdoc
, struct mdoc_node
*node
)
645 enum mdoc_msec msecs
[2];
647 assert(MDOC_ELEM
== node
->type
);
648 assert(MDOC_Rv
== node
->tok
);
652 if ( ! pre_check_msecs(mdoc
, node
, 2, msecs
))
654 return(pre_check_stdarg(mdoc
, node
));
659 pre_ex(struct mdoc
*mdoc
, struct mdoc_node
*node
)
661 enum mdoc_msec msecs
[3];
663 assert(MDOC_ELEM
== node
->type
);
664 assert(MDOC_Ex
== node
->tok
);
669 if ( ! pre_check_msecs(mdoc
, node
, 3, msecs
))
671 return(pre_check_stdarg(mdoc
, node
));
676 pre_er(struct mdoc
*mdoc
, struct mdoc_node
*node
)
678 enum mdoc_msec msecs
[1];
681 return(pre_check_msecs(mdoc
, node
, 1, msecs
));
686 pre_cd(struct mdoc
*mdoc
, struct mdoc_node
*node
)
688 enum mdoc_msec msecs
[1];
691 return(pre_check_msecs(mdoc
, node
, 1, msecs
));
696 pre_it(struct mdoc
*mdoc
, struct mdoc_node
*node
)
699 if (MDOC_BLOCK
!= node
->type
)
701 return(pre_check_parent(mdoc
, node
, MDOC_Bl
, MDOC_BODY
));
706 pre_prologue(struct mdoc
*mdoc
, struct mdoc_node
*node
)
709 if (SEC_PROLOGUE
!= mdoc
->sec_lastn
)
710 return(mdoc_nerr(mdoc
, node
, "macro may only be invoked in the prologue"));
711 assert(MDOC_ELEM
== node
->type
);
713 /* Check for ordering. */
717 if (mdoc
->meta
.title
&& mdoc
->meta
.date
)
719 return(mdoc_nerr(mdoc
, node
, "prologue macro out-of-order"));
721 if (NULL
== mdoc
->meta
.title
&& mdoc
->meta
.date
)
723 return(mdoc_nerr(mdoc
, node
, "prologue macro out-of-order"));
725 if (NULL
== mdoc
->meta
.title
&& 0 == mdoc
->meta
.date
)
727 return(mdoc_nerr(mdoc
, node
, "prologue macro out-of-order"));
733 /* Check for repetition. */
737 if (NULL
== mdoc
->meta
.os
)
741 if (0 == mdoc
->meta
.date
)
745 if (NULL
== mdoc
->meta
.title
)
753 return(mdoc_nerr(mdoc
, node
, "prologue macro repeated"));
758 post_bf(struct mdoc
*mdoc
)
761 struct mdoc_node
*head
;
763 if (MDOC_BLOCK
!= mdoc
->last
->type
)
765 assert(MDOC_Bf
== mdoc
->last
->tok
);
766 head
= mdoc
->last
->data
.block
.head
;
769 if (0 == mdoc
->last
->data
.block
.argc
) {
771 assert(MDOC_TEXT
== head
->child
->type
);
772 p
= head
->child
->data
.text
.string
;
773 if (xstrcmp(p
, "Em"))
775 else if (xstrcmp(p
, "Li"))
777 else if (xstrcmp(p
, "Sm"))
779 return(mdoc_nerr(mdoc
, head
->child
, "invalid font mode"));
781 return(mdoc_err(mdoc
, "macro expects an argument or parameter"));
784 return(mdoc_err(mdoc
, "macro expects an argument or parameter"));
785 if (1 == mdoc
->last
->data
.block
.argc
)
787 return(mdoc_err(mdoc
, "macro expects an argument or parameter"));
792 post_nm(struct mdoc
*mdoc
)
795 assert(MDOC_ELEM
== mdoc
->last
->type
);
796 assert(MDOC_Nm
== mdoc
->last
->tok
);
797 if (mdoc
->last
->child
)
801 return(mdoc_err(mdoc
, "macro `%s' has not been invoked with a name",
802 mdoc_macronames
[MDOC_Nm
]));
807 post_xr(struct mdoc
*mdoc
)
811 assert(MDOC_ELEM
== mdoc
->last
->type
);
812 assert(MDOC_Xr
== mdoc
->last
->tok
);
813 assert(mdoc
->last
->child
);
814 assert(MDOC_TEXT
== mdoc
->last
->child
->type
);
816 if (NULL
== (n
= mdoc
->last
->child
->next
))
818 assert(MDOC_TEXT
== n
->type
);
819 if (MSEC_DEFAULT
!= mdoc_atomsec(n
->data
.text
.string
))
821 return(mdoc_nerr(mdoc
, n
, "invalid manual section"));
826 post_at(struct mdoc
*mdoc
)
829 assert(MDOC_ELEM
== mdoc
->last
->type
);
830 assert(MDOC_At
== mdoc
->last
->tok
);
832 if (NULL
== mdoc
->last
->child
)
834 assert(MDOC_TEXT
== mdoc
->last
->child
->type
);
836 if (ATT_DEFAULT
!= mdoc_atoatt(mdoc
->last
->child
->data
.text
.string
))
838 return(mdoc_err(mdoc
, "macro expects a valid AT&T version symbol"));
843 post_an(struct mdoc
*mdoc
)
846 assert(MDOC_ELEM
== mdoc
->last
->type
);
847 assert(MDOC_An
== mdoc
->last
->tok
);
849 if (0 != mdoc
->last
->data
.elem
.argc
) {
850 if (NULL
== mdoc
->last
->child
)
852 return(mdoc_err(mdoc
, "macro expects either argument or parameters"));
855 if (mdoc
->last
->child
)
857 return(mdoc_err(mdoc
, "macro expects either argument or parameters"));
862 post_ex(struct mdoc
*mdoc
)
865 assert(MDOC_ELEM
== mdoc
->last
->type
);
866 assert(MDOC_Ex
== mdoc
->last
->tok
);
868 if (0 == mdoc
->last
->data
.elem
.argc
) {
869 if (mdoc
->last
->child
)
871 return(mdoc_err(mdoc
, "macro expects `%s' or a single child",
872 mdoc_argnames
[MDOC_Std
]));
874 if (mdoc
->last
->child
)
875 return(mdoc_err(mdoc
, "macro expects `%s' or a single child",
876 mdoc_argnames
[MDOC_Std
]));
877 if (1 != mdoc
->last
->data
.elem
.argc
)
878 return(mdoc_err(mdoc
, "macro expects `%s' or a single child",
879 mdoc_argnames
[MDOC_Std
]));
880 if (MDOC_Std
!= mdoc
->last
->data
.elem
.argv
[0].arg
)
881 return(mdoc_err(mdoc
, "macro expects `%s' or a single child",
882 mdoc_argnames
[MDOC_Std
]));
887 /* Warn if `Bl' type-specific syntax isn't reflected in items. */
889 post_it(struct mdoc
*mdoc
)
892 #define TYPE_NONE (0)
893 #define TYPE_BODY (1)
894 #define TYPE_HEAD (2)
898 if (MDOC_BLOCK
!= mdoc
->last
->type
)
901 assert(MDOC_It
== mdoc
->last
->tok
);
903 n
= mdoc
->last
->parent
;
905 assert(MDOC_Bl
== n
->tok
);
908 assert(MDOC_BLOCK
== n
->type
);
909 assert(MDOC_Bl
== n
->tok
);
911 argc
= n
->data
.block
.argc
;
915 /* Some types require block-head, some not. */
918 for (i
= 0; TYPE_NONE
== type
&& i
< argc
; i
++)
919 switch (n
->data
.block
.argv
[(int)i
].arg
) {
930 sv
= n
->data
.block
.argv
[(int)i
].arg
;
944 sv
= n
->data
.block
.argv
[(int)i
].arg
;
950 assert(TYPE_NONE
!= type
);
952 if (TYPE_HEAD
== type
) {
953 n
= mdoc
->last
->data
.block
.head
;
955 if (NULL
== n
->child
)
956 if ( ! mdoc_warn(mdoc
, WARN_SYNTAX
, "macro suggests line parameters"))
959 n
= mdoc
->last
->data
.block
.body
;
961 if (NULL
== n
->child
)
962 if ( ! mdoc_warn(mdoc
, WARN_SYNTAX
, "macro suggests body children"))
968 assert(TYPE_BODY
== type
);
969 assert(mdoc
->last
->data
.block
.head
);
971 n
= mdoc
->last
->data
.block
.head
;
974 if ( ! mdoc_warn(mdoc
, WARN_SYNTAX
, "macro suggests no line parameters"))
977 n
= mdoc
->last
->data
.block
.body
;
979 if (NULL
== n
->child
)
980 if ( ! mdoc_warn(mdoc
, WARN_SYNTAX
, "macro suggests body children"))
984 if (MDOC_Column
!= sv
)
987 /* Make sure the number of columns is sane. */
989 argc
= mdoc
->last
->parent
->parent
->data
.block
.argv
->sz
;
990 n
= mdoc
->last
->data
.block
.head
->child
;
992 for (i
= 0; n
; n
= n
->next
)
997 return(mdoc_err(mdoc
, "expected %zu list columns, have %zu", argc
, i
));
1005 post_bl(struct mdoc
*mdoc
)
1007 struct mdoc_node
*n
;
1009 if (MDOC_BODY
!= mdoc
->last
->type
)
1011 assert(MDOC_Bl
== mdoc
->last
->tok
);
1014 for (n
= mdoc
->last
->child
; n
; n
= n
->next
) {
1015 if (MDOC_BLOCK
== n
->type
)
1016 if (MDOC_It
== n
->tok
)
1022 return(mdoc_nerr(mdoc
, n
, "invalid child of parent macro `Bl'"));
1027 ebool(struct mdoc
*mdoc
)
1029 struct mdoc_node
*n
;
1031 assert(MDOC_ELEM
== mdoc
->last
->type
);
1033 for (n
= mdoc
->last
->child
; n
; n
= n
->next
) {
1034 if (MDOC_TEXT
!= n
->type
)
1036 if (xstrcmp(n
->data
.text
.string
, "on"))
1038 if (xstrcmp(n
->data
.text
.string
, "off"))
1044 return(mdoc_nerr(mdoc
, n
, "expected boolean value"));
1049 post_root(struct mdoc
*mdoc
)
1052 if (NULL
== mdoc
->last
->child
)
1053 return(mdoc_err(mdoc
, "document has no data"));
1054 if (NULL
== mdoc
->meta
.title
)
1055 return(mdoc_err(mdoc
, "document has incomplete prologue"));
1056 if (NULL
== mdoc
->meta
.os
)
1057 return(mdoc_err(mdoc
, "document has incomplete prologue"));
1058 if (0 == mdoc
->meta
.date
)
1059 return(mdoc_err(mdoc
, "document has incomplete prologue"));
1064 /* Warn if conventional sections are out of order. */
1066 post_sh(struct mdoc
*mdoc
)
1071 if (MDOC_HEAD
!= mdoc
->last
->type
)
1073 assert(MDOC_Sh
== mdoc
->last
->tok
);
1075 if ( ! xstrlcats(buf
, mdoc
->last
->child
, 64))
1076 return(mdoc_err(mdoc
, "macro parameters too long"));
1078 if (SEC_CUSTOM
== (sec
= mdoc_atosec(buf
)))
1080 if (sec
> mdoc
->sec_lastn
)
1082 if (sec
== mdoc
->sec_lastn
)
1083 return(mdoc_warn(mdoc
, WARN_SYNTAX
, "section repeated"));
1084 return(mdoc_warn(mdoc
, WARN_SYNTAX
, "section out of conventional order"));
1089 mdoc_valid_pre(struct mdoc
*mdoc
, struct mdoc_node
*node
)
1093 if (MDOC_TEXT
== node
->type
)
1095 assert(MDOC_ROOT
!= node
->type
);
1097 if (NULL
== mdoc_valids
[node
->tok
].pre
)
1099 for (p
= mdoc_valids
[node
->tok
].pre
; *p
; p
++)
1100 if ( ! (*p
)(mdoc
, node
))
1107 mdoc_valid_post(struct mdoc
*mdoc
)
1111 if (MDOC_VALID
& mdoc
->last
->flags
)
1113 mdoc
->last
->flags
|= MDOC_VALID
;
1115 if (MDOC_TEXT
== mdoc
->last
->type
)
1117 if (MDOC_ROOT
== mdoc
->last
->type
)
1118 return(post_root(mdoc
));
1120 if (NULL
== mdoc_valids
[mdoc
->last
->tok
].post
)
1122 for (p
= mdoc_valids
[mdoc
->last
->tok
].post
; *p
; p
++)