]>
git.cameronkatri.com Git - mandoc.git/blob - mdoc_validate.c
1 /* $Id: mdoc_validate.c,v 1.115 2010/08/20 01:02:07 schwarze 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
39 typedef int (*v_pre
)(PRE_ARGS
);
40 typedef int (*v_post
)(POST_ARGS
);
47 static int check_parent(PRE_ARGS
, enum mdoct
, enum mdoc_type
);
48 static int check_stdarg(PRE_ARGS
);
49 static int check_text(struct mdoc
*, int, int, char *);
50 static int check_argv(struct mdoc
*,
51 struct mdoc_node
*, struct mdoc_argv
*);
52 static int check_args(struct mdoc
*, struct mdoc_node
*);
53 static int err_child_lt(struct mdoc
*, const char *, int);
54 static int warn_child_lt(struct mdoc
*, const char *, int);
55 static int err_child_gt(struct mdoc
*, const char *, int);
56 static int warn_child_gt(struct mdoc
*, const char *, int);
57 static int err_child_eq(struct mdoc
*, const char *, int);
58 static int warn_child_eq(struct mdoc
*, const char *, int);
59 static int warn_count(struct mdoc
*, const char *,
60 int, const char *, int);
61 static int err_count(struct mdoc
*, const char *,
62 int, const char *, int);
64 static int berr_ge1(POST_ARGS
);
65 static int bwarn_ge1(POST_ARGS
);
66 static int ebool(POST_ARGS
);
67 static int eerr_eq0(POST_ARGS
);
68 static int eerr_eq1(POST_ARGS
);
69 static int eerr_ge1(POST_ARGS
);
70 static int eerr_le1(POST_ARGS
);
71 static int ewarn_ge1(POST_ARGS
);
72 static int herr_eq0(POST_ARGS
);
73 static int herr_ge1(POST_ARGS
);
74 static int hwarn_eq1(POST_ARGS
);
75 static int hwarn_eq0(POST_ARGS
);
76 static int hwarn_le1(POST_ARGS
);
78 static int post_an(POST_ARGS
);
79 static int post_at(POST_ARGS
);
80 static int post_bf(POST_ARGS
);
81 static int post_bl(POST_ARGS
);
82 static int post_bl_head(POST_ARGS
);
83 static int post_dt(POST_ARGS
);
84 static int post_it(POST_ARGS
);
85 static int post_lb(POST_ARGS
);
86 static int post_nm(POST_ARGS
);
87 static int post_root(POST_ARGS
);
88 static int post_rs(POST_ARGS
);
89 static int post_sh(POST_ARGS
);
90 static int post_sh_body(POST_ARGS
);
91 static int post_sh_head(POST_ARGS
);
92 static int post_st(POST_ARGS
);
93 static int post_eoln(POST_ARGS
);
94 static int post_vt(POST_ARGS
);
95 static int pre_an(PRE_ARGS
);
96 static int pre_bd(PRE_ARGS
);
97 static int pre_bl(PRE_ARGS
);
98 static int pre_dd(PRE_ARGS
);
99 static int pre_display(PRE_ARGS
);
100 static int pre_dt(PRE_ARGS
);
101 static int pre_it(PRE_ARGS
);
102 static int pre_os(PRE_ARGS
);
103 static int pre_rv(PRE_ARGS
);
104 static int pre_sh(PRE_ARGS
);
105 static int pre_ss(PRE_ARGS
);
107 static v_post posts_an
[] = { post_an
, NULL
};
108 static v_post posts_at
[] = { post_at
, NULL
};
109 static v_post posts_bd_bk
[] = { hwarn_eq0
, bwarn_ge1
, NULL
};
110 static v_post posts_bf
[] = { hwarn_le1
, post_bf
, NULL
};
111 static v_post posts_bl
[] = { bwarn_ge1
, post_bl
, NULL
};
112 static v_post posts_bool
[] = { eerr_eq1
, ebool
, NULL
};
113 static v_post posts_eoln
[] = { post_eoln
, NULL
};
114 static v_post posts_dt
[] = { post_dt
, NULL
};
115 static v_post posts_fo
[] = { hwarn_eq1
, bwarn_ge1
, NULL
};
116 static v_post posts_it
[] = { post_it
, NULL
};
117 static v_post posts_lb
[] = { eerr_eq1
, post_lb
, NULL
};
118 static v_post posts_nd
[] = { berr_ge1
, NULL
};
119 static v_post posts_nm
[] = { post_nm
, NULL
};
120 static v_post posts_notext
[] = { eerr_eq0
, NULL
};
121 static v_post posts_rs
[] = { berr_ge1
, herr_eq0
, post_rs
, NULL
};
122 static v_post posts_sh
[] = { herr_ge1
, bwarn_ge1
, post_sh
, NULL
};
123 static v_post posts_sp
[] = { eerr_le1
, NULL
};
124 static v_post posts_ss
[] = { herr_ge1
, NULL
};
125 static v_post posts_st
[] = { eerr_eq1
, post_st
, NULL
};
126 static v_post posts_text
[] = { eerr_ge1
, NULL
};
127 static v_post posts_text1
[] = { eerr_eq1
, NULL
};
128 static v_post posts_vt
[] = { post_vt
, NULL
};
129 static v_post posts_wline
[] = { bwarn_ge1
, herr_eq0
, NULL
};
130 static v_post posts_wtext
[] = { ewarn_ge1
, NULL
};
131 static v_pre pres_an
[] = { pre_an
, NULL
};
132 static v_pre pres_bd
[] = { pre_display
, pre_bd
, NULL
};
133 static v_pre pres_bl
[] = { pre_bl
, NULL
};
134 static v_pre pres_d1
[] = { pre_display
, NULL
};
135 static v_pre pres_dd
[] = { pre_dd
, NULL
};
136 static v_pre pres_dt
[] = { pre_dt
, NULL
};
137 static v_pre pres_er
[] = { NULL
, NULL
};
138 static v_pre pres_ex
[] = { NULL
, NULL
};
139 static v_pre pres_fd
[] = { NULL
, NULL
};
140 static v_pre pres_it
[] = { pre_it
, NULL
};
141 static v_pre pres_os
[] = { pre_os
, NULL
};
142 static v_pre pres_rv
[] = { pre_rv
, NULL
};
143 static v_pre pres_sh
[] = { pre_sh
, NULL
};
144 static v_pre pres_ss
[] = { pre_ss
, NULL
};
146 const struct valids mdoc_valids
[MDOC_MAX
] = {
147 { NULL
, NULL
}, /* Ap */
148 { pres_dd
, posts_wtext
}, /* Dd */
149 { pres_dt
, posts_dt
}, /* Dt */
150 { pres_os
, NULL
}, /* Os */
151 { pres_sh
, posts_sh
}, /* Sh */
152 { pres_ss
, posts_ss
}, /* Ss */
153 { NULL
, posts_notext
}, /* Pp */
154 { pres_d1
, posts_wline
}, /* D1 */
155 { pres_d1
, posts_wline
}, /* Dl */
156 { pres_bd
, posts_bd_bk
}, /* Bd */
157 { NULL
, NULL
}, /* Ed */
158 { pres_bl
, posts_bl
}, /* Bl */
159 { NULL
, NULL
}, /* El */
160 { pres_it
, posts_it
}, /* It */
161 { NULL
, posts_text
}, /* Ad */
162 { pres_an
, posts_an
}, /* An */
163 { NULL
, NULL
}, /* Ar */
164 { NULL
, posts_text
}, /* Cd */
165 { NULL
, NULL
}, /* Cm */
166 { NULL
, NULL
}, /* Dv */
167 { pres_er
, posts_text
}, /* Er */
168 { NULL
, NULL
}, /* Ev */
169 { pres_ex
, NULL
}, /* Ex */
170 { NULL
, NULL
}, /* Fa */
171 { pres_fd
, posts_wtext
}, /* Fd */
172 { NULL
, NULL
}, /* Fl */
173 { NULL
, posts_text
}, /* Fn */
174 { NULL
, posts_wtext
}, /* Ft */
175 { NULL
, posts_text
}, /* Ic */
176 { NULL
, posts_text1
}, /* In */
177 { NULL
, NULL
}, /* Li */
178 { NULL
, posts_nd
}, /* Nd */
179 { NULL
, posts_nm
}, /* Nm */
180 { NULL
, posts_wline
}, /* Op */
181 { NULL
, NULL
}, /* Ot */
182 { NULL
, NULL
}, /* Pa */
183 { pres_rv
, NULL
}, /* Rv */
184 { NULL
, posts_st
}, /* St */
185 { NULL
, NULL
}, /* Va */
186 { NULL
, posts_vt
}, /* Vt */
187 { NULL
, posts_wtext
}, /* Xr */
188 { NULL
, posts_text
}, /* %A */
189 { NULL
, posts_text
}, /* %B */ /* FIXME: can be used outside Rs/Re. */
190 { NULL
, posts_text
}, /* %D */ /* FIXME: check date with mandoc_a2time(). */
191 { NULL
, posts_text
}, /* %I */
192 { NULL
, posts_text
}, /* %J */
193 { NULL
, posts_text
}, /* %N */
194 { NULL
, posts_text
}, /* %O */
195 { NULL
, posts_text
}, /* %P */
196 { NULL
, posts_text
}, /* %R */
197 { NULL
, posts_text
}, /* %T */ /* FIXME: can be used outside Rs/Re. */
198 { NULL
, posts_text
}, /* %V */
199 { NULL
, NULL
}, /* Ac */
200 { NULL
, NULL
}, /* Ao */
201 { NULL
, posts_wline
}, /* Aq */
202 { NULL
, posts_at
}, /* At */
203 { NULL
, NULL
}, /* Bc */
204 { NULL
, posts_bf
}, /* Bf */
205 { NULL
, NULL
}, /* Bo */
206 { NULL
, posts_wline
}, /* Bq */
207 { NULL
, NULL
}, /* Bsx */
208 { NULL
, NULL
}, /* Bx */
209 { NULL
, posts_bool
}, /* Db */
210 { NULL
, NULL
}, /* Dc */
211 { NULL
, NULL
}, /* Do */
212 { NULL
, posts_wline
}, /* Dq */
213 { NULL
, NULL
}, /* Ec */
214 { NULL
, NULL
}, /* Ef */
215 { NULL
, NULL
}, /* Em */
216 { NULL
, NULL
}, /* Eo */
217 { NULL
, NULL
}, /* Fx */
218 { NULL
, posts_text
}, /* Ms */
219 { NULL
, posts_notext
}, /* No */
220 { NULL
, posts_notext
}, /* Ns */
221 { NULL
, NULL
}, /* Nx */
222 { NULL
, NULL
}, /* Ox */
223 { NULL
, NULL
}, /* Pc */
224 { NULL
, posts_text1
}, /* Pf */
225 { NULL
, NULL
}, /* Po */
226 { NULL
, posts_wline
}, /* Pq */
227 { NULL
, NULL
}, /* Qc */
228 { NULL
, posts_wline
}, /* Ql */
229 { NULL
, NULL
}, /* Qo */
230 { NULL
, posts_wline
}, /* Qq */
231 { NULL
, NULL
}, /* Re */
232 { NULL
, posts_rs
}, /* Rs */
233 { NULL
, NULL
}, /* Sc */
234 { NULL
, NULL
}, /* So */
235 { NULL
, posts_wline
}, /* Sq */
236 { NULL
, posts_bool
}, /* Sm */
237 { NULL
, posts_text
}, /* Sx */
238 { NULL
, posts_text
}, /* Sy */
239 { NULL
, posts_text
}, /* Tn */
240 { NULL
, NULL
}, /* Ux */
241 { NULL
, NULL
}, /* Xc */
242 { NULL
, NULL
}, /* Xo */
243 { NULL
, posts_fo
}, /* Fo */
244 { NULL
, NULL
}, /* Fc */
245 { NULL
, NULL
}, /* Oo */
246 { NULL
, NULL
}, /* Oc */
247 { NULL
, posts_bd_bk
}, /* Bk */
248 { NULL
, NULL
}, /* Ek */
249 { NULL
, posts_eoln
}, /* Bt */
250 { NULL
, NULL
}, /* Hf */
251 { NULL
, NULL
}, /* Fr */
252 { NULL
, posts_eoln
}, /* Ud */
253 { NULL
, posts_lb
}, /* Lb */
254 { NULL
, posts_notext
}, /* Lp */
255 { NULL
, posts_text
}, /* Lk */
256 { NULL
, posts_text
}, /* Mt */
257 { NULL
, posts_wline
}, /* Brq */
258 { NULL
, NULL
}, /* Bro */
259 { NULL
, NULL
}, /* Brc */
260 { NULL
, posts_text
}, /* %C */
261 { NULL
, NULL
}, /* Es */
262 { NULL
, NULL
}, /* En */
263 { NULL
, NULL
}, /* Dx */
264 { NULL
, posts_text
}, /* %Q */
265 { NULL
, posts_notext
}, /* br */
266 { NULL
, posts_sp
}, /* sp */
267 { NULL
, posts_text1
}, /* %U */
268 { NULL
, NULL
}, /* Ta */
273 mdoc_valid_pre(struct mdoc
*mdoc
, struct mdoc_node
*n
)
279 if (MDOC_TEXT
== n
->type
) {
283 return(check_text(mdoc
, line
, pos
, tp
));
286 if ( ! check_args(mdoc
, n
))
288 if (NULL
== mdoc_valids
[n
->tok
].pre
)
290 for (p
= mdoc_valids
[n
->tok
].pre
; *p
; p
++)
291 if ( ! (*p
)(mdoc
, n
))
298 mdoc_valid_post(struct mdoc
*mdoc
)
302 if (MDOC_VALID
& mdoc
->last
->flags
)
304 mdoc
->last
->flags
|= MDOC_VALID
;
306 if (MDOC_TEXT
== mdoc
->last
->type
)
308 if (MDOC_ROOT
== mdoc
->last
->type
)
309 return(post_root(mdoc
));
311 if (NULL
== mdoc_valids
[mdoc
->last
->tok
].post
)
313 for (p
= mdoc_valids
[mdoc
->last
->tok
].post
; *p
; p
++)
322 warn_count(struct mdoc
*m
, const char *k
,
323 int want
, const char *v
, int has
)
326 return(mdoc_vmsg(m
, MANDOCERR_ARGCOUNT
,
327 m
->last
->line
, m
->last
->pos
,
328 "%s %s %d (have %d)", v
, k
, want
, has
));
333 err_count(struct mdoc
*m
, const char *k
,
334 int want
, const char *v
, int has
)
337 mdoc_vmsg(m
, MANDOCERR_SYNTARGCOUNT
,
338 m
->last
->line
, m
->last
->pos
,
339 "%s %s %d (have %d)",
346 * Build these up with macros because they're basically the same check
347 * for different inequalities. Yes, this could be done with functions,
348 * but this is reasonable for now.
351 #define CHECK_CHILD_DEFN(lvl, name, ineq) \
353 lvl##_child_##name(struct mdoc *mdoc, const char *p, int sz) \
355 if (mdoc->last->nchild ineq sz) \
357 return(lvl##_count(mdoc, #ineq, sz, p, mdoc->last->nchild)); \
360 #define CHECK_BODY_DEFN(name, lvl, func, num) \
362 b##lvl##_##name(POST_ARGS) \
364 if (MDOC_BODY != mdoc->last->type) \
366 return(func(mdoc, "multi-line arguments", (num))); \
369 #define CHECK_ELEM_DEFN(name, lvl, func, num) \
371 e##lvl##_##name(POST_ARGS) \
373 assert(MDOC_ELEM == mdoc->last->type); \
374 return(func(mdoc, "line arguments", (num))); \
377 #define CHECK_HEAD_DEFN(name, lvl, func, num) \
379 h##lvl##_##name(POST_ARGS) \
381 if (MDOC_HEAD != mdoc->last->type) \
383 return(func(mdoc, "line arguments", (num))); \
387 CHECK_CHILD_DEFN(warn
, gt
, >) /* warn_child_gt() */
388 CHECK_CHILD_DEFN(err
, gt
, >) /* err_child_gt() */
389 CHECK_CHILD_DEFN(warn
, eq
, ==) /* warn_child_eq() */
390 CHECK_CHILD_DEFN(err
, eq
, ==) /* err_child_eq() */
391 CHECK_CHILD_DEFN(err
, lt
, <) /* err_child_lt() */
392 CHECK_CHILD_DEFN(warn
, lt
, <) /* warn_child_lt() */
393 CHECK_BODY_DEFN(ge1
, warn
, warn_child_gt
, 0) /* bwarn_ge1() */
394 CHECK_BODY_DEFN(ge1
, err
, err_child_gt
, 0) /* berr_ge1() */
395 CHECK_ELEM_DEFN(ge1
, warn
, warn_child_gt
, 0) /* ewarn_ge1() */
396 CHECK_ELEM_DEFN(eq1
, err
, err_child_eq
, 1) /* eerr_eq1() */
397 CHECK_ELEM_DEFN(le1
, err
, err_child_lt
, 2) /* eerr_le1() */
398 CHECK_ELEM_DEFN(eq0
, err
, err_child_eq
, 0) /* eerr_eq0() */
399 CHECK_ELEM_DEFN(ge1
, err
, err_child_gt
, 0) /* eerr_ge1() */
400 CHECK_HEAD_DEFN(eq0
, err
, err_child_eq
, 0) /* herr_eq0() */
401 CHECK_HEAD_DEFN(le1
, warn
, warn_child_lt
, 2) /* hwarn_le1() */
402 CHECK_HEAD_DEFN(ge1
, err
, err_child_gt
, 0) /* herr_ge1() */
403 CHECK_HEAD_DEFN(eq1
, warn
, warn_child_eq
, 1) /* hwarn_eq1() */
404 CHECK_HEAD_DEFN(eq0
, warn
, warn_child_eq
, 0) /* hwarn_eq0() */
408 check_stdarg(PRE_ARGS
)
411 if (n
->args
&& 1 == n
->args
->argc
)
412 if (MDOC_Std
== n
->args
->argv
[0].arg
)
414 return(mdoc_nmsg(mdoc
, n
, MANDOCERR_NOARGV
));
419 check_args(struct mdoc
*m
, struct mdoc_node
*n
)
426 assert(n
->args
->argc
);
427 for (i
= 0; i
< (int)n
->args
->argc
; i
++)
428 if ( ! check_argv(m
, n
, &n
->args
->argv
[i
]))
436 check_argv(struct mdoc
*m
, struct mdoc_node
*n
, struct mdoc_argv
*v
)
440 for (i
= 0; i
< (int)v
->sz
; i
++)
441 if ( ! check_text(m
, v
->line
, v
->pos
, v
->value
[i
]))
444 if (MDOC_Std
== v
->arg
) {
445 if (v
->sz
|| m
->meta
.name
)
447 if ( ! mdoc_nmsg(m
, n
, MANDOCERR_NONAME
))
456 check_text(struct mdoc
*m
, int ln
, int pos
, char *p
)
461 for ( ; *p
; p
++, pos
++) {
462 sz
= strcspn(p
, "\t\\");
471 if (MDOC_LITERAL
& m
->flags
)
473 if (mdoc_pmsg(m
, ln
, pos
, MANDOCERR_BADTAB
))
478 /* Check the special character. */
480 c
= mandoc_special(p
);
485 mdoc_pmsg(m
, ln
, pos
, MANDOCERR_BADESCAPE
);
493 check_parent(PRE_ARGS
, enum mdoct tok
, enum mdoc_type t
)
497 if ((MDOC_ROOT
== t
|| tok
== n
->parent
->tok
) &&
498 (t
== n
->parent
->type
))
501 mdoc_vmsg(mdoc
, MANDOCERR_SYNTCHILD
,
502 n
->line
, n
->pos
, "want parent %s",
503 MDOC_ROOT
== t
? "<root>" :
504 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
;
538 struct mdoc_node
*np
;
540 if (MDOC_BLOCK
!= n
->type
) {
541 if (ENDBODY_NOT
!= n
->end
) {
543 np
= n
->pending
->parent
;
548 assert(MDOC_BLOCK
== np
->type
);
549 assert(MDOC_Bl
== np
->tok
);
551 n
->data
.Bl
= np
->data
.Bl
;
556 * First figure out which kind of list to use: bind ourselves to
557 * the first mentioned list type and warn about any remaining
558 * ones. If we find no list type, we default to LIST_item.
561 assert(NULL
== n
->data
.Bl
);
562 n
->data
.Bl
= mandoc_calloc(1, sizeof(struct mdoc_bl
));
565 for (i
= 0; n
->args
&& i
< (int)n
->args
->argc
; i
++) {
569 switch (n
->args
->argv
[i
].arg
) {
570 /* Set list types. */
604 /* Set list arguments. */
606 dup
= n
->data
.Bl
->comp
;
610 dup
= (NULL
!= n
->data
.Bl
->width
);
611 width
= n
->args
->argv
[i
].value
[0];
614 /* NB: this can be empty! */
615 if (n
->args
->argv
[i
].sz
) {
616 offs
= n
->args
->argv
[i
].value
[0];
617 dup
= (NULL
!= n
->data
.Bl
->offs
);
620 if ( ! mdoc_nmsg(mdoc
, n
, MANDOCERR_IGNARGV
))
627 /* Check: duplicate auxiliary arguments. */
629 if (dup
&& ! mdoc_nmsg(mdoc
, n
, MANDOCERR_ARGVREP
))
633 n
->data
.Bl
->comp
= comp
;
635 n
->data
.Bl
->offs
= offs
;
637 n
->data
.Bl
->width
= width
;
639 /* Check: multiple list types. */
641 if (LIST__NONE
!= lt
&& n
->data
.Bl
->type
!= LIST__NONE
)
642 if ( ! mdoc_nmsg(mdoc
, n
, MANDOCERR_LISTREP
))
645 /* Assign list type. */
647 if (LIST__NONE
!= lt
&& n
->data
.Bl
->type
== LIST__NONE
) {
648 n
->data
.Bl
->type
= lt
;
649 /* Set column information, too. */
650 if (LIST_column
== lt
) {
653 n
->data
.Bl
->cols
= (const char **)
654 n
->args
->argv
[i
].value
;
658 /* The list type should come first. */
660 if (n
->data
.Bl
->type
== LIST__NONE
)
661 if (n
->data
.Bl
->width
||
664 if ( ! mdoc_nmsg(mdoc
, n
, MANDOCERR_LISTFIRST
))
670 /* Allow lists to default to LIST_item. */
672 if (LIST__NONE
== n
->data
.Bl
->type
) {
673 if ( ! mdoc_nmsg(mdoc
, n
, MANDOCERR_LISTTYPE
))
675 n
->data
.Bl
->type
= LIST_item
;
679 * Validate the width field. Some list types don't need width
680 * types and should be warned about them. Others should have it
681 * and must also be warned.
684 switch (n
->data
.Bl
->type
) {
686 if (n
->data
.Bl
->width
)
688 if (mdoc_nmsg(mdoc
, n
, MANDOCERR_NOWIDTHARG
))
700 if (NULL
== n
->data
.Bl
->width
)
702 if (mdoc_nmsg(mdoc
, n
, MANDOCERR_WIDTHARG
))
719 struct mdoc_node
*np
;
721 if (MDOC_BLOCK
!= n
->type
) {
722 if (ENDBODY_NOT
!= n
->end
) {
724 np
= n
->pending
->parent
;
729 assert(MDOC_BLOCK
== np
->type
);
730 assert(MDOC_Bd
== np
->tok
);
732 n
->data
.Bd
= np
->data
.Bd
;
736 assert(NULL
== n
->data
.Bd
);
737 n
->data
.Bd
= mandoc_calloc(1, sizeof(struct mdoc_bd
));
740 for (i
= 0; n
->args
&& i
< (int)n
->args
->argc
; i
++) {
745 switch (n
->args
->argv
[i
].arg
) {
752 case (MDOC_Unfilled
):
762 mdoc_nmsg(mdoc
, n
, MANDOCERR_BADDISP
);
765 /* NB: this can be empty! */
766 if (n
->args
->argv
[i
].sz
) {
767 offs
= n
->args
->argv
[i
].value
[0];
768 dup
= (NULL
!= n
->data
.Bd
->offs
);
771 if ( ! mdoc_nmsg(mdoc
, n
, MANDOCERR_IGNARGV
))
776 dup
= n
->data
.Bd
->comp
;
783 /* Check whether we have duplicates. */
785 if (dup
&& ! mdoc_nmsg(mdoc
, n
, MANDOCERR_ARGVREP
))
788 /* Make our auxiliary assignments. */
791 n
->data
.Bd
->offs
= offs
;
793 n
->data
.Bd
->comp
= comp
;
795 /* Check whether a type has already been assigned. */
797 if (DISP__NONE
!= dt
&& n
->data
.Bd
->type
!= DISP__NONE
)
798 if ( ! mdoc_nmsg(mdoc
, n
, MANDOCERR_DISPREP
))
801 /* Make our type assignment. */
803 if (DISP__NONE
!= dt
&& n
->data
.Bd
->type
== DISP__NONE
)
804 n
->data
.Bd
->type
= dt
;
807 if (DISP__NONE
== n
->data
.Bd
->type
) {
808 if ( ! mdoc_nmsg(mdoc
, n
, MANDOCERR_DISPTYPE
))
810 n
->data
.Bd
->type
= DISP_ragged
;
821 if (MDOC_BLOCK
!= n
->type
)
823 return(check_parent(mdoc
, n
, MDOC_Sh
, MDOC_BODY
));
831 if (MDOC_BLOCK
!= n
->type
)
834 mdoc
->regs
->regs
[(int)REG_nS
].set
= 0;
835 return(check_parent(mdoc
, n
, MDOC_MAX
, MDOC_ROOT
));
843 if (MDOC_BLOCK
!= n
->type
)
846 * FIXME: this can probably be lifted if we make the It into
847 * something else on-the-fly?
849 return(check_parent(mdoc
, n
, MDOC_Bl
, MDOC_BODY
));
859 if (n
->args
->argc
> 1)
860 if ( ! mdoc_nmsg(mdoc
, n
, MANDOCERR_ARGCOUNT
))
863 if (MDOC_Split
== n
->args
->argv
[0].arg
)
864 n
->data
.An
.auth
= AUTH_split
;
865 else if (MDOC_Nosplit
== n
->args
->argv
[0].arg
)
866 n
->data
.An
.auth
= AUTH_nosplit
;
878 return(check_stdarg(mdoc
, n
));
885 const struct mdoc_node
*nn
;
888 if (NULL
!= (nn
= mdoc
->last
->child
))
889 for (p
= nn
->string
; *p
; p
++) {
890 if (toupper((u_char
)*p
) == *p
)
892 if ( ! mdoc_nmsg(mdoc
, nn
, MANDOCERR_UPPERCASE
))
905 if (0 == mdoc
->meta
.date
|| mdoc
->meta
.os
)
906 if ( ! mdoc_nmsg(mdoc
, n
, MANDOCERR_PROLOGOOO
))
908 if (mdoc
->meta
.title
)
909 if ( ! mdoc_nmsg(mdoc
, n
, MANDOCERR_PROLOGREP
))
919 if (NULL
== mdoc
->meta
.title
|| 0 == mdoc
->meta
.date
)
920 if ( ! mdoc_nmsg(mdoc
, n
, MANDOCERR_PROLOGOOO
))
923 if ( ! mdoc_nmsg(mdoc
, n
, MANDOCERR_PROLOGREP
))
933 if (mdoc
->meta
.title
|| mdoc
->meta
.os
)
934 if ( ! mdoc_nmsg(mdoc
, n
, MANDOCERR_PROLOGOOO
))
937 if ( ! mdoc_nmsg(mdoc
, n
, MANDOCERR_PROLOGREP
))
946 struct mdoc_node
*np
;
950 * Unlike other data pointers, these are "housed" by the HEAD
951 * element, which contains the goods.
954 if (MDOC_HEAD
!= mdoc
->last
->type
) {
955 if (ENDBODY_NOT
!= mdoc
->last
->end
) {
956 assert(mdoc
->last
->pending
);
957 np
= mdoc
->last
->pending
->parent
->head
;
958 } else if (MDOC_BLOCK
!= mdoc
->last
->type
) {
959 np
= mdoc
->last
->parent
->head
;
961 np
= mdoc
->last
->head
;
964 assert(MDOC_HEAD
== np
->type
);
965 assert(MDOC_Bf
== np
->tok
);
967 mdoc
->last
->data
.Bf
= np
->data
.Bf
;
972 assert(MDOC_BLOCK
== np
->parent
->type
);
973 assert(MDOC_Bf
== np
->parent
->tok
);
974 np
->data
.Bf
= mandoc_calloc(1, sizeof(struct mdoc_bf
));
977 * Cannot have both argument and parameter.
978 * If neither is specified, let it through with a warning.
981 if (np
->parent
->args
&& np
->child
) {
982 mdoc_nmsg(mdoc
, np
, MANDOCERR_SYNTARGVCOUNT
);
984 } else if (NULL
== np
->parent
->args
&& NULL
== np
->child
)
985 return(mdoc_nmsg(mdoc
, np
, MANDOCERR_FONTTYPE
));
987 /* Extract argument into data. */
989 if (np
->parent
->args
) {
990 arg
= np
->parent
->args
->argv
[0].arg
;
991 if (MDOC_Emphasis
== arg
)
992 np
->data
.Bf
->font
= FONT_Em
;
993 else if (MDOC_Literal
== arg
)
994 np
->data
.Bf
->font
= FONT_Li
;
995 else if (MDOC_Symbolic
== arg
)
996 np
->data
.Bf
->font
= FONT_Sy
;
1002 /* Extract parameter into data. */
1004 if (0 == strcmp(np
->child
->string
, "Em"))
1005 np
->data
.Bf
->font
= FONT_Em
;
1006 else if (0 == strcmp(np
->child
->string
, "Li"))
1007 np
->data
.Bf
->font
= FONT_Li
;
1008 else if (0 == strcmp(np
->child
->string
, "Sy"))
1009 np
->data
.Bf
->font
= FONT_Sy
;
1010 else if ( ! mdoc_nmsg(mdoc
, np
, MANDOCERR_FONTTYPE
))
1021 if (mdoc_a2lib(mdoc
->last
->child
->string
))
1023 return(mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_BADLIB
));
1028 post_eoln(POST_ARGS
)
1031 if (NULL
== mdoc
->last
->child
)
1033 return(mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_ARGSLOST
));
1040 const struct mdoc_node
*n
;
1043 * The Vt macro comes in both ELEM and BLOCK form, both of which
1044 * have different syntaxes (yet more context-sensitive
1045 * behaviour). ELEM types must have a child; BLOCK types,
1046 * specifically the BODY, should only have TEXT children.
1049 if (MDOC_ELEM
== mdoc
->last
->type
)
1050 return(eerr_ge1(mdoc
));
1051 if (MDOC_BODY
!= mdoc
->last
->type
)
1054 for (n
= mdoc
->last
->child
; n
; n
= n
->next
)
1055 if (MDOC_TEXT
!= n
->type
)
1056 if ( ! mdoc_nmsg(mdoc
, n
, MANDOCERR_CHILD
))
1067 if (mdoc
->last
->child
)
1069 if (mdoc
->meta
.name
)
1071 return(mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_NONAME
));
1079 if (NULL
== mdoc
->last
->child
)
1081 assert(MDOC_TEXT
== mdoc
->last
->child
->type
);
1082 if (mdoc_a2att(mdoc
->last
->child
->string
))
1084 return(mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_BADATT
));
1091 struct mdoc_node
*np
;
1094 if (AUTH__NONE
!= np
->data
.An
.auth
&& np
->child
)
1095 return(mdoc_nmsg(mdoc
, np
, MANDOCERR_ARGCOUNT
));
1096 if (AUTH__NONE
!= np
->data
.An
.auth
|| np
->child
)
1098 return(mdoc_nmsg(mdoc
, np
, MANDOCERR_NOARGS
));
1107 struct mdoc_node
*n
, *c
;
1110 if (MDOC_BLOCK
!= mdoc
->last
->type
)
1113 n
= mdoc
->last
->parent
->parent
;
1115 lt
= n
->data
.Bl
->type
;
1117 if (LIST__NONE
== lt
) {
1118 mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_LISTTYPE
);
1124 if (mdoc
->last
->head
->child
)
1126 /* FIXME: give this a dummy value. */
1127 if ( ! mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_NOARGS
))
1137 if (NULL
== mdoc
->last
->head
->child
)
1138 if ( ! mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_NOARGS
))
1148 if (NULL
== mdoc
->last
->body
->child
)
1149 if ( ! mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_NOBODY
))
1153 if (mdoc
->last
->head
->child
)
1154 if ( ! mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_ARGSLOST
))
1158 cols
= (int)n
->data
.Bl
->ncols
;
1160 assert(NULL
== mdoc
->last
->head
->child
);
1162 if (NULL
== mdoc
->last
->body
->child
)
1163 if ( ! mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_NOBODY
))
1166 for (i
= 0, c
= mdoc
->last
->child
; c
; c
= c
->next
)
1167 if (MDOC_BODY
== c
->type
)
1171 er
= MANDOCERR_ARGCOUNT
;
1172 else if (i
== cols
|| i
== cols
+ 1)
1175 er
= MANDOCERR_SYNTARGCOUNT
;
1177 rc
= mdoc_vmsg(mdoc
, er
,
1178 mdoc
->last
->line
, mdoc
->last
->pos
,
1179 "columns == %d (have %d)", cols
, i
);
1190 post_bl_head(POST_ARGS
)
1193 struct mdoc_node
*n
;
1195 assert(mdoc
->last
->parent
);
1196 n
= mdoc
->last
->parent
;
1198 if (LIST_column
== n
->data
.Bl
->type
) {
1199 if (n
->data
.Bl
->ncols
&& mdoc
->last
->nchild
) {
1200 mdoc_nmsg(mdoc
, n
, MANDOCERR_COLUMNS
);
1206 if (0 == (i
= mdoc
->last
->nchild
))
1208 return(warn_count(mdoc
, "==", 0, "line arguments", i
));
1215 struct mdoc_node
*n
;
1217 if (MDOC_HEAD
== mdoc
->last
->type
)
1218 return(post_bl_head(mdoc
));
1219 if (MDOC_BODY
!= mdoc
->last
->type
)
1221 if (NULL
== mdoc
->last
->child
)
1225 * We only allow certain children of `Bl'. This is usually on
1226 * `It', but apparently `Sm' occurs here and there, so we let
1227 * that one through, too.
1231 for (n
= mdoc
->last
->child
; n
; n
= n
->next
) {
1232 if (MDOC_BLOCK
== n
->type
&& MDOC_It
== n
->tok
)
1234 if (MDOC_Sm
== n
->tok
)
1236 mdoc_nmsg(mdoc
, n
, MANDOCERR_SYNTCHILD
);
1245 ebool(struct mdoc
*mdoc
)
1247 struct mdoc_node
*n
;
1250 for (n
= mdoc
->last
->child
; n
; n
= n
->next
) {
1251 if (MDOC_TEXT
!= n
->type
)
1253 if (0 == strcmp(n
->string
, "on"))
1255 if (0 == strcmp(n
->string
, "off"))
1262 return(mdoc_nmsg(mdoc
, n
, MANDOCERR_BADBOOL
));
1267 post_root(POST_ARGS
)
1270 if (NULL
== mdoc
->first
->child
)
1271 mdoc_nmsg(mdoc
, mdoc
->first
, MANDOCERR_NODOCBODY
);
1272 else if ( ! (MDOC_PBODY
& mdoc
->flags
))
1273 mdoc_nmsg(mdoc
, mdoc
->first
, MANDOCERR_NODOCPROLOG
);
1274 else if (MDOC_BLOCK
!= mdoc
->first
->child
->type
)
1275 mdoc_nmsg(mdoc
, mdoc
->first
, MANDOCERR_NODOCBODY
);
1276 else if (MDOC_Sh
!= mdoc
->first
->child
->tok
)
1277 mdoc_nmsg(mdoc
, mdoc
->first
, MANDOCERR_NODOCBODY
);
1289 if (mdoc_a2st(mdoc
->last
->child
->string
))
1291 return(mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_BADSTANDARD
));
1298 struct mdoc_node
*nn
;
1300 if (MDOC_BODY
!= mdoc
->last
->type
)
1303 for (nn
= mdoc
->last
->child
; nn
; nn
= nn
->next
)
1334 mdoc_nmsg(mdoc
, nn
, MANDOCERR_SYNTCHILD
);
1346 if (MDOC_HEAD
== mdoc
->last
->type
)
1347 return(post_sh_head(mdoc
));
1348 if (MDOC_BODY
== mdoc
->last
->type
)
1349 return(post_sh_body(mdoc
));
1356 post_sh_body(POST_ARGS
)
1358 struct mdoc_node
*n
;
1360 if (SEC_NAME
!= mdoc
->lastsec
)
1364 * Warn if the NAME section doesn't contain the `Nm' and `Nd'
1365 * macros (can have multiple `Nm' and one `Nd'). Note that the
1366 * children of the BODY declaration can also be "text".
1369 if (NULL
== (n
= mdoc
->last
->child
))
1370 return(mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_BADNAMESEC
));
1372 for ( ; n
&& n
->next
; n
= n
->next
) {
1373 if (MDOC_ELEM
== n
->type
&& MDOC_Nm
== n
->tok
)
1375 if (MDOC_TEXT
== n
->type
)
1377 if ( ! mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_BADNAMESEC
))
1382 if (MDOC_BLOCK
== n
->type
&& MDOC_Nd
== n
->tok
)
1384 return(mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_BADNAMESEC
));
1389 post_sh_head(POST_ARGS
)
1393 const struct mdoc_node
*n
;
1396 * Process a new section. Sections are either "named" or
1397 * "custom"; custom sections are user-defined, while named ones
1398 * usually follow a conventional order and may only appear in
1399 * certain manual sections.
1405 * FIXME: yes, these can use a dynamic buffer, but I don't do so
1406 * in the interests of simplicity.
1409 for (n
= mdoc
->last
->child
; n
; n
= n
->next
) {
1410 /* XXX - copied from compact(). */
1411 assert(MDOC_TEXT
== n
->type
);
1413 if (strlcat(buf
, n
->string
, BUFSIZ
) >= BUFSIZ
) {
1414 mdoc_nmsg(mdoc
, n
, MANDOCERR_MEM
);
1417 if (NULL
== n
->next
)
1419 if (strlcat(buf
, " ", BUFSIZ
) >= BUFSIZ
) {
1420 mdoc_nmsg(mdoc
, n
, MANDOCERR_MEM
);
1425 sec
= mdoc_str2sec(buf
);
1428 * Check: NAME should always be first, CUSTOM has no roles,
1429 * non-CUSTOM has a conventional order to be followed.
1432 if (SEC_NAME
!= sec
&& SEC_NONE
== mdoc
->lastnamed
)
1433 if ( ! mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_NAMESECFIRST
))
1436 if (SEC_CUSTOM
== sec
)
1439 if (sec
== mdoc
->lastnamed
)
1440 if ( ! mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_SECREP
))
1443 if (sec
< mdoc
->lastnamed
)
1444 if ( ! mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_SECOOO
))
1448 * Check particular section/manual conventions. LIBRARY can
1449 * only occur in manual section 2, 3, and 9.
1454 assert(mdoc
->meta
.msec
);
1455 if (*mdoc
->meta
.msec
== '2')
1457 if (*mdoc
->meta
.msec
== '3')
1459 if (*mdoc
->meta
.msec
== '9')
1461 return(mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_SECMSEC
));