]>
git.cameronkatri.com Git - mandoc.git/blob - validate.c
1 /* $Id: validate.c,v 1.41 2009/01/19 23:11:12 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: make sure prologue is complete. */
31 /* FIXME: valid character-escape checks. */
32 /* FIXME: make sure required sections are included (NAME, ...). */
41 static int pre_check_parent(struct mdoc
*, struct mdoc_node
*,
43 static int pre_check_msecs(struct mdoc
*, struct mdoc_node
*,
44 int, enum mdoc_msec
*);
45 static int pre_check_stdarg(struct mdoc
*, struct mdoc_node
*);
46 static int post_check_children_count(struct mdoc
*);
47 static int post_check_children_lt(struct mdoc
*, const char *, int);
48 static int post_check_children_gt(struct mdoc
*, const char *, int);
49 static int post_check_children_wgt(struct mdoc
*, const char *, int);
50 static int post_check_children_eq(struct mdoc
*, const char *, int);
51 static int post_check_children_weq(struct mdoc
*, const char *, int);
53 /* Specific pre-child-parse routines. */
55 static int pre_display(struct mdoc
*, struct mdoc_node
*);
56 static int pre_sh(struct mdoc
*, struct mdoc_node
*);
57 static int pre_ss(struct mdoc
*, struct mdoc_node
*);
58 static int pre_bd(struct mdoc
*, struct mdoc_node
*);
59 static int pre_bl(struct mdoc
*, struct mdoc_node
*);
60 static int pre_it(struct mdoc
*, struct mdoc_node
*);
61 static int pre_cd(struct mdoc
*, struct mdoc_node
*);
62 static int pre_er(struct mdoc
*, struct mdoc_node
*);
63 static int pre_ex(struct mdoc
*, struct mdoc_node
*);
64 static int pre_rv(struct mdoc
*, struct mdoc_node
*);
65 static int pre_an(struct mdoc
*, struct mdoc_node
*);
66 static int pre_st(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
*);
69 static int pre_prologue(struct mdoc
*, struct mdoc_node
*);
71 /* Specific post-child-parse routines. */
73 static int herr_ge1(struct mdoc
*);
74 static int herr_le1(struct mdoc
*);
75 static int hwarn_ge1(struct mdoc
*);
76 static int herr_eq0(struct mdoc
*);
77 static int eerr_eq0(struct mdoc
*);
78 static int eerr_le1(struct mdoc
*);
79 static int eerr_le2(struct mdoc
*);
80 static int eerr_eq1(struct mdoc
*);
81 static int eerr_ge1(struct mdoc
*);
82 static int ewarn_eq0(struct mdoc
*);
83 static int ewarn_eq1(struct mdoc
*);
84 static int bwarn_ge1(struct mdoc
*);
85 static int berr_eq0(struct mdoc
*);
86 static int ewarn_ge1(struct mdoc
*);
87 static int ebool(struct mdoc
*);
88 static int post_sh(struct mdoc
*);
89 static int post_bl(struct mdoc
*);
90 static int post_it(struct mdoc
*);
91 static int post_ex(struct mdoc
*);
92 static int post_an(struct mdoc
*);
93 static int post_at(struct mdoc
*);
94 static int post_xr(struct mdoc
*);
95 static int post_nm(struct mdoc
*);
96 static int post_bf(struct mdoc
*);
97 static int post_root(struct mdoc
*);
99 /* Collections of pre-child-parse routines. */
101 static v_pre pres_prologue
[] = { pre_prologue
, NULL
};
102 static v_pre pres_d1
[] = { pre_display
, NULL
};
103 static v_pre pres_bd
[] = { pre_display
, pre_bd
, NULL
};
104 static v_pre pres_bl
[] = { pre_bl
, NULL
};
105 static v_pre pres_it
[] = { pre_it
, NULL
};
106 static v_pre pres_ss
[] = { pre_ss
, NULL
};
107 static v_pre pres_sh
[] = { pre_sh
, NULL
};
108 static v_pre pres_cd
[] = { pre_cd
, NULL
};
109 static v_pre pres_er
[] = { pre_er
, NULL
};
110 static v_pre pres_ex
[] = { pre_ex
, NULL
};
111 static v_pre pres_rv
[] = { pre_rv
, NULL
};
112 static v_pre pres_an
[] = { pre_an
, NULL
};
113 static v_pre pres_st
[] = { pre_st
, NULL
};
115 /* Collections of post-child-parse routines. */
117 static v_post posts_bool
[] = { eerr_eq1
, ebool
, NULL
};
118 static v_post posts_bd
[] = { herr_eq0
, bwarn_ge1
, NULL
};
119 static v_post posts_text
[] = { eerr_ge1
, NULL
};
120 static v_post posts_wtext
[] = { ewarn_ge1
, NULL
};
121 static v_post posts_notext
[] = { eerr_eq0
, NULL
};
122 static v_post posts_wline
[] = { hwarn_ge1
, berr_eq0
, NULL
};
123 static v_post posts_sh
[] = { herr_ge1
, bwarn_ge1
, post_sh
, NULL
};
124 static v_post posts_bl
[] = { herr_eq0
, bwarn_ge1
, post_bl
, NULL
};
125 static v_post posts_it
[] = { post_it
, NULL
};
126 static v_post posts_in
[] = { ewarn_eq1
, NULL
};
127 static v_post posts_ss
[] = { herr_ge1
, NULL
};
128 static v_post posts_pp
[] = { ewarn_eq0
, NULL
};
129 static v_post posts_d1
[] = { herr_ge1
, NULL
};
130 static v_post posts_ex
[] = { eerr_le1
, post_ex
, NULL
};
131 static v_post posts_an
[] = { post_an
, NULL
};
132 static v_post posts_at
[] = { post_at
, NULL
};
133 static v_post posts_xr
[] = { eerr_ge1
, eerr_le2
, post_xr
, NULL
};
134 static v_post posts_nm
[] = { post_nm
, NULL
};
135 static v_post posts_bf
[] = { herr_le1
, post_bf
, NULL
};
137 /* Per-macro pre- and post-child-check routine collections. */
139 const struct valids mdoc_valids
[MDOC_MAX
] = {
140 { NULL
, NULL
}, /* \" */
141 { pres_prologue
, posts_text
}, /* Dd */
142 { pres_prologue
, NULL
}, /* Dt */
143 { pres_prologue
, NULL
}, /* Os */
144 /* FIXME: NAME section internal ordering. */
145 { pres_sh
, posts_sh
}, /* Sh */
146 { pres_ss
, posts_ss
}, /* Ss */
147 { NULL
, posts_pp
}, /* Pp */
148 { pres_d1
, posts_d1
}, /* D1 */
149 { pres_d1
, posts_d1
}, /* Dl */
150 { pres_bd
, posts_bd
}, /* Bd */
151 { NULL
, NULL
}, /* Ed */
152 { pres_bl
, posts_bl
}, /* Bl */
153 { NULL
, NULL
}, /* El */
154 { pres_it
, posts_it
}, /* It */
155 { NULL
, posts_text
}, /* Ad */
156 { pres_an
, posts_an
}, /* An */
157 { NULL
, NULL
}, /* Ar */
158 { pres_cd
, posts_text
}, /* Cd */
159 { NULL
, NULL
}, /* Cm */
160 { NULL
, posts_text
}, /* Dv */
161 { pres_er
, posts_text
}, /* Er */
162 { NULL
, posts_text
}, /* Ev */
163 { pres_ex
, posts_ex
}, /* Ex */
164 { NULL
, posts_text
}, /* Fa */
165 /* FIXME: only in SYNOPSIS section. */
166 { NULL
, posts_wtext
}, /* Fd */
167 { NULL
, NULL
}, /* Fl */
168 { NULL
, posts_text
}, /* Fn */
169 { NULL
, posts_wtext
}, /* Ft */
170 { NULL
, posts_text
}, /* Ic */
171 { NULL
, posts_in
}, /* In */
172 { NULL
, posts_text
}, /* Li */
173 { NULL
, posts_wtext
}, /* Nd */
174 { NULL
, posts_nm
}, /* Nm */
175 { NULL
, posts_wline
}, /* Op */
176 { NULL
, NULL
}, /* Ot */
177 { NULL
, NULL
}, /* Pa */
178 { pres_rv
, posts_notext
}, /* Rv */
179 { pres_st
, posts_notext
}, /* St */
180 { NULL
, posts_text
}, /* Va */
181 { NULL
, posts_text
}, /* Vt */
182 { NULL
, posts_xr
}, /* Xr */
183 { NULL
, posts_text
}, /* %A */
184 { NULL
, posts_text
}, /* %B */
185 { NULL
, posts_text
}, /* %D */
186 { NULL
, posts_text
}, /* %I */
187 { NULL
, posts_text
}, /* %J */
188 { NULL
, posts_text
}, /* %N */
189 { NULL
, posts_text
}, /* %O */
190 { NULL
, posts_text
}, /* %P */
191 { NULL
, posts_text
}, /* %R */
192 { NULL
, posts_text
}, /* %T */
193 { NULL
, posts_text
}, /* %V */
194 { NULL
, NULL
}, /* Ac */
195 { NULL
, NULL
}, /* Ao */
196 { NULL
, posts_wline
}, /* Aq */
197 { NULL
, posts_at
}, /* At */
198 { NULL
, NULL
}, /* Bc */
199 { NULL
, posts_bf
}, /* Bf */ /* FIXME */
200 { NULL
, NULL
}, /* Bo */
201 { NULL
, posts_wline
}, /* Bq */
202 { NULL
, NULL
}, /* Bsx */
203 { NULL
, NULL
}, /* Bx */
204 { NULL
, posts_bool
}, /* Db */
205 { NULL
, NULL
}, /* Dc */
206 { NULL
, NULL
}, /* Do */
207 { NULL
, posts_wline
}, /* Dq */
208 { NULL
, NULL
}, /* Ec */
209 { NULL
, NULL
}, /* Ef */ /* -symbolic, etc. */
210 { NULL
, posts_text
}, /* Em */
211 { NULL
, NULL
}, /* Eo */
212 { NULL
, NULL
}, /* Fx */
213 { NULL
, posts_text
}, /* Ms */
214 { NULL
, posts_notext
}, /* No */
215 { NULL
, posts_notext
}, /* Ns */
216 { NULL
, NULL
}, /* Nx */
217 { NULL
, NULL
}, /* Ox */
218 { NULL
, NULL
}, /* Pc */
219 { NULL
, NULL
}, /* Pf */
220 { NULL
, NULL
}, /* Po */
221 { NULL
, posts_wline
}, /* Pq */
222 { NULL
, NULL
}, /* Qc */
223 { NULL
, posts_wline
}, /* Ql */
224 { NULL
, NULL
}, /* Qo */
225 { NULL
, posts_wline
}, /* Qq */
226 { NULL
, NULL
}, /* Re */
227 { NULL
, NULL
}, /* Rs */
228 { NULL
, NULL
}, /* Sc */
229 { NULL
, NULL
}, /* So */
230 { NULL
, posts_wline
}, /* Sq */
231 { NULL
, posts_bool
}, /* Sm */
232 { NULL
, posts_text
}, /* Sx */
233 { NULL
, posts_text
}, /* Sy */
234 { NULL
, posts_text
}, /* Tn */
235 { NULL
, NULL
}, /* Ux */
236 { NULL
, NULL
}, /* Xc */
237 { NULL
, NULL
}, /* Xo */
238 { NULL
, NULL
}, /* Fo */
239 { NULL
, NULL
}, /* Fc */
240 { NULL
, NULL
}, /* Oo */
241 { NULL
, NULL
}, /* Oc */
242 { NULL
, NULL
}, /* Bk */
243 { NULL
, NULL
}, /* Ek */
244 { NULL
, posts_notext
}, /* Bt */
245 { NULL
, NULL
}, /* Hf */
246 { NULL
, NULL
}, /* Fr */
247 { NULL
, posts_notext
}, /* Ud */
252 post_check_children_count(struct mdoc
*mdoc
)
257 for (i
= 0, n
= mdoc
->last
->child
; n
; n
= n
->next
, i
++)
264 post_check_children_wgt(struct mdoc
*mdoc
, const char *p
, int sz
)
268 if ((i
= post_check_children_count(mdoc
)) > sz
)
270 return(mdoc_warn(mdoc
, WARN_SYNTAX
, "macro suggests more "
271 "than %d %s (has %d)", sz
, p
, i
));
276 post_check_children_gt(struct mdoc
*mdoc
, const char *p
, int sz
)
280 if ((i
= post_check_children_count(mdoc
)) > sz
)
282 return(mdoc_err(mdoc
, "macro requires more than %d "
283 "%s (has %d)", sz
, p
, i
));
288 post_check_children_weq(struct mdoc
*mdoc
, const char *p
, int sz
)
292 if ((i
= post_check_children_count(mdoc
)) == sz
)
294 return(mdoc_warn(mdoc
, WARN_SYNTAX
, "macro suggests %d "
295 "%s (has %d)", sz
, p
, i
));
300 post_check_children_eq(struct mdoc
*mdoc
, const char *p
, int sz
)
304 if ((i
= post_check_children_count(mdoc
)) == sz
)
306 return(mdoc_err(mdoc
, "macro requires %d %s "
307 "(have %d)", sz
, p
, i
));
312 post_check_children_lt(struct mdoc
*mdoc
, const char *p
, int sz
)
316 if ((i
= post_check_children_count(mdoc
)) < sz
)
318 return(mdoc_err(mdoc
, "macro requires less than %d "
319 "%s (have %d)", sz
, p
, i
));
324 pre_check_stdarg(struct mdoc
*mdoc
, struct mdoc_node
*node
)
327 if (1 == node
->data
.elem
.argc
&&
328 MDOC_Std
== node
->data
.elem
.argv
[0].arg
)
330 return(mdoc_nwarn(mdoc
, node
, WARN_COMPAT
,
331 "macro suggests single `%s' argument",
332 mdoc_argnames
[MDOC_Std
]));
337 pre_check_msecs(struct mdoc
*mdoc
, struct mdoc_node
*node
,
338 int sz
, enum mdoc_msec
*msecs
)
342 for (i
= 0; i
< sz
; i
++)
343 if (msecs
[i
] == mdoc
->meta
.msec
)
345 return(mdoc_nwarn(mdoc
, node
, WARN_COMPAT
, "macro not "
346 "appropriate for manual section"));
351 pre_check_parent(struct mdoc
*mdoc
, struct mdoc_node
*node
,
352 int tok
, enum mdoc_type type
)
355 if (type
!= node
->parent
->type
)
356 return(mdoc_nerr(mdoc
, node
, "invalid macro parent class %s, expected %s",
357 mdoc_type2a(node
->parent
->type
),
359 if (MDOC_ROOT
!= type
&& tok
!= node
->parent
->tok
)
360 return(mdoc_nerr(mdoc
, node
, "invalid macro parent `%s', expected `%s'",
361 mdoc_macronames
[node
->parent
->tok
],
362 mdoc_macronames
[tok
]));
368 berr_eq0(struct mdoc
*mdoc
)
371 if (MDOC_BODY
!= mdoc
->last
->type
)
373 return(post_check_children_eq(mdoc
, "body children", 0));
378 bwarn_ge1(struct mdoc
*mdoc
)
381 if (MDOC_BODY
!= mdoc
->last
->type
)
383 return(post_check_children_wgt(mdoc
, "body children", 0));
388 ewarn_eq1(struct mdoc
*mdoc
)
391 assert(MDOC_ELEM
== mdoc
->last
->type
);
392 return(post_check_children_weq(mdoc
, "parameters", 1));
397 ewarn_eq0(struct mdoc
*mdoc
)
400 assert(MDOC_ELEM
== mdoc
->last
->type
);
401 return(post_check_children_weq(mdoc
, "parameters", 0));
406 ewarn_ge1(struct mdoc
*mdoc
)
409 assert(MDOC_ELEM
== mdoc
->last
->type
);
410 return(post_check_children_wgt(mdoc
, "parameters", 0));
415 eerr_eq1(struct mdoc
*mdoc
)
418 assert(MDOC_ELEM
== mdoc
->last
->type
);
419 return(post_check_children_eq(mdoc
, "parameters", 1));
424 eerr_le2(struct mdoc
*mdoc
)
427 assert(MDOC_ELEM
== mdoc
->last
->type
);
428 return(post_check_children_lt(mdoc
, "parameters", 3));
433 eerr_le1(struct mdoc
*mdoc
)
436 assert(MDOC_ELEM
== mdoc
->last
->type
);
437 return(post_check_children_lt(mdoc
, "parameters", 2));
442 eerr_eq0(struct mdoc
*mdoc
)
445 assert(MDOC_ELEM
== mdoc
->last
->type
);
446 return(post_check_children_eq(mdoc
, "parameters", 0));
451 eerr_ge1(struct mdoc
*mdoc
)
454 assert(MDOC_ELEM
== mdoc
->last
->type
);
455 return(post_check_children_gt(mdoc
, "parameters", 0));
460 herr_eq0(struct mdoc
*mdoc
)
463 if (MDOC_HEAD
!= mdoc
->last
->type
)
465 return(post_check_children_eq(mdoc
, "parameters", 0));
470 hwarn_ge1(struct mdoc
*mdoc
)
473 if (MDOC_HEAD
!= mdoc
->last
->type
)
475 return(post_check_children_wgt(mdoc
, "parameters", 0));
480 herr_le1(struct mdoc
*mdoc
)
482 if (MDOC_HEAD
!= mdoc
->last
->type
)
484 return(post_check_children_lt(mdoc
, "parameters", 2));
489 herr_ge1(struct mdoc
*mdoc
)
492 if (MDOC_HEAD
!= mdoc
->last
->type
)
494 return(post_check_children_gt(mdoc
, "parameters", 0));
499 pre_display(struct mdoc
*mdoc
, struct mdoc_node
*node
)
503 if (MDOC_BLOCK
!= node
->type
)
508 for (n
= mdoc
->last
->parent
; n
; n
= n
->parent
)
509 if (MDOC_BLOCK
== n
->type
)
510 if (MDOC_Bd
== n
->tok
)
514 return(mdoc_nerr(mdoc
, node
, "displays may not be nested"));
519 pre_bl(struct mdoc
*mdoc
, struct mdoc_node
*node
)
522 struct mdoc_arg
*argv
;
525 if (MDOC_BLOCK
!= node
->type
)
527 assert(MDOC_Bl
== node
->tok
);
530 argc
= node
->data
.block
.argc
;
533 for (i
= type
= err
= 0; i
< argc
; i
++) {
534 argv
= &node
->data
.block
.argv
[(int)i
];
567 return(mdoc_err(mdoc
, "no list type specified"));
571 return(mdoc_perr(mdoc
, argv
->line
,
572 argv
->pos
, "only one list type possible"));
577 pre_bd(struct mdoc
*mdoc
, struct mdoc_node
*node
)
580 struct mdoc_arg
*argv
;
583 if (MDOC_BLOCK
!= node
->type
)
585 assert(MDOC_Bd
== node
->tok
);
588 argc
= node
->data
.block
.argc
;
591 for (err
= i
= type
= 0; 0 == err
&& i
< argc
; i
++) {
592 argv
= &node
->data
.block
.argv
[(int)i
];
597 case (MDOC_Unfilled
):
613 return(mdoc_err(mdoc
, "no display type specified"));
617 return(mdoc_perr(mdoc
, argv
->line
,
618 argv
->pos
, "only one display type possible"));
623 pre_ss(struct mdoc
*mdoc
, struct mdoc_node
*node
)
626 if (MDOC_BLOCK
!= node
->type
)
628 return(pre_check_parent(mdoc
, node
, MDOC_Sh
, MDOC_BODY
));
633 pre_sh(struct mdoc
*mdoc
, struct mdoc_node
*node
)
636 if (MDOC_BLOCK
!= node
->type
)
638 return(pre_check_parent(mdoc
, node
, -1, MDOC_ROOT
));
643 pre_st(struct mdoc
*mdoc
, struct mdoc_node
*node
)
646 assert(MDOC_ELEM
== node
->type
);
647 assert(MDOC_St
== node
->tok
);
648 if (1 == node
->data
.elem
.argc
)
650 return(mdoc_nerr(mdoc
, node
, "macro must have one argument"));
655 pre_an(struct mdoc
*mdoc
, struct mdoc_node
*node
)
658 assert(MDOC_ELEM
== node
->type
);
659 assert(MDOC_An
== node
->tok
);
660 if (1 >= node
->data
.elem
.argc
)
662 return(mdoc_nerr(mdoc
, node
, "macro may only have one argument"));
667 pre_rv(struct mdoc
*mdoc
, struct mdoc_node
*node
)
669 enum mdoc_msec msecs
[2];
671 assert(MDOC_ELEM
== node
->type
);
672 assert(MDOC_Rv
== node
->tok
);
676 if ( ! pre_check_msecs(mdoc
, node
, 2, msecs
))
678 return(pre_check_stdarg(mdoc
, node
));
683 pre_ex(struct mdoc
*mdoc
, struct mdoc_node
*node
)
685 enum mdoc_msec msecs
[3];
687 assert(MDOC_ELEM
== node
->type
);
688 assert(MDOC_Ex
== node
->tok
);
693 if ( ! pre_check_msecs(mdoc
, node
, 3, msecs
))
695 return(pre_check_stdarg(mdoc
, node
));
700 pre_er(struct mdoc
*mdoc
, struct mdoc_node
*node
)
702 enum mdoc_msec msecs
[1];
705 return(pre_check_msecs(mdoc
, node
, 1, msecs
));
710 pre_cd(struct mdoc
*mdoc
, struct mdoc_node
*node
)
712 enum mdoc_msec msecs
[1];
715 return(pre_check_msecs(mdoc
, node
, 1, msecs
));
720 pre_it(struct mdoc
*mdoc
, struct mdoc_node
*node
)
723 if (MDOC_BLOCK
!= node
->type
)
725 return(pre_check_parent(mdoc
, node
, MDOC_Bl
, MDOC_BODY
));
730 pre_prologue(struct mdoc
*mdoc
, struct mdoc_node
*node
)
733 if (SEC_PROLOGUE
!= mdoc
->sec_lastn
)
734 return(mdoc_nerr(mdoc
, node
, "macro may only be invoked in the prologue"));
735 assert(MDOC_ELEM
== node
->type
);
737 /* Check for ordering. */
741 if (mdoc
->meta
.title
&& mdoc
->meta
.date
)
743 return(mdoc_nerr(mdoc
, node
, "prologue macro out-of-order"));
745 if (NULL
== mdoc
->meta
.title
&& mdoc
->meta
.date
)
747 return(mdoc_nerr(mdoc
, node
, "prologue macro out-of-order"));
749 if (NULL
== mdoc
->meta
.title
&& 0 == mdoc
->meta
.date
)
751 return(mdoc_nerr(mdoc
, node
, "prologue macro out-of-order"));
757 /* Check for repetition. */
761 if (NULL
== mdoc
->meta
.os
)
765 if (0 == mdoc
->meta
.date
)
769 if (NULL
== mdoc
->meta
.title
)
777 return(mdoc_nerr(mdoc
, node
, "prologue macro repeated"));
782 post_bf(struct mdoc
*mdoc
)
785 struct mdoc_node
*head
;
787 if (MDOC_BLOCK
!= mdoc
->last
->type
)
789 assert(MDOC_Bf
== mdoc
->last
->tok
);
790 head
= mdoc
->last
->data
.block
.head
;
793 if (0 == mdoc
->last
->data
.block
.argc
) {
795 assert(MDOC_TEXT
== head
->child
->type
);
796 p
= head
->child
->data
.text
.string
;
797 if (xstrcmp(p
, "Em"))
799 else if (xstrcmp(p
, "Li"))
801 else if (xstrcmp(p
, "Sm"))
803 return(mdoc_nerr(mdoc
, head
->child
, "invalid font mode"));
805 return(mdoc_err(mdoc
, "macro expects an argument or parameter"));
808 return(mdoc_err(mdoc
, "macro expects an argument or parameter"));
809 if (1 == mdoc
->last
->data
.block
.argc
)
811 return(mdoc_err(mdoc
, "macro expects an argument or parameter"));
816 post_nm(struct mdoc
*mdoc
)
819 assert(MDOC_ELEM
== mdoc
->last
->type
);
820 assert(MDOC_Nm
== mdoc
->last
->tok
);
821 if (mdoc
->last
->child
)
825 return(mdoc_err(mdoc
, "macro `%s' has not been invoked with a name",
826 mdoc_macronames
[MDOC_Nm
]));
831 post_xr(struct mdoc
*mdoc
)
835 assert(MDOC_ELEM
== mdoc
->last
->type
);
836 assert(MDOC_Xr
== mdoc
->last
->tok
);
837 assert(mdoc
->last
->child
);
838 assert(MDOC_TEXT
== mdoc
->last
->child
->type
);
840 if (NULL
== (n
= mdoc
->last
->child
->next
))
842 assert(MDOC_TEXT
== n
->type
);
843 if (MSEC_DEFAULT
!= mdoc_atomsec(n
->data
.text
.string
))
845 return(mdoc_nerr(mdoc
, n
, "invalid manual section"));
850 post_at(struct mdoc
*mdoc
)
853 assert(MDOC_ELEM
== mdoc
->last
->type
);
854 assert(MDOC_At
== mdoc
->last
->tok
);
856 if (NULL
== mdoc
->last
->child
)
858 assert(MDOC_TEXT
== mdoc
->last
->child
->type
);
860 if (ATT_DEFAULT
!= mdoc_atoatt(mdoc
->last
->child
->data
.text
.string
))
862 return(mdoc_err(mdoc
, "macro expects a valid AT&T version symbol"));
867 post_an(struct mdoc
*mdoc
)
870 assert(MDOC_ELEM
== mdoc
->last
->type
);
871 assert(MDOC_An
== mdoc
->last
->tok
);
873 if (0 != mdoc
->last
->data
.elem
.argc
) {
874 if (NULL
== mdoc
->last
->child
)
876 return(mdoc_err(mdoc
, "macro expects either argument or parameters"));
879 if (mdoc
->last
->child
)
881 return(mdoc_err(mdoc
, "macro expects either argument or parameters"));
886 post_ex(struct mdoc
*mdoc
)
889 assert(MDOC_ELEM
== mdoc
->last
->type
);
890 assert(MDOC_Ex
== mdoc
->last
->tok
);
892 if (0 == mdoc
->last
->data
.elem
.argc
) {
893 if (mdoc
->last
->child
)
895 return(mdoc_err(mdoc
, "macro expects `%s' or a single child",
896 mdoc_argnames
[MDOC_Std
]));
898 if (mdoc
->last
->child
)
899 return(mdoc_err(mdoc
, "macro expects `%s' or a single child",
900 mdoc_argnames
[MDOC_Std
]));
901 if (1 != mdoc
->last
->data
.elem
.argc
)
902 return(mdoc_err(mdoc
, "macro expects `%s' or a single child",
903 mdoc_argnames
[MDOC_Std
]));
904 if (MDOC_Std
!= mdoc
->last
->data
.elem
.argv
[0].arg
)
905 return(mdoc_err(mdoc
, "macro expects `%s' or a single child",
906 mdoc_argnames
[MDOC_Std
]));
911 /* Warn if `Bl' type-specific syntax isn't reflected in items. */
913 post_it(struct mdoc
*mdoc
)
916 #define TYPE_NONE (0)
917 #define TYPE_BODY (1)
918 #define TYPE_HEAD (2)
922 if (MDOC_BLOCK
!= mdoc
->last
->type
)
925 assert(MDOC_It
== mdoc
->last
->tok
);
927 n
= mdoc
->last
->parent
;
929 assert(MDOC_Bl
== n
->tok
);
932 assert(MDOC_BLOCK
== n
->type
);
933 assert(MDOC_Bl
== n
->tok
);
935 argc
= n
->data
.block
.argc
;
939 /* Some types require block-head, some not. */
942 for (i
= 0; TYPE_NONE
== type
&& i
< argc
; i
++)
943 switch (n
->data
.block
.argv
[(int)i
].arg
) {
954 sv
= n
->data
.block
.argv
[(int)i
].arg
;
968 sv
= n
->data
.block
.argv
[(int)i
].arg
;
974 assert(TYPE_NONE
!= type
);
976 if (TYPE_HEAD
== type
) {
977 n
= mdoc
->last
->data
.block
.head
;
979 if (NULL
== n
->child
)
980 if ( ! mdoc_warn(mdoc
, WARN_SYNTAX
, "macro suggests line parameters"))
983 n
= mdoc
->last
->data
.block
.body
;
985 if (NULL
== n
->child
)
986 if ( ! mdoc_warn(mdoc
, WARN_SYNTAX
, "macro suggests body children"))
992 assert(TYPE_BODY
== type
);
993 assert(mdoc
->last
->data
.block
.head
);
995 n
= mdoc
->last
->data
.block
.head
;
998 if ( ! mdoc_warn(mdoc
, WARN_SYNTAX
, "macro suggests no line parameters"))
1001 n
= mdoc
->last
->data
.block
.body
;
1003 if (NULL
== n
->child
)
1004 if ( ! mdoc_warn(mdoc
, WARN_SYNTAX
, "macro suggests body children"))
1008 if (MDOC_Column
!= sv
)
1011 /* Make sure the number of columns is sane. */
1013 argc
= mdoc
->last
->parent
->parent
->data
.block
.argv
->sz
;
1014 n
= mdoc
->last
->data
.block
.head
->child
;
1016 for (i
= 0; n
; n
= n
->next
)
1021 return(mdoc_err(mdoc
, "expected %zu list columns, have %zu", argc
, i
));
1029 post_bl(struct mdoc
*mdoc
)
1031 struct mdoc_node
*n
;
1033 if (MDOC_BODY
!= mdoc
->last
->type
)
1035 assert(MDOC_Bl
== mdoc
->last
->tok
);
1038 for (n
= mdoc
->last
->child
; n
; n
= n
->next
) {
1039 if (MDOC_BLOCK
== n
->type
)
1040 if (MDOC_It
== n
->tok
)
1046 return(mdoc_nerr(mdoc
, n
, "invalid child of parent macro `Bl'"));
1051 ebool(struct mdoc
*mdoc
)
1053 struct mdoc_node
*n
;
1055 assert(MDOC_ELEM
== mdoc
->last
->type
);
1057 for (n
= mdoc
->last
->child
; n
; n
= n
->next
) {
1058 if (MDOC_TEXT
!= n
->type
)
1060 if (xstrcmp(n
->data
.text
.string
, "on"))
1062 if (xstrcmp(n
->data
.text
.string
, "off"))
1068 return(mdoc_nerr(mdoc
, n
, "expected boolean value"));
1073 post_root(struct mdoc
*mdoc
)
1076 if (NULL
== mdoc
->last
->child
)
1077 return(mdoc_err(mdoc
, "document has no data"));
1078 if (NULL
== mdoc
->meta
.title
)
1079 return(mdoc_err(mdoc
, "document has incomplete prologue"));
1080 if (NULL
== mdoc
->meta
.os
)
1081 return(mdoc_err(mdoc
, "document has incomplete prologue"));
1082 if (0 == mdoc
->meta
.date
)
1083 return(mdoc_err(mdoc
, "document has incomplete prologue"));
1088 /* Warn if conventional sections are out of order. */
1090 post_sh(struct mdoc
*mdoc
)
1095 if (MDOC_HEAD
!= mdoc
->last
->type
)
1097 assert(MDOC_Sh
== mdoc
->last
->tok
);
1099 if ( ! xstrlcats(buf
, mdoc
->last
->child
, 64))
1100 return(mdoc_err(mdoc
, "macro parameters too long"));
1102 if (SEC_CUSTOM
== (sec
= mdoc_atosec(buf
)))
1104 if (sec
> mdoc
->sec_lastn
)
1106 if (sec
== mdoc
->sec_lastn
)
1107 return(mdoc_warn(mdoc
, WARN_SYNTAX
, "section repeated"));
1108 return(mdoc_warn(mdoc
, WARN_SYNTAX
, "section out of conventional order"));
1113 mdoc_valid_pre(struct mdoc
*mdoc
, struct mdoc_node
*node
)
1117 if (MDOC_TEXT
== node
->type
)
1119 assert(MDOC_ROOT
!= node
->type
);
1121 if (NULL
== mdoc_valids
[node
->tok
].pre
)
1123 for (p
= mdoc_valids
[node
->tok
].pre
; *p
; p
++)
1124 if ( ! (*p
)(mdoc
, node
))
1131 mdoc_valid_post(struct mdoc
*mdoc
)
1135 if (MDOC_VALID
& mdoc
->last
->flags
)
1137 mdoc
->last
->flags
|= MDOC_VALID
;
1139 if (MDOC_TEXT
== mdoc
->last
->type
)
1141 if (MDOC_ROOT
== mdoc
->last
->type
)
1142 return(post_root(mdoc
));
1144 if (NULL
== mdoc_valids
[mdoc
->last
->tok
].post
)
1146 for (p
= mdoc_valids
[mdoc
->last
->tok
].post
; *p
; p
++)