]>
git.cameronkatri.com Git - mandoc.git/blob - mdoc_validate.c
e9f04be1fdb187e46b94f24d6ec7c5817e51d48e
1 /* $Id: mdoc_validate.c,v 1.131 2010/11/30 12:35:10 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
54 typedef int (*v_pre
)(PRE_ARGS
);
55 typedef int (*v_post
)(POST_ARGS
);
62 static int check_count(struct mdoc
*, enum mdoc_type
,
63 enum check_lvl
, enum check_ineq
, int);
64 static int check_parent(PRE_ARGS
, enum mdoct
, enum mdoc_type
);
65 static int check_stdarg(PRE_ARGS
);
66 static int check_text(struct mdoc
*, int, int, char *);
67 static int check_argv(struct mdoc
*,
68 struct mdoc_node
*, struct mdoc_argv
*);
69 static int check_args(struct mdoc
*, struct mdoc_node
*);
71 static int ebool(POST_ARGS
);
72 static int berr_ge1(POST_ARGS
);
73 static int bwarn_ge1(POST_ARGS
);
74 static int eerr_eq0(POST_ARGS
);
75 static int eerr_eq1(POST_ARGS
);
76 static int eerr_ge1(POST_ARGS
);
77 static int eerr_le1(POST_ARGS
);
78 static int ewarn_eq0(POST_ARGS
);
79 static int ewarn_ge1(POST_ARGS
);
80 static int herr_eq0(POST_ARGS
);
81 static int herr_ge1(POST_ARGS
);
82 static int hwarn_eq0(POST_ARGS
);
83 static int hwarn_eq1(POST_ARGS
);
84 static int hwarn_le1(POST_ARGS
);
86 static int post_an(POST_ARGS
);
87 static int post_at(POST_ARGS
);
88 static int post_bf(POST_ARGS
);
89 static int post_bl(POST_ARGS
);
90 static int post_bl_block(POST_ARGS
);
91 static int post_bl_block_width(POST_ARGS
);
92 static int post_bl_block_tag(POST_ARGS
);
93 static int post_bl_head(POST_ARGS
);
94 static int post_defaults(POST_ARGS
);
95 static int post_literal(POST_ARGS
);
96 static int post_eoln(POST_ARGS
);
97 static int post_dt(POST_ARGS
);
98 static int post_it(POST_ARGS
);
99 static int post_lb(POST_ARGS
);
100 static int post_nm(POST_ARGS
);
101 static int post_root(POST_ARGS
);
102 static int post_rs(POST_ARGS
);
103 static int post_sh(POST_ARGS
);
104 static int post_sh_body(POST_ARGS
);
105 static int post_sh_head(POST_ARGS
);
106 static int post_st(POST_ARGS
);
107 static int post_vt(POST_ARGS
);
108 static int pre_an(PRE_ARGS
);
109 static int pre_bd(PRE_ARGS
);
110 static int pre_bl(PRE_ARGS
);
111 static int pre_dd(PRE_ARGS
);
112 static int pre_display(PRE_ARGS
);
113 static int pre_dt(PRE_ARGS
);
114 static int pre_it(PRE_ARGS
);
115 static int pre_literal(PRE_ARGS
);
116 static int pre_os(PRE_ARGS
);
117 static int pre_par(PRE_ARGS
);
118 static int pre_rv(PRE_ARGS
);
119 static int pre_sh(PRE_ARGS
);
120 static int pre_ss(PRE_ARGS
);
122 static v_post posts_an
[] = { post_an
, NULL
};
123 static v_post posts_at
[] = { post_at
, post_defaults
, NULL
};
124 static v_post posts_bd
[] = { post_literal
, hwarn_eq0
, bwarn_ge1
, NULL
};
125 static v_post posts_bf
[] = { hwarn_le1
, post_bf
, NULL
};
126 static v_post posts_bk
[] = { hwarn_eq0
, bwarn_ge1
, NULL
};
127 static v_post posts_bl
[] = { bwarn_ge1
, post_bl
, NULL
};
128 static v_post posts_bool
[] = { eerr_eq1
, ebool
, NULL
};
129 static v_post posts_eoln
[] = { post_eoln
, NULL
};
130 static v_post posts_defaults
[] = { post_defaults
, NULL
};
131 static v_post posts_dl
[] = { post_literal
, bwarn_ge1
, herr_eq0
, NULL
};
132 static v_post posts_dt
[] = { post_dt
, NULL
};
133 static v_post posts_fo
[] = { hwarn_eq1
, bwarn_ge1
, NULL
};
134 static v_post posts_it
[] = { post_it
, NULL
};
135 static v_post posts_lb
[] = { eerr_eq1
, post_lb
, NULL
};
136 static v_post posts_nd
[] = { berr_ge1
, NULL
};
137 static v_post posts_nm
[] = { post_nm
, NULL
};
138 static v_post posts_notext
[] = { ewarn_eq0
, NULL
};
139 static v_post posts_rs
[] = { berr_ge1
, herr_eq0
, post_rs
, NULL
};
140 static v_post posts_sh
[] = { herr_ge1
, bwarn_ge1
, post_sh
, NULL
};
141 static v_post posts_sp
[] = { eerr_le1
, NULL
};
142 static v_post posts_ss
[] = { herr_ge1
, NULL
};
143 static v_post posts_st
[] = { eerr_eq1
, post_st
, NULL
};
144 static v_post posts_text
[] = { eerr_ge1
, NULL
};
145 static v_post posts_text1
[] = { eerr_eq1
, NULL
};
146 static v_post posts_vt
[] = { post_vt
, NULL
};
147 static v_post posts_wline
[] = { bwarn_ge1
, herr_eq0
, NULL
};
148 static v_post posts_wtext
[] = { ewarn_ge1
, NULL
};
149 static v_pre pres_an
[] = { pre_an
, NULL
};
150 static v_pre pres_bd
[] = { pre_display
, pre_bd
, pre_literal
, pre_par
, NULL
};
151 static v_pre pres_bl
[] = { pre_bl
, pre_par
, NULL
};
152 static v_pre pres_d1
[] = { pre_display
, NULL
};
153 static v_pre pres_dl
[] = { pre_literal
, pre_display
, NULL
};
154 static v_pre pres_dd
[] = { pre_dd
, NULL
};
155 static v_pre pres_dt
[] = { pre_dt
, NULL
};
156 static v_pre pres_er
[] = { NULL
, NULL
};
157 static v_pre pres_ex
[] = { NULL
, NULL
};
158 static v_pre pres_fd
[] = { NULL
, NULL
};
159 static v_pre pres_it
[] = { pre_it
, NULL
};
160 static v_pre pres_os
[] = { pre_os
, NULL
};
161 static v_pre pres_pp
[] = { pre_par
, NULL
};
162 static v_pre pres_rv
[] = { pre_rv
, NULL
};
163 static v_pre pres_sh
[] = { pre_sh
, NULL
};
164 static v_pre pres_ss
[] = { pre_ss
, NULL
};
166 const struct valids mdoc_valids
[MDOC_MAX
] = {
167 { NULL
, NULL
}, /* Ap */
168 { pres_dd
, posts_wtext
}, /* Dd */
169 { pres_dt
, posts_dt
}, /* Dt */
170 { pres_os
, NULL
}, /* Os */
171 { pres_sh
, posts_sh
}, /* Sh */
172 { pres_ss
, posts_ss
}, /* Ss */
173 { pres_pp
, posts_notext
}, /* Pp */
174 { pres_d1
, posts_wline
}, /* D1 */
175 { pres_dl
, posts_dl
}, /* Dl */
176 { pres_bd
, posts_bd
}, /* Bd */
177 { NULL
, NULL
}, /* Ed */
178 { pres_bl
, posts_bl
}, /* Bl */
179 { NULL
, NULL
}, /* El */
180 { pres_it
, posts_it
}, /* It */
181 { NULL
, posts_text
}, /* Ad */
182 { pres_an
, posts_an
}, /* An */
183 { NULL
, posts_defaults
}, /* Ar */
184 { NULL
, posts_text
}, /* Cd */
185 { NULL
, NULL
}, /* Cm */
186 { NULL
, NULL
}, /* Dv */
187 { pres_er
, posts_text
}, /* Er */
188 { NULL
, NULL
}, /* Ev */
189 { pres_ex
, NULL
}, /* Ex */
190 { NULL
, NULL
}, /* Fa */
191 { pres_fd
, posts_wtext
}, /* Fd */
192 { NULL
, NULL
}, /* Fl */
193 { NULL
, posts_text
}, /* Fn */
194 { NULL
, posts_wtext
}, /* Ft */
195 { NULL
, posts_text
}, /* Ic */
196 { NULL
, posts_text1
}, /* In */
197 { NULL
, posts_defaults
}, /* Li */
198 { NULL
, posts_nd
}, /* Nd */
199 { NULL
, posts_nm
}, /* Nm */
200 { NULL
, posts_wline
}, /* Op */
201 { NULL
, NULL
}, /* Ot */
202 { NULL
, posts_defaults
}, /* Pa */
203 { pres_rv
, NULL
}, /* Rv */
204 { NULL
, posts_st
}, /* St */
205 { NULL
, NULL
}, /* Va */
206 { NULL
, posts_vt
}, /* Vt */
207 { NULL
, posts_wtext
}, /* Xr */
208 { NULL
, posts_text
}, /* %A */
209 { NULL
, posts_text
}, /* %B */ /* FIXME: can be used outside Rs/Re. */
210 { NULL
, posts_text
}, /* %D */ /* FIXME: check date with mandoc_a2time(). */
211 { NULL
, posts_text
}, /* %I */
212 { NULL
, posts_text
}, /* %J */
213 { NULL
, posts_text
}, /* %N */
214 { NULL
, posts_text
}, /* %O */
215 { NULL
, posts_text
}, /* %P */
216 { NULL
, posts_text
}, /* %R */
217 { NULL
, posts_text
}, /* %T */ /* FIXME: can be used outside Rs/Re. */
218 { NULL
, posts_text
}, /* %V */
219 { NULL
, NULL
}, /* Ac */
220 { NULL
, NULL
}, /* Ao */
221 { NULL
, posts_wline
}, /* Aq */
222 { NULL
, posts_at
}, /* At */
223 { NULL
, NULL
}, /* Bc */
224 { NULL
, posts_bf
}, /* Bf */
225 { NULL
, NULL
}, /* Bo */
226 { NULL
, posts_wline
}, /* Bq */
227 { NULL
, NULL
}, /* Bsx */
228 { NULL
, NULL
}, /* Bx */
229 { NULL
, posts_bool
}, /* Db */
230 { NULL
, NULL
}, /* Dc */
231 { NULL
, NULL
}, /* Do */
232 { NULL
, posts_wline
}, /* Dq */
233 { NULL
, NULL
}, /* Ec */
234 { NULL
, NULL
}, /* Ef */
235 { NULL
, NULL
}, /* Em */
236 { NULL
, NULL
}, /* Eo */
237 { NULL
, NULL
}, /* Fx */
238 { NULL
, posts_text
}, /* Ms */
239 { NULL
, posts_notext
}, /* No */
240 { NULL
, posts_notext
}, /* Ns */
241 { NULL
, NULL
}, /* Nx */
242 { NULL
, NULL
}, /* Ox */
243 { NULL
, NULL
}, /* Pc */
244 { NULL
, posts_text1
}, /* Pf */
245 { NULL
, NULL
}, /* Po */
246 { NULL
, posts_wline
}, /* Pq */
247 { NULL
, NULL
}, /* Qc */
248 { NULL
, posts_wline
}, /* Ql */
249 { NULL
, NULL
}, /* Qo */
250 { NULL
, posts_wline
}, /* Qq */
251 { NULL
, NULL
}, /* Re */
252 { NULL
, posts_rs
}, /* Rs */
253 { NULL
, NULL
}, /* Sc */
254 { NULL
, NULL
}, /* So */
255 { NULL
, posts_wline
}, /* Sq */
256 { NULL
, posts_bool
}, /* Sm */
257 { NULL
, posts_text
}, /* Sx */
258 { NULL
, posts_text
}, /* Sy */
259 { NULL
, posts_text
}, /* Tn */
260 { NULL
, NULL
}, /* Ux */
261 { NULL
, NULL
}, /* Xc */
262 { NULL
, NULL
}, /* Xo */
263 { NULL
, posts_fo
}, /* Fo */
264 { NULL
, NULL
}, /* Fc */
265 { NULL
, NULL
}, /* Oo */
266 { NULL
, NULL
}, /* Oc */
267 { NULL
, posts_bk
}, /* Bk */
268 { NULL
, NULL
}, /* Ek */
269 { NULL
, posts_eoln
}, /* Bt */
270 { NULL
, NULL
}, /* Hf */
271 { NULL
, NULL
}, /* Fr */
272 { NULL
, posts_eoln
}, /* Ud */
273 { NULL
, posts_lb
}, /* Lb */
274 { NULL
, posts_notext
}, /* Lp */
275 { NULL
, posts_text
}, /* Lk */
276 { NULL
, posts_defaults
}, /* Mt */
277 { NULL
, posts_wline
}, /* Brq */
278 { NULL
, NULL
}, /* Bro */
279 { NULL
, NULL
}, /* Brc */
280 { NULL
, posts_text
}, /* %C */
281 { NULL
, NULL
}, /* Es */
282 { NULL
, NULL
}, /* En */
283 { NULL
, NULL
}, /* Dx */
284 { NULL
, posts_text
}, /* %Q */
285 { NULL
, posts_notext
}, /* br */
286 { pres_pp
, posts_sp
}, /* sp */
287 { NULL
, posts_text1
}, /* %U */
288 { NULL
, NULL
}, /* Ta */
291 #define RSORD_MAX 14 /* Number of `Rs' blocks. */
293 static const enum mdoct rsord
[RSORD_MAX
] = {
312 mdoc_valid_pre(struct mdoc
*mdoc
, struct mdoc_node
*n
)
318 if (MDOC_TEXT
== n
->type
) {
322 return(check_text(mdoc
, line
, pos
, tp
));
325 if ( ! check_args(mdoc
, n
))
327 if (NULL
== mdoc_valids
[n
->tok
].pre
)
329 for (p
= mdoc_valids
[n
->tok
].pre
; *p
; p
++)
330 if ( ! (*p
)(mdoc
, n
))
337 mdoc_valid_post(struct mdoc
*mdoc
)
341 if (MDOC_VALID
& mdoc
->last
->flags
)
343 mdoc
->last
->flags
|= MDOC_VALID
;
345 if (MDOC_TEXT
== mdoc
->last
->type
)
347 if (MDOC_ROOT
== mdoc
->last
->type
)
348 return(post_root(mdoc
));
350 if (NULL
== mdoc_valids
[mdoc
->last
->tok
].post
)
352 for (p
= mdoc_valids
[mdoc
->last
->tok
].post
; *p
; p
++)
360 check_count(struct mdoc
*m
, enum mdoc_type type
,
361 enum check_lvl lvl
, enum check_ineq ineq
, int val
)
365 if (m
->last
->type
!= type
)
371 if (m
->last
->nchild
< val
)
376 if (m
->last
->nchild
> val
)
381 if (val
== m
->last
->nchild
)
386 if (CHECK_WARN
== lvl
) {
387 return(mdoc_vmsg(m
, MANDOCERR_ARGCOUNT
,
388 m
->last
->line
, m
->last
->pos
,
389 "want %s%d children (have %d)",
390 p
, val
, m
->last
->nchild
));
393 return(mdoc_vmsg(m
, MANDOCERR_ARGCOUNT
,
394 m
->last
->line
, m
->last
->pos
,
395 "require %s%d children (have %d)",
396 p
, val
, m
->last
->nchild
));
403 return(check_count(mdoc
, MDOC_BODY
, CHECK_FATAL
, CHECK_GT
, 0));
409 return(check_count(mdoc
, MDOC_BODY
, CHECK_WARN
, CHECK_GT
, 0));
415 return(check_count(mdoc
, MDOC_ELEM
, CHECK_FATAL
, CHECK_EQ
, 0));
421 return(check_count(mdoc
, MDOC_ELEM
, CHECK_FATAL
, CHECK_EQ
, 1));
427 return(check_count(mdoc
, MDOC_ELEM
, CHECK_FATAL
, CHECK_GT
, 0));
433 return(check_count(mdoc
, MDOC_ELEM
, CHECK_FATAL
, CHECK_LT
, 2));
439 return(check_count(mdoc
, MDOC_ELEM
, CHECK_WARN
, CHECK_EQ
, 0));
445 return(check_count(mdoc
, MDOC_ELEM
, CHECK_WARN
, CHECK_GT
, 0));
451 return(check_count(mdoc
, MDOC_HEAD
, CHECK_FATAL
, CHECK_EQ
, 0));
457 return(check_count(mdoc
, MDOC_HEAD
, CHECK_FATAL
, CHECK_GT
, 0));
463 return(check_count(mdoc
, MDOC_HEAD
, CHECK_WARN
, CHECK_EQ
, 0));
469 return(check_count(mdoc
, MDOC_HEAD
, CHECK_WARN
, CHECK_EQ
, 1));
475 return(check_count(mdoc
, MDOC_HEAD
, CHECK_WARN
, CHECK_LT
, 2));
480 check_stdarg(PRE_ARGS
)
483 if (n
->args
&& 1 == n
->args
->argc
)
484 if (MDOC_Std
== n
->args
->argv
[0].arg
)
486 return(mdoc_nmsg(mdoc
, n
, MANDOCERR_NOARGV
));
491 check_args(struct mdoc
*m
, struct mdoc_node
*n
)
498 assert(n
->args
->argc
);
499 for (i
= 0; i
< (int)n
->args
->argc
; i
++)
500 if ( ! check_argv(m
, n
, &n
->args
->argv
[i
]))
508 check_argv(struct mdoc
*m
, struct mdoc_node
*n
, struct mdoc_argv
*v
)
512 for (i
= 0; i
< (int)v
->sz
; i
++)
513 if ( ! check_text(m
, v
->line
, v
->pos
, v
->value
[i
]))
516 if (MDOC_Std
== v
->arg
) {
517 if (v
->sz
|| m
->meta
.name
)
519 if ( ! mdoc_nmsg(m
, n
, MANDOCERR_NONAME
))
528 check_text(struct mdoc
*m
, int ln
, int pos
, char *p
)
533 for ( ; *p
; p
++, pos
++) {
534 sz
= strcspn(p
, "\t\\");
543 if (MDOC_LITERAL
& m
->flags
)
545 if (mdoc_pmsg(m
, ln
, pos
, MANDOCERR_BADTAB
))
550 /* Check the special character. */
552 c
= mandoc_special(p
);
557 mdoc_pmsg(m
, ln
, pos
, MANDOCERR_BADESCAPE
);
565 check_parent(PRE_ARGS
, enum mdoct tok
, enum mdoc_type t
)
569 if ((MDOC_ROOT
== t
|| tok
== n
->parent
->tok
) &&
570 (t
== n
->parent
->type
))
573 mdoc_vmsg(mdoc
, MANDOCERR_SYNTCHILD
,
574 n
->line
, n
->pos
, "want parent %s",
575 MDOC_ROOT
== t
? "<root>" :
576 mdoc_macronames
[tok
]);
582 pre_display(PRE_ARGS
)
584 struct mdoc_node
*node
;
586 /* Display elements (`Bd', `D1'...) cannot be nested. */
588 if (MDOC_BLOCK
!= n
->type
)
592 for (node
= mdoc
->last
->parent
; node
; node
= node
->parent
)
593 if (MDOC_BLOCK
== node
->type
)
594 if (MDOC_Bd
== node
->tok
)
600 mdoc_nmsg(mdoc
, n
, MANDOCERR_NESTEDDISP
);
609 const char *offs
, *width
;
611 struct mdoc_node
*np
;
613 if (MDOC_BLOCK
!= n
->type
) {
614 if (ENDBODY_NOT
!= n
->end
) {
616 np
= n
->pending
->parent
;
621 assert(MDOC_BLOCK
== np
->type
);
622 assert(MDOC_Bl
== np
->tok
);
624 n
->data
.Bl
= np
->data
.Bl
;
629 * First figure out which kind of list to use: bind ourselves to
630 * the first mentioned list type and warn about any remaining
631 * ones. If we find no list type, we default to LIST_item.
634 assert(NULL
== n
->data
.Bl
);
635 n
->data
.Bl
= mandoc_calloc(1, sizeof(struct mdoc_bl
));
638 for (i
= 0; n
->args
&& i
< (int)n
->args
->argc
; i
++) {
642 switch (n
->args
->argv
[i
].arg
) {
643 /* Set list types. */
677 /* Set list arguments. */
679 dup
= n
->data
.Bl
->comp
;
683 dup
= (NULL
!= n
->data
.Bl
->width
);
684 width
= n
->args
->argv
[i
].value
[0];
687 /* NB: this can be empty! */
688 if (n
->args
->argv
[i
].sz
) {
689 offs
= n
->args
->argv
[i
].value
[0];
690 dup
= (NULL
!= n
->data
.Bl
->offs
);
693 if ( ! mdoc_nmsg(mdoc
, n
, MANDOCERR_IGNARGV
))
700 /* Check: duplicate auxiliary arguments. */
702 if (dup
&& ! mdoc_nmsg(mdoc
, n
, MANDOCERR_ARGVREP
))
706 n
->data
.Bl
->comp
= comp
;
708 n
->data
.Bl
->offs
= offs
;
710 n
->data
.Bl
->width
= width
;
712 /* Check: multiple list types. */
714 if (LIST__NONE
!= lt
&& n
->data
.Bl
->type
!= LIST__NONE
)
715 if ( ! mdoc_nmsg(mdoc
, n
, MANDOCERR_LISTREP
))
718 /* Assign list type. */
720 if (LIST__NONE
!= lt
&& n
->data
.Bl
->type
== LIST__NONE
) {
721 n
->data
.Bl
->type
= lt
;
722 /* Set column information, too. */
723 if (LIST_column
== lt
) {
726 n
->data
.Bl
->cols
= (const char **)
727 n
->args
->argv
[i
].value
;
731 /* The list type should come first. */
733 if (n
->data
.Bl
->type
== LIST__NONE
)
734 if (n
->data
.Bl
->width
||
737 if ( ! mdoc_nmsg(mdoc
, n
, MANDOCERR_LISTFIRST
))
743 /* Allow lists to default to LIST_item. */
745 if (LIST__NONE
== n
->data
.Bl
->type
) {
746 if ( ! mdoc_nmsg(mdoc
, n
, MANDOCERR_LISTTYPE
))
748 n
->data
.Bl
->type
= LIST_item
;
752 * Validate the width field. Some list types don't need width
753 * types and should be warned about them. Others should have it
754 * and must also be warned.
757 switch (n
->data
.Bl
->type
) {
759 if (n
->data
.Bl
->width
)
761 if (mdoc_nmsg(mdoc
, n
, MANDOCERR_NOWIDTHARG
))
773 if (NULL
== n
->data
.Bl
->width
)
775 if (mdoc_nmsg(mdoc
, n
, MANDOCERR_WIDTHARG
))
792 struct mdoc_node
*np
;
794 if (MDOC_BLOCK
!= n
->type
) {
795 if (ENDBODY_NOT
!= n
->end
) {
797 np
= n
->pending
->parent
;
802 assert(MDOC_BLOCK
== np
->type
);
803 assert(MDOC_Bd
== np
->tok
);
805 n
->data
.Bd
= np
->data
.Bd
;
809 assert(NULL
== n
->data
.Bd
);
810 n
->data
.Bd
= mandoc_calloc(1, sizeof(struct mdoc_bd
));
813 for (i
= 0; n
->args
&& i
< (int)n
->args
->argc
; i
++) {
818 switch (n
->args
->argv
[i
].arg
) {
825 case (MDOC_Unfilled
):
835 mdoc_nmsg(mdoc
, n
, MANDOCERR_BADDISP
);
838 /* NB: this can be empty! */
839 if (n
->args
->argv
[i
].sz
) {
840 offs
= n
->args
->argv
[i
].value
[0];
841 dup
= (NULL
!= n
->data
.Bd
->offs
);
844 if ( ! mdoc_nmsg(mdoc
, n
, MANDOCERR_IGNARGV
))
849 dup
= n
->data
.Bd
->comp
;
856 /* Check whether we have duplicates. */
858 if (dup
&& ! mdoc_nmsg(mdoc
, n
, MANDOCERR_ARGVREP
))
861 /* Make our auxiliary assignments. */
864 n
->data
.Bd
->offs
= offs
;
866 n
->data
.Bd
->comp
= comp
;
868 /* Check whether a type has already been assigned. */
870 if (DISP__NONE
!= dt
&& n
->data
.Bd
->type
!= DISP__NONE
)
871 if ( ! mdoc_nmsg(mdoc
, n
, MANDOCERR_DISPREP
))
874 /* Make our type assignment. */
876 if (DISP__NONE
!= dt
&& n
->data
.Bd
->type
== DISP__NONE
)
877 n
->data
.Bd
->type
= dt
;
880 if (DISP__NONE
== n
->data
.Bd
->type
) {
881 if ( ! mdoc_nmsg(mdoc
, n
, MANDOCERR_DISPTYPE
))
883 n
->data
.Bd
->type
= DISP_ragged
;
894 if (MDOC_BLOCK
!= n
->type
)
896 return(check_parent(mdoc
, n
, MDOC_Sh
, MDOC_BODY
));
904 if (MDOC_BLOCK
!= n
->type
)
907 mdoc
->regs
->regs
[(int)REG_nS
].set
= 0;
908 return(check_parent(mdoc
, n
, MDOC_MAX
, MDOC_ROOT
));
916 if (MDOC_BLOCK
!= n
->type
)
919 * FIXME: this can probably be lifted if we make the It into
920 * something else on-the-fly?
922 return(check_parent(mdoc
, n
, MDOC_Bl
, MDOC_BODY
));
934 for (i
= 1; i
< (int)n
->args
->argc
; i
++)
935 if ( ! mdoc_pmsg(mdoc
, n
->args
->argv
[i
].line
,
936 n
->args
->argv
[i
].pos
, MANDOCERR_IGNARGV
))
939 if (MDOC_Split
== n
->args
->argv
[0].arg
)
940 n
->data
.An
.auth
= AUTH_split
;
941 else if (MDOC_Nosplit
== n
->args
->argv
[0].arg
)
942 n
->data
.An
.auth
= AUTH_nosplit
;
954 return(check_stdarg(mdoc
, n
));
961 const struct mdoc_node
*nn
;
964 if (NULL
!= (nn
= mdoc
->last
->child
))
965 for (p
= nn
->string
; *p
; p
++) {
966 if (toupper((u_char
)*p
) == *p
)
968 if ( ! mdoc_nmsg(mdoc
, nn
, MANDOCERR_UPPERCASE
))
981 if (0 == mdoc
->meta
.date
|| mdoc
->meta
.os
)
982 if ( ! mdoc_nmsg(mdoc
, n
, MANDOCERR_PROLOGOOO
))
984 if (mdoc
->meta
.title
)
985 if ( ! mdoc_nmsg(mdoc
, n
, MANDOCERR_PROLOGREP
))
995 if (NULL
== mdoc
->meta
.title
|| 0 == mdoc
->meta
.date
)
996 if ( ! mdoc_nmsg(mdoc
, n
, MANDOCERR_PROLOGOOO
))
999 if ( ! mdoc_nmsg(mdoc
, n
, MANDOCERR_PROLOGREP
))
1009 if (mdoc
->meta
.title
|| mdoc
->meta
.os
)
1010 if ( ! mdoc_nmsg(mdoc
, n
, MANDOCERR_PROLOGOOO
))
1012 if (mdoc
->meta
.date
)
1013 if ( ! mdoc_nmsg(mdoc
, n
, MANDOCERR_PROLOGREP
))
1022 struct mdoc_node
*np
;
1026 * Unlike other data pointers, these are "housed" by the HEAD
1027 * element, which contains the goods.
1030 if (MDOC_HEAD
!= mdoc
->last
->type
) {
1031 if (ENDBODY_NOT
!= mdoc
->last
->end
) {
1032 assert(mdoc
->last
->pending
);
1033 np
= mdoc
->last
->pending
->parent
->head
;
1034 } else if (MDOC_BLOCK
!= mdoc
->last
->type
) {
1035 np
= mdoc
->last
->parent
->head
;
1037 np
= mdoc
->last
->head
;
1040 assert(MDOC_HEAD
== np
->type
);
1041 assert(MDOC_Bf
== np
->tok
);
1042 assert(np
->data
.Bf
);
1043 mdoc
->last
->data
.Bf
= np
->data
.Bf
;
1048 assert(MDOC_BLOCK
== np
->parent
->type
);
1049 assert(MDOC_Bf
== np
->parent
->tok
);
1050 np
->data
.Bf
= mandoc_calloc(1, sizeof(struct mdoc_bf
));
1053 * Cannot have both argument and parameter.
1054 * If neither is specified, let it through with a warning.
1057 if (np
->parent
->args
&& np
->child
) {
1058 mdoc_nmsg(mdoc
, np
, MANDOCERR_SYNTARGVCOUNT
);
1060 } else if (NULL
== np
->parent
->args
&& NULL
== np
->child
)
1061 return(mdoc_nmsg(mdoc
, np
, MANDOCERR_FONTTYPE
));
1063 /* Extract argument into data. */
1065 if (np
->parent
->args
) {
1066 arg
= np
->parent
->args
->argv
[0].arg
;
1067 if (MDOC_Emphasis
== arg
)
1068 np
->data
.Bf
->font
= FONT_Em
;
1069 else if (MDOC_Literal
== arg
)
1070 np
->data
.Bf
->font
= FONT_Li
;
1071 else if (MDOC_Symbolic
== arg
)
1072 np
->data
.Bf
->font
= FONT_Sy
;
1078 /* Extract parameter into data. */
1080 if (0 == strcmp(np
->child
->string
, "Em"))
1081 np
->data
.Bf
->font
= FONT_Em
;
1082 else if (0 == strcmp(np
->child
->string
, "Li"))
1083 np
->data
.Bf
->font
= FONT_Li
;
1084 else if (0 == strcmp(np
->child
->string
, "Sy"))
1085 np
->data
.Bf
->font
= FONT_Sy
;
1086 else if ( ! mdoc_nmsg(mdoc
, np
, MANDOCERR_FONTTYPE
))
1099 assert(mdoc
->last
->child
);
1100 assert(MDOC_TEXT
== mdoc
->last
->child
->type
);
1102 p
= mdoc_a2lib(mdoc
->last
->child
->string
);
1104 /* If lookup ok, replace with table value. */
1107 free(mdoc
->last
->child
->string
);
1108 mdoc
->last
->child
->string
= mandoc_strdup(p
);
1112 /* If not, use "library ``xxxx''. */
1114 sz
= strlen(mdoc
->last
->child
->string
) +
1115 2 + strlen("\\(lqlibrary\\(rq");
1116 buf
= mandoc_malloc(sz
);
1117 snprintf(buf
, sz
, "library \\(lq%s\\(rq",
1118 mdoc
->last
->child
->string
);
1119 free(mdoc
->last
->child
->string
);
1120 mdoc
->last
->child
->string
= buf
;
1125 post_eoln(POST_ARGS
)
1128 if (NULL
== mdoc
->last
->child
)
1130 return(mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_ARGSLOST
));
1137 const struct mdoc_node
*n
;
1140 * The Vt macro comes in both ELEM and BLOCK form, both of which
1141 * have different syntaxes (yet more context-sensitive
1142 * behaviour). ELEM types must have a child; BLOCK types,
1143 * specifically the BODY, should only have TEXT children.
1146 if (MDOC_ELEM
== mdoc
->last
->type
)
1147 return(eerr_ge1(mdoc
));
1148 if (MDOC_BODY
!= mdoc
->last
->type
)
1151 for (n
= mdoc
->last
->child
; n
; n
= n
->next
)
1152 if (MDOC_TEXT
!= n
->type
)
1153 if ( ! mdoc_nmsg(mdoc
, n
, MANDOCERR_CHILD
))
1163 struct mdoc_node
*nn
;
1166 /* If no child specified, make sure we have the meta name. */
1168 if (NULL
== mdoc
->last
->child
&& NULL
== mdoc
->meta
.name
) {
1169 mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_NONAME
);
1171 } else if (mdoc
->meta
.name
)
1174 /* If no meta name, set it from the child. */
1178 for (nn
= mdoc
->last
->child
; nn
; nn
= nn
->next
) {
1179 /* XXX - copied from concat(). */
1180 assert(MDOC_TEXT
== nn
->type
);
1182 if (strlcat(buf
, nn
->string
, BUFSIZ
) >= BUFSIZ
) {
1183 mdoc_nmsg(mdoc
, nn
, MANDOCERR_MEM
);
1187 if (NULL
== nn
->next
)
1190 if (strlcat(buf
, " ", BUFSIZ
) >= BUFSIZ
) {
1191 mdoc_nmsg(mdoc
, nn
, MANDOCERR_MEM
);
1196 mdoc
->meta
.name
= mandoc_strdup(buf
);
1201 post_literal(POST_ARGS
)
1205 * The `Dl' (note "el" not "one") and `Bd' macros unset the
1206 * MDOC_LITERAL flag as they leave. Note that `Bd' only sets
1207 * this in literal mode, but it doesn't hurt to just switch it
1208 * off in general since displays can't be nested.
1211 if (MDOC_BODY
== mdoc
->last
->type
)
1212 mdoc
->last
->flags
&= ~MDOC_LITERAL
;
1218 post_defaults(POST_ARGS
)
1220 struct mdoc_node
*nn
;
1223 * The `Ar' defaults to "file ..." if no value is provided as an
1224 * argument; the `Mt' and `Pa' macros use "~"; the `Li' just
1225 * gets an empty string.
1228 if (mdoc
->last
->child
)
1232 mdoc
->next
= MDOC_NEXT_CHILD
;
1236 if ( ! mdoc_word_alloc(mdoc
, nn
->line
, nn
->pos
, "file"))
1238 if ( ! mdoc_word_alloc(mdoc
, nn
->line
, nn
->pos
, "..."))
1242 if ( ! mdoc_word_alloc(mdoc
, nn
->line
, nn
->pos
, "AT&T"))
1244 if ( ! mdoc_word_alloc(mdoc
, nn
->line
, nn
->pos
, "UNIX"))
1248 if ( ! mdoc_word_alloc(mdoc
, nn
->line
, nn
->pos
, ""))
1254 if ( ! mdoc_word_alloc(mdoc
, nn
->line
, nn
->pos
, "~"))
1274 * If we have a child, look it up in the standard keys. If a
1275 * key exist, use that instead of the child; if it doesn't,
1276 * prefix "AT&T UNIX " to the existing data.
1279 if (NULL
== mdoc
->last
->child
)
1282 assert(MDOC_TEXT
== mdoc
->last
->child
->type
);
1283 p
= mdoc_a2att(mdoc
->last
->child
->string
);
1286 free(mdoc
->last
->child
->string
);
1287 mdoc
->last
->child
->string
= mandoc_strdup(p
);
1289 mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_BADATT
);
1291 q
= mdoc
->last
->child
->string
;
1292 sz
= strlen(p
) + strlen(q
) + 1;
1293 buf
= mandoc_malloc(sz
);
1294 strlcpy(buf
, p
, sz
);
1295 strlcat(buf
, q
, sz
);
1296 free(mdoc
->last
->child
->string
);
1297 mdoc
->last
->child
->string
= buf
;
1306 struct mdoc_node
*np
;
1309 if (AUTH__NONE
!= np
->data
.An
.auth
&& np
->child
)
1310 return(eerr_eq0(mdoc
));
1312 * FIXME: make this ewarn and make sure that the front-ends
1313 * don't print the arguments.
1315 if (AUTH__NONE
!= np
->data
.An
.auth
|| np
->child
)
1317 return(mdoc_nmsg(mdoc
, np
, MANDOCERR_NOARGS
));
1326 struct mdoc_node
*n
, *c
;
1329 if (MDOC_BLOCK
!= mdoc
->last
->type
)
1332 n
= mdoc
->last
->parent
->parent
;
1334 lt
= n
->data
.Bl
->type
;
1336 if (LIST__NONE
== lt
) {
1337 mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_LISTTYPE
);
1343 if (mdoc
->last
->head
->child
)
1345 /* FIXME: give this a dummy value. */
1346 if ( ! mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_NOARGS
))
1356 if (NULL
== mdoc
->last
->head
->child
)
1357 if ( ! mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_NOARGS
))
1367 if (NULL
== mdoc
->last
->body
->child
)
1368 if ( ! mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_NOBODY
))
1372 if (mdoc
->last
->head
->child
)
1373 if ( ! mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_ARGSLOST
))
1377 cols
= (int)n
->data
.Bl
->ncols
;
1379 assert(NULL
== mdoc
->last
->head
->child
);
1381 if (NULL
== mdoc
->last
->body
->child
)
1382 if ( ! mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_NOBODY
))
1385 for (i
= 0, c
= mdoc
->last
->child
; c
; c
= c
->next
)
1386 if (MDOC_BODY
== c
->type
)
1390 er
= MANDOCERR_ARGCOUNT
;
1391 else if (i
== cols
|| i
== cols
+ 1)
1394 er
= MANDOCERR_SYNTARGCOUNT
;
1396 rc
= mdoc_vmsg(mdoc
, er
,
1397 mdoc
->last
->line
, mdoc
->last
->pos
,
1398 "columns == %d (have %d)", cols
, i
);
1408 post_bl_block(POST_ARGS
)
1410 struct mdoc_node
*n
;
1413 * These are fairly complicated, so we've broken them into two
1414 * functions. post_bl_block_tag() is called when a -tag is
1415 * specified, but no -width (it must be guessed). The second
1416 * when a -width is specified (macro indicators must be
1417 * rewritten into real lengths).
1422 if (LIST_tag
== n
->data
.Bl
->type
&&
1423 NULL
== n
->data
.Bl
->width
) {
1424 if ( ! post_bl_block_tag(mdoc
))
1426 } else if (NULL
!= n
->data
.Bl
->width
) {
1427 if ( ! post_bl_block_width(mdoc
))
1432 assert(n
->data
.Bl
->width
);
1437 post_bl_block_width(POST_ARGS
)
1442 struct mdoc_node
*n
;
1448 * Calculate the real width of a list from the -width string,
1449 * which may contain a macro (with a known default width), a
1450 * literal string, or a scaling width.
1452 * If the value to -width is a macro, then we re-write it to be
1453 * the macro's width as set in share/tmac/mdoc/doc-common.
1456 if (0 == strcmp(n
->data
.Bl
->width
, "Ds"))
1458 else if (MDOC_MAX
== (tok
= mdoc_hash_find(n
->data
.Bl
->width
)))
1460 else if (0 == (width
= mdoc_macro2len(tok
)))
1461 return(mdoc_nmsg(mdoc
, n
, MANDOCERR_BADWIDTH
));
1463 /* The value already exists: free and reallocate it. */
1467 for (i
= 0; i
< (int)n
->args
->argc
; i
++)
1468 if (MDOC_Width
== n
->args
->argv
[i
].arg
)
1471 assert(i
< (int)n
->args
->argc
);
1473 snprintf(buf
, NUMSIZ
, "%zun", width
);
1474 free(n
->args
->argv
[i
].value
[0]);
1475 n
->args
->argv
[i
].value
[0] = mandoc_strdup(buf
);
1477 /* Set our width! */
1478 n
->data
.Bl
->width
= n
->args
->argv
[i
].value
[0];
1483 post_bl_block_tag(POST_ARGS
)
1485 struct mdoc_node
*n
, *nn
;
1491 * Calculate the -width for a `Bl -tag' list if it hasn't been
1492 * provided. Uses the first head macro. NOTE AGAIN: this is
1493 * ONLY if the -width argument has NOT been provided. See
1494 * post_bl_block_width() for converting the -width string.
1500 for (nn
= n
->body
->child
; nn
; nn
= nn
->next
) {
1501 if (MDOC_It
!= nn
->tok
)
1504 assert(MDOC_BLOCK
== nn
->type
);
1505 nn
= nn
->head
->child
;
1508 /* No -width for .Bl and first .It is emtpy */
1509 if ( ! mdoc_nmsg(mdoc
, n
, MANDOCERR_NOWIDTHARG
))
1514 if (MDOC_TEXT
== nn
->type
) {
1515 sz
= strlen(nn
->string
) + 1;
1519 if (0 != (ssz
= mdoc_macro2len(nn
->tok
)))
1521 else if ( ! mdoc_nmsg(mdoc
, n
, MANDOCERR_NOWIDTHARG
))
1527 /* Defaults to ten ens. */
1529 snprintf(buf
, NUMSIZ
, "%zun", sz
);
1532 * We have to dynamically add this to the macro's argument list.
1533 * We're guaranteed that a MDOC_Width doesn't already exist.
1537 i
= (int)(n
->args
->argc
)++;
1539 n
->args
->argv
= mandoc_realloc(n
->args
->argv
,
1540 n
->args
->argc
* sizeof(struct mdoc_argv
));
1542 n
->args
->argv
[i
].arg
= MDOC_Width
;
1543 n
->args
->argv
[i
].line
= n
->line
;
1544 n
->args
->argv
[i
].pos
= n
->pos
;
1545 n
->args
->argv
[i
].sz
= 1;
1546 n
->args
->argv
[i
].value
= mandoc_malloc(sizeof(char *));
1547 n
->args
->argv
[i
].value
[0] = mandoc_strdup(buf
);
1549 /* Set our width! */
1550 n
->data
.Bl
->width
= n
->args
->argv
[i
].value
[0];
1556 post_bl_head(POST_ARGS
)
1558 struct mdoc_node
*np
, *nn
, *nnp
;
1561 if (LIST_column
!= mdoc
->last
->data
.Bl
->type
)
1562 /* FIXME: this should be ERROR class... */
1563 return(hwarn_eq0(mdoc
));
1566 * Convert old-style lists, where the column width specifiers
1567 * trail as macro parameters, to the new-style ("normal-form")
1568 * lists where they're argument values following -column.
1571 /* First, disallow both types and allow normal-form. */
1574 * TODO: technically, we can accept both and just merge the two
1575 * lists, but I'll leave that for another day.
1578 if (mdoc
->last
->data
.Bl
->ncols
&& mdoc
->last
->nchild
) {
1579 mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_COLUMNS
);
1581 } else if (NULL
== mdoc
->last
->child
)
1584 np
= mdoc
->last
->parent
;
1587 for (j
= 0; j
< (int)np
->args
->argc
; j
++)
1588 if (MDOC_Column
== np
->args
->argv
[j
].arg
)
1591 assert(j
< (int)np
->args
->argc
);
1592 assert(0 == np
->args
->argv
[j
].sz
);
1595 * Accomodate for new-style groff column syntax. Shuffle the
1596 * child nodes, all of which must be TEXT, as arguments for the
1597 * column field. Then, delete the head children.
1600 np
->args
->argv
[j
].sz
= (size_t)mdoc
->last
->nchild
;
1601 np
->args
->argv
[j
].value
= mandoc_malloc
1602 ((size_t)mdoc
->last
->nchild
* sizeof(char *));
1604 mdoc
->last
->data
.Bl
->ncols
= np
->args
->argv
[j
].sz
;
1605 mdoc
->last
->data
.Bl
->cols
= (const char **)np
->args
->argv
[j
].value
;
1607 for (i
= 0, nn
= mdoc
->last
->child
; nn
; i
++) {
1608 np
->args
->argv
[j
].value
[i
] = nn
->string
;
1612 mdoc_node_delete(NULL
, nnp
);
1615 mdoc
->last
->nchild
= 0;
1616 mdoc
->last
->child
= NULL
;
1624 struct mdoc_node
*n
;
1626 if (MDOC_HEAD
== mdoc
->last
->type
)
1627 return(post_bl_head(mdoc
));
1628 if (MDOC_BLOCK
== mdoc
->last
->type
)
1629 return(post_bl_block(mdoc
));
1630 if (MDOC_BODY
!= mdoc
->last
->type
)
1632 if (NULL
== mdoc
->last
->child
)
1636 * We only allow certain children of `Bl'. This is usually on
1637 * `It', but apparently `Sm' occurs here and there, so we let
1638 * that one through, too.
1642 for (n
= mdoc
->last
->child
; n
; n
= n
->next
) {
1643 if (MDOC_BLOCK
== n
->type
&& MDOC_It
== n
->tok
)
1645 if (MDOC_Sm
== n
->tok
)
1647 mdoc_nmsg(mdoc
, n
, MANDOCERR_SYNTCHILD
);
1656 ebool(struct mdoc
*mdoc
)
1658 struct mdoc_node
*n
;
1661 for (n
= mdoc
->last
->child
; n
; n
= n
->next
) {
1662 if (MDOC_TEXT
!= n
->type
)
1664 if (0 == strcmp(n
->string
, "on"))
1666 if (0 == strcmp(n
->string
, "off"))
1673 return(mdoc_nmsg(mdoc
, n
, MANDOCERR_BADBOOL
));
1678 post_root(POST_ARGS
)
1681 if (NULL
== mdoc
->first
->child
)
1682 mdoc_nmsg(mdoc
, mdoc
->first
, MANDOCERR_NODOCBODY
);
1683 else if ( ! (MDOC_PBODY
& mdoc
->flags
))
1684 mdoc_nmsg(mdoc
, mdoc
->first
, MANDOCERR_NODOCPROLOG
);
1685 else if (MDOC_BLOCK
!= mdoc
->first
->child
->type
)
1686 mdoc_nmsg(mdoc
, mdoc
->first
, MANDOCERR_NODOCBODY
);
1687 else if (MDOC_Sh
!= mdoc
->first
->child
->tok
)
1688 mdoc_nmsg(mdoc
, mdoc
->first
, MANDOCERR_NODOCBODY
);
1700 assert(MDOC_TEXT
== mdoc
->last
->child
->type
);
1702 p
= mdoc_a2st(mdoc
->last
->child
->string
);
1705 mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_BADSTANDARD
);
1706 mdoc_node_delete(mdoc
, mdoc
->last
);
1708 free(mdoc
->last
->child
->string
);
1709 mdoc
->last
->child
->string
= mandoc_strdup(p
);
1718 struct mdoc_node
*nn
, *next
, *prev
;
1721 if (MDOC_BODY
!= mdoc
->last
->type
)
1725 * Make sure only certain types of nodes are allowed within the
1726 * the `Rs' body. Delete offending nodes and raise a warning.
1727 * Do this before re-ordering for the sake of clarity.
1731 for (nn
= mdoc
->last
->child
; nn
; nn
= next
) {
1732 for (i
= 0; i
< RSORD_MAX
; i
++)
1733 if (nn
->tok
== rsord
[i
])
1736 if (i
< RSORD_MAX
) {
1742 mdoc_nmsg(mdoc
, nn
, MANDOCERR_CHILD
);
1743 mdoc_node_delete(mdoc
, nn
);
1747 * The full `Rs' block needs special handling to order the
1748 * sub-elements according to `rsord'. Pick through each element
1749 * and correctly order it. This is a insertion sort.
1753 for (nn
= mdoc
->last
->child
->next
; nn
; nn
= next
) {
1754 /* Determine order of `nn'. */
1755 for (i
= 0; i
< RSORD_MAX
; i
++)
1756 if (rsord
[i
] == nn
->tok
)
1760 * Remove `nn' from the chain. This somewhat
1761 * repeats mdoc_node_unlink(), but since we're
1762 * just re-ordering, there's no need for the
1763 * full unlink process.
1766 if (NULL
!= (next
= nn
->next
))
1767 next
->prev
= nn
->prev
;
1769 if (NULL
!= (prev
= nn
->prev
))
1770 prev
->next
= nn
->next
;
1772 nn
->prev
= nn
->next
= NULL
;
1775 * Scan back until we reach a node that's
1776 * ordered before `nn'.
1779 for ( ; prev
; prev
= prev
->prev
) {
1780 /* Determine order of `prev'. */
1781 for (j
= 0; j
< RSORD_MAX
; j
++)
1782 if (rsord
[j
] == prev
->tok
)
1790 * Set `nn' back into its correct place in front
1791 * of the `prev' node.
1798 prev
->next
->prev
= nn
;
1799 nn
->next
= prev
->next
;
1802 mdoc
->last
->child
->prev
= nn
;
1803 nn
->next
= mdoc
->last
->child
;
1804 mdoc
->last
->child
= nn
;
1816 if (MDOC_HEAD
== mdoc
->last
->type
)
1817 return(post_sh_head(mdoc
));
1818 if (MDOC_BODY
== mdoc
->last
->type
)
1819 return(post_sh_body(mdoc
));
1826 post_sh_body(POST_ARGS
)
1828 struct mdoc_node
*n
;
1830 if (SEC_NAME
!= mdoc
->lastsec
)
1834 * Warn if the NAME section doesn't contain the `Nm' and `Nd'
1835 * macros (can have multiple `Nm' and one `Nd'). Note that the
1836 * children of the BODY declaration can also be "text".
1839 if (NULL
== (n
= mdoc
->last
->child
))
1840 return(mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_BADNAMESEC
));
1842 for ( ; n
&& n
->next
; n
= n
->next
) {
1843 if (MDOC_ELEM
== n
->type
&& MDOC_Nm
== n
->tok
)
1845 if (MDOC_TEXT
== n
->type
)
1847 if ( ! mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_BADNAMESEC
))
1852 if (MDOC_BLOCK
== n
->type
&& MDOC_Nd
== n
->tok
)
1854 return(mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_BADNAMESEC
));
1859 post_sh_head(POST_ARGS
)
1863 struct mdoc_node
*n
;
1866 * Process a new section. Sections are either "named" or
1867 * "custom". Custom sections are user-defined, while named ones
1868 * follow a conventional order and may only appear in certain
1874 /* FIXME: use dynamic buffer... */
1876 for (n
= mdoc
->last
->child
; n
; n
= n
->next
) {
1877 /* XXX - copied from concat(). */
1878 assert(MDOC_TEXT
== n
->type
);
1880 if (strlcat(buf
, n
->string
, BUFSIZ
) >= BUFSIZ
) {
1881 mdoc_nmsg(mdoc
, n
, MANDOCERR_MEM
);
1885 if (NULL
== n
->next
)
1888 if (strlcat(buf
, " ", BUFSIZ
) >= BUFSIZ
) {
1889 mdoc_nmsg(mdoc
, n
, MANDOCERR_MEM
);
1894 sec
= mdoc_str2sec(buf
);
1896 /* The NAME should be first. */
1898 if (SEC_NAME
!= sec
&& SEC_NONE
== mdoc
->lastnamed
)
1899 mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_NAMESECFIRST
);
1901 /* The SYNOPSIS gets special attention in other areas. */
1903 if (SEC_SYNOPSIS
== sec
)
1904 mdoc
->flags
|= MDOC_SYNOPSIS
;
1906 mdoc
->flags
&= ~MDOC_SYNOPSIS
;
1908 /* Mark our last section. */
1910 mdoc
->lastsec
= sec
;
1912 /* We don't care about custom sections after this. */
1914 if (SEC_CUSTOM
== sec
)
1918 * Check whether our non-custom section is being repeated or is
1922 if (sec
== mdoc
->lastnamed
)
1923 mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_SECREP
);
1925 if (sec
< mdoc
->lastnamed
)
1926 mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_SECOOO
);
1928 /* Mark the last named section. */
1930 mdoc
->lastnamed
= sec
;
1932 /* Check particular section/manual conventions. */
1934 assert(mdoc
->meta
.msec
);
1937 case (SEC_RETURN_VALUES
):
1942 if (*mdoc
->meta
.msec
== '2')
1944 if (*mdoc
->meta
.msec
== '3')
1946 if (*mdoc
->meta
.msec
== '9')
1948 mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_SECMSEC
);
1961 if (NULL
== mdoc
->last
)
1965 * Don't allow prior `Lp' or `Pp' prior to a paragraph-type
1966 * block: `Lp', `Pp', or non-compact `Bd' or `Bl'.
1969 if (MDOC_Pp
!= mdoc
->last
->tok
&& MDOC_Lp
!= mdoc
->last
->tok
)
1972 if (MDOC_Bl
== n
->tok
&& n
->data
.Bl
->comp
)
1974 if (MDOC_Bd
== n
->tok
&& n
->data
.Bd
->comp
)
1977 mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_IGNPAR
);
1978 mdoc_node_delete(mdoc
, mdoc
->last
);
1983 pre_literal(PRE_ARGS
)
1986 if (MDOC_BODY
!= n
->type
)
1990 * The `Dl' (note "el" not "one") and `Bd -literal' and `Bd
1991 * -unfilled' macros set MDOC_LITERAL on entrance to the body.
1996 mdoc
->flags
|= MDOC_LITERAL
;
2000 if (DISP_literal
== n
->data
.Bd
->type
)
2001 mdoc
->flags
|= MDOC_LITERAL
;
2002 if (DISP_unfilled
== n
->data
.Bd
->type
)
2003 mdoc
->flags
|= MDOC_LITERAL
;