]>
git.cameronkatri.com Git - mandoc.git/blob - mdoc_validate.c
3c61f3e21ff160774ae738f609c2b5d0ae87d341
1 /* $Id: mdoc_validate.c,v 1.132 2010/11/30 12:59:20 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.
22 #include <sys/utsname.h>
25 #include <sys/types.h>
37 #include "libmandoc.h"
39 /* FIXME: .Bl -diag can't have non-text children in HEAD. */
41 #define PRE_ARGS struct mdoc *mdoc, struct mdoc_node *n
42 #define POST_ARGS struct mdoc *mdoc
59 typedef int (*v_pre
)(PRE_ARGS
);
60 typedef int (*v_post
)(POST_ARGS
);
67 static int check_count(struct mdoc
*, enum mdoc_type
,
68 enum check_lvl
, enum check_ineq
, int);
69 static int check_parent(PRE_ARGS
, enum mdoct
, enum mdoc_type
);
70 static int check_stdarg(PRE_ARGS
);
71 static int check_text(struct mdoc
*, int, int, char *);
72 static int check_argv(struct mdoc
*,
73 struct mdoc_node
*, struct mdoc_argv
*);
74 static int check_args(struct mdoc
*, struct mdoc_node
*);
76 static int concat(struct mdoc
*, char *,
77 const struct mdoc_node
*, size_t);
79 static int ebool(POST_ARGS
);
80 static int berr_ge1(POST_ARGS
);
81 static int bwarn_ge1(POST_ARGS
);
82 static int eerr_eq0(POST_ARGS
);
83 static int eerr_eq1(POST_ARGS
);
84 static int eerr_ge1(POST_ARGS
);
85 static int eerr_le1(POST_ARGS
);
86 static int ewarn_eq0(POST_ARGS
);
87 static int ewarn_ge1(POST_ARGS
);
88 static int herr_eq0(POST_ARGS
);
89 static int herr_ge1(POST_ARGS
);
90 static int hwarn_eq0(POST_ARGS
);
91 static int hwarn_eq1(POST_ARGS
);
92 static int hwarn_le1(POST_ARGS
);
94 static int post_an(POST_ARGS
);
95 static int post_at(POST_ARGS
);
96 static int post_bf(POST_ARGS
);
97 static int post_bl(POST_ARGS
);
98 static int post_bl_block(POST_ARGS
);
99 static int post_bl_block_width(POST_ARGS
);
100 static int post_bl_block_tag(POST_ARGS
);
101 static int post_bl_head(POST_ARGS
);
102 static int post_dd(POST_ARGS
);
103 static int post_dt(POST_ARGS
);
104 static int post_defaults(POST_ARGS
);
105 static int post_literal(POST_ARGS
);
106 static int post_eoln(POST_ARGS
);
107 static int post_it(POST_ARGS
);
108 static int post_lb(POST_ARGS
);
109 static int post_nm(POST_ARGS
);
110 static int post_os(POST_ARGS
);
111 static int post_prol(POST_ARGS
);
112 static int post_root(POST_ARGS
);
113 static int post_rs(POST_ARGS
);
114 static int post_sh(POST_ARGS
);
115 static int post_sh_body(POST_ARGS
);
116 static int post_sh_head(POST_ARGS
);
117 static int post_st(POST_ARGS
);
118 static int post_std(POST_ARGS
);
119 static int post_vt(POST_ARGS
);
120 static int pre_an(PRE_ARGS
);
121 static int pre_bd(PRE_ARGS
);
122 static int pre_bl(PRE_ARGS
);
123 static int pre_dd(PRE_ARGS
);
124 static int pre_display(PRE_ARGS
);
125 static int pre_dt(PRE_ARGS
);
126 static int pre_it(PRE_ARGS
);
127 static int pre_literal(PRE_ARGS
);
128 static int pre_os(PRE_ARGS
);
129 static int pre_par(PRE_ARGS
);
130 static int pre_rv(PRE_ARGS
);
131 static int pre_sh(PRE_ARGS
);
132 static int pre_ss(PRE_ARGS
);
134 static v_post posts_an
[] = { post_an
, NULL
};
135 static v_post posts_at
[] = { post_at
, post_defaults
, NULL
};
136 static v_post posts_bd
[] = { post_literal
, hwarn_eq0
, bwarn_ge1
, NULL
};
137 static v_post posts_bf
[] = { hwarn_le1
, post_bf
, NULL
};
138 static v_post posts_bk
[] = { hwarn_eq0
, bwarn_ge1
, NULL
};
139 static v_post posts_bl
[] = { bwarn_ge1
, post_bl
, NULL
};
140 static v_post posts_bool
[] = { eerr_eq1
, ebool
, NULL
};
141 static v_post posts_eoln
[] = { post_eoln
, NULL
};
142 static v_post posts_defaults
[] = { post_defaults
, NULL
};
143 static v_post posts_dd
[] = { ewarn_ge1
, post_dd
, post_prol
, NULL
};
144 static v_post posts_dl
[] = { post_literal
, bwarn_ge1
, herr_eq0
, NULL
};
145 static v_post posts_dt
[] = { post_dt
, post_prol
, NULL
};
146 static v_post posts_fo
[] = { hwarn_eq1
, bwarn_ge1
, NULL
};
147 static v_post posts_it
[] = { post_it
, NULL
};
148 static v_post posts_lb
[] = { eerr_eq1
, post_lb
, NULL
};
149 static v_post posts_nd
[] = { berr_ge1
, NULL
};
150 static v_post posts_nm
[] = { post_nm
, NULL
};
151 static v_post posts_notext
[] = { ewarn_eq0
, NULL
};
152 static v_post posts_os
[] = { post_os
, post_prol
, NULL
};
153 static v_post posts_rs
[] = { berr_ge1
, herr_eq0
, post_rs
, NULL
};
154 static v_post posts_sh
[] = { herr_ge1
, bwarn_ge1
, post_sh
, NULL
};
155 static v_post posts_sp
[] = { eerr_le1
, NULL
};
156 static v_post posts_ss
[] = { herr_ge1
, NULL
};
157 static v_post posts_st
[] = { eerr_eq1
, post_st
, NULL
};
158 static v_post posts_std
[] = { post_std
, NULL
};
159 static v_post posts_text
[] = { eerr_ge1
, NULL
};
160 static v_post posts_text1
[] = { eerr_eq1
, NULL
};
161 static v_post posts_vt
[] = { post_vt
, NULL
};
162 static v_post posts_wline
[] = { bwarn_ge1
, herr_eq0
, NULL
};
163 static v_post posts_wtext
[] = { ewarn_ge1
, NULL
};
164 static v_pre pres_an
[] = { pre_an
, NULL
};
165 static v_pre pres_bd
[] = { pre_display
, pre_bd
, pre_literal
, pre_par
, NULL
};
166 static v_pre pres_bl
[] = { pre_bl
, pre_par
, NULL
};
167 static v_pre pres_d1
[] = { pre_display
, NULL
};
168 static v_pre pres_dl
[] = { pre_literal
, pre_display
, NULL
};
169 static v_pre pres_dd
[] = { pre_dd
, NULL
};
170 static v_pre pres_dt
[] = { pre_dt
, NULL
};
171 static v_pre pres_er
[] = { NULL
, NULL
};
172 static v_pre pres_ex
[] = { NULL
, NULL
};
173 static v_pre pres_fd
[] = { NULL
, NULL
};
174 static v_pre pres_it
[] = { pre_it
, NULL
};
175 static v_pre pres_os
[] = { pre_os
, NULL
};
176 static v_pre pres_pp
[] = { pre_par
, NULL
};
177 static v_pre pres_rv
[] = { pre_rv
, NULL
};
178 static v_pre pres_sh
[] = { pre_sh
, NULL
};
179 static v_pre pres_ss
[] = { pre_ss
, NULL
};
181 const struct valids mdoc_valids
[MDOC_MAX
] = {
182 { NULL
, NULL
}, /* Ap */
183 { pres_dd
, posts_dd
}, /* Dd */
184 { pres_dt
, posts_dt
}, /* Dt */
185 { pres_os
, posts_os
}, /* Os */
186 { pres_sh
, posts_sh
}, /* Sh */
187 { pres_ss
, posts_ss
}, /* Ss */
188 { pres_pp
, posts_notext
}, /* Pp */
189 { pres_d1
, posts_wline
}, /* D1 */
190 { pres_dl
, posts_dl
}, /* Dl */
191 { pres_bd
, posts_bd
}, /* Bd */
192 { NULL
, NULL
}, /* Ed */
193 { pres_bl
, posts_bl
}, /* Bl */
194 { NULL
, NULL
}, /* El */
195 { pres_it
, posts_it
}, /* It */
196 { NULL
, posts_text
}, /* Ad */
197 { pres_an
, posts_an
}, /* An */
198 { NULL
, posts_defaults
}, /* Ar */
199 { NULL
, posts_text
}, /* Cd */
200 { NULL
, NULL
}, /* Cm */
201 { NULL
, NULL
}, /* Dv */
202 { pres_er
, posts_text
}, /* Er */
203 { NULL
, NULL
}, /* Ev */
204 { pres_ex
, posts_std
}, /* Ex */
205 { NULL
, NULL
}, /* Fa */
206 { pres_fd
, posts_wtext
}, /* Fd */
207 { NULL
, NULL
}, /* Fl */
208 { NULL
, posts_text
}, /* Fn */
209 { NULL
, posts_wtext
}, /* Ft */
210 { NULL
, posts_text
}, /* Ic */
211 { NULL
, posts_text1
}, /* In */
212 { NULL
, posts_defaults
}, /* Li */
213 { NULL
, posts_nd
}, /* Nd */
214 { NULL
, posts_nm
}, /* Nm */
215 { NULL
, posts_wline
}, /* Op */
216 { NULL
, NULL
}, /* Ot */
217 { NULL
, posts_defaults
}, /* Pa */
218 { pres_rv
, posts_std
}, /* Rv */
219 { NULL
, posts_st
}, /* St */
220 { NULL
, NULL
}, /* Va */
221 { NULL
, posts_vt
}, /* Vt */
222 { NULL
, posts_wtext
}, /* Xr */
223 { NULL
, posts_text
}, /* %A */
224 { NULL
, posts_text
}, /* %B */ /* FIXME: can be used outside Rs/Re. */
225 { NULL
, posts_text
}, /* %D */ /* FIXME: check date with mandoc_a2time(). */
226 { NULL
, posts_text
}, /* %I */
227 { NULL
, posts_text
}, /* %J */
228 { NULL
, posts_text
}, /* %N */
229 { NULL
, posts_text
}, /* %O */
230 { NULL
, posts_text
}, /* %P */
231 { NULL
, posts_text
}, /* %R */
232 { NULL
, posts_text
}, /* %T */ /* FIXME: can be used outside Rs/Re. */
233 { NULL
, posts_text
}, /* %V */
234 { NULL
, NULL
}, /* Ac */
235 { NULL
, NULL
}, /* Ao */
236 { NULL
, posts_wline
}, /* Aq */
237 { NULL
, posts_at
}, /* At */
238 { NULL
, NULL
}, /* Bc */
239 { NULL
, posts_bf
}, /* Bf */
240 { NULL
, NULL
}, /* Bo */
241 { NULL
, posts_wline
}, /* Bq */
242 { NULL
, NULL
}, /* Bsx */
243 { NULL
, NULL
}, /* Bx */
244 { NULL
, posts_bool
}, /* Db */
245 { NULL
, NULL
}, /* Dc */
246 { NULL
, NULL
}, /* Do */
247 { NULL
, posts_wline
}, /* Dq */
248 { NULL
, NULL
}, /* Ec */
249 { NULL
, NULL
}, /* Ef */
250 { NULL
, NULL
}, /* Em */
251 { NULL
, NULL
}, /* Eo */
252 { NULL
, NULL
}, /* Fx */
253 { NULL
, posts_text
}, /* Ms */
254 { NULL
, posts_notext
}, /* No */
255 { NULL
, posts_notext
}, /* Ns */
256 { NULL
, NULL
}, /* Nx */
257 { NULL
, NULL
}, /* Ox */
258 { NULL
, NULL
}, /* Pc */
259 { NULL
, posts_text1
}, /* Pf */
260 { NULL
, NULL
}, /* Po */
261 { NULL
, posts_wline
}, /* Pq */
262 { NULL
, NULL
}, /* Qc */
263 { NULL
, posts_wline
}, /* Ql */
264 { NULL
, NULL
}, /* Qo */
265 { NULL
, posts_wline
}, /* Qq */
266 { NULL
, NULL
}, /* Re */
267 { NULL
, posts_rs
}, /* Rs */
268 { NULL
, NULL
}, /* Sc */
269 { NULL
, NULL
}, /* So */
270 { NULL
, posts_wline
}, /* Sq */
271 { NULL
, posts_bool
}, /* Sm */
272 { NULL
, posts_text
}, /* Sx */
273 { NULL
, posts_text
}, /* Sy */
274 { NULL
, posts_text
}, /* Tn */
275 { NULL
, NULL
}, /* Ux */
276 { NULL
, NULL
}, /* Xc */
277 { NULL
, NULL
}, /* Xo */
278 { NULL
, posts_fo
}, /* Fo */
279 { NULL
, NULL
}, /* Fc */
280 { NULL
, NULL
}, /* Oo */
281 { NULL
, NULL
}, /* Oc */
282 { NULL
, posts_bk
}, /* Bk */
283 { NULL
, NULL
}, /* Ek */
284 { NULL
, posts_eoln
}, /* Bt */
285 { NULL
, NULL
}, /* Hf */
286 { NULL
, NULL
}, /* Fr */
287 { NULL
, posts_eoln
}, /* Ud */
288 { NULL
, posts_lb
}, /* Lb */
289 { NULL
, posts_notext
}, /* Lp */
290 { NULL
, posts_text
}, /* Lk */
291 { NULL
, posts_defaults
}, /* Mt */
292 { NULL
, posts_wline
}, /* Brq */
293 { NULL
, NULL
}, /* Bro */
294 { NULL
, NULL
}, /* Brc */
295 { NULL
, posts_text
}, /* %C */
296 { NULL
, NULL
}, /* Es */
297 { NULL
, NULL
}, /* En */
298 { NULL
, NULL
}, /* Dx */
299 { NULL
, posts_text
}, /* %Q */
300 { NULL
, posts_notext
}, /* br */
301 { pres_pp
, posts_sp
}, /* sp */
302 { NULL
, posts_text1
}, /* %U */
303 { NULL
, NULL
}, /* Ta */
306 #define RSORD_MAX 14 /* Number of `Rs' blocks. */
308 static const enum mdoct rsord
[RSORD_MAX
] = {
327 mdoc_valid_pre(struct mdoc
*mdoc
, struct mdoc_node
*n
)
333 if (MDOC_TEXT
== n
->type
) {
337 return(check_text(mdoc
, line
, pos
, tp
));
340 if ( ! check_args(mdoc
, n
))
342 if (NULL
== mdoc_valids
[n
->tok
].pre
)
344 for (p
= mdoc_valids
[n
->tok
].pre
; *p
; p
++)
345 if ( ! (*p
)(mdoc
, n
))
352 mdoc_valid_post(struct mdoc
*mdoc
)
356 if (MDOC_VALID
& mdoc
->last
->flags
)
358 mdoc
->last
->flags
|= MDOC_VALID
;
360 if (MDOC_TEXT
== mdoc
->last
->type
)
362 if (MDOC_ROOT
== mdoc
->last
->type
)
363 return(post_root(mdoc
));
365 if (NULL
== mdoc_valids
[mdoc
->last
->tok
].post
)
367 for (p
= mdoc_valids
[mdoc
->last
->tok
].post
; *p
; p
++)
375 check_count(struct mdoc
*m
, enum mdoc_type type
,
376 enum check_lvl lvl
, enum check_ineq ineq
, int val
)
380 if (m
->last
->type
!= type
)
386 if (m
->last
->nchild
< val
)
391 if (m
->last
->nchild
> val
)
396 if (val
== m
->last
->nchild
)
401 if (CHECK_WARN
== lvl
) {
402 return(mdoc_vmsg(m
, MANDOCERR_ARGCOUNT
,
403 m
->last
->line
, m
->last
->pos
,
404 "want %s%d children (have %d)",
405 p
, val
, m
->last
->nchild
));
408 return(mdoc_vmsg(m
, MANDOCERR_ARGCOUNT
,
409 m
->last
->line
, m
->last
->pos
,
410 "require %s%d children (have %d)",
411 p
, val
, m
->last
->nchild
));
418 return(check_count(mdoc
, MDOC_BODY
, CHECK_FATAL
, CHECK_GT
, 0));
424 return(check_count(mdoc
, MDOC_BODY
, CHECK_WARN
, CHECK_GT
, 0));
430 return(check_count(mdoc
, MDOC_ELEM
, CHECK_FATAL
, CHECK_EQ
, 0));
436 return(check_count(mdoc
, MDOC_ELEM
, CHECK_FATAL
, CHECK_EQ
, 1));
442 return(check_count(mdoc
, MDOC_ELEM
, CHECK_FATAL
, CHECK_GT
, 0));
448 return(check_count(mdoc
, MDOC_ELEM
, CHECK_FATAL
, CHECK_LT
, 2));
454 return(check_count(mdoc
, MDOC_ELEM
, CHECK_WARN
, CHECK_EQ
, 0));
460 return(check_count(mdoc
, MDOC_ELEM
, CHECK_WARN
, CHECK_GT
, 0));
466 return(check_count(mdoc
, MDOC_HEAD
, CHECK_FATAL
, CHECK_EQ
, 0));
472 return(check_count(mdoc
, MDOC_HEAD
, CHECK_FATAL
, CHECK_GT
, 0));
478 return(check_count(mdoc
, MDOC_HEAD
, CHECK_WARN
, CHECK_EQ
, 0));
484 return(check_count(mdoc
, MDOC_HEAD
, CHECK_WARN
, CHECK_EQ
, 1));
490 return(check_count(mdoc
, MDOC_HEAD
, CHECK_WARN
, CHECK_LT
, 2));
495 check_stdarg(PRE_ARGS
)
498 if (n
->args
&& 1 == n
->args
->argc
)
499 if (MDOC_Std
== n
->args
->argv
[0].arg
)
501 return(mdoc_nmsg(mdoc
, n
, MANDOCERR_NOARGV
));
506 check_args(struct mdoc
*m
, struct mdoc_node
*n
)
513 assert(n
->args
->argc
);
514 for (i
= 0; i
< (int)n
->args
->argc
; i
++)
515 if ( ! check_argv(m
, n
, &n
->args
->argv
[i
]))
523 check_argv(struct mdoc
*m
, struct mdoc_node
*n
, struct mdoc_argv
*v
)
527 for (i
= 0; i
< (int)v
->sz
; i
++)
528 if ( ! check_text(m
, v
->line
, v
->pos
, v
->value
[i
]))
531 if (MDOC_Std
== v
->arg
) {
532 if (v
->sz
|| m
->meta
.name
)
534 if ( ! mdoc_nmsg(m
, n
, MANDOCERR_NONAME
))
543 check_text(struct mdoc
*m
, int ln
, int pos
, char *p
)
548 for ( ; *p
; p
++, pos
++) {
549 sz
= strcspn(p
, "\t\\");
558 if (MDOC_LITERAL
& m
->flags
)
560 if (mdoc_pmsg(m
, ln
, pos
, MANDOCERR_BADTAB
))
565 /* Check the special character. */
567 c
= mandoc_special(p
);
572 mdoc_pmsg(m
, ln
, pos
, MANDOCERR_BADESCAPE
);
580 check_parent(PRE_ARGS
, enum mdoct tok
, enum mdoc_type t
)
584 if ((MDOC_ROOT
== t
|| tok
== n
->parent
->tok
) &&
585 (t
== n
->parent
->type
))
588 mdoc_vmsg(mdoc
, MANDOCERR_SYNTCHILD
,
589 n
->line
, n
->pos
, "want parent %s",
590 MDOC_ROOT
== t
? "<root>" :
591 mdoc_macronames
[tok
]);
597 pre_display(PRE_ARGS
)
599 struct mdoc_node
*node
;
601 /* Display elements (`Bd', `D1'...) cannot be nested. */
603 if (MDOC_BLOCK
!= n
->type
)
607 for (node
= mdoc
->last
->parent
; node
; node
= node
->parent
)
608 if (MDOC_BLOCK
== node
->type
)
609 if (MDOC_Bd
== node
->tok
)
615 mdoc_nmsg(mdoc
, n
, MANDOCERR_NESTEDDISP
);
624 const char *offs
, *width
;
626 struct mdoc_node
*np
;
628 if (MDOC_BLOCK
!= n
->type
) {
629 if (ENDBODY_NOT
!= n
->end
) {
631 np
= n
->pending
->parent
;
636 assert(MDOC_BLOCK
== np
->type
);
637 assert(MDOC_Bl
== np
->tok
);
639 n
->data
.Bl
= np
->data
.Bl
;
644 * First figure out which kind of list to use: bind ourselves to
645 * the first mentioned list type and warn about any remaining
646 * ones. If we find no list type, we default to LIST_item.
649 assert(NULL
== n
->data
.Bl
);
650 n
->data
.Bl
= mandoc_calloc(1, sizeof(struct mdoc_bl
));
653 for (i
= 0; n
->args
&& i
< (int)n
->args
->argc
; i
++) {
657 switch (n
->args
->argv
[i
].arg
) {
658 /* Set list types. */
692 /* Set list arguments. */
694 dup
= n
->data
.Bl
->comp
;
698 dup
= (NULL
!= n
->data
.Bl
->width
);
699 width
= n
->args
->argv
[i
].value
[0];
702 /* NB: this can be empty! */
703 if (n
->args
->argv
[i
].sz
) {
704 offs
= n
->args
->argv
[i
].value
[0];
705 dup
= (NULL
!= n
->data
.Bl
->offs
);
708 if ( ! mdoc_nmsg(mdoc
, n
, MANDOCERR_IGNARGV
))
715 /* Check: duplicate auxiliary arguments. */
717 if (dup
&& ! mdoc_nmsg(mdoc
, n
, MANDOCERR_ARGVREP
))
721 n
->data
.Bl
->comp
= comp
;
723 n
->data
.Bl
->offs
= offs
;
725 n
->data
.Bl
->width
= width
;
727 /* Check: multiple list types. */
729 if (LIST__NONE
!= lt
&& n
->data
.Bl
->type
!= LIST__NONE
)
730 if ( ! mdoc_nmsg(mdoc
, n
, MANDOCERR_LISTREP
))
733 /* Assign list type. */
735 if (LIST__NONE
!= lt
&& n
->data
.Bl
->type
== LIST__NONE
) {
736 n
->data
.Bl
->type
= lt
;
737 /* Set column information, too. */
738 if (LIST_column
== lt
) {
741 n
->data
.Bl
->cols
= (const char **)
742 n
->args
->argv
[i
].value
;
746 /* The list type should come first. */
748 if (n
->data
.Bl
->type
== LIST__NONE
)
749 if (n
->data
.Bl
->width
||
752 if ( ! mdoc_nmsg(mdoc
, n
, MANDOCERR_LISTFIRST
))
758 /* Allow lists to default to LIST_item. */
760 if (LIST__NONE
== n
->data
.Bl
->type
) {
761 if ( ! mdoc_nmsg(mdoc
, n
, MANDOCERR_LISTTYPE
))
763 n
->data
.Bl
->type
= LIST_item
;
767 * Validate the width field. Some list types don't need width
768 * types and should be warned about them. Others should have it
769 * and must also be warned.
772 switch (n
->data
.Bl
->type
) {
774 if (n
->data
.Bl
->width
)
776 if (mdoc_nmsg(mdoc
, n
, MANDOCERR_NOWIDTHARG
))
788 if (NULL
== n
->data
.Bl
->width
)
790 if (mdoc_nmsg(mdoc
, n
, MANDOCERR_WIDTHARG
))
807 struct mdoc_node
*np
;
809 if (MDOC_BLOCK
!= n
->type
) {
810 if (ENDBODY_NOT
!= n
->end
) {
812 np
= n
->pending
->parent
;
817 assert(MDOC_BLOCK
== np
->type
);
818 assert(MDOC_Bd
== np
->tok
);
820 n
->data
.Bd
= np
->data
.Bd
;
824 assert(NULL
== n
->data
.Bd
);
825 n
->data
.Bd
= mandoc_calloc(1, sizeof(struct mdoc_bd
));
828 for (i
= 0; n
->args
&& i
< (int)n
->args
->argc
; i
++) {
833 switch (n
->args
->argv
[i
].arg
) {
840 case (MDOC_Unfilled
):
850 mdoc_nmsg(mdoc
, n
, MANDOCERR_BADDISP
);
853 /* NB: this can be empty! */
854 if (n
->args
->argv
[i
].sz
) {
855 offs
= n
->args
->argv
[i
].value
[0];
856 dup
= (NULL
!= n
->data
.Bd
->offs
);
859 if ( ! mdoc_nmsg(mdoc
, n
, MANDOCERR_IGNARGV
))
864 dup
= n
->data
.Bd
->comp
;
871 /* Check whether we have duplicates. */
873 if (dup
&& ! mdoc_nmsg(mdoc
, n
, MANDOCERR_ARGVREP
))
876 /* Make our auxiliary assignments. */
879 n
->data
.Bd
->offs
= offs
;
881 n
->data
.Bd
->comp
= comp
;
883 /* Check whether a type has already been assigned. */
885 if (DISP__NONE
!= dt
&& n
->data
.Bd
->type
!= DISP__NONE
)
886 if ( ! mdoc_nmsg(mdoc
, n
, MANDOCERR_DISPREP
))
889 /* Make our type assignment. */
891 if (DISP__NONE
!= dt
&& n
->data
.Bd
->type
== DISP__NONE
)
892 n
->data
.Bd
->type
= dt
;
895 if (DISP__NONE
== n
->data
.Bd
->type
) {
896 if ( ! mdoc_nmsg(mdoc
, n
, MANDOCERR_DISPTYPE
))
898 n
->data
.Bd
->type
= DISP_ragged
;
909 if (MDOC_BLOCK
!= n
->type
)
911 return(check_parent(mdoc
, n
, MDOC_Sh
, MDOC_BODY
));
919 if (MDOC_BLOCK
!= n
->type
)
922 mdoc
->regs
->regs
[(int)REG_nS
].set
= 0;
923 return(check_parent(mdoc
, n
, MDOC_MAX
, MDOC_ROOT
));
931 if (MDOC_BLOCK
!= n
->type
)
934 * FIXME: this can probably be lifted if we make the It into
935 * something else on-the-fly?
937 return(check_parent(mdoc
, n
, MDOC_Bl
, MDOC_BODY
));
949 for (i
= 1; i
< (int)n
->args
->argc
; i
++)
950 if ( ! mdoc_pmsg(mdoc
, n
->args
->argv
[i
].line
,
951 n
->args
->argv
[i
].pos
, MANDOCERR_IGNARGV
))
954 if (MDOC_Split
== n
->args
->argv
[0].arg
)
955 n
->data
.An
.auth
= AUTH_split
;
956 else if (MDOC_Nosplit
== n
->args
->argv
[0].arg
)
957 n
->data
.An
.auth
= AUTH_nosplit
;
968 return(check_stdarg(mdoc
, n
));
975 if (0 == mdoc
->meta
.date
|| mdoc
->meta
.os
)
976 if ( ! mdoc_nmsg(mdoc
, n
, MANDOCERR_PROLOGOOO
))
978 if (mdoc
->meta
.title
)
979 if ( ! mdoc_nmsg(mdoc
, n
, MANDOCERR_PROLOGREP
))
988 if (NULL
== mdoc
->meta
.title
|| 0 == mdoc
->meta
.date
)
989 if ( ! mdoc_nmsg(mdoc
, n
, MANDOCERR_PROLOGOOO
))
992 if ( ! mdoc_nmsg(mdoc
, n
, MANDOCERR_PROLOGREP
))
1001 if (mdoc
->meta
.title
|| mdoc
->meta
.os
)
1002 if ( ! mdoc_nmsg(mdoc
, n
, MANDOCERR_PROLOGOOO
))
1004 if (mdoc
->meta
.date
)
1005 if ( ! mdoc_nmsg(mdoc
, n
, MANDOCERR_PROLOGREP
))
1014 struct mdoc_node
*np
;
1018 * Unlike other data pointers, these are "housed" by the HEAD
1019 * element, which contains the goods.
1022 if (MDOC_HEAD
!= mdoc
->last
->type
) {
1023 if (ENDBODY_NOT
!= mdoc
->last
->end
) {
1024 assert(mdoc
->last
->pending
);
1025 np
= mdoc
->last
->pending
->parent
->head
;
1026 } else if (MDOC_BLOCK
!= mdoc
->last
->type
) {
1027 np
= mdoc
->last
->parent
->head
;
1029 np
= mdoc
->last
->head
;
1032 assert(MDOC_HEAD
== np
->type
);
1033 assert(MDOC_Bf
== np
->tok
);
1034 assert(np
->data
.Bf
);
1035 mdoc
->last
->data
.Bf
= np
->data
.Bf
;
1040 assert(MDOC_BLOCK
== np
->parent
->type
);
1041 assert(MDOC_Bf
== np
->parent
->tok
);
1042 np
->data
.Bf
= mandoc_calloc(1, sizeof(struct mdoc_bf
));
1045 * Cannot have both argument and parameter.
1046 * If neither is specified, let it through with a warning.
1049 if (np
->parent
->args
&& np
->child
) {
1050 mdoc_nmsg(mdoc
, np
, MANDOCERR_SYNTARGVCOUNT
);
1052 } else if (NULL
== np
->parent
->args
&& NULL
== np
->child
)
1053 return(mdoc_nmsg(mdoc
, np
, MANDOCERR_FONTTYPE
));
1055 /* Extract argument into data. */
1057 if (np
->parent
->args
) {
1058 arg
= np
->parent
->args
->argv
[0].arg
;
1059 if (MDOC_Emphasis
== arg
)
1060 np
->data
.Bf
->font
= FONT_Em
;
1061 else if (MDOC_Literal
== arg
)
1062 np
->data
.Bf
->font
= FONT_Li
;
1063 else if (MDOC_Symbolic
== arg
)
1064 np
->data
.Bf
->font
= FONT_Sy
;
1070 /* Extract parameter into data. */
1072 if (0 == strcmp(np
->child
->string
, "Em"))
1073 np
->data
.Bf
->font
= FONT_Em
;
1074 else if (0 == strcmp(np
->child
->string
, "Li"))
1075 np
->data
.Bf
->font
= FONT_Li
;
1076 else if (0 == strcmp(np
->child
->string
, "Sy"))
1077 np
->data
.Bf
->font
= FONT_Sy
;
1078 else if ( ! mdoc_nmsg(mdoc
, np
, MANDOCERR_FONTTYPE
))
1091 assert(mdoc
->last
->child
);
1092 assert(MDOC_TEXT
== mdoc
->last
->child
->type
);
1094 p
= mdoc_a2lib(mdoc
->last
->child
->string
);
1096 /* If lookup ok, replace with table value. */
1099 free(mdoc
->last
->child
->string
);
1100 mdoc
->last
->child
->string
= mandoc_strdup(p
);
1104 /* If not, use "library ``xxxx''. */
1106 sz
= strlen(mdoc
->last
->child
->string
) +
1107 2 + strlen("\\(lqlibrary\\(rq");
1108 buf
= mandoc_malloc(sz
);
1109 snprintf(buf
, sz
, "library \\(lq%s\\(rq",
1110 mdoc
->last
->child
->string
);
1111 free(mdoc
->last
->child
->string
);
1112 mdoc
->last
->child
->string
= buf
;
1117 post_eoln(POST_ARGS
)
1120 if (NULL
== mdoc
->last
->child
)
1122 return(mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_ARGSLOST
));
1129 const struct mdoc_node
*n
;
1132 * The Vt macro comes in both ELEM and BLOCK form, both of which
1133 * have different syntaxes (yet more context-sensitive
1134 * behaviour). ELEM types must have a child; BLOCK types,
1135 * specifically the BODY, should only have TEXT children.
1138 if (MDOC_ELEM
== mdoc
->last
->type
)
1139 return(eerr_ge1(mdoc
));
1140 if (MDOC_BODY
!= mdoc
->last
->type
)
1143 for (n
= mdoc
->last
->child
; n
; n
= n
->next
)
1144 if (MDOC_TEXT
!= n
->type
)
1145 if ( ! mdoc_nmsg(mdoc
, n
, MANDOCERR_CHILD
))
1157 /* If no child specified, make sure we have the meta name. */
1159 if (NULL
== mdoc
->last
->child
&& NULL
== mdoc
->meta
.name
) {
1160 mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_NONAME
);
1162 } else if (mdoc
->meta
.name
)
1165 /* If no meta name, set it from the child. */
1167 if ( ! concat(mdoc
, buf
, mdoc
->last
->child
, BUFSIZ
))
1170 mdoc
->meta
.name
= mandoc_strdup(buf
);
1176 post_literal(POST_ARGS
)
1180 * The `Dl' (note "el" not "one") and `Bd' macros unset the
1181 * MDOC_LITERAL flag as they leave. Note that `Bd' only sets
1182 * this in literal mode, but it doesn't hurt to just switch it
1183 * off in general since displays can't be nested.
1186 if (MDOC_BODY
== mdoc
->last
->type
)
1187 mdoc
->last
->flags
&= ~MDOC_LITERAL
;
1193 post_defaults(POST_ARGS
)
1195 struct mdoc_node
*nn
;
1198 * The `Ar' defaults to "file ..." if no value is provided as an
1199 * argument; the `Mt' and `Pa' macros use "~"; the `Li' just
1200 * gets an empty string.
1203 if (mdoc
->last
->child
)
1207 mdoc
->next
= MDOC_NEXT_CHILD
;
1211 if ( ! mdoc_word_alloc(mdoc
, nn
->line
, nn
->pos
, "file"))
1213 if ( ! mdoc_word_alloc(mdoc
, nn
->line
, nn
->pos
, "..."))
1217 if ( ! mdoc_word_alloc(mdoc
, nn
->line
, nn
->pos
, "AT&T"))
1219 if ( ! mdoc_word_alloc(mdoc
, nn
->line
, nn
->pos
, "UNIX"))
1223 if ( ! mdoc_word_alloc(mdoc
, nn
->line
, nn
->pos
, ""))
1229 if ( ! mdoc_word_alloc(mdoc
, nn
->line
, nn
->pos
, "~"))
1249 * If we have a child, look it up in the standard keys. If a
1250 * key exist, use that instead of the child; if it doesn't,
1251 * prefix "AT&T UNIX " to the existing data.
1254 if (NULL
== mdoc
->last
->child
)
1257 assert(MDOC_TEXT
== mdoc
->last
->child
->type
);
1258 p
= mdoc_a2att(mdoc
->last
->child
->string
);
1261 free(mdoc
->last
->child
->string
);
1262 mdoc
->last
->child
->string
= mandoc_strdup(p
);
1264 mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_BADATT
);
1266 q
= mdoc
->last
->child
->string
;
1267 sz
= strlen(p
) + strlen(q
) + 1;
1268 buf
= mandoc_malloc(sz
);
1269 strlcpy(buf
, p
, sz
);
1270 strlcat(buf
, q
, sz
);
1271 free(mdoc
->last
->child
->string
);
1272 mdoc
->last
->child
->string
= buf
;
1281 struct mdoc_node
*np
;
1284 if (AUTH__NONE
!= np
->data
.An
.auth
&& np
->child
)
1285 return(eerr_eq0(mdoc
));
1287 * FIXME: make this ewarn and make sure that the front-ends
1288 * don't print the arguments.
1290 if (AUTH__NONE
!= np
->data
.An
.auth
|| np
->child
)
1292 return(mdoc_nmsg(mdoc
, np
, MANDOCERR_NOARGS
));
1301 struct mdoc_node
*n
, *c
;
1304 if (MDOC_BLOCK
!= mdoc
->last
->type
)
1307 n
= mdoc
->last
->parent
->parent
;
1309 lt
= n
->data
.Bl
->type
;
1311 if (LIST__NONE
== lt
) {
1312 mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_LISTTYPE
);
1318 if (mdoc
->last
->head
->child
)
1320 /* FIXME: give this a dummy value. */
1321 if ( ! mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_NOARGS
))
1331 if (NULL
== mdoc
->last
->head
->child
)
1332 if ( ! mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_NOARGS
))
1342 if (NULL
== mdoc
->last
->body
->child
)
1343 if ( ! mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_NOBODY
))
1347 if (mdoc
->last
->head
->child
)
1348 if ( ! mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_ARGSLOST
))
1352 cols
= (int)n
->data
.Bl
->ncols
;
1354 assert(NULL
== mdoc
->last
->head
->child
);
1356 if (NULL
== mdoc
->last
->body
->child
)
1357 if ( ! mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_NOBODY
))
1360 for (i
= 0, c
= mdoc
->last
->child
; c
; c
= c
->next
)
1361 if (MDOC_BODY
== c
->type
)
1365 er
= MANDOCERR_ARGCOUNT
;
1366 else if (i
== cols
|| i
== cols
+ 1)
1369 er
= MANDOCERR_SYNTARGCOUNT
;
1371 rc
= mdoc_vmsg(mdoc
, er
,
1372 mdoc
->last
->line
, mdoc
->last
->pos
,
1373 "columns == %d (have %d)", cols
, i
);
1383 post_bl_block(POST_ARGS
)
1385 struct mdoc_node
*n
;
1388 * These are fairly complicated, so we've broken them into two
1389 * functions. post_bl_block_tag() is called when a -tag is
1390 * specified, but no -width (it must be guessed). The second
1391 * when a -width is specified (macro indicators must be
1392 * rewritten into real lengths).
1397 if (LIST_tag
== n
->data
.Bl
->type
&&
1398 NULL
== n
->data
.Bl
->width
) {
1399 if ( ! post_bl_block_tag(mdoc
))
1401 } else if (NULL
!= n
->data
.Bl
->width
) {
1402 if ( ! post_bl_block_width(mdoc
))
1407 assert(n
->data
.Bl
->width
);
1412 post_bl_block_width(POST_ARGS
)
1417 struct mdoc_node
*n
;
1423 * Calculate the real width of a list from the -width string,
1424 * which may contain a macro (with a known default width), a
1425 * literal string, or a scaling width.
1427 * If the value to -width is a macro, then we re-write it to be
1428 * the macro's width as set in share/tmac/mdoc/doc-common.
1431 if (0 == strcmp(n
->data
.Bl
->width
, "Ds"))
1433 else if (MDOC_MAX
== (tok
= mdoc_hash_find(n
->data
.Bl
->width
)))
1435 else if (0 == (width
= mdoc_macro2len(tok
)))
1436 return(mdoc_nmsg(mdoc
, n
, MANDOCERR_BADWIDTH
));
1438 /* The value already exists: free and reallocate it. */
1442 for (i
= 0; i
< (int)n
->args
->argc
; i
++)
1443 if (MDOC_Width
== n
->args
->argv
[i
].arg
)
1446 assert(i
< (int)n
->args
->argc
);
1448 snprintf(buf
, NUMSIZ
, "%zun", width
);
1449 free(n
->args
->argv
[i
].value
[0]);
1450 n
->args
->argv
[i
].value
[0] = mandoc_strdup(buf
);
1452 /* Set our width! */
1453 n
->data
.Bl
->width
= n
->args
->argv
[i
].value
[0];
1458 post_bl_block_tag(POST_ARGS
)
1460 struct mdoc_node
*n
, *nn
;
1466 * Calculate the -width for a `Bl -tag' list if it hasn't been
1467 * provided. Uses the first head macro. NOTE AGAIN: this is
1468 * ONLY if the -width argument has NOT been provided. See
1469 * post_bl_block_width() for converting the -width string.
1475 for (nn
= n
->body
->child
; nn
; nn
= nn
->next
) {
1476 if (MDOC_It
!= nn
->tok
)
1479 assert(MDOC_BLOCK
== nn
->type
);
1480 nn
= nn
->head
->child
;
1483 /* No -width for .Bl and first .It is emtpy */
1484 if ( ! mdoc_nmsg(mdoc
, n
, MANDOCERR_NOWIDTHARG
))
1489 if (MDOC_TEXT
== nn
->type
) {
1490 sz
= strlen(nn
->string
) + 1;
1494 if (0 != (ssz
= mdoc_macro2len(nn
->tok
)))
1496 else if ( ! mdoc_nmsg(mdoc
, n
, MANDOCERR_NOWIDTHARG
))
1502 /* Defaults to ten ens. */
1504 snprintf(buf
, NUMSIZ
, "%zun", sz
);
1507 * We have to dynamically add this to the macro's argument list.
1508 * We're guaranteed that a MDOC_Width doesn't already exist.
1512 i
= (int)(n
->args
->argc
)++;
1514 n
->args
->argv
= mandoc_realloc(n
->args
->argv
,
1515 n
->args
->argc
* sizeof(struct mdoc_argv
));
1517 n
->args
->argv
[i
].arg
= MDOC_Width
;
1518 n
->args
->argv
[i
].line
= n
->line
;
1519 n
->args
->argv
[i
].pos
= n
->pos
;
1520 n
->args
->argv
[i
].sz
= 1;
1521 n
->args
->argv
[i
].value
= mandoc_malloc(sizeof(char *));
1522 n
->args
->argv
[i
].value
[0] = mandoc_strdup(buf
);
1524 /* Set our width! */
1525 n
->data
.Bl
->width
= n
->args
->argv
[i
].value
[0];
1531 post_bl_head(POST_ARGS
)
1533 struct mdoc_node
*np
, *nn
, *nnp
;
1536 if (LIST_column
!= mdoc
->last
->data
.Bl
->type
)
1537 /* FIXME: this should be ERROR class... */
1538 return(hwarn_eq0(mdoc
));
1541 * Convert old-style lists, where the column width specifiers
1542 * trail as macro parameters, to the new-style ("normal-form")
1543 * lists where they're argument values following -column.
1546 /* First, disallow both types and allow normal-form. */
1549 * TODO: technically, we can accept both and just merge the two
1550 * lists, but I'll leave that for another day.
1553 if (mdoc
->last
->data
.Bl
->ncols
&& mdoc
->last
->nchild
) {
1554 mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_COLUMNS
);
1556 } else if (NULL
== mdoc
->last
->child
)
1559 np
= mdoc
->last
->parent
;
1562 for (j
= 0; j
< (int)np
->args
->argc
; j
++)
1563 if (MDOC_Column
== np
->args
->argv
[j
].arg
)
1566 assert(j
< (int)np
->args
->argc
);
1567 assert(0 == np
->args
->argv
[j
].sz
);
1570 * Accomodate for new-style groff column syntax. Shuffle the
1571 * child nodes, all of which must be TEXT, as arguments for the
1572 * column field. Then, delete the head children.
1575 np
->args
->argv
[j
].sz
= (size_t)mdoc
->last
->nchild
;
1576 np
->args
->argv
[j
].value
= mandoc_malloc
1577 ((size_t)mdoc
->last
->nchild
* sizeof(char *));
1579 mdoc
->last
->data
.Bl
->ncols
= np
->args
->argv
[j
].sz
;
1580 mdoc
->last
->data
.Bl
->cols
= (const char **)np
->args
->argv
[j
].value
;
1582 for (i
= 0, nn
= mdoc
->last
->child
; nn
; i
++) {
1583 np
->args
->argv
[j
].value
[i
] = nn
->string
;
1587 mdoc_node_delete(NULL
, nnp
);
1590 mdoc
->last
->nchild
= 0;
1591 mdoc
->last
->child
= NULL
;
1599 struct mdoc_node
*n
;
1601 if (MDOC_HEAD
== mdoc
->last
->type
)
1602 return(post_bl_head(mdoc
));
1603 if (MDOC_BLOCK
== mdoc
->last
->type
)
1604 return(post_bl_block(mdoc
));
1605 if (MDOC_BODY
!= mdoc
->last
->type
)
1607 if (NULL
== mdoc
->last
->child
)
1611 * We only allow certain children of `Bl'. This is usually on
1612 * `It', but apparently `Sm' occurs here and there, so we let
1613 * that one through, too.
1617 for (n
= mdoc
->last
->child
; n
; n
= n
->next
) {
1618 if (MDOC_BLOCK
== n
->type
&& MDOC_It
== n
->tok
)
1620 if (MDOC_Sm
== n
->tok
)
1622 mdoc_nmsg(mdoc
, n
, MANDOCERR_SYNTCHILD
);
1631 ebool(struct mdoc
*mdoc
)
1633 struct mdoc_node
*n
;
1636 for (n
= mdoc
->last
->child
; n
; n
= n
->next
) {
1637 if (MDOC_TEXT
!= n
->type
)
1639 if (0 == strcmp(n
->string
, "on"))
1641 if (0 == strcmp(n
->string
, "off"))
1648 return(mdoc_nmsg(mdoc
, n
, MANDOCERR_BADBOOL
));
1653 post_root(POST_ARGS
)
1656 if (NULL
== mdoc
->first
->child
)
1657 mdoc_nmsg(mdoc
, mdoc
->first
, MANDOCERR_NODOCBODY
);
1658 else if ( ! (MDOC_PBODY
& mdoc
->flags
))
1659 mdoc_nmsg(mdoc
, mdoc
->first
, MANDOCERR_NODOCPROLOG
);
1660 else if (MDOC_BLOCK
!= mdoc
->first
->child
->type
)
1661 mdoc_nmsg(mdoc
, mdoc
->first
, MANDOCERR_NODOCBODY
);
1662 else if (MDOC_Sh
!= mdoc
->first
->child
->tok
)
1663 mdoc_nmsg(mdoc
, mdoc
->first
, MANDOCERR_NODOCBODY
);
1675 assert(MDOC_TEXT
== mdoc
->last
->child
->type
);
1677 p
= mdoc_a2st(mdoc
->last
->child
->string
);
1680 mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_BADSTANDARD
);
1681 mdoc_node_delete(mdoc
, mdoc
->last
);
1683 free(mdoc
->last
->child
->string
);
1684 mdoc
->last
->child
->string
= mandoc_strdup(p
);
1693 struct mdoc_node
*nn
, *next
, *prev
;
1696 if (MDOC_BODY
!= mdoc
->last
->type
)
1700 * Make sure only certain types of nodes are allowed within the
1701 * the `Rs' body. Delete offending nodes and raise a warning.
1702 * Do this before re-ordering for the sake of clarity.
1706 for (nn
= mdoc
->last
->child
; nn
; nn
= next
) {
1707 for (i
= 0; i
< RSORD_MAX
; i
++)
1708 if (nn
->tok
== rsord
[i
])
1711 if (i
< RSORD_MAX
) {
1717 mdoc_nmsg(mdoc
, nn
, MANDOCERR_CHILD
);
1718 mdoc_node_delete(mdoc
, nn
);
1722 * The full `Rs' block needs special handling to order the
1723 * sub-elements according to `rsord'. Pick through each element
1724 * and correctly order it. This is a insertion sort.
1728 for (nn
= mdoc
->last
->child
->next
; nn
; nn
= next
) {
1729 /* Determine order of `nn'. */
1730 for (i
= 0; i
< RSORD_MAX
; i
++)
1731 if (rsord
[i
] == nn
->tok
)
1735 * Remove `nn' from the chain. This somewhat
1736 * repeats mdoc_node_unlink(), but since we're
1737 * just re-ordering, there's no need for the
1738 * full unlink process.
1741 if (NULL
!= (next
= nn
->next
))
1742 next
->prev
= nn
->prev
;
1744 if (NULL
!= (prev
= nn
->prev
))
1745 prev
->next
= nn
->next
;
1747 nn
->prev
= nn
->next
= NULL
;
1750 * Scan back until we reach a node that's
1751 * ordered before `nn'.
1754 for ( ; prev
; prev
= prev
->prev
) {
1755 /* Determine order of `prev'. */
1756 for (j
= 0; j
< RSORD_MAX
; j
++)
1757 if (rsord
[j
] == prev
->tok
)
1765 * Set `nn' back into its correct place in front
1766 * of the `prev' node.
1773 prev
->next
->prev
= nn
;
1774 nn
->next
= prev
->next
;
1777 mdoc
->last
->child
->prev
= nn
;
1778 nn
->next
= mdoc
->last
->child
;
1779 mdoc
->last
->child
= nn
;
1791 if (MDOC_HEAD
== mdoc
->last
->type
)
1792 return(post_sh_head(mdoc
));
1793 if (MDOC_BODY
== mdoc
->last
->type
)
1794 return(post_sh_body(mdoc
));
1801 post_sh_body(POST_ARGS
)
1803 struct mdoc_node
*n
;
1805 if (SEC_NAME
!= mdoc
->lastsec
)
1809 * Warn if the NAME section doesn't contain the `Nm' and `Nd'
1810 * macros (can have multiple `Nm' and one `Nd'). Note that the
1811 * children of the BODY declaration can also be "text".
1814 if (NULL
== (n
= mdoc
->last
->child
))
1815 return(mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_BADNAMESEC
));
1817 for ( ; n
&& n
->next
; n
= n
->next
) {
1818 if (MDOC_ELEM
== n
->type
&& MDOC_Nm
== n
->tok
)
1820 if (MDOC_TEXT
== n
->type
)
1822 if ( ! mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_BADNAMESEC
))
1827 if (MDOC_BLOCK
== n
->type
&& MDOC_Nd
== n
->tok
)
1829 return(mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_BADNAMESEC
));
1834 post_sh_head(POST_ARGS
)
1840 * Process a new section. Sections are either "named" or
1841 * "custom". Custom sections are user-defined, while named ones
1842 * follow a conventional order and may only appear in certain
1846 if ( ! concat(mdoc
, buf
, mdoc
->last
->child
, BUFSIZ
))
1849 sec
= mdoc_str2sec(buf
);
1851 /* The NAME should be first. */
1853 if (SEC_NAME
!= sec
&& SEC_NONE
== mdoc
->lastnamed
)
1854 mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_NAMESECFIRST
);
1856 /* The SYNOPSIS gets special attention in other areas. */
1858 if (SEC_SYNOPSIS
== sec
)
1859 mdoc
->flags
|= MDOC_SYNOPSIS
;
1861 mdoc
->flags
&= ~MDOC_SYNOPSIS
;
1863 /* Mark our last section. */
1865 mdoc
->lastsec
= sec
;
1867 /* We don't care about custom sections after this. */
1869 if (SEC_CUSTOM
== sec
)
1873 * Check whether our non-custom section is being repeated or is
1877 if (sec
== mdoc
->lastnamed
)
1878 mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_SECREP
);
1880 if (sec
< mdoc
->lastnamed
)
1881 mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_SECOOO
);
1883 /* Mark the last named section. */
1885 mdoc
->lastnamed
= sec
;
1887 /* Check particular section/manual conventions. */
1889 assert(mdoc
->meta
.msec
);
1892 case (SEC_RETURN_VALUES
):
1897 if (*mdoc
->meta
.msec
== '2')
1899 if (*mdoc
->meta
.msec
== '3')
1901 if (*mdoc
->meta
.msec
== '9')
1903 mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_SECMSEC
);
1916 if (NULL
== mdoc
->last
)
1920 * Don't allow prior `Lp' or `Pp' prior to a paragraph-type
1921 * block: `Lp', `Pp', or non-compact `Bd' or `Bl'.
1924 if (MDOC_Pp
!= mdoc
->last
->tok
&& MDOC_Lp
!= mdoc
->last
->tok
)
1927 if (MDOC_Bl
== n
->tok
&& n
->data
.Bl
->comp
)
1929 if (MDOC_Bd
== n
->tok
&& n
->data
.Bd
->comp
)
1932 mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_IGNPAR
);
1933 mdoc_node_delete(mdoc
, mdoc
->last
);
1938 pre_literal(PRE_ARGS
)
1941 if (MDOC_BODY
!= n
->type
)
1945 * The `Dl' (note "el" not "one") and `Bd -literal' and `Bd
1946 * -unfilled' macros set MDOC_LITERAL on entrance to the body.
1951 mdoc
->flags
|= MDOC_LITERAL
;
1955 if (DISP_literal
== n
->data
.Bd
->type
)
1956 mdoc
->flags
|= MDOC_LITERAL
;
1957 if (DISP_unfilled
== n
->data
.Bd
->type
)
1958 mdoc
->flags
|= MDOC_LITERAL
;
1972 struct mdoc_node
*n
;
1976 if (NULL
== n
->child
) {
1977 mdoc
->meta
.date
= time(NULL
);
1978 return(post_prol(mdoc
));
1981 if ( ! concat(mdoc
, buf
, n
->child
, DATESIZ
))
1984 mdoc
->meta
.date
= mandoc_a2time
1985 (MTIME_MDOCDATE
| MTIME_CANONICAL
, buf
);
1987 if (0 == mdoc
->meta
.date
) {
1988 if ( ! mdoc_nmsg(mdoc
, n
, MANDOCERR_BADDATE
))
1990 mdoc
->meta
.date
= time(NULL
);
1999 struct mdoc_node
*nn
, *n
;
2005 if (mdoc
->meta
.title
)
2006 free(mdoc
->meta
.title
);
2008 free(mdoc
->meta
.vol
);
2009 if (mdoc
->meta
.arch
)
2010 free(mdoc
->meta
.arch
);
2012 mdoc
->meta
.title
= mdoc
->meta
.vol
= mdoc
->meta
.arch
= NULL
;
2014 /* First make all characters uppercase. */
2016 if (NULL
!= (nn
= n
->child
))
2017 for (p
= nn
->string
; *p
; p
++) {
2018 if (toupper((u_char
)*p
) == *p
)
2020 if ( ! mdoc_nmsg(mdoc
, nn
, MANDOCERR_UPPERCASE
))
2026 * --> title = unknown, volume = local, msec = 0, arch = NULL
2029 if (NULL
== (nn
= n
->child
)) {
2030 /* XXX: make these macro values. */
2031 /* FIXME: warn about missing values. */
2032 mdoc
->meta
.title
= mandoc_strdup("UNKNOWN");
2033 mdoc
->meta
.vol
= mandoc_strdup("LOCAL");
2034 mdoc
->meta
.msec
= mandoc_strdup("1");
2038 /* Handles: `.Dt TITLE'
2039 * --> title = TITLE, volume = local, msec = 0, arch = NULL
2042 mdoc
->meta
.title
= mandoc_strdup
2043 ('\0' == nn
->string
[0] ? "UNKNOWN" : nn
->string
);
2045 if (NULL
== (nn
= nn
->next
)) {
2046 /* FIXME: warn about missing msec. */
2047 /* XXX: make this a macro value. */
2048 mdoc
->meta
.vol
= mandoc_strdup("LOCAL");
2049 mdoc
->meta
.msec
= mandoc_strdup("1");
2053 /* Handles: `.Dt TITLE SEC'
2054 * --> title = TITLE, volume = SEC is msec ?
2055 * format(msec) : SEC,
2056 * msec = SEC is msec ? atoi(msec) : 0,
2060 cp
= mdoc_a2msec(nn
->string
);
2062 mdoc
->meta
.vol
= mandoc_strdup(cp
);
2063 mdoc
->meta
.msec
= mandoc_strdup(nn
->string
);
2064 } else if (mdoc_nmsg(mdoc
, n
, MANDOCERR_BADMSEC
)) {
2065 mdoc
->meta
.vol
= mandoc_strdup(nn
->string
);
2066 mdoc
->meta
.msec
= mandoc_strdup(nn
->string
);
2070 if (NULL
== (nn
= nn
->next
))
2073 /* Handles: `.Dt TITLE SEC VOL'
2074 * --> title = TITLE, volume = VOL is vol ?
2076 * VOL is arch ? format(arch) :
2080 cp
= mdoc_a2vol(nn
->string
);
2082 free(mdoc
->meta
.vol
);
2083 mdoc
->meta
.vol
= mandoc_strdup(cp
);
2085 /* FIXME: warn about bad arch. */
2086 cp
= mdoc_a2arch(nn
->string
);
2088 free(mdoc
->meta
.vol
);
2089 mdoc
->meta
.vol
= mandoc_strdup(nn
->string
);
2091 mdoc
->meta
.arch
= mandoc_strdup(cp
);
2094 /* Ignore any subsequent parameters... */
2095 /* FIXME: warn about subsequent parameters. */
2101 post_prol(POST_ARGS
)
2104 * Remove prologue macros from the document after they're
2105 * processed. The final document uses mdoc_meta for these
2106 * values and discards the originals.
2109 mdoc_node_delete(mdoc
, mdoc
->last
);
2110 if (mdoc
->meta
.title
&& mdoc
->meta
.date
&& mdoc
->meta
.os
)
2111 mdoc
->flags
|= MDOC_PBODY
;
2119 struct mdoc_node
*n
;
2122 struct utsname utsname
;
2128 * Set the operating system by way of the `Os' macro. Note that
2129 * if an argument isn't provided and -DOSNAME="\"foo\"" is
2130 * provided during compilation, this value will be used instead
2131 * of filling in "sysname release" from uname().
2135 free(mdoc
->meta
.os
);
2137 if ( ! concat(mdoc
, buf
, n
->child
, BUFSIZ
))
2140 /* XXX: yes, these can all be dynamically-adjusted buffers, but
2141 * it's really not worth the extra hackery.
2144 if ('\0' == buf
[0]) {
2146 if (strlcat(buf
, OSNAME
, BUFSIZ
) >= BUFSIZ
) {
2147 mdoc_nmsg(mdoc
, n
, MANDOCERR_MEM
);
2151 if (-1 == uname(&utsname
))
2152 return(mdoc_nmsg(mdoc
, n
, MANDOCERR_UTSNAME
));
2154 if (strlcat(buf
, utsname
.sysname
, BUFSIZ
) >= BUFSIZ
) {
2155 mdoc_nmsg(mdoc
, n
, MANDOCERR_MEM
);
2158 if (strlcat(buf
, " ", 64) >= BUFSIZ
) {
2159 mdoc_nmsg(mdoc
, n
, MANDOCERR_MEM
);
2162 if (strlcat(buf
, utsname
.release
, BUFSIZ
) >= BUFSIZ
) {
2163 mdoc_nmsg(mdoc
, n
, MANDOCERR_MEM
);
2169 mdoc
->meta
.os
= mandoc_strdup(buf
);
2176 struct mdoc_node
*nn
, *n
;
2181 * Macros accepting `-std' as an argument have the name of the
2182 * current document (`Nm') filled in as the argument if it's not
2188 if (NULL
== mdoc
->meta
.name
)
2192 mdoc
->next
= MDOC_NEXT_CHILD
;
2194 if ( ! mdoc_word_alloc(mdoc
, n
->line
, n
->pos
, mdoc
->meta
.name
))
2201 concat(struct mdoc
*m
, char *p
, const struct mdoc_node
*n
, size_t sz
)
2207 * Concatenate sibling nodes together. All siblings must be of
2208 * type MDOC_TEXT or an assertion is raised. Concatenation is
2209 * separated by a single whitespace.
2212 for ( ; n
; n
= n
->next
) {
2213 assert(MDOC_TEXT
== n
->type
);
2215 if (strlcat(p
, n
->string
, sz
) >= sz
) {
2216 mdoc_nmsg(m
, n
, MANDOCERR_MEM
);
2220 if (NULL
== n
->next
)
2223 if (strlcat(p
, " ", sz
) >= sz
) {
2224 mdoc_nmsg(m
, n
, MANDOCERR_MEM
);