]>
git.cameronkatri.com Git - mandoc.git/blob - mdoc_validate.c
1 /* $Id: mdoc_validate.c,v 1.101 2010/06/27 17:53:27 schwarze Exp $ */
3 * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@bsd.lv>
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 #include <sys/types.h>
32 #include "libmandoc.h"
34 /* FIXME: .Bl -diag can't have non-text children in HEAD. */
35 /* TODO: ignoring Pp (it's superfluous in some invocations). */
37 #define PRE_ARGS struct mdoc *mdoc, struct mdoc_node *n
38 #define POST_ARGS struct mdoc *mdoc
40 typedef int (*v_pre
)(PRE_ARGS
);
41 typedef int (*v_post
)(POST_ARGS
);
48 static int check_parent(PRE_ARGS
, enum mdoct
, enum mdoc_type
);
49 static int check_stdarg(PRE_ARGS
);
50 static int check_text(struct mdoc
*, int, int, char *);
51 static int check_argv(struct mdoc
*,
52 struct mdoc_node
*, struct mdoc_argv
*);
53 static int check_args(struct mdoc
*, struct mdoc_node
*);
54 static int err_child_lt(struct mdoc
*, const char *, int);
55 static int warn_child_lt(struct mdoc
*, const char *, int);
56 static int err_child_gt(struct mdoc
*, const char *, int);
57 static int warn_child_gt(struct mdoc
*, const char *, int);
58 static int err_child_eq(struct mdoc
*, const char *, int);
59 static int warn_child_eq(struct mdoc
*, const char *, int);
60 static int warn_count(struct mdoc
*, const char *,
61 int, const char *, int);
62 static int err_count(struct mdoc
*, const char *,
63 int, const char *, int);
65 static int berr_ge1(POST_ARGS
);
66 static int bwarn_ge1(POST_ARGS
);
67 static int ebool(POST_ARGS
);
68 static int eerr_eq0(POST_ARGS
);
69 static int eerr_eq1(POST_ARGS
);
70 static int eerr_ge1(POST_ARGS
);
71 static int eerr_le1(POST_ARGS
);
72 static int ewarn_ge1(POST_ARGS
);
73 static int herr_eq0(POST_ARGS
);
74 static int herr_ge1(POST_ARGS
);
75 static int hwarn_eq1(POST_ARGS
);
76 static int hwarn_eq0(POST_ARGS
);
77 static int hwarn_le1(POST_ARGS
);
79 static int post_an(POST_ARGS
);
80 static int post_at(POST_ARGS
);
81 static int post_bf(POST_ARGS
);
82 static int post_bl(POST_ARGS
);
83 static int post_bl_head(POST_ARGS
);
84 static int post_dt(POST_ARGS
);
85 static int post_it(POST_ARGS
);
86 static int post_lb(POST_ARGS
);
87 static int post_nm(POST_ARGS
);
88 static int post_root(POST_ARGS
);
89 static int post_rs(POST_ARGS
);
90 static int post_sh(POST_ARGS
);
91 static int post_sh_body(POST_ARGS
);
92 static int post_sh_head(POST_ARGS
);
93 static int post_st(POST_ARGS
);
94 static int post_eoln(POST_ARGS
);
95 static int post_vt(POST_ARGS
);
96 static int pre_an(PRE_ARGS
);
97 static int pre_bd(PRE_ARGS
);
98 static int pre_bl(PRE_ARGS
);
99 static int pre_dd(PRE_ARGS
);
100 static int pre_display(PRE_ARGS
);
101 static int pre_dt(PRE_ARGS
);
102 static int pre_it(PRE_ARGS
);
103 static int pre_os(PRE_ARGS
);
104 static int pre_rv(PRE_ARGS
);
105 static int pre_sh(PRE_ARGS
);
106 static int pre_ss(PRE_ARGS
);
108 static v_post posts_an
[] = { post_an
, NULL
};
109 static v_post posts_at
[] = { post_at
, NULL
};
110 static v_post posts_bd_bk
[] = { hwarn_eq0
, bwarn_ge1
, NULL
};
111 static v_post posts_bf
[] = { hwarn_le1
, post_bf
, NULL
};
112 static v_post posts_bl
[] = { bwarn_ge1
, post_bl
, NULL
};
113 static v_post posts_bool
[] = { eerr_eq1
, ebool
, NULL
};
114 static v_post posts_eoln
[] = { post_eoln
, NULL
};
115 static v_post posts_dt
[] = { post_dt
, NULL
};
116 static v_post posts_fo
[] = { hwarn_eq1
, bwarn_ge1
, NULL
};
117 static v_post posts_it
[] = { post_it
, NULL
};
118 static v_post posts_lb
[] = { eerr_eq1
, post_lb
, NULL
};
119 static v_post posts_nd
[] = { berr_ge1
, NULL
};
120 static v_post posts_nm
[] = { post_nm
, NULL
};
121 static v_post posts_notext
[] = { eerr_eq0
, NULL
};
122 static v_post posts_rs
[] = { berr_ge1
, herr_eq0
, post_rs
, NULL
};
123 static v_post posts_sh
[] = { herr_ge1
, bwarn_ge1
, post_sh
, NULL
};
124 static v_post posts_sp
[] = { eerr_le1
, NULL
};
125 static v_post posts_ss
[] = { herr_ge1
, NULL
};
126 static v_post posts_st
[] = { eerr_eq1
, post_st
, NULL
};
127 static v_post posts_text
[] = { eerr_ge1
, NULL
};
128 static v_post posts_text1
[] = { eerr_eq1
, NULL
};
129 static v_post posts_vt
[] = { post_vt
, NULL
};
130 static v_post posts_wline
[] = { bwarn_ge1
, herr_eq0
, NULL
};
131 static v_post posts_wtext
[] = { ewarn_ge1
, NULL
};
132 static v_pre pres_an
[] = { pre_an
, NULL
};
133 static v_pre pres_bd
[] = { pre_display
, pre_bd
, NULL
};
134 static v_pre pres_bl
[] = { pre_bl
, NULL
};
135 static v_pre pres_d1
[] = { pre_display
, NULL
};
136 static v_pre pres_dd
[] = { pre_dd
, NULL
};
137 static v_pre pres_dt
[] = { pre_dt
, NULL
};
138 static v_pre pres_er
[] = { NULL
, NULL
};
139 static v_pre pres_ex
[] = { NULL
, NULL
};
140 static v_pre pres_fd
[] = { NULL
, NULL
};
141 static v_pre pres_it
[] = { pre_it
, NULL
};
142 static v_pre pres_os
[] = { pre_os
, NULL
};
143 static v_pre pres_rv
[] = { pre_rv
, NULL
};
144 static v_pre pres_sh
[] = { pre_sh
, NULL
};
145 static v_pre pres_ss
[] = { pre_ss
, NULL
};
147 const struct valids mdoc_valids
[MDOC_MAX
] = {
148 { NULL
, NULL
}, /* Ap */
149 { pres_dd
, posts_text
}, /* Dd */
150 { pres_dt
, posts_dt
}, /* Dt */
151 { pres_os
, NULL
}, /* Os */
152 { pres_sh
, posts_sh
}, /* Sh */
153 { pres_ss
, posts_ss
}, /* Ss */
154 { NULL
, posts_notext
}, /* Pp */
155 { pres_d1
, posts_wline
}, /* D1 */
156 { pres_d1
, posts_wline
}, /* Dl */
157 { pres_bd
, posts_bd_bk
}, /* Bd */
158 { NULL
, NULL
}, /* Ed */
159 { pres_bl
, posts_bl
}, /* Bl */
160 { NULL
, NULL
}, /* El */
161 { pres_it
, posts_it
}, /* It */
162 { NULL
, posts_text
}, /* Ad */
163 { pres_an
, posts_an
}, /* An */
164 { NULL
, NULL
}, /* Ar */
165 { NULL
, posts_text
}, /* Cd */
166 { NULL
, NULL
}, /* Cm */
167 { NULL
, NULL
}, /* Dv */
168 { pres_er
, posts_text
}, /* Er */
169 { NULL
, NULL
}, /* Ev */
170 { pres_ex
, NULL
}, /* Ex */
171 { NULL
, NULL
}, /* Fa */
172 { pres_fd
, posts_wtext
}, /* Fd */
173 { NULL
, NULL
}, /* Fl */
174 { NULL
, posts_text
}, /* Fn */
175 { NULL
, posts_wtext
}, /* Ft */
176 { NULL
, posts_text
}, /* Ic */
177 { NULL
, posts_text1
}, /* In */
178 { NULL
, NULL
}, /* Li */
179 { NULL
, posts_nd
}, /* Nd */
180 { NULL
, posts_nm
}, /* Nm */
181 { NULL
, posts_wline
}, /* Op */
182 { NULL
, NULL
}, /* Ot */
183 { NULL
, NULL
}, /* Pa */
184 { pres_rv
, NULL
}, /* Rv */
185 { NULL
, posts_st
}, /* St */
186 { NULL
, NULL
}, /* Va */
187 { NULL
, posts_vt
}, /* Vt */
188 { NULL
, posts_wtext
}, /* Xr */
189 { NULL
, posts_text
}, /* %A */
190 { NULL
, posts_text
}, /* %B */ /* FIXME: can be used outside Rs/Re. */
191 { NULL
, posts_text
}, /* %D */ /* FIXME: check date with mandoc_a2time(). */
192 { NULL
, posts_text
}, /* %I */
193 { NULL
, posts_text
}, /* %J */
194 { NULL
, posts_text
}, /* %N */
195 { NULL
, posts_text
}, /* %O */
196 { NULL
, posts_text
}, /* %P */
197 { NULL
, posts_text
}, /* %R */
198 { NULL
, posts_text
}, /* %T */ /* FIXME: can be used outside Rs/Re. */
199 { NULL
, posts_text
}, /* %V */
200 { NULL
, NULL
}, /* Ac */
201 { NULL
, NULL
}, /* Ao */
202 { NULL
, posts_wline
}, /* Aq */
203 { NULL
, posts_at
}, /* At */
204 { NULL
, NULL
}, /* Bc */
205 { NULL
, posts_bf
}, /* Bf */
206 { NULL
, NULL
}, /* Bo */
207 { NULL
, posts_wline
}, /* Bq */
208 { NULL
, NULL
}, /* Bsx */
209 { NULL
, NULL
}, /* Bx */
210 { NULL
, posts_bool
}, /* Db */
211 { NULL
, NULL
}, /* Dc */
212 { NULL
, NULL
}, /* Do */
213 { NULL
, posts_wline
}, /* Dq */
214 { NULL
, NULL
}, /* Ec */
215 { NULL
, NULL
}, /* Ef */
216 { NULL
, NULL
}, /* Em */
217 { NULL
, NULL
}, /* Eo */
218 { NULL
, NULL
}, /* Fx */
219 { NULL
, posts_text
}, /* Ms */
220 { NULL
, posts_notext
}, /* No */
221 { NULL
, posts_notext
}, /* Ns */
222 { NULL
, NULL
}, /* Nx */
223 { NULL
, NULL
}, /* Ox */
224 { NULL
, NULL
}, /* Pc */
225 { NULL
, posts_text1
}, /* Pf */
226 { NULL
, NULL
}, /* Po */
227 { NULL
, posts_wline
}, /* Pq */
228 { NULL
, NULL
}, /* Qc */
229 { NULL
, posts_wline
}, /* Ql */
230 { NULL
, NULL
}, /* Qo */
231 { NULL
, posts_wline
}, /* Qq */
232 { NULL
, NULL
}, /* Re */
233 { NULL
, posts_rs
}, /* Rs */
234 { NULL
, NULL
}, /* Sc */
235 { NULL
, NULL
}, /* So */
236 { NULL
, posts_wline
}, /* Sq */
237 { NULL
, posts_bool
}, /* Sm */
238 { NULL
, posts_text
}, /* Sx */
239 { NULL
, posts_text
}, /* Sy */
240 { NULL
, posts_text
}, /* Tn */
241 { NULL
, NULL
}, /* Ux */
242 { NULL
, NULL
}, /* Xc */
243 { NULL
, NULL
}, /* Xo */
244 { NULL
, posts_fo
}, /* Fo */
245 { NULL
, NULL
}, /* Fc */
246 { NULL
, NULL
}, /* Oo */
247 { NULL
, NULL
}, /* Oc */
248 { NULL
, posts_bd_bk
}, /* Bk */
249 { NULL
, NULL
}, /* Ek */
250 { NULL
, posts_eoln
}, /* Bt */
251 { NULL
, NULL
}, /* Hf */
252 { NULL
, NULL
}, /* Fr */
253 { NULL
, posts_eoln
}, /* Ud */
254 { NULL
, posts_lb
}, /* Lb */
255 { NULL
, posts_notext
}, /* Lp */
256 { NULL
, posts_text
}, /* Lk */
257 { NULL
, posts_text
}, /* Mt */
258 { NULL
, posts_wline
}, /* Brq */
259 { NULL
, NULL
}, /* Bro */
260 { NULL
, NULL
}, /* Brc */
261 { NULL
, posts_text
}, /* %C */
262 { NULL
, NULL
}, /* Es */
263 { NULL
, NULL
}, /* En */
264 { NULL
, NULL
}, /* Dx */
265 { NULL
, posts_text
}, /* %Q */
266 { NULL
, posts_notext
}, /* br */
267 { NULL
, posts_sp
}, /* sp */
268 { NULL
, posts_text1
}, /* %U */
269 { NULL
, NULL
}, /* Ta */
274 mdoc_valid_pre(struct mdoc
*mdoc
, struct mdoc_node
*n
)
280 if (MDOC_TEXT
== n
->type
) {
284 return(check_text(mdoc
, line
, pos
, tp
));
287 if ( ! check_args(mdoc
, n
))
289 if (NULL
== mdoc_valids
[n
->tok
].pre
)
291 for (p
= mdoc_valids
[n
->tok
].pre
; *p
; p
++)
292 if ( ! (*p
)(mdoc
, n
))
299 mdoc_valid_post(struct mdoc
*mdoc
)
303 if (MDOC_VALID
& mdoc
->last
->flags
)
305 mdoc
->last
->flags
|= MDOC_VALID
;
307 if (MDOC_TEXT
== mdoc
->last
->type
)
309 if (MDOC_ROOT
== mdoc
->last
->type
)
310 return(post_root(mdoc
));
312 if (NULL
== mdoc_valids
[mdoc
->last
->tok
].post
)
314 for (p
= mdoc_valids
[mdoc
->last
->tok
].post
; *p
; p
++)
323 warn_count(struct mdoc
*m
, const char *k
,
324 int want
, const char *v
, int has
)
327 return(mdoc_vmsg(m
, MANDOCERR_ARGCOUNT
,
328 m
->last
->line
, m
->last
->pos
,
329 "%s %s %d (have %d)", v
, k
, want
, has
));
334 err_count(struct mdoc
*m
, const char *k
,
335 int want
, const char *v
, int has
)
338 mdoc_vmsg(m
, MANDOCERR_SYNTARGCOUNT
,
339 m
->last
->line
, m
->last
->pos
,
340 "%s %s %d (have %d)",
347 * Build these up with macros because they're basically the same check
348 * for different inequalities. Yes, this could be done with functions,
349 * but this is reasonable for now.
352 #define CHECK_CHILD_DEFN(lvl, name, ineq) \
354 lvl##_child_##name(struct mdoc *mdoc, const char *p, int sz) \
356 if (mdoc->last->nchild ineq sz) \
358 return(lvl##_count(mdoc, #ineq, sz, p, mdoc->last->nchild)); \
361 #define CHECK_BODY_DEFN(name, lvl, func, num) \
363 b##lvl##_##name(POST_ARGS) \
365 if (MDOC_BODY != mdoc->last->type) \
367 return(func(mdoc, "multi-line arguments", (num))); \
370 #define CHECK_ELEM_DEFN(name, lvl, func, num) \
372 e##lvl##_##name(POST_ARGS) \
374 assert(MDOC_ELEM == mdoc->last->type); \
375 return(func(mdoc, "line arguments", (num))); \
378 #define CHECK_HEAD_DEFN(name, lvl, func, num) \
380 h##lvl##_##name(POST_ARGS) \
382 if (MDOC_HEAD != mdoc->last->type) \
384 return(func(mdoc, "line arguments", (num))); \
388 CHECK_CHILD_DEFN(warn
, gt
, >) /* warn_child_gt() */
389 CHECK_CHILD_DEFN(err
, gt
, >) /* err_child_gt() */
390 CHECK_CHILD_DEFN(warn
, eq
, ==) /* warn_child_eq() */
391 CHECK_CHILD_DEFN(err
, eq
, ==) /* err_child_eq() */
392 CHECK_CHILD_DEFN(err
, lt
, <) /* err_child_lt() */
393 CHECK_CHILD_DEFN(warn
, lt
, <) /* warn_child_lt() */
394 CHECK_BODY_DEFN(ge1
, warn
, warn_child_gt
, 0) /* bwarn_ge1() */
395 CHECK_BODY_DEFN(ge1
, err
, err_child_gt
, 0) /* berr_ge1() */
396 CHECK_ELEM_DEFN(ge1
, warn
, warn_child_gt
, 0) /* ewarn_ge1() */
397 CHECK_ELEM_DEFN(eq1
, err
, err_child_eq
, 1) /* eerr_eq1() */
398 CHECK_ELEM_DEFN(le1
, err
, err_child_lt
, 2) /* eerr_le1() */
399 CHECK_ELEM_DEFN(eq0
, err
, err_child_eq
, 0) /* eerr_eq0() */
400 CHECK_ELEM_DEFN(ge1
, err
, err_child_gt
, 0) /* eerr_ge1() */
401 CHECK_HEAD_DEFN(eq0
, err
, err_child_eq
, 0) /* herr_eq0() */
402 CHECK_HEAD_DEFN(le1
, warn
, warn_child_lt
, 2) /* hwarn_le1() */
403 CHECK_HEAD_DEFN(ge1
, err
, err_child_gt
, 0) /* herr_ge1() */
404 CHECK_HEAD_DEFN(eq1
, warn
, warn_child_eq
, 1) /* hwarn_eq1() */
405 CHECK_HEAD_DEFN(eq0
, warn
, warn_child_eq
, 0) /* hwarn_eq0() */
409 check_stdarg(PRE_ARGS
)
412 if (n
->args
&& 1 == n
->args
->argc
)
413 if (MDOC_Std
== n
->args
->argv
[0].arg
)
415 return(mdoc_nmsg(mdoc
, n
, MANDOCERR_NOARGV
));
420 check_args(struct mdoc
*m
, struct mdoc_node
*n
)
427 assert(n
->args
->argc
);
428 for (i
= 0; i
< (int)n
->args
->argc
; i
++)
429 if ( ! check_argv(m
, n
, &n
->args
->argv
[i
]))
437 check_argv(struct mdoc
*m
, struct mdoc_node
*n
, struct mdoc_argv
*v
)
441 for (i
= 0; i
< (int)v
->sz
; i
++)
442 if ( ! check_text(m
, v
->line
, v
->pos
, v
->value
[i
]))
445 if (MDOC_Std
== v
->arg
) {
446 if (v
->sz
|| m
->meta
.name
)
448 if ( ! mdoc_nmsg(m
, n
, MANDOCERR_NONAME
))
457 check_text(struct mdoc
*mdoc
, int line
, int pos
, char *p
)
461 for ( ; *p
; p
++, pos
++) {
463 if ( ! (MDOC_LITERAL
& mdoc
->flags
))
464 if ( ! mdoc_pmsg(mdoc
, line
, pos
, MANDOCERR_BADCHAR
))
466 } else if ( ! isprint((u_char
)*p
) && ASCII_HYPH
!= *p
)
467 if ( ! mdoc_pmsg(mdoc
, line
, pos
, MANDOCERR_BADCHAR
))
473 c
= mandoc_special(p
);
480 c
= mdoc_pmsg(mdoc
, line
, pos
, MANDOCERR_BADESCAPE
);
481 if ( ! (MDOC_IGN_ESCAPE
& mdoc
->pflags
) && ! c
)
492 check_parent(PRE_ARGS
, enum mdoct tok
, enum mdoc_type t
)
496 if ((MDOC_ROOT
== t
|| tok
== n
->parent
->tok
) &&
497 (t
== n
->parent
->type
))
500 mdoc_vmsg(mdoc
, MANDOCERR_SYNTCHILD
,
501 n
->line
, n
->pos
, "want parent %s",
502 MDOC_ROOT
== t
? "<root>" :
503 mdoc_macronames
[tok
]);
510 pre_display(PRE_ARGS
)
512 struct mdoc_node
*node
;
514 /* Display elements (`Bd', `D1'...) cannot be nested. */
516 if (MDOC_BLOCK
!= n
->type
)
520 for (node
= mdoc
->last
->parent
; node
; node
= node
->parent
)
521 if (MDOC_BLOCK
== node
->type
)
522 if (MDOC_Bd
== node
->tok
)
527 mdoc_nmsg(mdoc
, n
, MANDOCERR_NESTEDDISP
);
536 const char *offs
, *width
;
539 if (MDOC_BLOCK
!= n
->type
) {
541 assert(MDOC_BLOCK
== n
->parent
->type
);
542 assert(MDOC_Bl
== n
->parent
->tok
);
543 assert(LIST__NONE
!= n
->parent
->data
.Bl
.type
);
544 memcpy(&n
->data
.Bl
, &n
->parent
->data
.Bl
,
545 sizeof(struct mdoc_bl
));
550 * First figure out which kind of list to use: bind ourselves to
551 * the first mentioned list type and warn about any remaining
552 * ones. If we find no list type, we default to LIST_item.
555 assert(LIST__NONE
== n
->data
.Bl
.type
);
558 for (i
= 0; n
->args
&& i
< (int)n
->args
->argc
; i
++) {
562 switch (n
->args
->argv
[i
].arg
) {
563 /* Set list types. */
597 /* Set list arguments. */
599 dup
= n
->data
.Bl
.comp
;
603 dup
= (NULL
!= n
->data
.Bl
.width
);
604 width
= n
->args
->argv
[i
].value
[0];
607 /* NB: this can be empty! */
608 if (n
->args
->argv
[i
].sz
) {
609 offs
= n
->args
->argv
[i
].value
[0];
610 dup
= (NULL
!= n
->data
.Bl
.offs
);
613 if ( ! mdoc_nmsg(mdoc
, n
, MANDOCERR_IGNARGV
))
618 /* Check: duplicate auxiliary arguments. */
620 if (dup
&& ! mdoc_nmsg(mdoc
, n
, MANDOCERR_ARGVREP
))
624 n
->data
.Bl
.comp
= comp
;
626 n
->data
.Bl
.offs
= offs
;
628 n
->data
.Bl
.width
= width
;
630 /* Check: multiple list types. */
632 if (LIST__NONE
!= lt
&& n
->data
.Bl
.type
!= LIST__NONE
)
633 if ( ! mdoc_nmsg(mdoc
, n
, MANDOCERR_LISTREP
))
636 /* Assign list type. */
638 if (LIST__NONE
!= lt
&& n
->data
.Bl
.type
== LIST__NONE
)
639 n
->data
.Bl
.type
= lt
;
641 /* The list type should come first. */
643 if (n
->data
.Bl
.type
== LIST__NONE
)
644 if (n
->data
.Bl
.width
||
647 if ( ! mdoc_nmsg(mdoc
, n
, MANDOCERR_LISTFIRST
))
653 /* Allow lists to default to LIST_item. */
655 if (LIST__NONE
== n
->data
.Bl
.type
) {
656 if ( ! mdoc_nmsg(mdoc
, n
, MANDOCERR_LISTTYPE
))
658 n
->data
.Bl
.type
= LIST_item
;
662 * Validate the width field. Some list types don't need width
663 * types and should be warned about them. Others should have it
664 * and must also be warned.
667 switch (n
->data
.Bl
.type
) {
669 if (n
->data
.Bl
.width
)
671 if (mdoc_nmsg(mdoc
, n
, MANDOCERR_NOWIDTHARG
))
683 if (NULL
== n
->data
.Bl
.width
)
685 if (mdoc_nmsg(mdoc
, n
, MANDOCERR_WIDTHARG
))
703 if (MDOC_BLOCK
!= n
->type
) {
705 assert(MDOC_BLOCK
== n
->parent
->type
);
706 assert(MDOC_Bd
== n
->parent
->tok
);
707 assert(DISP__NONE
!= n
->parent
->data
.Bd
.type
);
708 memcpy(&n
->data
.Bd
, &n
->parent
->data
.Bd
,
709 sizeof(struct mdoc_bd
));
713 assert(DISP__NONE
== n
->data
.Bd
.type
);
716 for (i
= 0; n
->args
&& i
< (int)n
->args
->argc
; i
++) {
721 switch (n
->args
->argv
[i
].arg
) {
728 case (MDOC_Unfilled
):
738 mdoc_nmsg(mdoc
, n
, MANDOCERR_BADDISP
);
741 /* NB: this can be empty! */
742 if (n
->args
->argv
[i
].sz
) {
743 offs
= n
->args
->argv
[i
].value
[0];
744 dup
= (NULL
!= n
->data
.Bd
.offs
);
747 if ( ! mdoc_nmsg(mdoc
, n
, MANDOCERR_IGNARGV
))
752 dup
= n
->data
.Bd
.comp
;
759 /* Check whether we have duplicates. */
761 if (dup
&& ! mdoc_nmsg(mdoc
, n
, MANDOCERR_ARGVREP
))
764 /* Make our auxiliary assignments. */
767 n
->data
.Bd
.offs
= offs
;
769 n
->data
.Bd
.comp
= comp
;
771 /* Check whether a type has already been assigned. */
773 if (DISP__NONE
!= dt
&& n
->data
.Bd
.type
!= DISP__NONE
)
774 if ( ! mdoc_nmsg(mdoc
, n
, MANDOCERR_DISPREP
))
777 /* Make our type assignment. */
779 if (DISP__NONE
!= dt
&& n
->data
.Bd
.type
== DISP__NONE
)
780 n
->data
.Bd
.type
= dt
;
783 if (DISP__NONE
== n
->data
.Bd
.type
) {
784 if ( ! mdoc_nmsg(mdoc
, n
, MANDOCERR_DISPTYPE
))
786 n
->data
.Bd
.type
= DISP_ragged
;
797 if (MDOC_BLOCK
!= n
->type
)
799 return(check_parent(mdoc
, n
, MDOC_Sh
, MDOC_BODY
));
807 if (MDOC_BLOCK
!= n
->type
)
810 mdoc
->regs
->regs
[(int)REG_nS
].set
= 0;
811 return(check_parent(mdoc
, n
, MDOC_MAX
, MDOC_ROOT
));
819 if (MDOC_BLOCK
!= n
->type
)
822 * FIXME: this can probably be lifted if we make the It into
823 * something else on-the-fly?
825 return(check_parent(mdoc
, n
, MDOC_Bl
, MDOC_BODY
));
833 if (NULL
== n
->args
|| 1 == n
->args
->argc
)
835 mdoc_vmsg(mdoc
, MANDOCERR_SYNTARGCOUNT
,
837 "line arguments == 1 (have %d)",
847 return(check_stdarg(mdoc
, n
));
854 const struct mdoc_node
*nn
;
857 if (NULL
!= (nn
= mdoc
->last
->child
))
858 for (p
= nn
->string
; *p
; p
++) {
859 if (toupper((u_char
)*p
) == *p
)
861 if ( ! mdoc_nmsg(mdoc
, nn
, MANDOCERR_UPPERCASE
))
874 if (0 == mdoc
->meta
.date
|| mdoc
->meta
.os
)
875 if ( ! mdoc_nmsg(mdoc
, n
, MANDOCERR_PROLOGOOO
))
877 if (mdoc
->meta
.title
)
878 if ( ! mdoc_nmsg(mdoc
, n
, MANDOCERR_PROLOGREP
))
888 if (NULL
== mdoc
->meta
.title
|| 0 == mdoc
->meta
.date
)
889 if ( ! mdoc_nmsg(mdoc
, n
, MANDOCERR_PROLOGOOO
))
892 if ( ! mdoc_nmsg(mdoc
, n
, MANDOCERR_PROLOGREP
))
902 if (mdoc
->meta
.title
|| mdoc
->meta
.os
)
903 if ( ! mdoc_nmsg(mdoc
, n
, MANDOCERR_PROLOGOOO
))
906 if ( ! mdoc_nmsg(mdoc
, n
, MANDOCERR_PROLOGREP
))
916 struct mdoc_node
*head
;
918 if (MDOC_BLOCK
!= mdoc
->last
->type
)
921 head
= mdoc
->last
->head
;
923 if (mdoc
->last
->args
&& head
->child
) {
924 /* FIXME: this should provide a default. */
925 mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_SYNTARGVCOUNT
);
927 } else if (mdoc
->last
->args
)
930 if (NULL
== head
->child
|| MDOC_TEXT
!= head
->child
->type
) {
931 /* FIXME: this should provide a default. */
932 mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_SYNTARGVCOUNT
);
936 p
= head
->child
->string
;
938 if (0 == strcmp(p
, "Em"))
940 else if (0 == strcmp(p
, "Li"))
942 else if (0 == strcmp(p
, "Sy"))
945 mdoc_nmsg(mdoc
, head
, MANDOCERR_FONTTYPE
);
954 if (mdoc_a2lib(mdoc
->last
->child
->string
))
956 return(mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_BADLIB
));
964 if (NULL
== mdoc
->last
->child
)
966 return(mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_ARGSLOST
));
973 const struct mdoc_node
*n
;
976 * The Vt macro comes in both ELEM and BLOCK form, both of which
977 * have different syntaxes (yet more context-sensitive
978 * behaviour). ELEM types must have a child; BLOCK types,
979 * specifically the BODY, should only have TEXT children.
982 if (MDOC_ELEM
== mdoc
->last
->type
)
983 return(eerr_ge1(mdoc
));
984 if (MDOC_BODY
!= mdoc
->last
->type
)
987 for (n
= mdoc
->last
->child
; n
; n
= n
->next
)
988 if (MDOC_TEXT
!= n
->type
)
989 if ( ! mdoc_nmsg(mdoc
, n
, MANDOCERR_CHILD
))
1000 if (mdoc
->last
->child
)
1002 if (mdoc
->meta
.name
)
1004 return(mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_NONAME
));
1012 if (NULL
== mdoc
->last
->child
)
1014 assert(MDOC_TEXT
== mdoc
->last
->child
->type
);
1015 if (mdoc_a2att(mdoc
->last
->child
->string
))
1017 return(mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_BADATT
));
1025 if (mdoc
->last
->args
) {
1026 if (NULL
== mdoc
->last
->child
)
1028 return(mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_ARGCOUNT
));
1031 if (mdoc
->last
->child
)
1033 return(mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_NOARGS
));
1042 struct mdoc_node
*n
, *c
;
1045 if (MDOC_BLOCK
!= mdoc
->last
->type
)
1048 n
= mdoc
->last
->parent
->parent
;
1049 lt
= n
->data
.Bl
.type
;
1051 if (LIST__NONE
== lt
) {
1052 mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_LISTTYPE
);
1058 if (mdoc
->last
->head
->child
)
1060 /* FIXME: give this a dummy value. */
1061 if ( ! mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_NOARGS
))
1071 if (NULL
== mdoc
->last
->head
->child
)
1072 if ( ! mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_NOARGS
))
1074 if (NULL
== mdoc
->last
->body
->child
)
1075 if ( ! mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_NOBODY
))
1087 if (mdoc
->last
->head
->child
)
1088 if ( ! mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_ARGSLOST
))
1090 if (NULL
== mdoc
->last
->body
->child
)
1091 if ( ! mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_NOBODY
))
1096 for (i
= 0; i
< (int)n
->args
->argc
; i
++)
1097 if (MDOC_Column
== n
->args
->argv
[i
].arg
) {
1098 cols
= (int)n
->args
->argv
[i
].sz
;
1103 assert(NULL
== mdoc
->last
->head
->child
);
1105 if (NULL
== mdoc
->last
->body
->child
)
1106 if ( ! mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_NOBODY
))
1109 for (i
= 0, c
= mdoc
->last
->child
; c
; c
= c
->next
)
1110 if (MDOC_BODY
== c
->type
)
1114 er
= MANDOCERR_ARGCOUNT
;
1115 else if (i
== cols
|| i
== cols
+ 1)
1118 er
= MANDOCERR_SYNTARGCOUNT
;
1120 rc
= mdoc_vmsg(mdoc
, er
,
1121 mdoc
->last
->line
, mdoc
->last
->pos
,
1122 "columns == %d (have %d)", cols
, i
);
1133 post_bl_head(POST_ARGS
)
1136 struct mdoc_node
*n
;
1138 assert(mdoc
->last
->parent
);
1139 n
= mdoc
->last
->parent
;
1141 if (LIST_column
== n
->data
.Bl
.type
) {
1142 for (i
= 0; i
< (int)n
->args
->argc
; i
++)
1143 if (MDOC_Column
== n
->args
->argv
[i
].arg
)
1145 assert(i
< (int)n
->args
->argc
);
1147 if (n
->args
->argv
[i
].sz
&& mdoc
->last
->nchild
) {
1148 mdoc_nmsg(mdoc
, n
, MANDOCERR_COLUMNS
);
1154 if (0 == (i
= mdoc
->last
->nchild
))
1156 return(warn_count(mdoc
, "==", 0, "line arguments", i
));
1163 struct mdoc_node
*n
;
1165 if (MDOC_HEAD
== mdoc
->last
->type
)
1166 return(post_bl_head(mdoc
));
1167 if (MDOC_BODY
!= mdoc
->last
->type
)
1169 if (NULL
== mdoc
->last
->child
)
1173 * We only allow certain children of `Bl'. This is usually on
1174 * `It', but apparently `Sm' occurs here and there, so we let
1175 * that one through, too.
1179 for (n
= mdoc
->last
->child
; n
; n
= n
->next
) {
1180 if (MDOC_BLOCK
== n
->type
&& MDOC_It
== n
->tok
)
1182 if (MDOC_Sm
== n
->tok
)
1184 mdoc_nmsg(mdoc
, n
, MANDOCERR_SYNTCHILD
);
1193 ebool(struct mdoc
*mdoc
)
1195 struct mdoc_node
*n
;
1198 for (n
= mdoc
->last
->child
; n
; n
= n
->next
) {
1199 if (MDOC_TEXT
!= n
->type
)
1201 if (0 == strcmp(n
->string
, "on"))
1203 if (0 == strcmp(n
->string
, "off"))
1210 return(mdoc_nmsg(mdoc
, n
, MANDOCERR_BADBOOL
));
1215 post_root(POST_ARGS
)
1218 if (NULL
== mdoc
->first
->child
)
1219 mdoc_nmsg(mdoc
, mdoc
->first
, MANDOCERR_NODOCBODY
);
1220 else if ( ! (MDOC_PBODY
& mdoc
->flags
))
1221 mdoc_nmsg(mdoc
, mdoc
->first
, MANDOCERR_NODOCPROLOG
);
1222 else if (MDOC_BLOCK
!= mdoc
->first
->child
->type
)
1223 mdoc_nmsg(mdoc
, mdoc
->first
, MANDOCERR_NODOCBODY
);
1224 else if (MDOC_Sh
!= mdoc
->first
->child
->tok
)
1225 mdoc_nmsg(mdoc
, mdoc
->first
, MANDOCERR_NODOCBODY
);
1237 if (mdoc_a2st(mdoc
->last
->child
->string
))
1239 return(mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_BADSTANDARD
));
1246 struct mdoc_node
*nn
;
1248 if (MDOC_BODY
!= mdoc
->last
->type
)
1251 for (nn
= mdoc
->last
->child
; nn
; nn
= nn
->next
)
1282 mdoc_nmsg(mdoc
, nn
, MANDOCERR_SYNTCHILD
);
1294 if (MDOC_HEAD
== mdoc
->last
->type
)
1295 return(post_sh_head(mdoc
));
1296 if (MDOC_BODY
== mdoc
->last
->type
)
1297 return(post_sh_body(mdoc
));
1304 post_sh_body(POST_ARGS
)
1306 struct mdoc_node
*n
;
1308 if (SEC_NAME
!= mdoc
->lastsec
)
1312 * Warn if the NAME section doesn't contain the `Nm' and `Nd'
1313 * macros (can have multiple `Nm' and one `Nd'). Note that the
1314 * children of the BODY declaration can also be "text".
1317 if (NULL
== (n
= mdoc
->last
->child
))
1318 return(mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_BADNAMESEC
));
1320 for ( ; n
&& n
->next
; n
= n
->next
) {
1321 if (MDOC_ELEM
== n
->type
&& MDOC_Nm
== n
->tok
)
1323 if (MDOC_TEXT
== n
->type
)
1325 if ( ! mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_BADNAMESEC
))
1330 if (MDOC_BLOCK
== n
->type
&& MDOC_Nd
== n
->tok
)
1332 return(mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_BADNAMESEC
));
1337 post_sh_head(POST_ARGS
)
1341 const struct mdoc_node
*n
;
1344 * Process a new section. Sections are either "named" or
1345 * "custom"; custom sections are user-defined, while named ones
1346 * usually follow a conventional order and may only appear in
1347 * certain manual sections.
1353 * FIXME: yes, these can use a dynamic buffer, but I don't do so
1354 * in the interests of simplicity.
1357 for (n
= mdoc
->last
->child
; n
; n
= n
->next
) {
1358 /* XXX - copied from compact(). */
1359 assert(MDOC_TEXT
== n
->type
);
1361 if (strlcat(buf
, n
->string
, BUFSIZ
) >= BUFSIZ
) {
1362 mdoc_nmsg(mdoc
, n
, MANDOCERR_MEM
);
1365 if (NULL
== n
->next
)
1367 if (strlcat(buf
, " ", BUFSIZ
) >= BUFSIZ
) {
1368 mdoc_nmsg(mdoc
, n
, MANDOCERR_MEM
);
1373 sec
= mdoc_str2sec(buf
);
1376 * Check: NAME should always be first, CUSTOM has no roles,
1377 * non-CUSTOM has a conventional order to be followed.
1380 if (SEC_NAME
!= sec
&& SEC_NONE
== mdoc
->lastnamed
)
1381 if ( ! mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_NAMESECFIRST
))
1384 if (SEC_CUSTOM
== sec
)
1387 if (sec
== mdoc
->lastnamed
)
1388 if ( ! mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_SECREP
))
1391 if (sec
< mdoc
->lastnamed
)
1392 if ( ! mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_SECOOO
))
1396 * Check particular section/manual conventions. LIBRARY can
1397 * only occur in manual section 2, 3, and 9.
1402 assert(mdoc
->meta
.msec
);
1403 if (*mdoc
->meta
.msec
== '2')
1405 if (*mdoc
->meta
.msec
== '3')
1407 if (*mdoc
->meta
.msec
== '9')
1409 return(mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_SECMSEC
));