]>
git.cameronkatri.com Git - mandoc.git/blob - mdoc_validate.c
1aa743da2fc76e0e98651056f94c28e08fcd4a4e
1 /* $Id: mdoc_validate.c,v 1.220 2014/07/02 11:43:20 schwarze Exp $ */
3 * Copyright (c) 2008-2012 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2010-2014 Ingo Schwarze <schwarze@openbsd.org>
5 * Copyright (c) 2010 Joerg Sonnenberger <joerg@netbsd.org>
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
24 #include <sys/utsname.h>
27 #include <sys/types.h>
39 #include "mandoc_aux.h"
41 #include "libmandoc.h"
43 /* FIXME: .Bl -diag can't have non-text children in HEAD. */
45 #define PRE_ARGS struct mdoc *mdoc, struct mdoc_node *n
46 #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 void check_text(struct mdoc
*, int, int, char *);
71 static void check_argv(struct mdoc
*,
72 struct mdoc_node
*, struct mdoc_argv
*);
73 static void check_args(struct mdoc
*, struct mdoc_node
*);
74 static enum mdoc_sec
a2sec(const char *);
75 static size_t macro2len(enum mdoct
);
77 static int ebool(POST_ARGS
);
78 static int berr_ge1(POST_ARGS
);
79 static int bwarn_ge1(POST_ARGS
);
80 static int ewarn_eq0(POST_ARGS
);
81 static int ewarn_eq1(POST_ARGS
);
82 static int ewarn_ge1(POST_ARGS
);
83 static int ewarn_le1(POST_ARGS
);
84 static int hwarn_eq0(POST_ARGS
);
85 static int hwarn_eq1(POST_ARGS
);
86 static int hwarn_ge1(POST_ARGS
);
87 static int hwarn_le1(POST_ARGS
);
89 static int post_an(POST_ARGS
);
90 static int post_at(POST_ARGS
);
91 static int post_bf(POST_ARGS
);
92 static int post_bl(POST_ARGS
);
93 static int post_bl_block(POST_ARGS
);
94 static int post_bl_block_width(POST_ARGS
);
95 static int post_bl_block_tag(POST_ARGS
);
96 static int post_bl_head(POST_ARGS
);
97 static int post_bx(POST_ARGS
);
98 static int post_defaults(POST_ARGS
);
99 static int post_dd(POST_ARGS
);
100 static int post_dt(POST_ARGS
);
101 static int post_en(POST_ARGS
);
102 static int post_es(POST_ARGS
);
103 static int post_eoln(POST_ARGS
);
104 static int post_hyph(POST_ARGS
);
105 static int post_ignpar(POST_ARGS
);
106 static int post_it(POST_ARGS
);
107 static int post_lb(POST_ARGS
);
108 static int post_literal(POST_ARGS
);
109 static int post_nm(POST_ARGS
);
110 static int post_ns(POST_ARGS
);
111 static int post_os(POST_ARGS
);
112 static int post_par(POST_ARGS
);
113 static int post_prol(POST_ARGS
);
114 static int post_root(POST_ARGS
);
115 static int post_rs(POST_ARGS
);
116 static int post_sh(POST_ARGS
);
117 static int post_sh_body(POST_ARGS
);
118 static int post_sh_head(POST_ARGS
);
119 static int post_st(POST_ARGS
);
120 static int post_std(POST_ARGS
);
121 static int post_vt(POST_ARGS
);
122 static int pre_an(PRE_ARGS
);
123 static int pre_bd(PRE_ARGS
);
124 static int pre_bl(PRE_ARGS
);
125 static int pre_dd(PRE_ARGS
);
126 static int pre_display(PRE_ARGS
);
127 static int pre_dt(PRE_ARGS
);
128 static int pre_it(PRE_ARGS
);
129 static int pre_literal(PRE_ARGS
);
130 static int pre_obsolete(PRE_ARGS
);
131 static int pre_os(PRE_ARGS
);
132 static int pre_par(PRE_ARGS
);
133 static int pre_sh(PRE_ARGS
);
134 static int pre_ss(PRE_ARGS
);
135 static int pre_std(PRE_ARGS
);
137 static v_post posts_an
[] = { post_an
, NULL
};
138 static v_post posts_at
[] = { post_at
, post_defaults
, NULL
};
139 static v_post posts_bd
[] = { post_literal
, hwarn_eq0
, bwarn_ge1
, NULL
};
140 static v_post posts_bf
[] = { hwarn_le1
, post_bf
, NULL
};
141 static v_post posts_bk
[] = { hwarn_eq0
, bwarn_ge1
, NULL
};
142 static v_post posts_bl
[] = { bwarn_ge1
, post_bl
, NULL
};
143 static v_post posts_bx
[] = { post_bx
, NULL
};
144 static v_post posts_bool
[] = { ebool
, NULL
};
145 static v_post posts_eoln
[] = { post_eoln
, NULL
};
146 static v_post posts_defaults
[] = { post_defaults
, NULL
};
147 static v_post posts_d1
[] = { bwarn_ge1
, post_hyph
, NULL
};
148 static v_post posts_dd
[] = { post_dd
, post_prol
, NULL
};
149 static v_post posts_dl
[] = { post_literal
, bwarn_ge1
, NULL
};
150 static v_post posts_dt
[] = { post_dt
, post_prol
, NULL
};
151 static v_post posts_en
[] = { post_en
, NULL
};
152 static v_post posts_es
[] = { post_es
, NULL
};
153 static v_post posts_fo
[] = { hwarn_eq1
, bwarn_ge1
, NULL
};
154 static v_post posts_hyph
[] = { post_hyph
, NULL
};
155 static v_post posts_hyphtext
[] = { ewarn_ge1
, post_hyph
, NULL
};
156 static v_post posts_it
[] = { post_it
, NULL
};
157 static v_post posts_lb
[] = { post_lb
, NULL
};
158 static v_post posts_nd
[] = { berr_ge1
, post_hyph
, NULL
};
159 static v_post posts_nm
[] = { post_nm
, NULL
};
160 static v_post posts_notext
[] = { ewarn_eq0
, NULL
};
161 static v_post posts_ns
[] = { post_ns
, NULL
};
162 static v_post posts_os
[] = { post_os
, post_prol
, NULL
};
163 static v_post posts_pp
[] = { post_par
, ewarn_eq0
, NULL
};
164 static v_post posts_rs
[] = { post_rs
, NULL
};
165 static v_post posts_sh
[] = { post_ignpar
,hwarn_ge1
,post_sh
,post_hyph
,NULL
};
166 static v_post posts_sp
[] = { post_par
, ewarn_le1
, NULL
};
167 static v_post posts_ss
[] = { post_ignpar
, hwarn_ge1
, post_hyph
, NULL
};
168 static v_post posts_st
[] = { post_st
, NULL
};
169 static v_post posts_std
[] = { post_std
, NULL
};
170 static v_post posts_text
[] = { ewarn_ge1
, NULL
};
171 static v_post posts_text1
[] = { ewarn_eq1
, NULL
};
172 static v_post posts_vt
[] = { post_vt
, NULL
};
173 static v_pre pres_an
[] = { pre_an
, NULL
};
174 static v_pre pres_bd
[] = { pre_display
, pre_bd
, pre_literal
, pre_par
, NULL
};
175 static v_pre pres_bl
[] = { pre_bl
, pre_par
, NULL
};
176 static v_pre pres_d1
[] = { pre_display
, NULL
};
177 static v_pre pres_dl
[] = { pre_literal
, pre_display
, NULL
};
178 static v_pre pres_dd
[] = { pre_dd
, NULL
};
179 static v_pre pres_dt
[] = { pre_dt
, NULL
};
180 static v_pre pres_it
[] = { pre_it
, pre_par
, NULL
};
181 static v_pre pres_obsolete
[] = { pre_obsolete
, NULL
};
182 static v_pre pres_os
[] = { pre_os
, NULL
};
183 static v_pre pres_pp
[] = { pre_par
, NULL
};
184 static v_pre pres_sh
[] = { pre_sh
, NULL
};
185 static v_pre pres_ss
[] = { pre_ss
, NULL
};
186 static v_pre pres_std
[] = { pre_std
, NULL
};
188 static const struct valids mdoc_valids
[MDOC_MAX
] = {
189 { NULL
, NULL
}, /* Ap */
190 { pres_dd
, posts_dd
}, /* Dd */
191 { pres_dt
, posts_dt
}, /* Dt */
192 { pres_os
, posts_os
}, /* Os */
193 { pres_sh
, posts_sh
}, /* Sh */
194 { pres_ss
, posts_ss
}, /* Ss */
195 { pres_pp
, posts_pp
}, /* Pp */
196 { pres_d1
, posts_d1
}, /* D1 */
197 { pres_dl
, posts_dl
}, /* Dl */
198 { pres_bd
, posts_bd
}, /* Bd */
199 { NULL
, NULL
}, /* Ed */
200 { pres_bl
, posts_bl
}, /* Bl */
201 { NULL
, NULL
}, /* El */
202 { pres_it
, posts_it
}, /* It */
203 { NULL
, NULL
}, /* Ad */
204 { pres_an
, posts_an
}, /* An */
205 { NULL
, posts_defaults
}, /* Ar */
206 { NULL
, NULL
}, /* Cd */
207 { NULL
, NULL
}, /* Cm */
208 { NULL
, NULL
}, /* Dv */
209 { NULL
, NULL
}, /* Er */
210 { NULL
, NULL
}, /* Ev */
211 { pres_std
, posts_std
}, /* Ex */
212 { NULL
, NULL
}, /* Fa */
213 { NULL
, posts_text
}, /* Fd */
214 { NULL
, NULL
}, /* Fl */
215 { NULL
, NULL
}, /* Fn */
216 { NULL
, NULL
}, /* Ft */
217 { NULL
, NULL
}, /* Ic */
218 { NULL
, posts_text1
}, /* In */
219 { NULL
, posts_defaults
}, /* Li */
220 { NULL
, posts_nd
}, /* Nd */
221 { NULL
, posts_nm
}, /* Nm */
222 { NULL
, NULL
}, /* Op */
223 { pres_obsolete
, NULL
}, /* Ot */
224 { NULL
, posts_defaults
}, /* Pa */
225 { pres_std
, posts_std
}, /* Rv */
226 { NULL
, posts_st
}, /* St */
227 { NULL
, NULL
}, /* Va */
228 { NULL
, posts_vt
}, /* Vt */
229 { NULL
, posts_text
}, /* Xr */
230 { NULL
, posts_text
}, /* %A */
231 { NULL
, posts_hyphtext
}, /* %B */ /* FIXME: can be used outside Rs/Re. */
232 { NULL
, posts_text
}, /* %D */
233 { NULL
, posts_text
}, /* %I */
234 { NULL
, posts_text
}, /* %J */
235 { NULL
, posts_hyphtext
}, /* %N */
236 { NULL
, posts_hyphtext
}, /* %O */
237 { NULL
, posts_text
}, /* %P */
238 { NULL
, posts_hyphtext
}, /* %R */
239 { NULL
, posts_hyphtext
}, /* %T */ /* FIXME: can be used outside Rs/Re. */
240 { NULL
, posts_text
}, /* %V */
241 { NULL
, NULL
}, /* Ac */
242 { NULL
, NULL
}, /* Ao */
243 { NULL
, NULL
}, /* Aq */
244 { NULL
, posts_at
}, /* At */
245 { NULL
, NULL
}, /* Bc */
246 { NULL
, posts_bf
}, /* Bf */
247 { NULL
, NULL
}, /* Bo */
248 { NULL
, NULL
}, /* Bq */
249 { NULL
, NULL
}, /* Bsx */
250 { NULL
, posts_bx
}, /* Bx */
251 { NULL
, posts_bool
}, /* Db */
252 { NULL
, NULL
}, /* Dc */
253 { NULL
, NULL
}, /* Do */
254 { NULL
, NULL
}, /* Dq */
255 { NULL
, NULL
}, /* Ec */
256 { NULL
, NULL
}, /* Ef */
257 { NULL
, NULL
}, /* Em */
258 { NULL
, NULL
}, /* Eo */
259 { NULL
, NULL
}, /* Fx */
260 { NULL
, NULL
}, /* Ms */
261 { NULL
, posts_notext
}, /* No */
262 { NULL
, posts_ns
}, /* Ns */
263 { NULL
, NULL
}, /* Nx */
264 { NULL
, NULL
}, /* Ox */
265 { NULL
, NULL
}, /* Pc */
266 { NULL
, posts_text1
}, /* Pf */
267 { NULL
, NULL
}, /* Po */
268 { NULL
, NULL
}, /* Pq */
269 { NULL
, NULL
}, /* Qc */
270 { NULL
, NULL
}, /* Ql */
271 { NULL
, NULL
}, /* Qo */
272 { NULL
, NULL
}, /* Qq */
273 { NULL
, NULL
}, /* Re */
274 { NULL
, posts_rs
}, /* Rs */
275 { NULL
, NULL
}, /* Sc */
276 { NULL
, NULL
}, /* So */
277 { NULL
, NULL
}, /* Sq */
278 { NULL
, posts_bool
}, /* Sm */
279 { NULL
, posts_hyph
}, /* Sx */
280 { NULL
, NULL
}, /* Sy */
281 { NULL
, NULL
}, /* Tn */
282 { NULL
, NULL
}, /* Ux */
283 { NULL
, NULL
}, /* Xc */
284 { NULL
, NULL
}, /* Xo */
285 { NULL
, posts_fo
}, /* Fo */
286 { NULL
, NULL
}, /* Fc */
287 { NULL
, NULL
}, /* Oo */
288 { NULL
, NULL
}, /* Oc */
289 { NULL
, posts_bk
}, /* Bk */
290 { NULL
, NULL
}, /* Ek */
291 { NULL
, posts_eoln
}, /* Bt */
292 { NULL
, NULL
}, /* Hf */
293 { pres_obsolete
, NULL
}, /* Fr */
294 { NULL
, posts_eoln
}, /* Ud */
295 { NULL
, posts_lb
}, /* Lb */
296 { pres_pp
, posts_pp
}, /* Lp */
297 { NULL
, NULL
}, /* Lk */
298 { NULL
, posts_defaults
}, /* Mt */
299 { NULL
, NULL
}, /* Brq */
300 { NULL
, NULL
}, /* Bro */
301 { NULL
, NULL
}, /* Brc */
302 { NULL
, posts_text
}, /* %C */
303 { pres_obsolete
, posts_es
}, /* Es */
304 { pres_obsolete
, posts_en
}, /* En */
305 { NULL
, NULL
}, /* Dx */
306 { NULL
, posts_text
}, /* %Q */
307 { NULL
, posts_pp
}, /* br */
308 { NULL
, posts_sp
}, /* sp */
309 { NULL
, posts_text1
}, /* %U */
310 { NULL
, NULL
}, /* Ta */
311 { NULL
, NULL
}, /* ll */
314 #define RSORD_MAX 14 /* Number of `Rs' blocks. */
316 static const enum mdoct rsord
[RSORD_MAX
] = {
333 static const char * const secnames
[SEC__MAX
] = {
340 "IMPLEMENTATION NOTES",
355 "SECURITY CONSIDERATIONS",
361 mdoc_valid_pre(struct mdoc
*mdoc
, struct mdoc_node
*n
)
372 check_text(mdoc
, line
, pos
, tp
);
386 if (NULL
== mdoc_valids
[n
->tok
].pre
)
388 for (p
= mdoc_valids
[n
->tok
].pre
; *p
; p
++)
389 if ( ! (*p
)(mdoc
, n
))
395 mdoc_valid_post(struct mdoc
*mdoc
)
399 if (MDOC_VALID
& mdoc
->last
->flags
)
401 mdoc
->last
->flags
|= MDOC_VALID
;
403 switch (mdoc
->last
->type
) {
411 return(post_root(mdoc
));
416 if (NULL
== mdoc_valids
[mdoc
->last
->tok
].post
)
418 for (p
= mdoc_valids
[mdoc
->last
->tok
].post
; *p
; p
++)
426 check_count(struct mdoc
*mdoc
, enum mdoc_type type
,
427 enum check_lvl lvl
, enum check_ineq ineq
, int val
)
432 if (mdoc
->last
->type
!= type
)
438 if (mdoc
->last
->nchild
< val
)
443 if (mdoc
->last
->nchild
> val
)
448 if (val
== mdoc
->last
->nchild
)
456 t
= lvl
== CHECK_WARN
? MANDOCERR_ARGCWARN
: MANDOCERR_ARGCOUNT
;
457 mandoc_vmsg(t
, mdoc
->parse
, mdoc
->last
->line
,
458 mdoc
->last
->pos
, "want %s%d children (have %d)",
459 p
, val
, mdoc
->last
->nchild
);
467 return(check_count(mdoc
, MDOC_BODY
, CHECK_ERROR
, CHECK_GT
, 0));
473 return(check_count(mdoc
, MDOC_BODY
, CHECK_WARN
, CHECK_GT
, 0));
479 return(check_count(mdoc
, MDOC_ELEM
, CHECK_WARN
, CHECK_EQ
, 0));
485 return(check_count(mdoc
, MDOC_ELEM
, CHECK_WARN
, CHECK_EQ
, 1));
491 return(check_count(mdoc
, MDOC_ELEM
, CHECK_WARN
, CHECK_GT
, 0));
497 return(check_count(mdoc
, MDOC_ELEM
, CHECK_WARN
, CHECK_LT
, 2));
503 return(check_count(mdoc
, MDOC_HEAD
, CHECK_WARN
, CHECK_EQ
, 0));
509 return(check_count(mdoc
, MDOC_HEAD
, CHECK_WARN
, CHECK_EQ
, 1));
515 return(check_count(mdoc
, MDOC_HEAD
, CHECK_WARN
, CHECK_GT
, 0));
521 return(check_count(mdoc
, MDOC_HEAD
, CHECK_WARN
, CHECK_LT
, 2));
525 check_args(struct mdoc
*mdoc
, struct mdoc_node
*n
)
532 assert(n
->args
->argc
);
533 for (i
= 0; i
< (int)n
->args
->argc
; i
++)
534 check_argv(mdoc
, n
, &n
->args
->argv
[i
]);
538 check_argv(struct mdoc
*mdoc
, struct mdoc_node
*n
, struct mdoc_argv
*v
)
542 for (i
= 0; i
< (int)v
->sz
; i
++)
543 check_text(mdoc
, v
->line
, v
->pos
, v
->value
[i
]);
545 /* FIXME: move to post_std(). */
547 if (MDOC_Std
== v
->arg
)
548 if ( ! (v
->sz
|| mdoc
->meta
.name
))
549 mdoc_nmsg(mdoc
, n
, MANDOCERR_NONAME
);
553 check_text(struct mdoc
*mdoc
, int ln
, int pos
, char *p
)
557 if (MDOC_LITERAL
& mdoc
->flags
)
560 for (cp
= p
; NULL
!= (p
= strchr(p
, '\t')); p
++)
561 mdoc_pmsg(mdoc
, ln
, pos
+ (int)(p
- cp
), MANDOCERR_BADTAB
);
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 mandoc_vmsg(MANDOCERR_SYNTCHILD
, mdoc
->parse
,
574 n
->line
, n
->pos
, "want parent %s",
575 MDOC_ROOT
== t
? "<root>" : mdoc_macronames
[tok
]);
581 pre_display(PRE_ARGS
)
583 struct mdoc_node
*node
;
585 if (MDOC_BLOCK
!= n
->type
)
588 for (node
= mdoc
->last
->parent
; node
; node
= node
->parent
)
589 if (MDOC_BLOCK
== node
->type
)
590 if (MDOC_Bd
== node
->tok
)
594 mandoc_vmsg(MANDOCERR_BD_NEST
,
595 mdoc
->parse
, n
->line
, n
->pos
,
596 "%s in Bd", mdoc_macronames
[n
->tok
]);
605 const char *offs
, *width
;
607 struct mdoc_node
*np
;
609 if (MDOC_BLOCK
!= n
->type
) {
610 if (ENDBODY_NOT
!= n
->end
) {
612 np
= n
->pending
->parent
;
617 assert(MDOC_BLOCK
== np
->type
);
618 assert(MDOC_Bl
== np
->tok
);
623 * First figure out which kind of list to use: bind ourselves to
624 * the first mentioned list type and warn about any remaining
625 * ones. If we find no list type, we default to LIST_item.
628 for (i
= 0; n
->args
&& i
< (int)n
->args
->argc
; i
++) {
632 switch (n
->args
->argv
[i
].arg
) {
633 /* Set list types. */
667 /* Set list arguments. */
669 dup
= n
->norm
->Bl
.comp
;
673 /* NB: this can be empty! */
674 if (n
->args
->argv
[i
].sz
) {
675 width
= n
->args
->argv
[i
].value
[0];
676 dup
= (NULL
!= n
->norm
->Bl
.width
);
679 mdoc_nmsg(mdoc
, n
, MANDOCERR_IGNARGV
);
682 /* NB: this can be empty! */
683 if (n
->args
->argv
[i
].sz
) {
684 offs
= n
->args
->argv
[i
].value
[0];
685 dup
= (NULL
!= n
->norm
->Bl
.offs
);
688 mdoc_nmsg(mdoc
, n
, MANDOCERR_IGNARGV
);
694 /* Check: duplicate auxiliary arguments. */
697 mdoc_nmsg(mdoc
, n
, MANDOCERR_ARGVREP
);
700 n
->norm
->Bl
.comp
= comp
;
702 n
->norm
->Bl
.offs
= offs
;
704 n
->norm
->Bl
.width
= width
;
706 /* Check: multiple list types. */
708 if (LIST__NONE
!= lt
&& n
->norm
->Bl
.type
!= LIST__NONE
)
709 mdoc_nmsg(mdoc
, n
, MANDOCERR_LISTREP
);
711 /* Assign list type. */
713 if (LIST__NONE
!= lt
&& n
->norm
->Bl
.type
== LIST__NONE
) {
714 n
->norm
->Bl
.type
= lt
;
715 /* Set column information, too. */
716 if (LIST_column
== lt
) {
719 n
->norm
->Bl
.cols
= (void *)
720 n
->args
->argv
[i
].value
;
724 /* The list type should come first. */
726 if (n
->norm
->Bl
.type
== LIST__NONE
)
727 if (n
->norm
->Bl
.width
||
730 mdoc_nmsg(mdoc
, n
, MANDOCERR_LISTFIRST
);
735 /* Allow lists to default to LIST_item. */
737 if (LIST__NONE
== n
->norm
->Bl
.type
) {
738 mdoc_nmsg(mdoc
, n
, MANDOCERR_LISTTYPE
);
739 n
->norm
->Bl
.type
= LIST_item
;
743 * Validate the width field. Some list types don't need width
744 * types and should be warned about them. Others should have it
745 * and must also be warned. Yet others have a default and need
749 switch (n
->norm
->Bl
.type
) {
751 if (NULL
== n
->norm
->Bl
.width
)
752 mdoc_nmsg(mdoc
, n
, MANDOCERR_NOWIDTHARG
);
763 if (n
->norm
->Bl
.width
)
764 mdoc_nmsg(mdoc
, n
, MANDOCERR_IGNARGV
);
771 if (NULL
== n
->norm
->Bl
.width
)
772 n
->norm
->Bl
.width
= "2n";
775 if (NULL
== n
->norm
->Bl
.width
)
776 n
->norm
->Bl
.width
= "3n";
791 struct mdoc_node
*np
;
793 if (MDOC_BLOCK
!= n
->type
) {
794 if (ENDBODY_NOT
!= n
->end
) {
796 np
= n
->pending
->parent
;
801 assert(MDOC_BLOCK
== np
->type
);
802 assert(MDOC_Bd
== np
->tok
);
806 for (i
= 0; n
->args
&& i
< (int)n
->args
->argc
; i
++) {
811 switch (n
->args
->argv
[i
].arg
) {
828 mdoc_nmsg(mdoc
, n
, MANDOCERR_BADDISP
);
831 /* NB: this can be empty! */
832 if (n
->args
->argv
[i
].sz
) {
833 offs
= n
->args
->argv
[i
].value
[0];
834 dup
= (NULL
!= n
->norm
->Bd
.offs
);
837 mdoc_nmsg(mdoc
, n
, MANDOCERR_IGNARGV
);
841 dup
= n
->norm
->Bd
.comp
;
848 /* Check whether we have duplicates. */
851 mdoc_nmsg(mdoc
, n
, MANDOCERR_ARGVREP
);
853 /* Make our auxiliary assignments. */
856 n
->norm
->Bd
.offs
= offs
;
858 n
->norm
->Bd
.comp
= comp
;
860 /* Check whether a type has already been assigned. */
862 if (DISP__NONE
!= dt
&& n
->norm
->Bd
.type
!= DISP__NONE
)
863 mdoc_nmsg(mdoc
, n
, MANDOCERR_DISPREP
);
865 /* Make our type assignment. */
867 if (DISP__NONE
!= dt
&& n
->norm
->Bd
.type
== DISP__NONE
)
868 n
->norm
->Bd
.type
= dt
;
871 if (DISP__NONE
== n
->norm
->Bd
.type
) {
872 mdoc_nmsg(mdoc
, n
, MANDOCERR_DISPTYPE
);
873 n
->norm
->Bd
.type
= DISP_ragged
;
883 if (MDOC_BLOCK
!= n
->type
)
885 return(check_parent(mdoc
, n
, MDOC_Sh
, MDOC_BODY
));
892 if (MDOC_BLOCK
!= n
->type
)
894 return(check_parent(mdoc
, n
, MDOC_MAX
, MDOC_ROOT
));
901 if (MDOC_BLOCK
!= n
->type
)
904 return(check_parent(mdoc
, n
, MDOC_Bl
, MDOC_BODY
));
915 for (i
= 1; i
< (int)n
->args
->argc
; i
++)
916 mdoc_pmsg(mdoc
, n
->args
->argv
[i
].line
,
917 n
->args
->argv
[i
].pos
, MANDOCERR_IGNARGV
);
919 if (MDOC_Split
== n
->args
->argv
[0].arg
)
920 n
->norm
->An
.auth
= AUTH_split
;
921 else if (MDOC_Nosplit
== n
->args
->argv
[0].arg
)
922 n
->norm
->An
.auth
= AUTH_nosplit
;
933 if (n
->args
&& 1 == n
->args
->argc
)
934 if (MDOC_Std
== n
->args
->argv
[0].arg
)
937 mdoc_nmsg(mdoc
, n
, MANDOCERR_NOARGV
);
942 pre_obsolete(PRE_ARGS
)
945 if (MDOC_ELEM
== n
->type
|| MDOC_BLOCK
== n
->type
)
946 mandoc_msg(MANDOCERR_MACRO_OBS
, mdoc
->parse
,
947 n
->line
, n
->pos
, mdoc_macronames
[n
->tok
]);
955 if (NULL
== mdoc
->meta
.date
|| mdoc
->meta
.os
)
956 mandoc_msg(MANDOCERR_PROLOG_ORDER
, mdoc
->parse
,
957 n
->line
, n
->pos
, "Dt");
959 if (mdoc
->meta
.title
)
960 mandoc_msg(MANDOCERR_PROLOG_REP
, mdoc
->parse
,
961 n
->line
, n
->pos
, "Dt");
970 if (NULL
== mdoc
->meta
.title
|| NULL
== mdoc
->meta
.date
)
971 mandoc_msg(MANDOCERR_PROLOG_ORDER
, mdoc
->parse
,
972 n
->line
, n
->pos
, "Os");
975 mandoc_msg(MANDOCERR_PROLOG_REP
, mdoc
->parse
,
976 n
->line
, n
->pos
, "Os");
985 if (mdoc
->meta
.title
|| mdoc
->meta
.os
)
986 mandoc_msg(MANDOCERR_PROLOG_ORDER
, mdoc
->parse
,
987 n
->line
, n
->pos
, "Dd");
990 mandoc_msg(MANDOCERR_PROLOG_REP
, mdoc
->parse
,
991 n
->line
, n
->pos
, "Dd");
999 struct mdoc_node
*np
;
1003 * Unlike other data pointers, these are "housed" by the HEAD
1004 * element, which contains the goods.
1007 if (MDOC_HEAD
!= mdoc
->last
->type
) {
1008 if (ENDBODY_NOT
!= mdoc
->last
->end
) {
1009 assert(mdoc
->last
->pending
);
1010 np
= mdoc
->last
->pending
->parent
->head
;
1011 } else if (MDOC_BLOCK
!= mdoc
->last
->type
) {
1012 np
= mdoc
->last
->parent
->head
;
1014 np
= mdoc
->last
->head
;
1017 assert(MDOC_HEAD
== np
->type
);
1018 assert(MDOC_Bf
== np
->tok
);
1023 assert(MDOC_BLOCK
== np
->parent
->type
);
1024 assert(MDOC_Bf
== np
->parent
->tok
);
1027 * Cannot have both argument and parameter.
1028 * If neither is specified, let it through with a warning.
1031 if (np
->parent
->args
&& np
->child
) {
1032 mdoc_nmsg(mdoc
, np
, MANDOCERR_SYNTARGVCOUNT
);
1034 } else if (NULL
== np
->parent
->args
&& NULL
== np
->child
) {
1035 mdoc_nmsg(mdoc
, np
, MANDOCERR_FONTTYPE
);
1039 /* Extract argument into data. */
1041 if (np
->parent
->args
) {
1042 arg
= np
->parent
->args
->argv
[0].arg
;
1043 if (MDOC_Emphasis
== arg
)
1044 np
->norm
->Bf
.font
= FONT_Em
;
1045 else if (MDOC_Literal
== arg
)
1046 np
->norm
->Bf
.font
= FONT_Li
;
1047 else if (MDOC_Symbolic
== arg
)
1048 np
->norm
->Bf
.font
= FONT_Sy
;
1054 /* Extract parameter into data. */
1056 if (0 == strcmp(np
->child
->string
, "Em"))
1057 np
->norm
->Bf
.font
= FONT_Em
;
1058 else if (0 == strcmp(np
->child
->string
, "Li"))
1059 np
->norm
->Bf
.font
= FONT_Li
;
1060 else if (0 == strcmp(np
->child
->string
, "Sy"))
1061 np
->norm
->Bf
.font
= FONT_Sy
;
1063 mdoc_nmsg(mdoc
, np
, MANDOCERR_FONTTYPE
);
1071 struct mdoc_node
*n
;
1072 const char *stdlibname
;
1075 check_count(mdoc
, MDOC_ELEM
, CHECK_WARN
, CHECK_EQ
, 1);
1077 n
= mdoc
->last
->child
;
1080 assert(MDOC_TEXT
== n
->type
);
1082 if (NULL
== (stdlibname
= mdoc_a2lib(n
->string
)))
1083 mandoc_asprintf(&libname
,
1084 "library \\(lq%s\\(rq", n
->string
);
1086 libname
= mandoc_strdup(stdlibname
);
1089 n
->string
= libname
;
1094 post_eoln(POST_ARGS
)
1097 if (mdoc
->last
->child
)
1098 mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_ARGSLOST
);
1105 const struct mdoc_node
*n
;
1108 * The Vt macro comes in both ELEM and BLOCK form, both of which
1109 * have different syntaxes (yet more context-sensitive
1110 * behaviour). ELEM types must have a child, which is already
1111 * guaranteed by the in_line parsing routine; BLOCK types,
1112 * specifically the BODY, should only have TEXT children.
1115 if (MDOC_BODY
!= mdoc
->last
->type
)
1118 for (n
= mdoc
->last
->child
; n
; n
= n
->next
)
1119 if (MDOC_TEXT
!= n
->type
)
1120 mdoc_nmsg(mdoc
, n
, MANDOCERR_CHILD
);
1129 if (NULL
!= mdoc
->meta
.name
)
1132 mdoc_deroff(&mdoc
->meta
.name
, mdoc
->last
);
1134 if (NULL
== mdoc
->meta
.name
) {
1135 mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_NONAME
);
1136 mdoc
->meta
.name
= mandoc_strdup("UNKNOWN");
1142 post_literal(POST_ARGS
)
1146 * The `Dl' (note "el" not "one") and `Bd' macros unset the
1147 * MDOC_LITERAL flag as they leave. Note that `Bd' only sets
1148 * this in literal mode, but it doesn't hurt to just switch it
1149 * off in general since displays can't be nested.
1152 if (MDOC_BODY
== mdoc
->last
->type
)
1153 mdoc
->flags
&= ~MDOC_LITERAL
;
1159 post_defaults(POST_ARGS
)
1161 struct mdoc_node
*nn
;
1164 * The `Ar' defaults to "file ..." if no value is provided as an
1165 * argument; the `Mt' and `Pa' macros use "~"; the `Li' just
1166 * gets an empty string.
1169 if (mdoc
->last
->child
)
1173 mdoc
->next
= MDOC_NEXT_CHILD
;
1177 if ( ! mdoc_word_alloc(mdoc
, nn
->line
, nn
->pos
, "file"))
1179 if ( ! mdoc_word_alloc(mdoc
, nn
->line
, nn
->pos
, "..."))
1183 if ( ! mdoc_word_alloc(mdoc
, nn
->line
, nn
->pos
, "AT&T"))
1185 if ( ! mdoc_word_alloc(mdoc
, nn
->line
, nn
->pos
, "UNIX"))
1189 if ( ! mdoc_word_alloc(mdoc
, nn
->line
, nn
->pos
, ""))
1195 if ( ! mdoc_word_alloc(mdoc
, nn
->line
, nn
->pos
, "~"))
1210 struct mdoc_node
*n
;
1211 const char *std_att
;
1215 * If we have a child, look it up in the standard keys. If a
1216 * key exist, use that instead of the child; if it doesn't,
1217 * prefix "AT&T UNIX " to the existing data.
1220 if (NULL
== (n
= mdoc
->last
->child
))
1223 assert(MDOC_TEXT
== n
->type
);
1224 if (NULL
== (std_att
= mdoc_a2att(n
->string
))) {
1225 mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_BADATT
);
1226 mandoc_asprintf(&att
, "AT&T UNIX %s", n
->string
);
1228 att
= mandoc_strdup(std_att
);
1238 struct mdoc_node
*np
;
1241 if (AUTH__NONE
== np
->norm
->An
.auth
) {
1243 check_count(mdoc
, MDOC_ELEM
, CHECK_WARN
, CHECK_GT
, 0);
1244 } else if (np
->child
)
1245 check_count(mdoc
, MDOC_ELEM
, CHECK_WARN
, CHECK_EQ
, 0);
1254 if (MDOC_BLOCK
== mdoc
->last
->type
)
1255 mdoc
->last
->norm
->Es
= mdoc
->last_es
;
1263 mdoc
->last_es
= mdoc
->last
;
1272 struct mdoc_node
*n
, *c
;
1275 if (MDOC_BLOCK
!= mdoc
->last
->type
)
1278 n
= mdoc
->last
->parent
->parent
;
1279 lt
= n
->norm
->Bl
.type
;
1281 if (LIST__NONE
== lt
) {
1282 mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_LISTTYPE
);
1288 if (mdoc
->last
->head
->child
)
1290 /* FIXME: give this a dummy value. */
1291 mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_NOARGS
);
1300 if (NULL
== mdoc
->last
->head
->child
)
1301 mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_NOARGS
);
1310 if (NULL
== mdoc
->last
->body
->child
)
1311 mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_NOBODY
);
1314 if (mdoc
->last
->head
->child
)
1315 mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_ARGSLOST
);
1318 cols
= (int)n
->norm
->Bl
.ncols
;
1320 assert(NULL
== mdoc
->last
->head
->child
);
1322 if (NULL
== mdoc
->last
->body
->child
)
1323 mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_NOBODY
);
1325 for (i
= 0, c
= mdoc
->last
->child
; c
; c
= c
->next
)
1326 if (MDOC_BODY
== c
->type
)
1330 er
= MANDOCERR_ARGCOUNT
;
1331 else if (i
== cols
|| i
== cols
+ 1)
1334 er
= MANDOCERR_SYNTARGCOUNT
;
1336 mandoc_vmsg(er
, mdoc
->parse
,
1337 mdoc
->last
->line
, mdoc
->last
->pos
,
1338 "columns == %d (have %d)", cols
, i
);
1339 return(MANDOCERR_ARGCOUNT
== er
);
1348 post_bl_block(POST_ARGS
)
1350 struct mdoc_node
*n
, *ni
, *nc
;
1353 * These are fairly complicated, so we've broken them into two
1354 * functions. post_bl_block_tag() is called when a -tag is
1355 * specified, but no -width (it must be guessed). The second
1356 * when a -width is specified (macro indicators must be
1357 * rewritten into real lengths).
1362 if (LIST_tag
== n
->norm
->Bl
.type
&&
1363 NULL
== n
->norm
->Bl
.width
) {
1364 if ( ! post_bl_block_tag(mdoc
))
1366 assert(n
->norm
->Bl
.width
);
1367 } else if (NULL
!= n
->norm
->Bl
.width
) {
1368 if ( ! post_bl_block_width(mdoc
))
1370 assert(n
->norm
->Bl
.width
);
1373 for (ni
= n
->body
->child
; ni
; ni
= ni
->next
) {
1374 if (NULL
== ni
->body
)
1376 nc
= ni
->body
->last
;
1377 while (NULL
!= nc
) {
1389 if (NULL
== ni
->next
) {
1390 mandoc_msg(MANDOCERR_PAR_MOVE
,
1391 mdoc
->parse
, nc
->line
, nc
->pos
,
1392 mdoc_macronames
[nc
->tok
]);
1393 if ( ! mdoc_node_relink(mdoc
, nc
))
1395 } else if (0 == n
->norm
->Bl
.comp
&&
1396 LIST_column
!= n
->norm
->Bl
.type
) {
1397 mandoc_vmsg(MANDOCERR_PAR_SKIP
,
1398 mdoc
->parse
, nc
->line
, nc
->pos
,
1400 mdoc_macronames
[nc
->tok
]);
1401 mdoc_node_delete(mdoc
, nc
);
1404 nc
= ni
->body
->last
;
1411 post_bl_block_width(POST_ARGS
)
1416 struct mdoc_node
*n
;
1422 * Calculate the real width of a list from the -width string,
1423 * which may contain a macro (with a known default width), a
1424 * literal string, or a scaling width.
1426 * If the value to -width is a macro, then we re-write it to be
1427 * the macro's width as set in share/tmac/mdoc/doc-common.
1430 if (0 == strcmp(n
->norm
->Bl
.width
, "Ds"))
1432 else if (MDOC_MAX
== (tok
= mdoc_hash_find(n
->norm
->Bl
.width
)))
1434 else if (0 == (width
= macro2len(tok
))) {
1435 mdoc_nmsg(mdoc
, n
, MANDOCERR_BADWIDTH
);
1439 /* The value already exists: free and reallocate it. */
1443 for (i
= 0; i
< (int)n
->args
->argc
; i
++)
1444 if (MDOC_Width
== n
->args
->argv
[i
].arg
)
1447 assert(i
< (int)n
->args
->argc
);
1449 (void)snprintf(buf
, sizeof(buf
), "%un", (unsigned int)width
);
1450 free(n
->args
->argv
[i
].value
[0]);
1451 n
->args
->argv
[i
].value
[0] = mandoc_strdup(buf
);
1453 /* Set our width! */
1454 n
->norm
->Bl
.width
= n
->args
->argv
[i
].value
[0];
1459 post_bl_block_tag(POST_ARGS
)
1461 struct mdoc_node
*n
, *nn
;
1467 * Calculate the -width for a `Bl -tag' list if it hasn't been
1468 * provided. Uses the first head macro. NOTE AGAIN: this is
1469 * ONLY if the -width argument has NOT been provided. See
1470 * post_bl_block_width() for converting the -width string.
1476 for (nn
= n
->body
->child
; nn
; nn
= nn
->next
) {
1477 if (MDOC_It
!= nn
->tok
)
1480 assert(MDOC_BLOCK
== nn
->type
);
1481 nn
= nn
->head
->child
;
1486 if (MDOC_TEXT
== nn
->type
) {
1487 sz
= strlen(nn
->string
) + 1;
1491 if (0 != (ssz
= macro2len(nn
->tok
)))
1497 /* Defaults to ten ens. */
1499 (void)snprintf(buf
, sizeof(buf
), "%un", (unsigned int)sz
);
1502 * We have to dynamically add this to the macro's argument list.
1503 * We're guaranteed that a MDOC_Width doesn't already exist.
1507 i
= (int)(n
->args
->argc
)++;
1509 n
->args
->argv
= mandoc_reallocarray(n
->args
->argv
,
1510 n
->args
->argc
, sizeof(struct mdoc_argv
));
1512 n
->args
->argv
[i
].arg
= MDOC_Width
;
1513 n
->args
->argv
[i
].line
= n
->line
;
1514 n
->args
->argv
[i
].pos
= n
->pos
;
1515 n
->args
->argv
[i
].sz
= 1;
1516 n
->args
->argv
[i
].value
= mandoc_malloc(sizeof(char *));
1517 n
->args
->argv
[i
].value
[0] = mandoc_strdup(buf
);
1519 /* Set our width! */
1520 n
->norm
->Bl
.width
= n
->args
->argv
[i
].value
[0];
1525 post_bl_head(POST_ARGS
)
1527 struct mdoc_node
*np
, *nn
, *nnp
;
1530 if (LIST_column
!= mdoc
->last
->norm
->Bl
.type
)
1531 /* FIXME: this should be ERROR class... */
1532 return(hwarn_eq0(mdoc
));
1535 * Convert old-style lists, where the column width specifiers
1536 * trail as macro parameters, to the new-style ("normal-form")
1537 * lists where they're argument values following -column.
1540 /* First, disallow both types and allow normal-form. */
1543 * TODO: technically, we can accept both and just merge the two
1544 * lists, but I'll leave that for another day.
1547 if (mdoc
->last
->norm
->Bl
.ncols
&& mdoc
->last
->nchild
) {
1548 mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_COLUMNS
);
1550 } else if (NULL
== mdoc
->last
->child
)
1553 np
= mdoc
->last
->parent
;
1556 for (j
= 0; j
< (int)np
->args
->argc
; j
++)
1557 if (MDOC_Column
== np
->args
->argv
[j
].arg
)
1560 assert(j
< (int)np
->args
->argc
);
1561 assert(0 == np
->args
->argv
[j
].sz
);
1564 * Accommodate for new-style groff column syntax. Shuffle the
1565 * child nodes, all of which must be TEXT, as arguments for the
1566 * column field. Then, delete the head children.
1569 np
->args
->argv
[j
].sz
= (size_t)mdoc
->last
->nchild
;
1570 np
->args
->argv
[j
].value
= mandoc_reallocarray(NULL
,
1571 (size_t)mdoc
->last
->nchild
, sizeof(char *));
1573 mdoc
->last
->norm
->Bl
.ncols
= np
->args
->argv
[j
].sz
;
1574 mdoc
->last
->norm
->Bl
.cols
= (void *)np
->args
->argv
[j
].value
;
1576 for (i
= 0, nn
= mdoc
->last
->child
; nn
; i
++) {
1577 np
->args
->argv
[j
].value
[i
] = nn
->string
;
1581 mdoc_node_delete(NULL
, nnp
);
1584 mdoc
->last
->nchild
= 0;
1585 mdoc
->last
->child
= NULL
;
1593 struct mdoc_node
*nparent
, *nprev
; /* of the Bl block */
1594 struct mdoc_node
*nblock
, *nbody
; /* of the Bl */
1595 struct mdoc_node
*nchild
, *nnext
; /* of the Bl body */
1598 switch (nbody
->type
) {
1600 return(post_bl_block(mdoc
));
1602 return(post_bl_head(mdoc
));
1609 nchild
= nbody
->child
;
1610 while (NULL
!= nchild
) {
1611 if (MDOC_It
== nchild
->tok
|| MDOC_Sm
== nchild
->tok
) {
1612 nchild
= nchild
->next
;
1616 mdoc_nmsg(mdoc
, nchild
, MANDOCERR_CHILD
);
1619 * Move the node out of the Bl block.
1620 * First, collect all required node pointers.
1623 nblock
= nbody
->parent
;
1624 nprev
= nblock
->prev
;
1625 nparent
= nblock
->parent
;
1626 nnext
= nchild
->next
;
1629 * Unlink this child.
1632 assert(NULL
== nchild
->prev
);
1633 if (0 == --nbody
->nchild
) {
1634 nbody
->child
= NULL
;
1636 assert(NULL
== nnext
);
1638 nbody
->child
= nnext
;
1643 * Relink this child.
1646 nchild
->parent
= nparent
;
1647 nchild
->prev
= nprev
;
1648 nchild
->next
= nblock
;
1650 nblock
->prev
= nchild
;
1653 nparent
->child
= nchild
;
1655 nprev
->next
= nchild
;
1664 ebool(struct mdoc
*mdoc
)
1667 if (NULL
== mdoc
->last
->child
) {
1668 mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_MACROEMPTY
);
1669 mdoc_node_delete(mdoc
, mdoc
->last
);
1672 check_count(mdoc
, MDOC_ELEM
, CHECK_WARN
, CHECK_EQ
, 1);
1674 assert(MDOC_TEXT
== mdoc
->last
->child
->type
);
1676 if (0 == strcmp(mdoc
->last
->child
->string
, "on")) {
1677 if (MDOC_Sm
== mdoc
->last
->tok
)
1678 mdoc
->flags
&= ~MDOC_SMOFF
;
1681 if (0 == strcmp(mdoc
->last
->child
->string
, "off")) {
1682 if (MDOC_Sm
== mdoc
->last
->tok
)
1683 mdoc
->flags
|= MDOC_SMOFF
;
1687 mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_BADBOOL
);
1692 post_root(POST_ARGS
)
1695 struct mdoc_node
*n
;
1699 /* Check that we have a finished prologue. */
1701 if ( ! (MDOC_PBODY
& mdoc
->flags
)) {
1703 mdoc_nmsg(mdoc
, mdoc
->first
, MANDOCERR_NODOCPROLOG
);
1709 /* Check that we begin with a proper `Sh'. */
1711 if (NULL
== n
->child
)
1712 mdoc_nmsg(mdoc
, n
, MANDOCERR_DOC_EMPTY
);
1713 else if (MDOC_Sh
!= n
->child
->tok
)
1714 mandoc_msg(MANDOCERR_SEC_BEFORE
, mdoc
->parse
,
1715 n
->child
->line
, n
->child
->pos
,
1716 mdoc_macronames
[n
->child
->tok
]);
1724 struct mdoc_node
*ch
;
1727 if (NULL
== (ch
= mdoc
->last
->child
)) {
1728 mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_MACROEMPTY
);
1729 mdoc_node_delete(mdoc
, mdoc
->last
);
1733 assert(MDOC_TEXT
== ch
->type
);
1735 if (NULL
== (p
= mdoc_a2st(ch
->string
))) {
1736 mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_BADSTANDARD
);
1737 mdoc_node_delete(mdoc
, mdoc
->last
);
1740 ch
->string
= mandoc_strdup(p
);
1749 struct mdoc_node
*nn
, *next
, *prev
;
1752 switch (mdoc
->last
->type
) {
1754 check_count(mdoc
, MDOC_HEAD
, CHECK_WARN
, CHECK_EQ
, 0);
1757 if (mdoc
->last
->child
)
1759 check_count(mdoc
, MDOC_BODY
, CHECK_WARN
, CHECK_GT
, 0);
1766 * Make sure only certain types of nodes are allowed within the
1767 * the `Rs' body. Delete offending nodes and raise a warning.
1768 * Do this before re-ordering for the sake of clarity.
1772 for (nn
= mdoc
->last
->child
; nn
; nn
= next
) {
1773 for (i
= 0; i
< RSORD_MAX
; i
++)
1774 if (nn
->tok
== rsord
[i
])
1777 if (i
< RSORD_MAX
) {
1778 if (MDOC__J
== rsord
[i
] || MDOC__B
== rsord
[i
])
1779 mdoc
->last
->norm
->Rs
.quote_T
++;
1785 mdoc_nmsg(mdoc
, nn
, MANDOCERR_CHILD
);
1786 mdoc_node_delete(mdoc
, nn
);
1790 * Nothing to sort if only invalid nodes were found
1791 * inside the `Rs' body.
1794 if (NULL
== mdoc
->last
->child
)
1798 * The full `Rs' block needs special handling to order the
1799 * sub-elements according to `rsord'. Pick through each element
1800 * and correctly order it. This is a insertion sort.
1804 for (nn
= mdoc
->last
->child
->next
; nn
; nn
= next
) {
1805 /* Determine order of `nn'. */
1806 for (i
= 0; i
< RSORD_MAX
; i
++)
1807 if (rsord
[i
] == nn
->tok
)
1811 * Remove `nn' from the chain. This somewhat
1812 * repeats mdoc_node_unlink(), but since we're
1813 * just re-ordering, there's no need for the
1814 * full unlink process.
1817 if (NULL
!= (next
= nn
->next
))
1818 next
->prev
= nn
->prev
;
1820 if (NULL
!= (prev
= nn
->prev
))
1821 prev
->next
= nn
->next
;
1823 nn
->prev
= nn
->next
= NULL
;
1826 * Scan back until we reach a node that's
1827 * ordered before `nn'.
1830 for ( ; prev
; prev
= prev
->prev
) {
1831 /* Determine order of `prev'. */
1832 for (j
= 0; j
< RSORD_MAX
; j
++)
1833 if (rsord
[j
] == prev
->tok
)
1841 * Set `nn' back into its correct place in front
1842 * of the `prev' node.
1849 prev
->next
->prev
= nn
;
1850 nn
->next
= prev
->next
;
1853 mdoc
->last
->child
->prev
= nn
;
1854 nn
->next
= mdoc
->last
->child
;
1855 mdoc
->last
->child
= nn
;
1863 * For some arguments of some macros,
1864 * convert all breakable hyphens into ASCII_HYPH.
1867 post_hyph(POST_ARGS
)
1869 struct mdoc_node
*n
, *nch
;
1875 if (MDOC_Sh
== n
->tok
|| MDOC_Ss
== n
->tok
)
1879 if (MDOC_D1
== n
->tok
|| MDOC_Nd
== n
->tok
)
1888 for (nch
= n
->child
; nch
; nch
= nch
->next
) {
1889 if (MDOC_TEXT
!= nch
->type
)
1894 while ('\0' != *(++cp
))
1896 isalpha((unsigned char)cp
[-1]) &&
1897 isalpha((unsigned char)cp
[1]))
1907 if (MDOC_LINE
& mdoc
->last
->flags
)
1908 mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_NS_SKIP
);
1916 if (MDOC_HEAD
== mdoc
->last
->type
)
1917 return(post_sh_head(mdoc
));
1918 if (MDOC_BODY
== mdoc
->last
->type
)
1919 return(post_sh_body(mdoc
));
1925 post_sh_body(POST_ARGS
)
1927 struct mdoc_node
*n
;
1929 if (SEC_NAME
!= mdoc
->lastsec
)
1933 * Warn if the NAME section doesn't contain the `Nm' and `Nd'
1934 * macros (can have multiple `Nm' and one `Nd'). Note that the
1935 * children of the BODY declaration can also be "text".
1938 if (NULL
== (n
= mdoc
->last
->child
)) {
1939 mandoc_msg(MANDOCERR_NAMESEC_BAD
, mdoc
->parse
,
1940 mdoc
->last
->line
, mdoc
->last
->pos
, "empty");
1944 for ( ; n
&& n
->next
; n
= n
->next
) {
1945 if (MDOC_ELEM
== n
->type
&& MDOC_Nm
== n
->tok
)
1947 if (MDOC_TEXT
== n
->type
)
1949 mandoc_msg(MANDOCERR_NAMESEC_BAD
, mdoc
->parse
,
1950 n
->line
, n
->pos
, mdoc_macronames
[n
->tok
]);
1954 if (MDOC_BLOCK
== n
->type
&& MDOC_Nd
== n
->tok
)
1957 mandoc_msg(MANDOCERR_NAMESEC_BAD
, mdoc
->parse
,
1958 n
->line
, n
->pos
, mdoc_macronames
[n
->tok
]);
1963 post_sh_head(POST_ARGS
)
1965 struct mdoc_node
*n
;
1966 const char *goodsec
;
1971 * Process a new section. Sections are either "named" or
1972 * "custom". Custom sections are user-defined, while named ones
1973 * follow a conventional order and may only appear in certain
1979 mdoc_deroff(&secname
, mdoc
->last
);
1980 sec
= NULL
== secname
? SEC_CUSTOM
: a2sec(secname
);
1982 /* The NAME should be first. */
1984 if (SEC_NAME
!= sec
&& SEC_NONE
== mdoc
->lastnamed
)
1985 mandoc_msg(MANDOCERR_NAMESEC_FIRST
, mdoc
->parse
,
1986 mdoc
->last
->line
, mdoc
->last
->pos
, secname
);
1988 /* The SYNOPSIS gets special attention in other areas. */
1990 if (SEC_SYNOPSIS
== sec
) {
1991 roff_setreg(mdoc
->roff
, "nS", 1, '=');
1992 mdoc
->flags
|= MDOC_SYNOPSIS
;
1994 roff_setreg(mdoc
->roff
, "nS", 0, '=');
1995 mdoc
->flags
&= ~MDOC_SYNOPSIS
;
1998 /* Mark our last section. */
2000 mdoc
->lastsec
= sec
;
2003 * Set the section attribute for the current HEAD, for its
2004 * parent BLOCK, and for the HEAD children; the latter can
2005 * only be TEXT nodes, so no recursion is needed.
2006 * For other blocks and elements, including .Sh BODY, this is
2007 * done when allocating the node data structures, but for .Sh
2008 * BLOCK and HEAD, the section is still unknown at that time.
2011 mdoc
->last
->parent
->sec
= sec
;
2012 mdoc
->last
->sec
= sec
;
2013 for (n
= mdoc
->last
->child
; n
; n
= n
->next
)
2016 /* We don't care about custom sections after this. */
2018 if (SEC_CUSTOM
== sec
) {
2024 * Check whether our non-custom section is being repeated or is
2028 if (sec
== mdoc
->lastnamed
)
2029 mandoc_msg(MANDOCERR_SEC_REP
, mdoc
->parse
,
2030 mdoc
->last
->line
, mdoc
->last
->pos
, secname
);
2032 if (sec
< mdoc
->lastnamed
)
2033 mandoc_msg(MANDOCERR_SEC_ORDER
, mdoc
->parse
,
2034 mdoc
->last
->line
, mdoc
->last
->pos
, secname
);
2036 /* Mark the last named section. */
2038 mdoc
->lastnamed
= sec
;
2040 /* Check particular section/manual conventions. */
2042 assert(mdoc
->meta
.msec
);
2047 if (*mdoc
->meta
.msec
== '4')
2049 goodsec
= "2, 3, 4, 9";
2051 case SEC_RETURN_VALUES
:
2054 if (*mdoc
->meta
.msec
== '2')
2056 if (*mdoc
->meta
.msec
== '3')
2058 if (NULL
== goodsec
)
2059 goodsec
= "2, 3, 9";
2062 if (*mdoc
->meta
.msec
== '9')
2064 if (NULL
== goodsec
)
2066 mandoc_vmsg(MANDOCERR_SEC_MSEC
, mdoc
->parse
,
2067 mdoc
->last
->line
, mdoc
->last
->pos
,
2068 "%s for %s only", secname
, goodsec
);
2079 post_ignpar(POST_ARGS
)
2081 struct mdoc_node
*np
;
2083 if (MDOC_BODY
!= mdoc
->last
->type
)
2086 if (NULL
!= (np
= mdoc
->last
->child
))
2087 if (MDOC_Pp
== np
->tok
|| MDOC_Lp
== np
->tok
) {
2088 mandoc_vmsg(MANDOCERR_PAR_SKIP
,
2089 mdoc
->parse
, np
->line
, np
->pos
,
2090 "%s after %s", mdoc_macronames
[np
->tok
],
2091 mdoc_macronames
[mdoc
->last
->tok
]);
2092 mdoc_node_delete(mdoc
, np
);
2095 if (NULL
!= (np
= mdoc
->last
->last
))
2096 if (MDOC_Pp
== np
->tok
|| MDOC_Lp
== np
->tok
) {
2097 mandoc_vmsg(MANDOCERR_PAR_SKIP
, mdoc
->parse
,
2098 np
->line
, np
->pos
, "%s at the end of %s",
2099 mdoc_macronames
[np
->tok
],
2100 mdoc_macronames
[mdoc
->last
->tok
]);
2101 mdoc_node_delete(mdoc
, np
);
2111 if (NULL
== mdoc
->last
)
2113 if (MDOC_ELEM
!= n
->type
&& MDOC_BLOCK
!= n
->type
)
2117 * Don't allow prior `Lp' or `Pp' prior to a paragraph-type
2118 * block: `Lp', `Pp', or non-compact `Bd' or `Bl'.
2121 if (MDOC_Pp
!= mdoc
->last
->tok
&&
2122 MDOC_Lp
!= mdoc
->last
->tok
&&
2123 MDOC_br
!= mdoc
->last
->tok
)
2125 if (MDOC_Bl
== n
->tok
&& n
->norm
->Bl
.comp
)
2127 if (MDOC_Bd
== n
->tok
&& n
->norm
->Bd
.comp
)
2129 if (MDOC_It
== n
->tok
&& n
->parent
->norm
->Bl
.comp
)
2132 mandoc_vmsg(MANDOCERR_PAR_SKIP
, mdoc
->parse
,
2133 mdoc
->last
->line
, mdoc
->last
->pos
,
2134 "%s before %s", mdoc_macronames
[mdoc
->last
->tok
],
2135 mdoc_macronames
[n
->tok
]);
2136 mdoc_node_delete(mdoc
, mdoc
->last
);
2143 struct mdoc_node
*np
;
2145 if (MDOC_ELEM
!= mdoc
->last
->type
&&
2146 MDOC_BLOCK
!= mdoc
->last
->type
)
2149 if (NULL
== (np
= mdoc
->last
->prev
)) {
2150 np
= mdoc
->last
->parent
;
2151 if (MDOC_Sh
!= np
->tok
&& MDOC_Ss
!= np
->tok
)
2154 if (MDOC_Pp
!= np
->tok
&& MDOC_Lp
!= np
->tok
&&
2155 (MDOC_br
!= mdoc
->last
->tok
||
2156 (MDOC_sp
!= np
->tok
&& MDOC_br
!= np
->tok
)))
2160 mandoc_vmsg(MANDOCERR_PAR_SKIP
, mdoc
->parse
,
2161 mdoc
->last
->line
, mdoc
->last
->pos
,
2162 "%s after %s", mdoc_macronames
[mdoc
->last
->tok
],
2163 mdoc_macronames
[np
->tok
]);
2164 mdoc_node_delete(mdoc
, mdoc
->last
);
2169 pre_literal(PRE_ARGS
)
2172 if (MDOC_BODY
!= n
->type
)
2176 * The `Dl' (note "el" not "one") and `Bd -literal' and `Bd
2177 * -unfilled' macros set MDOC_LITERAL on entrance to the body.
2182 mdoc
->flags
|= MDOC_LITERAL
;
2185 if (DISP_literal
== n
->norm
->Bd
.type
)
2186 mdoc
->flags
|= MDOC_LITERAL
;
2187 if (DISP_unfilled
== n
->norm
->Bd
.type
)
2188 mdoc
->flags
|= MDOC_LITERAL
;
2201 struct mdoc_node
*n
;
2204 if (mdoc
->meta
.date
)
2205 free(mdoc
->meta
.date
);
2208 if (NULL
== n
->child
|| '\0' == n
->child
->string
[0]) {
2209 mdoc
->meta
.date
= mdoc
->quick
? mandoc_strdup("") :
2210 mandoc_normdate(mdoc
->parse
, NULL
, n
->line
, n
->pos
);
2215 mdoc_deroff(&datestr
, n
);
2217 mdoc
->meta
.date
= datestr
;
2219 mdoc
->meta
.date
= mandoc_normdate(mdoc
->parse
,
2220 datestr
, n
->line
, n
->pos
);
2229 struct mdoc_node
*nn
, *n
;
2235 if (mdoc
->meta
.title
)
2236 free(mdoc
->meta
.title
);
2238 free(mdoc
->meta
.vol
);
2239 if (mdoc
->meta
.arch
)
2240 free(mdoc
->meta
.arch
);
2242 mdoc
->meta
.title
= mdoc
->meta
.vol
= mdoc
->meta
.arch
= NULL
;
2244 /* First check that all characters are uppercase. */
2246 if (NULL
!= (nn
= n
->child
))
2247 for (p
= nn
->string
; *p
; p
++) {
2248 if (toupper((unsigned char)*p
) == *p
)
2250 mandoc_msg(MANDOCERR_TITLE_CASE
,
2251 mdoc
->parse
, nn
->line
,
2252 nn
->pos
+ (p
- nn
->string
),
2258 * title = unknown, volume = local, msec = 0, arch = NULL
2261 if (NULL
== (nn
= n
->child
)) {
2262 /* XXX: make these macro values. */
2263 /* FIXME: warn about missing values. */
2264 mdoc
->meta
.title
= mandoc_strdup("UNKNOWN");
2265 mdoc
->meta
.vol
= mandoc_strdup("LOCAL");
2266 mdoc
->meta
.msec
= mandoc_strdup("1");
2270 /* Handles: `.Dt TITLE'
2271 * title = TITLE, volume = local, msec = 0, arch = NULL
2274 mdoc
->meta
.title
= mandoc_strdup(
2275 '\0' == nn
->string
[0] ? "UNKNOWN" : nn
->string
);
2277 if (NULL
== (nn
= nn
->next
)) {
2278 /* FIXME: warn about missing msec. */
2279 /* XXX: make this a macro value. */
2280 mdoc
->meta
.vol
= mandoc_strdup("LOCAL");
2281 mdoc
->meta
.msec
= mandoc_strdup("1");
2285 /* Handles: `.Dt TITLE SEC'
2287 * volume = SEC is msec ? format(msec) : SEC,
2288 * msec = SEC is msec ? atoi(msec) : 0,
2292 cp
= mandoc_a2msec(nn
->string
);
2294 mdoc
->meta
.vol
= mandoc_strdup(cp
);
2295 mdoc
->meta
.msec
= mandoc_strdup(nn
->string
);
2297 mandoc_msg(MANDOCERR_MSEC_BAD
, mdoc
->parse
,
2298 nn
->line
, nn
->pos
, nn
->string
);
2299 mdoc
->meta
.vol
= mandoc_strdup(nn
->string
);
2300 mdoc
->meta
.msec
= mandoc_strdup(nn
->string
);
2303 if (NULL
== (nn
= nn
->next
))
2306 /* Handles: `.Dt TITLE SEC VOL'
2308 * volume = VOL is vol ? format(VOL) :
2309 * VOL is arch ? format(arch) :
2313 cp
= mdoc_a2vol(nn
->string
);
2315 free(mdoc
->meta
.vol
);
2316 mdoc
->meta
.vol
= mandoc_strdup(cp
);
2318 cp
= mdoc_a2arch(nn
->string
);
2320 mandoc_msg(MANDOCERR_ARCH_BAD
, mdoc
->parse
,
2321 nn
->line
, nn
->pos
, nn
->string
);
2322 free(mdoc
->meta
.vol
);
2323 mdoc
->meta
.vol
= mandoc_strdup(nn
->string
);
2325 mdoc
->meta
.arch
= mandoc_strdup(cp
);
2328 /* Ignore any subsequent parameters... */
2329 /* FIXME: warn about subsequent parameters. */
2335 post_prol(POST_ARGS
)
2338 * Remove prologue macros from the document after they're
2339 * processed. The final document uses mdoc_meta for these
2340 * values and discards the originals.
2343 mdoc_node_delete(mdoc
, mdoc
->last
);
2344 if (mdoc
->meta
.title
&& mdoc
->meta
.date
&& mdoc
->meta
.os
)
2345 mdoc
->flags
|= MDOC_PBODY
;
2353 struct mdoc_node
*n
;
2356 * Make `Bx's second argument always start with an uppercase
2357 * letter. Groff checks if it's an "accepted" term, but we just
2358 * uppercase blindly.
2361 n
= mdoc
->last
->child
;
2362 if (n
&& NULL
!= (n
= n
->next
))
2363 *n
->string
= (char)toupper((unsigned char)*n
->string
);
2372 struct utsname utsname
;
2373 static char *defbuf
;
2375 struct mdoc_node
*n
;
2380 * Set the operating system by way of the `Os' macro.
2381 * The order of precedence is:
2382 * 1. the argument of the `Os' macro, unless empty
2383 * 2. the -Ios=foo command line argument, if provided
2384 * 3. -DOSNAME="\"foo\"", if provided during compilation
2385 * 4. "sysname release" from uname(3)
2388 free(mdoc
->meta
.os
);
2389 mdoc
->meta
.os
= NULL
;
2390 mdoc_deroff(&mdoc
->meta
.os
, n
);
2395 mdoc
->meta
.os
= mandoc_strdup(mdoc
->defos
);
2400 mdoc
->meta
.os
= mandoc_strdup(OSNAME
);
2402 if (NULL
== defbuf
) {
2403 if (-1 == uname(&utsname
)) {
2404 mdoc_nmsg(mdoc
, n
, MANDOCERR_UNAME
);
2405 defbuf
= mandoc_strdup("UNKNOWN");
2407 mandoc_asprintf(&defbuf
, "%s %s",
2408 utsname
.sysname
, utsname
.release
);
2410 mdoc
->meta
.os
= mandoc_strdup(defbuf
);
2418 struct mdoc_node
*nn
, *n
;
2423 * Macros accepting `-std' as an argument have the name of the
2424 * current document (`Nm') filled in as the argument if it's not
2431 if (NULL
== mdoc
->meta
.name
)
2435 mdoc
->next
= MDOC_NEXT_CHILD
;
2437 if ( ! mdoc_word_alloc(mdoc
, n
->line
, n
->pos
, mdoc
->meta
.name
))
2444 static enum mdoc_sec
2445 a2sec(const char *p
)
2449 for (i
= 0; i
< (int)SEC__MAX
; i
++)
2450 if (secnames
[i
] && 0 == strcmp(p
, secnames
[i
]))
2451 return((enum mdoc_sec
)i
);
2457 macro2len(enum mdoct macro
)