]>
git.cameronkatri.com Git - mandoc.git/blob - mdoc_validate.c
1 /* $Id: mdoc_validate.c,v 1.125 2010/11/29 13:51:03 kristaps Exp $ */
3 * Copyright (c) 2008, 2009, 2010 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. */
36 #define PRE_ARGS struct mdoc *mdoc, struct mdoc_node *n
37 #define POST_ARGS struct mdoc *mdoc
51 typedef int (*v_pre
)(PRE_ARGS
);
52 typedef int (*v_post
)(POST_ARGS
);
59 static int check_count(struct mdoc
*, enum mdoc_type
,
60 enum check_lvl
, enum check_ineq
, int);
61 static int check_parent(PRE_ARGS
, enum mdoct
, enum mdoc_type
);
62 static int check_stdarg(PRE_ARGS
);
63 static int check_text(struct mdoc
*, int, int, char *);
64 static int check_argv(struct mdoc
*,
65 struct mdoc_node
*, struct mdoc_argv
*);
66 static int check_args(struct mdoc
*, struct mdoc_node
*);
68 static int ebool(POST_ARGS
);
69 static int berr_ge1(POST_ARGS
);
70 static int bwarn_ge1(POST_ARGS
);
71 static int eerr_eq0(POST_ARGS
);
72 static int eerr_eq1(POST_ARGS
);
73 static int eerr_ge1(POST_ARGS
);
74 static int eerr_le1(POST_ARGS
);
75 static int ewarn_eq0(POST_ARGS
);
76 static int ewarn_ge1(POST_ARGS
);
77 static int herr_eq0(POST_ARGS
);
78 static int herr_ge1(POST_ARGS
);
79 static int hwarn_eq0(POST_ARGS
);
80 static int hwarn_eq1(POST_ARGS
);
81 static int hwarn_le1(POST_ARGS
);
83 static int post_an(POST_ARGS
);
84 static int post_at(POST_ARGS
);
85 static int post_bf(POST_ARGS
);
86 static int post_bl(POST_ARGS
);
87 static int post_bl_head(POST_ARGS
);
88 static int post_defaults(POST_ARGS
);
89 static int post_eoln(POST_ARGS
);
90 static int post_dt(POST_ARGS
);
91 static int post_it(POST_ARGS
);
92 static int post_lb(POST_ARGS
);
93 static int post_nm(POST_ARGS
);
94 static int post_root(POST_ARGS
);
95 static int post_rs(POST_ARGS
);
96 static int post_sh(POST_ARGS
);
97 static int post_sh_body(POST_ARGS
);
98 static int post_sh_head(POST_ARGS
);
99 static int post_st(POST_ARGS
);
100 static int post_vt(POST_ARGS
);
101 static int pre_an(PRE_ARGS
);
102 static int pre_bd(PRE_ARGS
);
103 static int pre_bl(PRE_ARGS
);
104 static int pre_dd(PRE_ARGS
);
105 static int pre_display(PRE_ARGS
);
106 static int pre_dt(PRE_ARGS
);
107 static int pre_it(PRE_ARGS
);
108 static int pre_os(PRE_ARGS
);
109 static int pre_par(PRE_ARGS
);
110 static int pre_rv(PRE_ARGS
);
111 static int pre_sh(PRE_ARGS
);
112 static int pre_ss(PRE_ARGS
);
114 static v_post posts_an
[] = { post_an
, NULL
};
115 static v_post posts_at
[] = { post_at
, NULL
};
116 static v_post posts_bd_bk
[] = { hwarn_eq0
, bwarn_ge1
, NULL
};
117 static v_post posts_bf
[] = { hwarn_le1
, post_bf
, NULL
};
118 static v_post posts_bl
[] = { bwarn_ge1
, post_bl
, NULL
};
119 static v_post posts_bool
[] = { eerr_eq1
, ebool
, NULL
};
120 static v_post posts_eoln
[] = { post_eoln
, NULL
};
121 static v_post posts_defaults
[] = { post_defaults
, NULL
};
122 static v_post posts_dt
[] = { post_dt
, NULL
};
123 static v_post posts_fo
[] = { hwarn_eq1
, bwarn_ge1
, NULL
};
124 static v_post posts_it
[] = { post_it
, NULL
};
125 static v_post posts_lb
[] = { eerr_eq1
, post_lb
, NULL
};
126 static v_post posts_nd
[] = { berr_ge1
, NULL
};
127 static v_post posts_nm
[] = { post_nm
, NULL
};
128 static v_post posts_notext
[] = { ewarn_eq0
, NULL
};
129 static v_post posts_rs
[] = { berr_ge1
, herr_eq0
, post_rs
, NULL
};
130 static v_post posts_sh
[] = { herr_ge1
, bwarn_ge1
, post_sh
, NULL
};
131 static v_post posts_sp
[] = { eerr_le1
, NULL
};
132 static v_post posts_ss
[] = { herr_ge1
, NULL
};
133 static v_post posts_st
[] = { eerr_eq1
, post_st
, NULL
};
134 static v_post posts_text
[] = { eerr_ge1
, NULL
};
135 static v_post posts_text1
[] = { eerr_eq1
, NULL
};
136 static v_post posts_vt
[] = { post_vt
, NULL
};
137 static v_post posts_wline
[] = { bwarn_ge1
, herr_eq0
, NULL
};
138 static v_post posts_wtext
[] = { ewarn_ge1
, NULL
};
139 static v_pre pres_an
[] = { pre_an
, NULL
};
140 static v_pre pres_bd
[] = { pre_display
, pre_bd
, pre_par
, NULL
};
141 static v_pre pres_bl
[] = { pre_bl
, pre_par
, NULL
};
142 static v_pre pres_d1
[] = { pre_display
, NULL
};
143 static v_pre pres_dd
[] = { pre_dd
, NULL
};
144 static v_pre pres_dt
[] = { pre_dt
, NULL
};
145 static v_pre pres_er
[] = { NULL
, NULL
};
146 static v_pre pres_ex
[] = { NULL
, NULL
};
147 static v_pre pres_fd
[] = { NULL
, NULL
};
148 static v_pre pres_it
[] = { pre_it
, NULL
};
149 static v_pre pres_os
[] = { pre_os
, NULL
};
150 static v_pre pres_pp
[] = { pre_par
, NULL
};
151 static v_pre pres_rv
[] = { pre_rv
, NULL
};
152 static v_pre pres_sh
[] = { pre_sh
, NULL
};
153 static v_pre pres_ss
[] = { pre_ss
, NULL
};
155 const struct valids mdoc_valids
[MDOC_MAX
] = {
156 { NULL
, NULL
}, /* Ap */
157 { pres_dd
, posts_wtext
}, /* Dd */
158 { pres_dt
, posts_dt
}, /* Dt */
159 { pres_os
, NULL
}, /* Os */
160 { pres_sh
, posts_sh
}, /* Sh */
161 { pres_ss
, posts_ss
}, /* Ss */
162 { pres_pp
, posts_notext
}, /* Pp */
163 { pres_d1
, posts_wline
}, /* D1 */
164 { pres_d1
, posts_wline
}, /* Dl */
165 { pres_bd
, posts_bd_bk
}, /* Bd */
166 { NULL
, NULL
}, /* Ed */
167 { pres_bl
, posts_bl
}, /* Bl */
168 { NULL
, NULL
}, /* El */
169 { pres_it
, posts_it
}, /* It */
170 { NULL
, posts_text
}, /* Ad */
171 { pres_an
, posts_an
}, /* An */
172 { NULL
, posts_defaults
}, /* Ar */
173 { NULL
, posts_text
}, /* Cd */
174 { NULL
, NULL
}, /* Cm */
175 { NULL
, NULL
}, /* Dv */
176 { pres_er
, posts_text
}, /* Er */
177 { NULL
, NULL
}, /* Ev */
178 { pres_ex
, NULL
}, /* Ex */
179 { NULL
, NULL
}, /* Fa */
180 { pres_fd
, posts_wtext
}, /* Fd */
181 { NULL
, NULL
}, /* Fl */
182 { NULL
, posts_text
}, /* Fn */
183 { NULL
, posts_wtext
}, /* Ft */
184 { NULL
, posts_text
}, /* Ic */
185 { NULL
, posts_text1
}, /* In */
186 { NULL
, posts_defaults
}, /* Li */
187 { NULL
, posts_nd
}, /* Nd */
188 { NULL
, posts_nm
}, /* Nm */
189 { NULL
, posts_wline
}, /* Op */
190 { NULL
, NULL
}, /* Ot */
191 { NULL
, NULL
}, /* Pa */
192 { pres_rv
, NULL
}, /* Rv */
193 { NULL
, posts_st
}, /* St */
194 { NULL
, NULL
}, /* Va */
195 { NULL
, posts_vt
}, /* Vt */
196 { NULL
, posts_wtext
}, /* Xr */
197 { NULL
, posts_text
}, /* %A */
198 { NULL
, posts_text
}, /* %B */ /* FIXME: can be used outside Rs/Re. */
199 { NULL
, posts_text
}, /* %D */ /* FIXME: check date with mandoc_a2time(). */
200 { NULL
, posts_text
}, /* %I */
201 { NULL
, posts_text
}, /* %J */
202 { NULL
, posts_text
}, /* %N */
203 { NULL
, posts_text
}, /* %O */
204 { NULL
, posts_text
}, /* %P */
205 { NULL
, posts_text
}, /* %R */
206 { NULL
, posts_text
}, /* %T */ /* FIXME: can be used outside Rs/Re. */
207 { NULL
, posts_text
}, /* %V */
208 { NULL
, NULL
}, /* Ac */
209 { NULL
, NULL
}, /* Ao */
210 { NULL
, posts_wline
}, /* Aq */
211 { NULL
, posts_at
}, /* At */
212 { NULL
, NULL
}, /* Bc */
213 { NULL
, posts_bf
}, /* Bf */
214 { NULL
, NULL
}, /* Bo */
215 { NULL
, posts_wline
}, /* Bq */
216 { NULL
, NULL
}, /* Bsx */
217 { NULL
, NULL
}, /* Bx */
218 { NULL
, posts_bool
}, /* Db */
219 { NULL
, NULL
}, /* Dc */
220 { NULL
, NULL
}, /* Do */
221 { NULL
, posts_wline
}, /* Dq */
222 { NULL
, NULL
}, /* Ec */
223 { NULL
, NULL
}, /* Ef */
224 { NULL
, NULL
}, /* Em */
225 { NULL
, NULL
}, /* Eo */
226 { NULL
, NULL
}, /* Fx */
227 { NULL
, posts_text
}, /* Ms */
228 { NULL
, posts_notext
}, /* No */
229 { NULL
, posts_notext
}, /* Ns */
230 { NULL
, NULL
}, /* Nx */
231 { NULL
, NULL
}, /* Ox */
232 { NULL
, NULL
}, /* Pc */
233 { NULL
, posts_text1
}, /* Pf */
234 { NULL
, NULL
}, /* Po */
235 { NULL
, posts_wline
}, /* Pq */
236 { NULL
, NULL
}, /* Qc */
237 { NULL
, posts_wline
}, /* Ql */
238 { NULL
, NULL
}, /* Qo */
239 { NULL
, posts_wline
}, /* Qq */
240 { NULL
, NULL
}, /* Re */
241 { NULL
, posts_rs
}, /* Rs */
242 { NULL
, NULL
}, /* Sc */
243 { NULL
, NULL
}, /* So */
244 { NULL
, posts_wline
}, /* Sq */
245 { NULL
, posts_bool
}, /* Sm */
246 { NULL
, posts_text
}, /* Sx */
247 { NULL
, posts_text
}, /* Sy */
248 { NULL
, posts_text
}, /* Tn */
249 { NULL
, NULL
}, /* Ux */
250 { NULL
, NULL
}, /* Xc */
251 { NULL
, NULL
}, /* Xo */
252 { NULL
, posts_fo
}, /* Fo */
253 { NULL
, NULL
}, /* Fc */
254 { NULL
, NULL
}, /* Oo */
255 { NULL
, NULL
}, /* Oc */
256 { NULL
, posts_bd_bk
}, /* Bk */
257 { NULL
, NULL
}, /* Ek */
258 { NULL
, posts_eoln
}, /* Bt */
259 { NULL
, NULL
}, /* Hf */
260 { NULL
, NULL
}, /* Fr */
261 { NULL
, posts_eoln
}, /* Ud */
262 { NULL
, posts_lb
}, /* Lb */
263 { NULL
, posts_notext
}, /* Lp */
264 { NULL
, posts_text
}, /* Lk */
265 { NULL
, posts_defaults
}, /* Mt */
266 { NULL
, posts_wline
}, /* Brq */
267 { NULL
, NULL
}, /* Bro */
268 { NULL
, NULL
}, /* Brc */
269 { NULL
, posts_text
}, /* %C */
270 { NULL
, NULL
}, /* Es */
271 { NULL
, NULL
}, /* En */
272 { NULL
, NULL
}, /* Dx */
273 { NULL
, posts_text
}, /* %Q */
274 { NULL
, posts_notext
}, /* br */
275 { pres_pp
, posts_sp
}, /* sp */
276 { NULL
, posts_text1
}, /* %U */
277 { NULL
, NULL
}, /* Ta */
280 #define RSORD_MAX 14 /* Number of `Rs' blocks. */
282 static const enum mdoct rsord
[RSORD_MAX
] = {
301 mdoc_valid_pre(struct mdoc
*mdoc
, struct mdoc_node
*n
)
307 if (MDOC_TEXT
== n
->type
) {
311 return(check_text(mdoc
, line
, pos
, tp
));
314 if ( ! check_args(mdoc
, n
))
316 if (NULL
== mdoc_valids
[n
->tok
].pre
)
318 for (p
= mdoc_valids
[n
->tok
].pre
; *p
; p
++)
319 if ( ! (*p
)(mdoc
, n
))
326 mdoc_valid_post(struct mdoc
*mdoc
)
330 if (MDOC_VALID
& mdoc
->last
->flags
)
332 mdoc
->last
->flags
|= MDOC_VALID
;
334 if (MDOC_TEXT
== mdoc
->last
->type
)
336 if (MDOC_ROOT
== mdoc
->last
->type
)
337 return(post_root(mdoc
));
339 if (NULL
== mdoc_valids
[mdoc
->last
->tok
].post
)
341 for (p
= mdoc_valids
[mdoc
->last
->tok
].post
; *p
; p
++)
349 check_count(struct mdoc
*m
, enum mdoc_type type
,
350 enum check_lvl lvl
, enum check_ineq ineq
, int val
)
354 if (m
->last
->type
!= type
)
360 if (m
->last
->nchild
< val
)
365 if (m
->last
->nchild
> val
)
370 if (val
== m
->last
->nchild
)
375 if (CHECK_WARN
== lvl
) {
376 return(mdoc_vmsg(m
, MANDOCERR_ARGCOUNT
,
377 m
->last
->line
, m
->last
->pos
,
378 "want %s%d children (have %d)",
379 p
, val
, m
->last
->nchild
));
382 return(mdoc_vmsg(m
, MANDOCERR_ARGCOUNT
,
383 m
->last
->line
, m
->last
->pos
,
384 "require %s%d children (have %d)",
385 p
, val
, m
->last
->nchild
));
392 return(check_count(mdoc
, MDOC_BODY
, CHECK_FATAL
, CHECK_GT
, 0));
398 return(check_count(mdoc
, MDOC_BODY
, CHECK_WARN
, CHECK_GT
, 0));
404 return(check_count(mdoc
, MDOC_ELEM
, CHECK_FATAL
, CHECK_EQ
, 0));
410 return(check_count(mdoc
, MDOC_ELEM
, CHECK_FATAL
, CHECK_EQ
, 1));
416 return(check_count(mdoc
, MDOC_ELEM
, CHECK_FATAL
, CHECK_GT
, 0));
422 return(check_count(mdoc
, MDOC_ELEM
, CHECK_FATAL
, CHECK_LT
, 2));
428 return(check_count(mdoc
, MDOC_ELEM
, CHECK_WARN
, CHECK_EQ
, 0));
434 return(check_count(mdoc
, MDOC_ELEM
, CHECK_WARN
, CHECK_GT
, 0));
440 return(check_count(mdoc
, MDOC_HEAD
, CHECK_FATAL
, CHECK_EQ
, 0));
446 return(check_count(mdoc
, MDOC_HEAD
, CHECK_FATAL
, CHECK_GT
, 0));
452 return(check_count(mdoc
, MDOC_HEAD
, CHECK_WARN
, CHECK_EQ
, 0));
458 return(check_count(mdoc
, MDOC_HEAD
, CHECK_WARN
, CHECK_EQ
, 1));
464 return(check_count(mdoc
, MDOC_HEAD
, CHECK_WARN
, CHECK_LT
, 2));
469 check_stdarg(PRE_ARGS
)
472 if (n
->args
&& 1 == n
->args
->argc
)
473 if (MDOC_Std
== n
->args
->argv
[0].arg
)
475 return(mdoc_nmsg(mdoc
, n
, MANDOCERR_NOARGV
));
480 check_args(struct mdoc
*m
, struct mdoc_node
*n
)
487 assert(n
->args
->argc
);
488 for (i
= 0; i
< (int)n
->args
->argc
; i
++)
489 if ( ! check_argv(m
, n
, &n
->args
->argv
[i
]))
497 check_argv(struct mdoc
*m
, struct mdoc_node
*n
, struct mdoc_argv
*v
)
501 for (i
= 0; i
< (int)v
->sz
; i
++)
502 if ( ! check_text(m
, v
->line
, v
->pos
, v
->value
[i
]))
505 if (MDOC_Std
== v
->arg
) {
506 if (v
->sz
|| m
->meta
.name
)
508 if ( ! mdoc_nmsg(m
, n
, MANDOCERR_NONAME
))
517 check_text(struct mdoc
*m
, int ln
, int pos
, char *p
)
522 for ( ; *p
; p
++, pos
++) {
523 sz
= strcspn(p
, "\t\\");
532 if (MDOC_LITERAL
& m
->flags
)
534 if (mdoc_pmsg(m
, ln
, pos
, MANDOCERR_BADTAB
))
539 /* Check the special character. */
541 c
= mandoc_special(p
);
546 mdoc_pmsg(m
, ln
, pos
, MANDOCERR_BADESCAPE
);
554 check_parent(PRE_ARGS
, enum mdoct tok
, enum mdoc_type t
)
558 if ((MDOC_ROOT
== t
|| tok
== n
->parent
->tok
) &&
559 (t
== n
->parent
->type
))
562 mdoc_vmsg(mdoc
, MANDOCERR_SYNTCHILD
,
563 n
->line
, n
->pos
, "want parent %s",
564 MDOC_ROOT
== t
? "<root>" :
565 mdoc_macronames
[tok
]);
571 pre_display(PRE_ARGS
)
573 struct mdoc_node
*node
;
575 /* Display elements (`Bd', `D1'...) cannot be nested. */
577 if (MDOC_BLOCK
!= n
->type
)
581 for (node
= mdoc
->last
->parent
; node
; node
= node
->parent
)
582 if (MDOC_BLOCK
== node
->type
)
583 if (MDOC_Bd
== node
->tok
)
588 mdoc_nmsg(mdoc
, n
, MANDOCERR_NESTEDDISP
);
597 const char *offs
, *width
;
599 struct mdoc_node
*np
;
601 if (MDOC_BLOCK
!= n
->type
) {
602 if (ENDBODY_NOT
!= n
->end
) {
604 np
= n
->pending
->parent
;
609 assert(MDOC_BLOCK
== np
->type
);
610 assert(MDOC_Bl
== np
->tok
);
612 n
->data
.Bl
= np
->data
.Bl
;
617 * First figure out which kind of list to use: bind ourselves to
618 * the first mentioned list type and warn about any remaining
619 * ones. If we find no list type, we default to LIST_item.
622 assert(NULL
== n
->data
.Bl
);
623 n
->data
.Bl
= mandoc_calloc(1, sizeof(struct mdoc_bl
));
626 for (i
= 0; n
->args
&& i
< (int)n
->args
->argc
; i
++) {
630 switch (n
->args
->argv
[i
].arg
) {
631 /* Set list types. */
665 /* Set list arguments. */
667 dup
= n
->data
.Bl
->comp
;
671 dup
= (NULL
!= n
->data
.Bl
->width
);
672 width
= n
->args
->argv
[i
].value
[0];
675 /* NB: this can be empty! */
676 if (n
->args
->argv
[i
].sz
) {
677 offs
= n
->args
->argv
[i
].value
[0];
678 dup
= (NULL
!= n
->data
.Bl
->offs
);
681 if ( ! mdoc_nmsg(mdoc
, n
, MANDOCERR_IGNARGV
))
688 /* Check: duplicate auxiliary arguments. */
690 if (dup
&& ! mdoc_nmsg(mdoc
, n
, MANDOCERR_ARGVREP
))
694 n
->data
.Bl
->comp
= comp
;
696 n
->data
.Bl
->offs
= offs
;
698 n
->data
.Bl
->width
= width
;
700 /* Check: multiple list types. */
702 if (LIST__NONE
!= lt
&& n
->data
.Bl
->type
!= LIST__NONE
)
703 if ( ! mdoc_nmsg(mdoc
, n
, MANDOCERR_LISTREP
))
706 /* Assign list type. */
708 if (LIST__NONE
!= lt
&& n
->data
.Bl
->type
== LIST__NONE
) {
709 n
->data
.Bl
->type
= lt
;
710 /* Set column information, too. */
711 if (LIST_column
== lt
) {
714 n
->data
.Bl
->cols
= (const char **)
715 n
->args
->argv
[i
].value
;
719 /* The list type should come first. */
721 if (n
->data
.Bl
->type
== LIST__NONE
)
722 if (n
->data
.Bl
->width
||
725 if ( ! mdoc_nmsg(mdoc
, n
, MANDOCERR_LISTFIRST
))
731 /* Allow lists to default to LIST_item. */
733 if (LIST__NONE
== n
->data
.Bl
->type
) {
734 if ( ! mdoc_nmsg(mdoc
, n
, MANDOCERR_LISTTYPE
))
736 n
->data
.Bl
->type
= LIST_item
;
740 * Validate the width field. Some list types don't need width
741 * types and should be warned about them. Others should have it
742 * and must also be warned.
745 switch (n
->data
.Bl
->type
) {
747 if (n
->data
.Bl
->width
)
749 if (mdoc_nmsg(mdoc
, n
, MANDOCERR_NOWIDTHARG
))
761 if (NULL
== n
->data
.Bl
->width
)
763 if (mdoc_nmsg(mdoc
, n
, MANDOCERR_WIDTHARG
))
780 struct mdoc_node
*np
;
782 if (MDOC_BLOCK
!= n
->type
) {
783 if (ENDBODY_NOT
!= n
->end
) {
785 np
= n
->pending
->parent
;
790 assert(MDOC_BLOCK
== np
->type
);
791 assert(MDOC_Bd
== np
->tok
);
793 n
->data
.Bd
= np
->data
.Bd
;
797 assert(NULL
== n
->data
.Bd
);
798 n
->data
.Bd
= mandoc_calloc(1, sizeof(struct mdoc_bd
));
801 for (i
= 0; n
->args
&& i
< (int)n
->args
->argc
; i
++) {
806 switch (n
->args
->argv
[i
].arg
) {
813 case (MDOC_Unfilled
):
823 mdoc_nmsg(mdoc
, n
, MANDOCERR_BADDISP
);
826 /* NB: this can be empty! */
827 if (n
->args
->argv
[i
].sz
) {
828 offs
= n
->args
->argv
[i
].value
[0];
829 dup
= (NULL
!= n
->data
.Bd
->offs
);
832 if ( ! mdoc_nmsg(mdoc
, n
, MANDOCERR_IGNARGV
))
837 dup
= n
->data
.Bd
->comp
;
844 /* Check whether we have duplicates. */
846 if (dup
&& ! mdoc_nmsg(mdoc
, n
, MANDOCERR_ARGVREP
))
849 /* Make our auxiliary assignments. */
852 n
->data
.Bd
->offs
= offs
;
854 n
->data
.Bd
->comp
= comp
;
856 /* Check whether a type has already been assigned. */
858 if (DISP__NONE
!= dt
&& n
->data
.Bd
->type
!= DISP__NONE
)
859 if ( ! mdoc_nmsg(mdoc
, n
, MANDOCERR_DISPREP
))
862 /* Make our type assignment. */
864 if (DISP__NONE
!= dt
&& n
->data
.Bd
->type
== DISP__NONE
)
865 n
->data
.Bd
->type
= dt
;
868 if (DISP__NONE
== n
->data
.Bd
->type
) {
869 if ( ! mdoc_nmsg(mdoc
, n
, MANDOCERR_DISPTYPE
))
871 n
->data
.Bd
->type
= DISP_ragged
;
882 if (MDOC_BLOCK
!= n
->type
)
884 return(check_parent(mdoc
, n
, MDOC_Sh
, MDOC_BODY
));
892 if (MDOC_BLOCK
!= n
->type
)
895 mdoc
->regs
->regs
[(int)REG_nS
].set
= 0;
896 return(check_parent(mdoc
, n
, MDOC_MAX
, MDOC_ROOT
));
904 if (MDOC_BLOCK
!= n
->type
)
907 * FIXME: this can probably be lifted if we make the It into
908 * something else on-the-fly?
910 return(check_parent(mdoc
, n
, MDOC_Bl
, MDOC_BODY
));
922 for (i
= 1; i
< (int)n
->args
->argc
; i
++)
923 if ( ! mdoc_pmsg(mdoc
, n
->args
->argv
[i
].line
,
924 n
->args
->argv
[i
].pos
, MANDOCERR_IGNARGV
))
927 if (MDOC_Split
== n
->args
->argv
[0].arg
)
928 n
->data
.An
.auth
= AUTH_split
;
929 else if (MDOC_Nosplit
== n
->args
->argv
[0].arg
)
930 n
->data
.An
.auth
= AUTH_nosplit
;
942 return(check_stdarg(mdoc
, n
));
949 const struct mdoc_node
*nn
;
952 if (NULL
!= (nn
= mdoc
->last
->child
))
953 for (p
= nn
->string
; *p
; p
++) {
954 if (toupper((u_char
)*p
) == *p
)
956 if ( ! mdoc_nmsg(mdoc
, nn
, MANDOCERR_UPPERCASE
))
969 if (0 == mdoc
->meta
.date
|| mdoc
->meta
.os
)
970 if ( ! mdoc_nmsg(mdoc
, n
, MANDOCERR_PROLOGOOO
))
972 if (mdoc
->meta
.title
)
973 if ( ! mdoc_nmsg(mdoc
, n
, MANDOCERR_PROLOGREP
))
983 if (NULL
== mdoc
->meta
.title
|| 0 == mdoc
->meta
.date
)
984 if ( ! mdoc_nmsg(mdoc
, n
, MANDOCERR_PROLOGOOO
))
987 if ( ! mdoc_nmsg(mdoc
, n
, MANDOCERR_PROLOGREP
))
997 if (mdoc
->meta
.title
|| mdoc
->meta
.os
)
998 if ( ! mdoc_nmsg(mdoc
, n
, MANDOCERR_PROLOGOOO
))
1000 if (mdoc
->meta
.date
)
1001 if ( ! mdoc_nmsg(mdoc
, n
, MANDOCERR_PROLOGREP
))
1010 struct mdoc_node
*np
;
1014 * Unlike other data pointers, these are "housed" by the HEAD
1015 * element, which contains the goods.
1018 if (MDOC_HEAD
!= mdoc
->last
->type
) {
1019 if (ENDBODY_NOT
!= mdoc
->last
->end
) {
1020 assert(mdoc
->last
->pending
);
1021 np
= mdoc
->last
->pending
->parent
->head
;
1022 } else if (MDOC_BLOCK
!= mdoc
->last
->type
) {
1023 np
= mdoc
->last
->parent
->head
;
1025 np
= mdoc
->last
->head
;
1028 assert(MDOC_HEAD
== np
->type
);
1029 assert(MDOC_Bf
== np
->tok
);
1030 assert(np
->data
.Bf
);
1031 mdoc
->last
->data
.Bf
= np
->data
.Bf
;
1036 assert(MDOC_BLOCK
== np
->parent
->type
);
1037 assert(MDOC_Bf
== np
->parent
->tok
);
1038 np
->data
.Bf
= mandoc_calloc(1, sizeof(struct mdoc_bf
));
1041 * Cannot have both argument and parameter.
1042 * If neither is specified, let it through with a warning.
1045 if (np
->parent
->args
&& np
->child
) {
1046 mdoc_nmsg(mdoc
, np
, MANDOCERR_SYNTARGVCOUNT
);
1048 } else if (NULL
== np
->parent
->args
&& NULL
== np
->child
)
1049 return(mdoc_nmsg(mdoc
, np
, MANDOCERR_FONTTYPE
));
1051 /* Extract argument into data. */
1053 if (np
->parent
->args
) {
1054 arg
= np
->parent
->args
->argv
[0].arg
;
1055 if (MDOC_Emphasis
== arg
)
1056 np
->data
.Bf
->font
= FONT_Em
;
1057 else if (MDOC_Literal
== arg
)
1058 np
->data
.Bf
->font
= FONT_Li
;
1059 else if (MDOC_Symbolic
== arg
)
1060 np
->data
.Bf
->font
= FONT_Sy
;
1066 /* Extract parameter into data. */
1068 if (0 == strcmp(np
->child
->string
, "Em"))
1069 np
->data
.Bf
->font
= FONT_Em
;
1070 else if (0 == strcmp(np
->child
->string
, "Li"))
1071 np
->data
.Bf
->font
= FONT_Li
;
1072 else if (0 == strcmp(np
->child
->string
, "Sy"))
1073 np
->data
.Bf
->font
= FONT_Sy
;
1074 else if ( ! mdoc_nmsg(mdoc
, np
, MANDOCERR_FONTTYPE
))
1085 if (mdoc_a2lib(mdoc
->last
->child
->string
))
1087 return(mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_BADLIB
));
1092 post_eoln(POST_ARGS
)
1095 if (NULL
== mdoc
->last
->child
)
1097 return(mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_ARGSLOST
));
1104 const struct mdoc_node
*n
;
1107 * The Vt macro comes in both ELEM and BLOCK form, both of which
1108 * have different syntaxes (yet more context-sensitive
1109 * behaviour). ELEM types must have a child; BLOCK types,
1110 * specifically the BODY, should only have TEXT children.
1113 if (MDOC_ELEM
== mdoc
->last
->type
)
1114 return(eerr_ge1(mdoc
));
1115 if (MDOC_BODY
!= mdoc
->last
->type
)
1118 for (n
= mdoc
->last
->child
; n
; n
= n
->next
)
1119 if (MDOC_TEXT
!= n
->type
)
1120 if ( ! mdoc_nmsg(mdoc
, n
, MANDOCERR_CHILD
))
1131 if (mdoc
->last
->child
)
1133 if (mdoc
->meta
.name
)
1135 return(mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_NONAME
));
1139 post_defaults(POST_ARGS
)
1141 struct mdoc_node
*nn
;
1144 * The `Ar' defaults to "file ..." if no value is provided as an
1145 * argument; the `Mt' macro uses "~"; the `Li' just gets an
1149 if (mdoc
->last
->child
)
1153 mdoc
->next
= MDOC_NEXT_CHILD
;
1157 if ( ! mdoc_word_alloc(mdoc
, nn
->line
, nn
->pos
, "file"))
1159 if ( ! mdoc_word_alloc(mdoc
, nn
->line
, nn
->pos
, "..."))
1163 if ( ! mdoc_word_alloc(mdoc
, nn
->line
, nn
->pos
, ""))
1167 if ( ! mdoc_word_alloc(mdoc
, nn
->line
, nn
->pos
, "~"))
1183 if (NULL
== mdoc
->last
->child
)
1185 assert(MDOC_TEXT
== mdoc
->last
->child
->type
);
1186 if (mdoc_a2att(mdoc
->last
->child
->string
))
1188 return(mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_BADATT
));
1195 struct mdoc_node
*np
;
1198 if (AUTH__NONE
!= np
->data
.An
.auth
&& np
->child
)
1199 return(eerr_eq0(mdoc
));
1201 * FIXME: make this ewarn and make sure that the front-ends
1202 * don't print the arguments.
1204 if (AUTH__NONE
!= np
->data
.An
.auth
|| np
->child
)
1206 return(mdoc_nmsg(mdoc
, np
, MANDOCERR_NOARGS
));
1215 struct mdoc_node
*n
, *c
;
1218 if (MDOC_BLOCK
!= mdoc
->last
->type
)
1221 n
= mdoc
->last
->parent
->parent
;
1223 lt
= n
->data
.Bl
->type
;
1225 if (LIST__NONE
== lt
) {
1226 mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_LISTTYPE
);
1232 if (mdoc
->last
->head
->child
)
1234 /* FIXME: give this a dummy value. */
1235 if ( ! mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_NOARGS
))
1245 if (NULL
== mdoc
->last
->head
->child
)
1246 if ( ! mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_NOARGS
))
1256 if (NULL
== mdoc
->last
->body
->child
)
1257 if ( ! mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_NOBODY
))
1261 if (mdoc
->last
->head
->child
)
1262 if ( ! mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_ARGSLOST
))
1266 cols
= (int)n
->data
.Bl
->ncols
;
1268 assert(NULL
== mdoc
->last
->head
->child
);
1270 if (NULL
== mdoc
->last
->body
->child
)
1271 if ( ! mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_NOBODY
))
1274 for (i
= 0, c
= mdoc
->last
->child
; c
; c
= c
->next
)
1275 if (MDOC_BODY
== c
->type
)
1279 er
= MANDOCERR_ARGCOUNT
;
1280 else if (i
== cols
|| i
== cols
+ 1)
1283 er
= MANDOCERR_SYNTARGCOUNT
;
1285 rc
= mdoc_vmsg(mdoc
, er
,
1286 mdoc
->last
->line
, mdoc
->last
->pos
,
1287 "columns == %d (have %d)", cols
, i
);
1298 post_bl_head(POST_ARGS
)
1300 struct mdoc_node
*n
;
1302 assert(mdoc
->last
->parent
);
1303 n
= mdoc
->last
->parent
;
1305 if (LIST_column
== n
->data
.Bl
->type
) {
1306 if (n
->data
.Bl
->ncols
&& mdoc
->last
->nchild
) {
1307 mdoc_nmsg(mdoc
, n
, MANDOCERR_COLUMNS
);
1313 /* FIXME: should be ERROR class. */
1314 return(hwarn_eq0(mdoc
));
1321 struct mdoc_node
*n
;
1323 if (MDOC_HEAD
== mdoc
->last
->type
)
1324 return(post_bl_head(mdoc
));
1325 if (MDOC_BODY
!= mdoc
->last
->type
)
1327 if (NULL
== mdoc
->last
->child
)
1331 * We only allow certain children of `Bl'. This is usually on
1332 * `It', but apparently `Sm' occurs here and there, so we let
1333 * that one through, too.
1337 for (n
= mdoc
->last
->child
; n
; n
= n
->next
) {
1338 if (MDOC_BLOCK
== n
->type
&& MDOC_It
== n
->tok
)
1340 if (MDOC_Sm
== n
->tok
)
1342 mdoc_nmsg(mdoc
, n
, MANDOCERR_SYNTCHILD
);
1351 ebool(struct mdoc
*mdoc
)
1353 struct mdoc_node
*n
;
1356 for (n
= mdoc
->last
->child
; n
; n
= n
->next
) {
1357 if (MDOC_TEXT
!= n
->type
)
1359 if (0 == strcmp(n
->string
, "on"))
1361 if (0 == strcmp(n
->string
, "off"))
1368 return(mdoc_nmsg(mdoc
, n
, MANDOCERR_BADBOOL
));
1373 post_root(POST_ARGS
)
1376 if (NULL
== mdoc
->first
->child
)
1377 mdoc_nmsg(mdoc
, mdoc
->first
, MANDOCERR_NODOCBODY
);
1378 else if ( ! (MDOC_PBODY
& mdoc
->flags
))
1379 mdoc_nmsg(mdoc
, mdoc
->first
, MANDOCERR_NODOCPROLOG
);
1380 else if (MDOC_BLOCK
!= mdoc
->first
->child
->type
)
1381 mdoc_nmsg(mdoc
, mdoc
->first
, MANDOCERR_NODOCBODY
);
1382 else if (MDOC_Sh
!= mdoc
->first
->child
->tok
)
1383 mdoc_nmsg(mdoc
, mdoc
->first
, MANDOCERR_NODOCBODY
);
1395 if (mdoc_a2st(mdoc
->last
->child
->string
))
1397 return(mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_BADSTANDARD
));
1404 struct mdoc_node
*nn
, *next
, *prev
;
1407 if (MDOC_BODY
!= mdoc
->last
->type
)
1411 * Make sure only certain types of nodes are allowed within the
1412 * the `Rs' body. Delete offending nodes and raise a warning.
1413 * Do this before re-ordering for the sake of clarity.
1417 for (nn
= mdoc
->last
->child
; nn
; nn
= next
) {
1418 for (i
= 0; i
< RSORD_MAX
; i
++)
1419 if (nn
->tok
== rsord
[i
])
1422 if (i
< RSORD_MAX
) {
1428 mdoc_nmsg(mdoc
, nn
, MANDOCERR_CHILD
);
1429 mdoc_node_delete(mdoc
, nn
);
1433 * The full `Rs' block needs special handling to order the
1434 * sub-elements according to `rsord'. Pick through each element
1435 * and correctly order it. This is a insertion sort.
1439 for (nn
= mdoc
->last
->child
->next
; nn
; nn
= next
) {
1440 /* Determine order of `nn'. */
1441 for (i
= 0; i
< RSORD_MAX
; i
++)
1442 if (rsord
[i
] == nn
->tok
)
1446 * Remove `nn' from the chain. This somewhat
1447 * repeats mdoc_node_unlink(), but since we're
1448 * just re-ordering, there's no need for the
1449 * full unlink process.
1452 if (NULL
!= (next
= nn
->next
))
1453 next
->prev
= nn
->prev
;
1455 if (NULL
!= (prev
= nn
->prev
))
1456 prev
->next
= nn
->next
;
1458 nn
->prev
= nn
->next
= NULL
;
1461 * Scan back until we reach a node that's
1462 * ordered before `nn'.
1465 for ( ; prev
; prev
= prev
->prev
) {
1466 /* Determine order of `prev'. */
1467 for (j
= 0; j
< RSORD_MAX
; j
++)
1468 if (rsord
[j
] == prev
->tok
)
1476 * Set `nn' back into its correct place in front
1477 * of the `prev' node.
1484 prev
->next
->prev
= nn
;
1485 nn
->next
= prev
->next
;
1488 mdoc
->last
->child
->prev
= nn
;
1489 nn
->next
= mdoc
->last
->child
;
1490 mdoc
->last
->child
= nn
;
1502 if (MDOC_HEAD
== mdoc
->last
->type
)
1503 return(post_sh_head(mdoc
));
1504 if (MDOC_BODY
== mdoc
->last
->type
)
1505 return(post_sh_body(mdoc
));
1512 post_sh_body(POST_ARGS
)
1514 struct mdoc_node
*n
;
1516 if (SEC_NAME
!= mdoc
->lastsec
)
1520 * Warn if the NAME section doesn't contain the `Nm' and `Nd'
1521 * macros (can have multiple `Nm' and one `Nd'). Note that the
1522 * children of the BODY declaration can also be "text".
1525 if (NULL
== (n
= mdoc
->last
->child
))
1526 return(mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_BADNAMESEC
));
1528 for ( ; n
&& n
->next
; n
= n
->next
) {
1529 if (MDOC_ELEM
== n
->type
&& MDOC_Nm
== n
->tok
)
1531 if (MDOC_TEXT
== n
->type
)
1533 if ( ! mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_BADNAMESEC
))
1538 if (MDOC_BLOCK
== n
->type
&& MDOC_Nd
== n
->tok
)
1540 return(mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_BADNAMESEC
));
1545 post_sh_head(POST_ARGS
)
1549 struct mdoc_node
*n
;
1552 * Process a new section. Sections are either "named" or
1553 * "custom". Custom sections are user-defined, while named ones
1554 * follow a conventional order and may only appear in certain
1560 /* FIXME: use dynamic buffer... */
1562 for (n
= mdoc
->last
->child
; n
; n
= n
->next
) {
1563 /* XXX - copied from concat(). */
1564 assert(MDOC_TEXT
== n
->type
);
1566 if (strlcat(buf
, n
->string
, BUFSIZ
) >= BUFSIZ
) {
1567 mdoc_nmsg(mdoc
, n
, MANDOCERR_MEM
);
1571 if (NULL
== n
->next
)
1574 if (strlcat(buf
, " ", BUFSIZ
) >= BUFSIZ
) {
1575 mdoc_nmsg(mdoc
, n
, MANDOCERR_MEM
);
1580 sec
= mdoc_str2sec(buf
);
1582 /* The NAME should be first. */
1584 if (SEC_NAME
!= sec
&& SEC_NONE
== mdoc
->lastnamed
)
1585 mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_NAMESECFIRST
);
1587 /* The SYNOPSIS gets special attention in other areas. */
1589 if (SEC_SYNOPSIS
== sec
)
1590 mdoc
->flags
|= MDOC_SYNOPSIS
;
1592 mdoc
->flags
&= ~MDOC_SYNOPSIS
;
1594 /* Mark our last section. */
1596 mdoc
->lastsec
= sec
;
1598 /* We don't care about custom sections after this. */
1600 if (SEC_CUSTOM
== sec
)
1604 * Check whether our non-custom section is being repeated or is
1608 if (sec
== mdoc
->lastnamed
)
1609 mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_SECREP
);
1611 if (sec
< mdoc
->lastnamed
)
1612 mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_SECOOO
);
1614 /* Mark the last named section. */
1616 mdoc
->lastnamed
= sec
;
1618 /* Check particular section/manual conventions. */
1620 assert(mdoc
->meta
.msec
);
1623 case (SEC_RETURN_VALUES
):
1628 if (*mdoc
->meta
.msec
== '2')
1630 if (*mdoc
->meta
.msec
== '3')
1632 if (*mdoc
->meta
.msec
== '9')
1634 mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_SECMSEC
);
1647 if (NULL
== mdoc
->last
)
1651 * Don't allow prior `Lp' or `Pp' prior to a paragraph-type
1652 * block: `Lp', `Pp', or non-compact `Bd' or `Bl'.
1655 if (MDOC_Pp
!= mdoc
->last
->tok
&& MDOC_Lp
!= mdoc
->last
->tok
)
1658 if (MDOC_Bl
== n
->tok
&& n
->data
.Bl
->comp
)
1660 if (MDOC_Bd
== n
->tok
&& n
->data
.Bd
->comp
)
1663 mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_IGNPAR
);
1664 mdoc_node_delete(mdoc
, mdoc
->last
);