]>
git.cameronkatri.com Git - mandoc.git/blob - mdoc_validate.c
1 /* $Id: mdoc_validate.c,v 1.241 2014/08/01 22:22:11 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 void check_text(struct mdoc
*, int, int, char *);
70 static void check_argv(struct mdoc
*,
71 struct mdoc_node
*, struct mdoc_argv
*);
72 static void check_args(struct mdoc
*, struct mdoc_node
*);
73 static enum mdoc_sec
a2sec(const char *);
74 static size_t macro2len(enum mdoct
);
76 static int ebool(POST_ARGS
);
77 static int berr_ge1(POST_ARGS
);
78 static int bwarn_ge1(POST_ARGS
);
79 static int ewarn_eq0(POST_ARGS
);
80 static int ewarn_eq1(POST_ARGS
);
81 static int ewarn_ge1(POST_ARGS
);
82 static int ewarn_le1(POST_ARGS
);
83 static int hwarn_eq0(POST_ARGS
);
84 static int hwarn_eq1(POST_ARGS
);
85 static int hwarn_ge1(POST_ARGS
);
87 static int post_an(POST_ARGS
);
88 static int post_at(POST_ARGS
);
89 static int post_bf(POST_ARGS
);
90 static int post_bl(POST_ARGS
);
91 static int post_bl_block(POST_ARGS
);
92 static int post_bl_block_width(POST_ARGS
);
93 static int post_bl_block_tag(POST_ARGS
);
94 static int post_bl_head(POST_ARGS
);
95 static int post_bx(POST_ARGS
);
96 static int post_defaults(POST_ARGS
);
97 static int post_dd(POST_ARGS
);
98 static int post_dt(POST_ARGS
);
99 static int post_en(POST_ARGS
);
100 static int post_es(POST_ARGS
);
101 static int post_eoln(POST_ARGS
);
102 static int post_ex(POST_ARGS
);
103 static int post_hyph(POST_ARGS
);
104 static int post_ignpar(POST_ARGS
);
105 static int post_it(POST_ARGS
);
106 static int post_lb(POST_ARGS
);
107 static int post_literal(POST_ARGS
);
108 static int post_nm(POST_ARGS
);
109 static int post_ns(POST_ARGS
);
110 static int post_os(POST_ARGS
);
111 static int post_par(POST_ARGS
);
112 static int post_prol(POST_ARGS
);
113 static int post_root(POST_ARGS
);
114 static int post_rs(POST_ARGS
);
115 static int post_sh(POST_ARGS
);
116 static int post_sh_body(POST_ARGS
);
117 static int post_sh_head(POST_ARGS
);
118 static int post_st(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_literal(PRE_ARGS
);
127 static int pre_obsolete(PRE_ARGS
);
128 static int pre_os(PRE_ARGS
);
129 static int pre_par(PRE_ARGS
);
130 static int pre_std(PRE_ARGS
);
132 static v_post posts_an
[] = { post_an
, NULL
};
133 static v_post posts_at
[] = { post_at
, post_defaults
, NULL
};
134 static v_post posts_bd
[] = { post_literal
, hwarn_eq0
, bwarn_ge1
, NULL
};
135 static v_post posts_bf
[] = { post_bf
, NULL
};
136 static v_post posts_bk
[] = { hwarn_eq0
, bwarn_ge1
, NULL
};
137 static v_post posts_bl
[] = { bwarn_ge1
, post_bl
, NULL
};
138 static v_post posts_bx
[] = { post_bx
, NULL
};
139 static v_post posts_bool
[] = { ebool
, NULL
};
140 static v_post posts_eoln
[] = { post_eoln
, NULL
};
141 static v_post posts_defaults
[] = { post_defaults
, NULL
};
142 static v_post posts_d1
[] = { bwarn_ge1
, post_hyph
, NULL
};
143 static v_post posts_dd
[] = { post_dd
, post_prol
, NULL
};
144 static v_post posts_dl
[] = { post_literal
, bwarn_ge1
, NULL
};
145 static v_post posts_dt
[] = { post_dt
, post_prol
, NULL
};
146 static v_post posts_en
[] = { post_en
, NULL
};
147 static v_post posts_es
[] = { post_es
, NULL
};
148 static v_post posts_ex
[] = { post_ex
, NULL
};
149 static v_post posts_fo
[] = { hwarn_eq1
, bwarn_ge1
, NULL
};
150 static v_post posts_hyph
[] = { post_hyph
, NULL
};
151 static v_post posts_hyphtext
[] = { ewarn_ge1
, post_hyph
, NULL
};
152 static v_post posts_it
[] = { post_it
, NULL
};
153 static v_post posts_lb
[] = { post_lb
, NULL
};
154 static v_post posts_nd
[] = { berr_ge1
, post_hyph
, NULL
};
155 static v_post posts_nm
[] = { post_nm
, NULL
};
156 static v_post posts_notext
[] = { ewarn_eq0
, NULL
};
157 static v_post posts_ns
[] = { post_ns
, NULL
};
158 static v_post posts_os
[] = { post_os
, post_prol
, NULL
};
159 static v_post posts_pp
[] = { post_par
, ewarn_eq0
, NULL
};
160 static v_post posts_rs
[] = { post_rs
, NULL
};
161 static v_post posts_sh
[] = { post_ignpar
,hwarn_ge1
,post_sh
,post_hyph
,NULL
};
162 static v_post posts_sp
[] = { post_par
, ewarn_le1
, NULL
};
163 static v_post posts_ss
[] = { post_ignpar
, hwarn_ge1
, post_hyph
, NULL
};
164 static v_post posts_st
[] = { post_st
, NULL
};
165 static v_post posts_text
[] = { ewarn_ge1
, NULL
};
166 static v_post posts_text1
[] = { ewarn_eq1
, NULL
};
167 static v_post posts_vt
[] = { post_vt
, NULL
};
169 static const struct valids mdoc_valids
[MDOC_MAX
] = {
170 { NULL
, NULL
}, /* Ap */
171 { pre_dd
, posts_dd
}, /* Dd */
172 { pre_dt
, posts_dt
}, /* Dt */
173 { pre_os
, posts_os
}, /* Os */
174 { NULL
, posts_sh
}, /* Sh */
175 { NULL
, posts_ss
}, /* Ss */
176 { pre_par
, posts_pp
}, /* Pp */
177 { pre_display
, posts_d1
}, /* D1 */
178 { pre_literal
, posts_dl
}, /* Dl */
179 { pre_bd
, posts_bd
}, /* Bd */
180 { NULL
, NULL
}, /* Ed */
181 { pre_bl
, posts_bl
}, /* Bl */
182 { NULL
, NULL
}, /* El */
183 { pre_par
, posts_it
}, /* It */
184 { NULL
, NULL
}, /* Ad */
185 { pre_an
, posts_an
}, /* An */
186 { NULL
, posts_defaults
}, /* Ar */
187 { NULL
, NULL
}, /* Cd */
188 { NULL
, NULL
}, /* Cm */
189 { NULL
, NULL
}, /* Dv */
190 { NULL
, NULL
}, /* Er */
191 { NULL
, NULL
}, /* Ev */
192 { pre_std
, posts_ex
}, /* Ex */
193 { NULL
, NULL
}, /* Fa */
194 { NULL
, posts_text
}, /* Fd */
195 { NULL
, NULL
}, /* Fl */
196 { NULL
, NULL
}, /* Fn */
197 { NULL
, NULL
}, /* Ft */
198 { NULL
, NULL
}, /* Ic */
199 { NULL
, posts_text1
}, /* In */
200 { NULL
, posts_defaults
}, /* Li */
201 { NULL
, posts_nd
}, /* Nd */
202 { NULL
, posts_nm
}, /* Nm */
203 { NULL
, NULL
}, /* Op */
204 { pre_obsolete
, NULL
}, /* Ot */
205 { NULL
, posts_defaults
}, /* Pa */
206 { pre_std
, NULL
}, /* Rv */
207 { NULL
, posts_st
}, /* St */
208 { NULL
, NULL
}, /* Va */
209 { NULL
, posts_vt
}, /* Vt */
210 { NULL
, posts_text
}, /* Xr */
211 { NULL
, posts_text
}, /* %A */
212 { NULL
, posts_hyphtext
}, /* %B */ /* FIXME: can be used outside Rs/Re. */
213 { NULL
, posts_text
}, /* %D */
214 { NULL
, posts_text
}, /* %I */
215 { NULL
, posts_text
}, /* %J */
216 { NULL
, posts_hyphtext
}, /* %N */
217 { NULL
, posts_hyphtext
}, /* %O */
218 { NULL
, posts_text
}, /* %P */
219 { NULL
, posts_hyphtext
}, /* %R */
220 { NULL
, posts_hyphtext
}, /* %T */ /* FIXME: can be used outside Rs/Re. */
221 { NULL
, posts_text
}, /* %V */
222 { NULL
, NULL
}, /* Ac */
223 { NULL
, NULL
}, /* Ao */
224 { NULL
, NULL
}, /* Aq */
225 { NULL
, posts_at
}, /* At */
226 { NULL
, NULL
}, /* Bc */
227 { NULL
, posts_bf
}, /* Bf */
228 { NULL
, NULL
}, /* Bo */
229 { NULL
, NULL
}, /* Bq */
230 { NULL
, NULL
}, /* Bsx */
231 { NULL
, posts_bx
}, /* Bx */
232 { NULL
, posts_bool
}, /* Db */
233 { NULL
, NULL
}, /* Dc */
234 { NULL
, NULL
}, /* Do */
235 { NULL
, NULL
}, /* Dq */
236 { NULL
, NULL
}, /* Ec */
237 { NULL
, NULL
}, /* Ef */
238 { NULL
, NULL
}, /* Em */
239 { NULL
, NULL
}, /* Eo */
240 { NULL
, NULL
}, /* Fx */
241 { NULL
, NULL
}, /* Ms */
242 { NULL
, posts_notext
}, /* No */
243 { NULL
, posts_ns
}, /* Ns */
244 { NULL
, NULL
}, /* Nx */
245 { NULL
, NULL
}, /* Ox */
246 { NULL
, NULL
}, /* Pc */
247 { NULL
, posts_text1
}, /* Pf */
248 { NULL
, NULL
}, /* Po */
249 { NULL
, NULL
}, /* Pq */
250 { NULL
, NULL
}, /* Qc */
251 { NULL
, NULL
}, /* Ql */
252 { NULL
, NULL
}, /* Qo */
253 { NULL
, NULL
}, /* Qq */
254 { NULL
, NULL
}, /* Re */
255 { NULL
, posts_rs
}, /* Rs */
256 { NULL
, NULL
}, /* Sc */
257 { NULL
, NULL
}, /* So */
258 { NULL
, NULL
}, /* Sq */
259 { NULL
, posts_bool
}, /* Sm */
260 { NULL
, posts_hyph
}, /* Sx */
261 { NULL
, NULL
}, /* Sy */
262 { NULL
, NULL
}, /* Tn */
263 { NULL
, NULL
}, /* Ux */
264 { NULL
, NULL
}, /* Xc */
265 { NULL
, NULL
}, /* Xo */
266 { NULL
, posts_fo
}, /* Fo */
267 { NULL
, NULL
}, /* Fc */
268 { NULL
, NULL
}, /* Oo */
269 { NULL
, NULL
}, /* Oc */
270 { NULL
, posts_bk
}, /* Bk */
271 { NULL
, NULL
}, /* Ek */
272 { NULL
, posts_eoln
}, /* Bt */
273 { NULL
, NULL
}, /* Hf */
274 { pre_obsolete
, NULL
}, /* Fr */
275 { NULL
, posts_eoln
}, /* Ud */
276 { NULL
, posts_lb
}, /* Lb */
277 { pre_par
, posts_pp
}, /* Lp */
278 { NULL
, NULL
}, /* Lk */
279 { NULL
, posts_defaults
}, /* Mt */
280 { NULL
, NULL
}, /* Brq */
281 { NULL
, NULL
}, /* Bro */
282 { NULL
, NULL
}, /* Brc */
283 { NULL
, posts_text
}, /* %C */
284 { pre_obsolete
, posts_es
}, /* Es */
285 { pre_obsolete
, posts_en
}, /* En */
286 { NULL
, NULL
}, /* Dx */
287 { NULL
, posts_text
}, /* %Q */
288 { NULL
, posts_pp
}, /* br */
289 { NULL
, posts_sp
}, /* sp */
290 { NULL
, posts_text1
}, /* %U */
291 { NULL
, NULL
}, /* Ta */
292 { NULL
, NULL
}, /* ll */
295 #define RSORD_MAX 14 /* Number of `Rs' blocks. */
297 static const enum mdoct rsord
[RSORD_MAX
] = {
314 static const char * const secnames
[SEC__MAX
] = {
321 "IMPLEMENTATION NOTES",
336 "SECURITY CONSIDERATIONS",
342 mdoc_valid_pre(struct mdoc
*mdoc
, struct mdoc_node
*n
)
348 check_text(mdoc
, n
->line
, n
->pos
, n
->string
);
361 p
= mdoc_valids
[n
->tok
].pre
;
362 return(*p
? (*p
)(mdoc
, n
) : 1);
366 mdoc_valid_post(struct mdoc
*mdoc
)
370 if (MDOC_VALID
& mdoc
->last
->flags
)
372 mdoc
->last
->flags
|= MDOC_VALID
;
374 switch (mdoc
->last
->type
) {
382 return(post_root(mdoc
));
387 if (NULL
== mdoc_valids
[mdoc
->last
->tok
].post
)
389 for (p
= mdoc_valids
[mdoc
->last
->tok
].post
; *p
; p
++)
397 check_count(struct mdoc
*mdoc
, enum mdoc_type type
,
398 enum check_lvl lvl
, enum check_ineq ineq
, int val
)
403 if (mdoc
->last
->type
!= type
)
409 if (mdoc
->last
->nchild
< val
)
414 if (mdoc
->last
->nchild
> val
)
419 if (val
== mdoc
->last
->nchild
)
427 t
= lvl
== CHECK_WARN
? MANDOCERR_ARGCWARN
: MANDOCERR_ARGCOUNT
;
428 mandoc_vmsg(t
, mdoc
->parse
, mdoc
->last
->line
,
429 mdoc
->last
->pos
, "want %s%d children (have %d)",
430 p
, val
, mdoc
->last
->nchild
);
438 return(check_count(mdoc
, MDOC_BODY
, CHECK_ERROR
, CHECK_GT
, 0));
444 return(check_count(mdoc
, MDOC_BODY
, CHECK_WARN
, CHECK_GT
, 0));
450 return(check_count(mdoc
, MDOC_ELEM
, CHECK_WARN
, CHECK_EQ
, 0));
456 return(check_count(mdoc
, MDOC_ELEM
, CHECK_WARN
, CHECK_EQ
, 1));
462 return(check_count(mdoc
, MDOC_ELEM
, CHECK_WARN
, CHECK_GT
, 0));
468 return(check_count(mdoc
, MDOC_ELEM
, CHECK_WARN
, CHECK_LT
, 2));
474 return(check_count(mdoc
, MDOC_HEAD
, CHECK_WARN
, CHECK_EQ
, 0));
480 return(check_count(mdoc
, MDOC_HEAD
, CHECK_WARN
, CHECK_EQ
, 1));
486 return(check_count(mdoc
, MDOC_HEAD
, CHECK_WARN
, CHECK_GT
, 0));
490 check_args(struct mdoc
*mdoc
, struct mdoc_node
*n
)
497 assert(n
->args
->argc
);
498 for (i
= 0; i
< (int)n
->args
->argc
; i
++)
499 check_argv(mdoc
, n
, &n
->args
->argv
[i
]);
503 check_argv(struct mdoc
*mdoc
, struct mdoc_node
*n
, struct mdoc_argv
*v
)
507 for (i
= 0; i
< (int)v
->sz
; i
++)
508 check_text(mdoc
, v
->line
, v
->pos
, v
->value
[i
]);
512 check_text(struct mdoc
*mdoc
, int ln
, int pos
, char *p
)
516 if (MDOC_LITERAL
& mdoc
->flags
)
519 for (cp
= p
; NULL
!= (p
= strchr(p
, '\t')); p
++)
520 mandoc_msg(MANDOCERR_FI_TAB
, mdoc
->parse
,
521 ln
, pos
+ (int)(p
- cp
), NULL
);
525 pre_display(PRE_ARGS
)
527 struct mdoc_node
*node
;
529 if (MDOC_BLOCK
!= n
->type
)
532 for (node
= mdoc
->last
->parent
; node
; node
= node
->parent
)
533 if (MDOC_BLOCK
== node
->type
)
534 if (MDOC_Bd
== node
->tok
)
538 mandoc_vmsg(MANDOCERR_BD_NEST
,
539 mdoc
->parse
, n
->line
, n
->pos
,
540 "%s in Bd", mdoc_macronames
[n
->tok
]);
548 struct mdoc_node
*np
;
549 struct mdoc_argv
*argv
, *wa
;
551 enum mdocargt mdoclt
;
554 if (MDOC_BLOCK
!= n
->type
) {
555 if (ENDBODY_NOT
!= n
->end
) {
557 np
= n
->pending
->parent
;
562 assert(MDOC_BLOCK
== np
->type
);
563 assert(MDOC_Bl
== np
->tok
);
568 * First figure out which kind of list to use: bind ourselves to
569 * the first mentioned list type and warn about any remaining
570 * ones. If we find no list type, we default to LIST_item.
574 mdoclt
= MDOC_ARG_MAX
;
575 for (i
= 0; n
->args
&& i
< (int)n
->args
->argc
; i
++) {
576 argv
= n
->args
->argv
+ i
;
579 /* Set list types. */
613 /* Set list arguments. */
615 if (n
->norm
->Bl
.comp
)
616 mandoc_msg(MANDOCERR_ARG_REP
,
617 mdoc
->parse
, argv
->line
,
618 argv
->pos
, "Bl -compact");
619 n
->norm
->Bl
.comp
= 1;
624 mandoc_msg(MANDOCERR_ARG_EMPTY
,
625 mdoc
->parse
, argv
->line
,
626 argv
->pos
, "Bl -width");
627 n
->norm
->Bl
.width
= "0n";
630 if (NULL
!= n
->norm
->Bl
.width
)
631 mandoc_vmsg(MANDOCERR_ARG_REP
,
632 mdoc
->parse
, argv
->line
,
633 argv
->pos
, "Bl -width %s",
635 n
->norm
->Bl
.width
= argv
->value
[0];
639 mandoc_msg(MANDOCERR_ARG_EMPTY
,
640 mdoc
->parse
, argv
->line
,
641 argv
->pos
, "Bl -offset");
644 if (NULL
!= n
->norm
->Bl
.offs
)
645 mandoc_vmsg(MANDOCERR_ARG_REP
,
646 mdoc
->parse
, argv
->line
,
647 argv
->pos
, "Bl -offset %s",
649 n
->norm
->Bl
.offs
= argv
->value
[0];
654 if (LIST__NONE
== lt
)
658 /* Check: multiple list types. */
660 if (LIST__NONE
!= n
->norm
->Bl
.type
) {
661 mandoc_vmsg(MANDOCERR_BL_REP
,
662 mdoc
->parse
, n
->line
, n
->pos
,
663 "Bl -%s", mdoc_argnames
[argv
->arg
]);
667 /* The list type should come first. */
669 if (n
->norm
->Bl
.width
||
672 mandoc_vmsg(MANDOCERR_BL_LATETYPE
,
673 mdoc
->parse
, n
->line
, n
->pos
, "Bl -%s",
674 mdoc_argnames
[n
->args
->argv
[0].arg
]);
676 n
->norm
->Bl
.type
= lt
;
677 if (LIST_column
== lt
) {
678 n
->norm
->Bl
.ncols
= argv
->sz
;
679 n
->norm
->Bl
.cols
= (void *)argv
->value
;
683 /* Allow lists to default to LIST_item. */
685 if (LIST__NONE
== n
->norm
->Bl
.type
) {
686 mandoc_msg(MANDOCERR_BL_NOTYPE
, mdoc
->parse
,
687 n
->line
, n
->pos
, "Bl");
688 n
->norm
->Bl
.type
= LIST_item
;
692 * Validate the width field. Some list types don't need width
693 * types and should be warned about them. Others should have it
694 * and must also be warned. Yet others have a default and need
698 switch (n
->norm
->Bl
.type
) {
700 if (NULL
== n
->norm
->Bl
.width
)
701 mandoc_msg(MANDOCERR_BL_NOWIDTH
, mdoc
->parse
,
702 n
->line
, n
->pos
, "Bl -tag");
713 if (n
->norm
->Bl
.width
)
714 mandoc_vmsg(MANDOCERR_BL_SKIPW
, mdoc
->parse
,
715 wa
->line
, wa
->pos
, "Bl -%s",
716 mdoc_argnames
[mdoclt
]);
723 if (NULL
== n
->norm
->Bl
.width
)
724 n
->norm
->Bl
.width
= "2n";
727 if (NULL
== n
->norm
->Bl
.width
)
728 n
->norm
->Bl
.width
= "3n";
734 return(pre_par(mdoc
, n
));
740 struct mdoc_node
*np
;
741 struct mdoc_argv
*argv
;
745 pre_literal(mdoc
, n
);
747 if (MDOC_BLOCK
!= n
->type
) {
748 if (ENDBODY_NOT
!= n
->end
) {
750 np
= n
->pending
->parent
;
755 assert(MDOC_BLOCK
== np
->type
);
756 assert(MDOC_Bd
== np
->tok
);
760 for (i
= 0; n
->args
&& i
< (int)n
->args
->argc
; i
++) {
761 argv
= n
->args
->argv
+ i
;
781 mandoc_msg(MANDOCERR_BD_FILE
, mdoc
->parse
,
782 n
->line
, n
->pos
, NULL
);
786 mandoc_msg(MANDOCERR_ARG_EMPTY
,
787 mdoc
->parse
, argv
->line
,
788 argv
->pos
, "Bd -offset");
791 if (NULL
!= n
->norm
->Bd
.offs
)
792 mandoc_vmsg(MANDOCERR_ARG_REP
,
793 mdoc
->parse
, argv
->line
,
794 argv
->pos
, "Bd -offset %s",
796 n
->norm
->Bd
.offs
= argv
->value
[0];
799 if (n
->norm
->Bd
.comp
)
800 mandoc_msg(MANDOCERR_ARG_REP
,
801 mdoc
->parse
, argv
->line
,
802 argv
->pos
, "Bd -compact");
803 n
->norm
->Bd
.comp
= 1;
809 if (DISP__NONE
== dt
)
812 if (DISP__NONE
== n
->norm
->Bd
.type
)
813 n
->norm
->Bd
.type
= dt
;
815 mandoc_vmsg(MANDOCERR_BD_REP
,
816 mdoc
->parse
, n
->line
, n
->pos
,
817 "Bd -%s", mdoc_argnames
[argv
->arg
]);
820 if (DISP__NONE
== n
->norm
->Bd
.type
) {
821 mandoc_msg(MANDOCERR_BD_NOTYPE
, mdoc
->parse
,
822 n
->line
, n
->pos
, "Bd");
823 n
->norm
->Bd
.type
= DISP_ragged
;
826 return(pre_par(mdoc
, n
));
832 struct mdoc_argv
*argv
;
838 for (i
= 1; i
< n
->args
->argc
; i
++) {
839 argv
= n
->args
->argv
+ i
;
840 mandoc_vmsg(MANDOCERR_AN_REP
,
841 mdoc
->parse
, argv
->line
, argv
->pos
,
842 "An -%s", mdoc_argnames
[argv
->arg
]);
845 argv
= n
->args
->argv
;
846 if (argv
->arg
== MDOC_Split
)
847 n
->norm
->An
.auth
= AUTH_split
;
848 else if (argv
->arg
== MDOC_Nosplit
)
849 n
->norm
->An
.auth
= AUTH_nosplit
;
860 if (n
->args
&& 1 == n
->args
->argc
)
861 if (MDOC_Std
== n
->args
->argv
[0].arg
)
864 mandoc_msg(MANDOCERR_ARG_STD
, mdoc
->parse
,
865 n
->line
, n
->pos
, mdoc_macronames
[n
->tok
]);
870 pre_obsolete(PRE_ARGS
)
873 if (MDOC_ELEM
== n
->type
|| MDOC_BLOCK
== n
->type
)
874 mandoc_msg(MANDOCERR_MACRO_OBS
, mdoc
->parse
,
875 n
->line
, n
->pos
, mdoc_macronames
[n
->tok
]);
883 if (NULL
== mdoc
->meta
.date
|| mdoc
->meta
.os
)
884 mandoc_msg(MANDOCERR_PROLOG_ORDER
, mdoc
->parse
,
885 n
->line
, n
->pos
, "Dt");
887 if (mdoc
->meta
.title
)
888 mandoc_msg(MANDOCERR_PROLOG_REP
, mdoc
->parse
,
889 n
->line
, n
->pos
, "Dt");
898 if (NULL
== mdoc
->meta
.title
|| NULL
== mdoc
->meta
.date
)
899 mandoc_msg(MANDOCERR_PROLOG_ORDER
, mdoc
->parse
,
900 n
->line
, n
->pos
, "Os");
903 mandoc_msg(MANDOCERR_PROLOG_REP
, mdoc
->parse
,
904 n
->line
, n
->pos
, "Os");
913 if (mdoc
->meta
.title
|| mdoc
->meta
.os
)
914 mandoc_msg(MANDOCERR_PROLOG_ORDER
, mdoc
->parse
,
915 n
->line
, n
->pos
, "Dd");
918 mandoc_msg(MANDOCERR_PROLOG_REP
, mdoc
->parse
,
919 n
->line
, n
->pos
, "Dd");
927 struct mdoc_node
*np
, *nch
;
931 * Unlike other data pointers, these are "housed" by the HEAD
932 * element, which contains the goods.
935 if (MDOC_HEAD
!= mdoc
->last
->type
) {
936 if (ENDBODY_NOT
!= mdoc
->last
->end
) {
937 assert(mdoc
->last
->pending
);
938 np
= mdoc
->last
->pending
->parent
->head
;
939 } else if (MDOC_BLOCK
!= mdoc
->last
->type
) {
940 np
= mdoc
->last
->parent
->head
;
942 np
= mdoc
->last
->head
;
945 assert(MDOC_HEAD
== np
->type
);
946 assert(MDOC_Bf
== np
->tok
);
951 assert(MDOC_BLOCK
== np
->parent
->type
);
952 assert(MDOC_Bf
== np
->parent
->tok
);
954 /* Check the number of arguments. */
957 if (NULL
== np
->parent
->args
) {
959 mandoc_msg(MANDOCERR_BF_NOFONT
, mdoc
->parse
,
960 np
->line
, np
->pos
, "Bf");
966 mandoc_vmsg(MANDOCERR_ARG_EXCESS
, mdoc
->parse
,
967 nch
->line
, nch
->pos
, "Bf ... %s", nch
->string
);
969 /* Extract argument into data. */
971 if (np
->parent
->args
) {
972 arg
= np
->parent
->args
->argv
[0].arg
;
973 if (MDOC_Emphasis
== arg
)
974 np
->norm
->Bf
.font
= FONT_Em
;
975 else if (MDOC_Literal
== arg
)
976 np
->norm
->Bf
.font
= FONT_Li
;
977 else if (MDOC_Symbolic
== arg
)
978 np
->norm
->Bf
.font
= FONT_Sy
;
984 /* Extract parameter into data. */
986 if (0 == strcmp(np
->child
->string
, "Em"))
987 np
->norm
->Bf
.font
= FONT_Em
;
988 else if (0 == strcmp(np
->child
->string
, "Li"))
989 np
->norm
->Bf
.font
= FONT_Li
;
990 else if (0 == strcmp(np
->child
->string
, "Sy"))
991 np
->norm
->Bf
.font
= FONT_Sy
;
993 mandoc_vmsg(MANDOCERR_BF_BADFONT
, mdoc
->parse
,
994 np
->child
->line
, np
->child
->pos
,
995 "Bf %s", np
->child
->string
);
1003 struct mdoc_node
*n
;
1004 const char *stdlibname
;
1007 check_count(mdoc
, MDOC_ELEM
, CHECK_WARN
, CHECK_EQ
, 1);
1009 n
= mdoc
->last
->child
;
1012 assert(MDOC_TEXT
== n
->type
);
1014 if (NULL
== (stdlibname
= mdoc_a2lib(n
->string
)))
1015 mandoc_asprintf(&libname
,
1016 "library \\(lq%s\\(rq", n
->string
);
1018 libname
= mandoc_strdup(stdlibname
);
1021 n
->string
= libname
;
1026 post_eoln(POST_ARGS
)
1028 const struct mdoc_node
*n
;
1032 mandoc_vmsg(MANDOCERR_ARG_SKIP
,
1033 mdoc
->parse
, n
->line
, n
->pos
,
1034 "%s %s", mdoc_macronames
[n
->tok
],
1042 const struct mdoc_node
*n
;
1045 * The Vt macro comes in both ELEM and BLOCK form, both of which
1046 * have different syntaxes (yet more context-sensitive
1047 * behaviour). ELEM types must have a child, which is already
1048 * guaranteed by the in_line parsing routine; BLOCK types,
1049 * specifically the BODY, should only have TEXT children.
1052 if (MDOC_BODY
!= mdoc
->last
->type
)
1055 for (n
= mdoc
->last
->child
; n
; n
= n
->next
)
1056 if (MDOC_TEXT
!= n
->type
)
1057 mandoc_msg(MANDOCERR_VT_CHILD
, mdoc
->parse
,
1058 n
->line
, n
->pos
, mdoc_macronames
[n
->tok
]);
1067 if (NULL
!= mdoc
->meta
.name
)
1070 mdoc_deroff(&mdoc
->meta
.name
, mdoc
->last
);
1072 if (NULL
== mdoc
->meta
.name
)
1073 mandoc_msg(MANDOCERR_NM_NONAME
, mdoc
->parse
,
1074 mdoc
->last
->line
, mdoc
->last
->pos
, "Nm");
1079 post_literal(POST_ARGS
)
1083 * The `Dl' (note "el" not "one") and `Bd' macros unset the
1084 * MDOC_LITERAL flag as they leave. Note that `Bd' only sets
1085 * this in literal mode, but it doesn't hurt to just switch it
1086 * off in general since displays can't be nested.
1089 if (MDOC_BODY
== mdoc
->last
->type
)
1090 mdoc
->flags
&= ~MDOC_LITERAL
;
1096 post_defaults(POST_ARGS
)
1098 struct mdoc_node
*nn
;
1101 * The `Ar' defaults to "file ..." if no value is provided as an
1102 * argument; the `Mt' and `Pa' macros use "~"; the `Li' just
1103 * gets an empty string.
1106 if (mdoc
->last
->child
)
1110 mdoc
->next
= MDOC_NEXT_CHILD
;
1114 if ( ! mdoc_word_alloc(mdoc
, nn
->line
, nn
->pos
, "file"))
1116 if ( ! mdoc_word_alloc(mdoc
, nn
->line
, nn
->pos
, "..."))
1120 if ( ! mdoc_word_alloc(mdoc
, nn
->line
, nn
->pos
, "AT&T"))
1122 if ( ! mdoc_word_alloc(mdoc
, nn
->line
, nn
->pos
, "UNIX"))
1126 if ( ! mdoc_word_alloc(mdoc
, nn
->line
, nn
->pos
, ""))
1132 if ( ! mdoc_word_alloc(mdoc
, nn
->line
, nn
->pos
, "~"))
1147 struct mdoc_node
*n
;
1148 const char *std_att
;
1152 * If we have a child, look it up in the standard keys. If a
1153 * key exist, use that instead of the child; if it doesn't,
1154 * prefix "AT&T UNIX " to the existing data.
1157 if (NULL
== (n
= mdoc
->last
->child
))
1160 assert(MDOC_TEXT
== n
->type
);
1161 if (NULL
== (std_att
= mdoc_a2att(n
->string
))) {
1162 mandoc_vmsg(MANDOCERR_AT_BAD
, mdoc
->parse
,
1163 n
->line
, n
->pos
, "At %s", n
->string
);
1164 mandoc_asprintf(&att
, "AT&T UNIX %s", n
->string
);
1166 att
= mandoc_strdup(std_att
);
1176 struct mdoc_node
*np
;
1179 if (AUTH__NONE
== np
->norm
->An
.auth
) {
1181 check_count(mdoc
, MDOC_ELEM
, CHECK_WARN
, CHECK_GT
, 0);
1182 } else if (np
->child
)
1183 check_count(mdoc
, MDOC_ELEM
, CHECK_WARN
, CHECK_EQ
, 0);
1192 if (MDOC_BLOCK
== mdoc
->last
->type
)
1193 mdoc
->last
->norm
->Es
= mdoc
->last_es
;
1201 mdoc
->last_es
= mdoc
->last
;
1210 struct mdoc_node
*nbl
, *nit
, *nch
;
1213 if (MDOC_BLOCK
!= nit
->type
)
1216 nbl
= nit
->parent
->parent
;
1217 lt
= nbl
->norm
->Bl
.type
;
1229 if (NULL
== nit
->head
->child
)
1230 mandoc_vmsg(MANDOCERR_IT_NOHEAD
,
1231 mdoc
->parse
, nit
->line
, nit
->pos
,
1233 mdoc_argnames
[nbl
->args
->argv
[0].arg
]);
1242 if (NULL
== nit
->body
->child
)
1243 mandoc_vmsg(MANDOCERR_IT_NOBODY
,
1244 mdoc
->parse
, nit
->line
, nit
->pos
,
1246 mdoc_argnames
[nbl
->args
->argv
[0].arg
]);
1249 if (NULL
!= nit
->head
->child
)
1250 mandoc_vmsg(MANDOCERR_ARG_SKIP
,
1251 mdoc
->parse
, nit
->line
, nit
->pos
,
1252 "It %s", nit
->head
->child
->string
);
1255 cols
= (int)nbl
->norm
->Bl
.ncols
;
1257 assert(NULL
== nit
->head
->child
);
1259 for (i
= 0, nch
= nit
->child
; nch
; nch
= nch
->next
)
1260 if (MDOC_BODY
== nch
->type
)
1263 if (i
< cols
|| i
> cols
+ 1)
1264 mandoc_vmsg(MANDOCERR_ARGCOUNT
,
1265 mdoc
->parse
, nit
->line
, nit
->pos
,
1266 "columns == %d (have %d)", cols
, i
);
1276 post_bl_block(POST_ARGS
)
1278 struct mdoc_node
*n
, *ni
, *nc
;
1281 * These are fairly complicated, so we've broken them into two
1282 * functions. post_bl_block_tag() is called when a -tag is
1283 * specified, but no -width (it must be guessed). The second
1284 * when a -width is specified (macro indicators must be
1285 * rewritten into real lengths).
1290 if (LIST_tag
== n
->norm
->Bl
.type
&&
1291 NULL
== n
->norm
->Bl
.width
) {
1292 if ( ! post_bl_block_tag(mdoc
))
1294 assert(n
->norm
->Bl
.width
);
1295 } else if (NULL
!= n
->norm
->Bl
.width
) {
1296 if ( ! post_bl_block_width(mdoc
))
1298 assert(n
->norm
->Bl
.width
);
1301 for (ni
= n
->body
->child
; ni
; ni
= ni
->next
) {
1302 if (NULL
== ni
->body
)
1304 nc
= ni
->body
->last
;
1305 while (NULL
!= nc
) {
1317 if (NULL
== ni
->next
) {
1318 mandoc_msg(MANDOCERR_PAR_MOVE
,
1319 mdoc
->parse
, nc
->line
, nc
->pos
,
1320 mdoc_macronames
[nc
->tok
]);
1321 if ( ! mdoc_node_relink(mdoc
, nc
))
1323 } else if (0 == n
->norm
->Bl
.comp
&&
1324 LIST_column
!= n
->norm
->Bl
.type
) {
1325 mandoc_vmsg(MANDOCERR_PAR_SKIP
,
1326 mdoc
->parse
, nc
->line
, nc
->pos
,
1328 mdoc_macronames
[nc
->tok
]);
1329 mdoc_node_delete(mdoc
, nc
);
1332 nc
= ni
->body
->last
;
1339 post_bl_block_width(POST_ARGS
)
1344 struct mdoc_node
*n
;
1350 * Calculate the real width of a list from the -width string,
1351 * which may contain a macro (with a known default width), a
1352 * literal string, or a scaling width.
1354 * If the value to -width is a macro, then we re-write it to be
1355 * the macro's width as set in share/tmac/mdoc/doc-common.
1358 if (0 == strcmp(n
->norm
->Bl
.width
, "Ds"))
1360 else if (MDOC_MAX
== (tok
= mdoc_hash_find(n
->norm
->Bl
.width
)))
1363 width
= macro2len(tok
);
1365 /* The value already exists: free and reallocate it. */
1369 for (i
= 0; i
< (int)n
->args
->argc
; i
++)
1370 if (MDOC_Width
== n
->args
->argv
[i
].arg
)
1373 assert(i
< (int)n
->args
->argc
);
1375 (void)snprintf(buf
, sizeof(buf
), "%un", (unsigned int)width
);
1376 free(n
->args
->argv
[i
].value
[0]);
1377 n
->args
->argv
[i
].value
[0] = mandoc_strdup(buf
);
1379 /* Set our width! */
1380 n
->norm
->Bl
.width
= n
->args
->argv
[i
].value
[0];
1385 post_bl_block_tag(POST_ARGS
)
1387 struct mdoc_node
*n
, *nn
;
1393 * Calculate the -width for a `Bl -tag' list if it hasn't been
1394 * provided. Uses the first head macro. NOTE AGAIN: this is
1395 * ONLY if the -width argument has NOT been provided. See
1396 * post_bl_block_width() for converting the -width string.
1402 for (nn
= n
->body
->child
; nn
; nn
= nn
->next
) {
1403 if (MDOC_It
!= nn
->tok
)
1406 assert(MDOC_BLOCK
== nn
->type
);
1407 nn
= nn
->head
->child
;
1412 if (MDOC_TEXT
== nn
->type
) {
1413 sz
= strlen(nn
->string
) + 1;
1417 if (0 != (ssz
= macro2len(nn
->tok
)))
1423 /* Defaults to ten ens. */
1425 (void)snprintf(buf
, sizeof(buf
), "%un", (unsigned int)sz
);
1428 * We have to dynamically add this to the macro's argument list.
1429 * We're guaranteed that a MDOC_Width doesn't already exist.
1433 i
= (int)(n
->args
->argc
)++;
1435 n
->args
->argv
= mandoc_reallocarray(n
->args
->argv
,
1436 n
->args
->argc
, sizeof(struct mdoc_argv
));
1438 n
->args
->argv
[i
].arg
= MDOC_Width
;
1439 n
->args
->argv
[i
].line
= n
->line
;
1440 n
->args
->argv
[i
].pos
= n
->pos
;
1441 n
->args
->argv
[i
].sz
= 1;
1442 n
->args
->argv
[i
].value
= mandoc_malloc(sizeof(char *));
1443 n
->args
->argv
[i
].value
[0] = mandoc_strdup(buf
);
1445 /* Set our width! */
1446 n
->norm
->Bl
.width
= n
->args
->argv
[i
].value
[0];
1451 post_bl_head(POST_ARGS
)
1453 struct mdoc_node
*np
, *nn
, *nnp
;
1454 struct mdoc_argv
*argv
;
1457 if (LIST_column
!= mdoc
->last
->norm
->Bl
.type
)
1458 /* FIXME: this should be ERROR class... */
1459 return(hwarn_eq0(mdoc
));
1462 * Append old-style lists, where the column width specifiers
1463 * trail as macro parameters, to the new-style ("normal-form")
1464 * lists where they're argument values following -column.
1467 if (mdoc
->last
->child
== NULL
)
1470 np
= mdoc
->last
->parent
;
1473 for (j
= 0; j
< (int)np
->args
->argc
; j
++)
1474 if (MDOC_Column
== np
->args
->argv
[j
].arg
)
1477 assert(j
< (int)np
->args
->argc
);
1480 * Accommodate for new-style groff column syntax. Shuffle the
1481 * child nodes, all of which must be TEXT, as arguments for the
1482 * column field. Then, delete the head children.
1485 argv
= np
->args
->argv
+ j
;
1487 argv
->sz
+= mdoc
->last
->nchild
;
1488 argv
->value
= mandoc_reallocarray(argv
->value
,
1489 argv
->sz
, sizeof(char *));
1491 mdoc
->last
->norm
->Bl
.ncols
= argv
->sz
;
1492 mdoc
->last
->norm
->Bl
.cols
= (void *)argv
->value
;
1494 for (nn
= mdoc
->last
->child
; nn
; i
++) {
1495 argv
->value
[i
] = nn
->string
;
1499 mdoc_node_delete(NULL
, nnp
);
1502 mdoc
->last
->nchild
= 0;
1503 mdoc
->last
->child
= NULL
;
1511 struct mdoc_node
*nparent
, *nprev
; /* of the Bl block */
1512 struct mdoc_node
*nblock
, *nbody
; /* of the Bl */
1513 struct mdoc_node
*nchild
, *nnext
; /* of the Bl body */
1516 switch (nbody
->type
) {
1518 return(post_bl_block(mdoc
));
1520 return(post_bl_head(mdoc
));
1527 nchild
= nbody
->child
;
1528 while (NULL
!= nchild
) {
1529 if (MDOC_It
== nchild
->tok
|| MDOC_Sm
== nchild
->tok
) {
1530 nchild
= nchild
->next
;
1534 mandoc_msg(MANDOCERR_BL_MOVE
, mdoc
->parse
,
1535 nchild
->line
, nchild
->pos
,
1536 mdoc_macronames
[nchild
->tok
]);
1539 * Move the node out of the Bl block.
1540 * First, collect all required node pointers.
1543 nblock
= nbody
->parent
;
1544 nprev
= nblock
->prev
;
1545 nparent
= nblock
->parent
;
1546 nnext
= nchild
->next
;
1549 * Unlink this child.
1552 assert(NULL
== nchild
->prev
);
1553 if (0 == --nbody
->nchild
) {
1554 nbody
->child
= NULL
;
1556 assert(NULL
== nnext
);
1558 nbody
->child
= nnext
;
1563 * Relink this child.
1566 nchild
->parent
= nparent
;
1567 nchild
->prev
= nprev
;
1568 nchild
->next
= nblock
;
1570 nblock
->prev
= nchild
;
1573 nparent
->child
= nchild
;
1575 nprev
->next
= nchild
;
1584 ebool(struct mdoc
*mdoc
)
1586 struct mdoc_node
*nch
;
1589 tok
= mdoc
->last
->tok
;
1590 nch
= mdoc
->last
->child
;
1594 mdoc
->flags
^= MDOC_SMOFF
;
1598 check_count(mdoc
, MDOC_ELEM
, CHECK_WARN
, CHECK_LT
, 2);
1600 assert(MDOC_TEXT
== nch
->type
);
1602 if (0 == strcmp(nch
->string
, "on")) {
1604 mdoc
->flags
&= ~MDOC_SMOFF
;
1607 if (0 == strcmp(nch
->string
, "off")) {
1609 mdoc
->flags
|= MDOC_SMOFF
;
1613 mandoc_vmsg(MANDOCERR_SM_BAD
,
1614 mdoc
->parse
, nch
->line
, nch
->pos
,
1615 "%s %s", mdoc_macronames
[tok
], nch
->string
);
1616 return(mdoc_node_relink(mdoc
, nch
));
1620 post_root(POST_ARGS
)
1622 struct mdoc_node
*n
;
1624 /* Add missing prologue data. */
1626 if ( ! (MDOC_PBODY
& mdoc
->flags
)) {
1627 mandoc_msg(MANDOCERR_PROLOG_BAD
, mdoc
->parse
, 0, 0, "EOF");
1628 if (mdoc
->meta
.date
== NULL
)
1629 mdoc
->meta
.date
= mdoc
->quick
?
1631 mandoc_normdate(mdoc
->parse
, NULL
, 0, 0);
1632 if (mdoc
->meta
.title
== NULL
)
1633 mdoc
->meta
.title
= mandoc_strdup("UNKNOWN");
1634 if (mdoc
->meta
.vol
== NULL
)
1635 mdoc
->meta
.vol
= mandoc_strdup("LOCAL");
1636 if (mdoc
->meta
.arch
== NULL
)
1637 mdoc
->meta
.msec
= mandoc_strdup("1");
1638 if (mdoc
->meta
.os
== NULL
)
1639 mdoc
->meta
.os
= mandoc_strdup("UNKNOWN");
1645 /* Check that we begin with a proper `Sh'. */
1647 if (NULL
== n
->child
)
1648 mandoc_msg(MANDOCERR_DOC_EMPTY
, mdoc
->parse
,
1649 n
->line
, n
->pos
, NULL
);
1650 else if (MDOC_Sh
!= n
->child
->tok
)
1651 mandoc_msg(MANDOCERR_SEC_BEFORE
, mdoc
->parse
,
1652 n
->child
->line
, n
->child
->pos
,
1653 mdoc_macronames
[n
->child
->tok
]);
1661 struct mdoc_node
*n
, *nch
;
1668 mandoc_msg(MANDOCERR_MACRO_EMPTY
, mdoc
->parse
,
1669 n
->line
, n
->pos
, mdoc_macronames
[n
->tok
]);
1670 mdoc_node_delete(mdoc
, n
);
1674 assert(MDOC_TEXT
== nch
->type
);
1676 if (NULL
== (p
= mdoc_a2st(nch
->string
))) {
1677 mandoc_vmsg(MANDOCERR_ST_BAD
, mdoc
->parse
,
1678 nch
->line
, nch
->pos
, "St %s", nch
->string
);
1679 mdoc_node_delete(mdoc
, n
);
1682 nch
->string
= mandoc_strdup(p
);
1691 struct mdoc_node
*nn
, *next
, *prev
;
1694 switch (mdoc
->last
->type
) {
1696 check_count(mdoc
, MDOC_HEAD
, CHECK_WARN
, CHECK_EQ
, 0);
1699 if (mdoc
->last
->child
)
1701 check_count(mdoc
, MDOC_BODY
, CHECK_WARN
, CHECK_GT
, 0);
1708 * The full `Rs' block needs special handling to order the
1709 * sub-elements according to `rsord'. Pick through each element
1710 * and correctly order it. This is an insertion sort.
1714 for (nn
= mdoc
->last
->child
->next
; nn
; nn
= next
) {
1715 /* Determine order of `nn'. */
1716 for (i
= 0; i
< RSORD_MAX
; i
++)
1717 if (rsord
[i
] == nn
->tok
)
1720 if (i
== RSORD_MAX
) {
1721 mandoc_msg(MANDOCERR_RS_BAD
,
1722 mdoc
->parse
, nn
->line
, nn
->pos
,
1723 mdoc_macronames
[nn
->tok
]);
1725 } else if (MDOC__J
== nn
->tok
|| MDOC__B
== nn
->tok
)
1726 mdoc
->last
->norm
->Rs
.quote_T
++;
1729 * Remove `nn' from the chain. This somewhat
1730 * repeats mdoc_node_unlink(), but since we're
1731 * just re-ordering, there's no need for the
1732 * full unlink process.
1735 if (NULL
!= (next
= nn
->next
))
1736 next
->prev
= nn
->prev
;
1738 if (NULL
!= (prev
= nn
->prev
))
1739 prev
->next
= nn
->next
;
1741 nn
->prev
= nn
->next
= NULL
;
1744 * Scan back until we reach a node that's
1745 * ordered before `nn'.
1748 for ( ; prev
; prev
= prev
->prev
) {
1749 /* Determine order of `prev'. */
1750 for (j
= 0; j
< RSORD_MAX
; j
++)
1751 if (rsord
[j
] == prev
->tok
)
1761 * Set `nn' back into its correct place in front
1762 * of the `prev' node.
1769 prev
->next
->prev
= nn
;
1770 nn
->next
= prev
->next
;
1773 mdoc
->last
->child
->prev
= nn
;
1774 nn
->next
= mdoc
->last
->child
;
1775 mdoc
->last
->child
= nn
;
1783 * For some arguments of some macros,
1784 * convert all breakable hyphens into ASCII_HYPH.
1787 post_hyph(POST_ARGS
)
1789 struct mdoc_node
*n
, *nch
;
1795 if (MDOC_Sh
== n
->tok
|| MDOC_Ss
== n
->tok
)
1799 if (MDOC_D1
== n
->tok
|| MDOC_Nd
== n
->tok
)
1808 for (nch
= n
->child
; nch
; nch
= nch
->next
) {
1809 if (MDOC_TEXT
!= nch
->type
)
1814 while ('\0' != *(++cp
))
1816 isalpha((unsigned char)cp
[-1]) &&
1817 isalpha((unsigned char)cp
[1]))
1827 if (MDOC_LINE
& mdoc
->last
->flags
)
1828 mandoc_msg(MANDOCERR_NS_SKIP
, mdoc
->parse
,
1829 mdoc
->last
->line
, mdoc
->last
->pos
, NULL
);
1837 if (MDOC_HEAD
== mdoc
->last
->type
)
1838 return(post_sh_head(mdoc
));
1839 if (MDOC_BODY
== mdoc
->last
->type
)
1840 return(post_sh_body(mdoc
));
1846 post_sh_body(POST_ARGS
)
1848 struct mdoc_node
*n
;
1850 if (SEC_NAME
!= mdoc
->lastsec
)
1854 * Warn if the NAME section doesn't contain the `Nm' and `Nd'
1855 * macros (can have multiple `Nm' and one `Nd'). Note that the
1856 * children of the BODY declaration can also be "text".
1859 if (NULL
== (n
= mdoc
->last
->child
)) {
1860 mandoc_msg(MANDOCERR_NAMESEC_BAD
, mdoc
->parse
,
1861 mdoc
->last
->line
, mdoc
->last
->pos
, "empty");
1865 for ( ; n
&& n
->next
; n
= n
->next
) {
1866 if (MDOC_ELEM
== n
->type
&& MDOC_Nm
== n
->tok
)
1868 if (MDOC_TEXT
== n
->type
)
1870 mandoc_msg(MANDOCERR_NAMESEC_BAD
, mdoc
->parse
,
1871 n
->line
, n
->pos
, mdoc_macronames
[n
->tok
]);
1875 if (MDOC_BLOCK
== n
->type
&& MDOC_Nd
== n
->tok
)
1878 mandoc_msg(MANDOCERR_NAMESEC_BAD
, mdoc
->parse
,
1879 n
->line
, n
->pos
, mdoc_macronames
[n
->tok
]);
1884 post_sh_head(POST_ARGS
)
1886 struct mdoc_node
*n
;
1887 const char *goodsec
;
1892 * Process a new section. Sections are either "named" or
1893 * "custom". Custom sections are user-defined, while named ones
1894 * follow a conventional order and may only appear in certain
1900 mdoc_deroff(&secname
, mdoc
->last
);
1901 sec
= NULL
== secname
? SEC_CUSTOM
: a2sec(secname
);
1903 /* The NAME should be first. */
1905 if (SEC_NAME
!= sec
&& SEC_NONE
== mdoc
->lastnamed
)
1906 mandoc_vmsg(MANDOCERR_NAMESEC_FIRST
, mdoc
->parse
,
1907 mdoc
->last
->line
, mdoc
->last
->pos
,
1910 /* The SYNOPSIS gets special attention in other areas. */
1912 if (SEC_SYNOPSIS
== sec
) {
1913 roff_setreg(mdoc
->roff
, "nS", 1, '=');
1914 mdoc
->flags
|= MDOC_SYNOPSIS
;
1916 roff_setreg(mdoc
->roff
, "nS", 0, '=');
1917 mdoc
->flags
&= ~MDOC_SYNOPSIS
;
1920 /* Mark our last section. */
1922 mdoc
->lastsec
= sec
;
1925 * Set the section attribute for the current HEAD, for its
1926 * parent BLOCK, and for the HEAD children; the latter can
1927 * only be TEXT nodes, so no recursion is needed.
1928 * For other blocks and elements, including .Sh BODY, this is
1929 * done when allocating the node data structures, but for .Sh
1930 * BLOCK and HEAD, the section is still unknown at that time.
1933 mdoc
->last
->parent
->sec
= sec
;
1934 mdoc
->last
->sec
= sec
;
1935 for (n
= mdoc
->last
->child
; n
; n
= n
->next
)
1938 /* We don't care about custom sections after this. */
1940 if (SEC_CUSTOM
== sec
) {
1946 * Check whether our non-custom section is being repeated or is
1950 if (sec
== mdoc
->lastnamed
)
1951 mandoc_vmsg(MANDOCERR_SEC_REP
, mdoc
->parse
,
1952 mdoc
->last
->line
, mdoc
->last
->pos
,
1955 if (sec
< mdoc
->lastnamed
)
1956 mandoc_vmsg(MANDOCERR_SEC_ORDER
, mdoc
->parse
,
1957 mdoc
->last
->line
, mdoc
->last
->pos
,
1960 /* Mark the last named section. */
1962 mdoc
->lastnamed
= sec
;
1964 /* Check particular section/manual conventions. */
1966 assert(mdoc
->meta
.msec
);
1971 if (*mdoc
->meta
.msec
== '4')
1973 goodsec
= "2, 3, 4, 9";
1975 case SEC_RETURN_VALUES
:
1978 if (*mdoc
->meta
.msec
== '2')
1980 if (*mdoc
->meta
.msec
== '3')
1982 if (NULL
== goodsec
)
1983 goodsec
= "2, 3, 9";
1986 if (*mdoc
->meta
.msec
== '9')
1988 if (NULL
== goodsec
)
1990 mandoc_vmsg(MANDOCERR_SEC_MSEC
, mdoc
->parse
,
1991 mdoc
->last
->line
, mdoc
->last
->pos
,
1992 "Sh %s for %s only", secname
, goodsec
);
2003 post_ignpar(POST_ARGS
)
2005 struct mdoc_node
*np
;
2007 if (MDOC_BODY
!= mdoc
->last
->type
)
2010 if (NULL
!= (np
= mdoc
->last
->child
))
2011 if (MDOC_Pp
== np
->tok
|| MDOC_Lp
== np
->tok
) {
2012 mandoc_vmsg(MANDOCERR_PAR_SKIP
,
2013 mdoc
->parse
, np
->line
, np
->pos
,
2014 "%s after %s", mdoc_macronames
[np
->tok
],
2015 mdoc_macronames
[mdoc
->last
->tok
]);
2016 mdoc_node_delete(mdoc
, np
);
2019 if (NULL
!= (np
= mdoc
->last
->last
))
2020 if (MDOC_Pp
== np
->tok
|| MDOC_Lp
== np
->tok
) {
2021 mandoc_vmsg(MANDOCERR_PAR_SKIP
, mdoc
->parse
,
2022 np
->line
, np
->pos
, "%s at the end of %s",
2023 mdoc_macronames
[np
->tok
],
2024 mdoc_macronames
[mdoc
->last
->tok
]);
2025 mdoc_node_delete(mdoc
, np
);
2035 if (NULL
== mdoc
->last
)
2037 if (MDOC_ELEM
!= n
->type
&& MDOC_BLOCK
!= n
->type
)
2041 * Don't allow prior `Lp' or `Pp' prior to a paragraph-type
2042 * block: `Lp', `Pp', or non-compact `Bd' or `Bl'.
2045 if (MDOC_Pp
!= mdoc
->last
->tok
&&
2046 MDOC_Lp
!= mdoc
->last
->tok
&&
2047 MDOC_br
!= mdoc
->last
->tok
)
2049 if (MDOC_Bl
== n
->tok
&& n
->norm
->Bl
.comp
)
2051 if (MDOC_Bd
== n
->tok
&& n
->norm
->Bd
.comp
)
2053 if (MDOC_It
== n
->tok
&& n
->parent
->norm
->Bl
.comp
)
2056 mandoc_vmsg(MANDOCERR_PAR_SKIP
, mdoc
->parse
,
2057 mdoc
->last
->line
, mdoc
->last
->pos
,
2058 "%s before %s", mdoc_macronames
[mdoc
->last
->tok
],
2059 mdoc_macronames
[n
->tok
]);
2060 mdoc_node_delete(mdoc
, mdoc
->last
);
2067 struct mdoc_node
*np
;
2069 if (MDOC_ELEM
!= mdoc
->last
->type
&&
2070 MDOC_BLOCK
!= mdoc
->last
->type
)
2073 if (NULL
== (np
= mdoc
->last
->prev
)) {
2074 np
= mdoc
->last
->parent
;
2075 if (MDOC_Sh
!= np
->tok
&& MDOC_Ss
!= np
->tok
)
2078 if (MDOC_Pp
!= np
->tok
&& MDOC_Lp
!= np
->tok
&&
2079 (MDOC_br
!= mdoc
->last
->tok
||
2080 (MDOC_sp
!= np
->tok
&& MDOC_br
!= np
->tok
)))
2084 mandoc_vmsg(MANDOCERR_PAR_SKIP
, mdoc
->parse
,
2085 mdoc
->last
->line
, mdoc
->last
->pos
,
2086 "%s after %s", mdoc_macronames
[mdoc
->last
->tok
],
2087 mdoc_macronames
[np
->tok
]);
2088 mdoc_node_delete(mdoc
, mdoc
->last
);
2093 pre_literal(PRE_ARGS
)
2096 pre_display(mdoc
, n
);
2098 if (MDOC_BODY
!= n
->type
)
2102 * The `Dl' (note "el" not "one") and `Bd -literal' and `Bd
2103 * -unfilled' macros set MDOC_LITERAL on entrance to the body.
2108 mdoc
->flags
|= MDOC_LITERAL
;
2111 if (DISP_literal
== n
->norm
->Bd
.type
)
2112 mdoc
->flags
|= MDOC_LITERAL
;
2113 if (DISP_unfilled
== n
->norm
->Bd
.type
)
2114 mdoc
->flags
|= MDOC_LITERAL
;
2127 struct mdoc_node
*n
;
2130 if (mdoc
->meta
.date
)
2131 free(mdoc
->meta
.date
);
2134 if (NULL
== n
->child
|| '\0' == n
->child
->string
[0]) {
2135 mdoc
->meta
.date
= mdoc
->quick
? mandoc_strdup("") :
2136 mandoc_normdate(mdoc
->parse
, NULL
, n
->line
, n
->pos
);
2141 mdoc_deroff(&datestr
, n
);
2143 mdoc
->meta
.date
= datestr
;
2145 mdoc
->meta
.date
= mandoc_normdate(mdoc
->parse
,
2146 datestr
, n
->line
, n
->pos
);
2155 struct mdoc_node
*nn
, *n
;
2161 if (mdoc
->meta
.title
)
2162 free(mdoc
->meta
.title
);
2164 free(mdoc
->meta
.vol
);
2165 if (mdoc
->meta
.arch
)
2166 free(mdoc
->meta
.arch
);
2168 mdoc
->meta
.title
= mdoc
->meta
.vol
= mdoc
->meta
.arch
= NULL
;
2170 /* First check that all characters are uppercase. */
2172 if (NULL
!= (nn
= n
->child
))
2173 for (p
= nn
->string
; *p
; p
++) {
2174 if (toupper((unsigned char)*p
) == *p
)
2176 mandoc_vmsg(MANDOCERR_TITLE_CASE
,
2177 mdoc
->parse
, nn
->line
,
2178 nn
->pos
+ (p
- nn
->string
),
2179 "Dt %s", nn
->string
);
2184 * title = unknown, volume = local, msec = 0, arch = NULL
2187 if (NULL
== (nn
= n
->child
)) {
2188 /* XXX: make these macro values. */
2189 /* FIXME: warn about missing values. */
2190 mdoc
->meta
.title
= mandoc_strdup("UNKNOWN");
2191 mdoc
->meta
.vol
= mandoc_strdup("LOCAL");
2192 mdoc
->meta
.msec
= mandoc_strdup("1");
2196 /* Handles: `.Dt TITLE'
2197 * title = TITLE, volume = local, msec = 0, arch = NULL
2200 mdoc
->meta
.title
= mandoc_strdup(
2201 '\0' == nn
->string
[0] ? "UNKNOWN" : nn
->string
);
2203 if (NULL
== (nn
= nn
->next
)) {
2204 /* FIXME: warn about missing msec. */
2205 /* XXX: make this a macro value. */
2206 mdoc
->meta
.vol
= mandoc_strdup("LOCAL");
2207 mdoc
->meta
.msec
= mandoc_strdup("1");
2211 /* Handles: `.Dt TITLE SEC'
2213 * volume = SEC is msec ? format(msec) : SEC,
2214 * msec = SEC is msec ? atoi(msec) : 0,
2218 cp
= mandoc_a2msec(nn
->string
);
2220 mdoc
->meta
.vol
= mandoc_strdup(cp
);
2221 mdoc
->meta
.msec
= mandoc_strdup(nn
->string
);
2223 mandoc_vmsg(MANDOCERR_MSEC_BAD
, mdoc
->parse
,
2224 nn
->line
, nn
->pos
, "Dt ... %s", nn
->string
);
2225 mdoc
->meta
.vol
= mandoc_strdup(nn
->string
);
2226 mdoc
->meta
.msec
= mandoc_strdup(nn
->string
);
2229 if (NULL
== (nn
= nn
->next
))
2232 /* Handles: `.Dt TITLE SEC VOL'
2234 * volume = VOL is vol ? format(VOL) :
2235 * VOL is arch ? format(arch) :
2239 cp
= mdoc_a2vol(nn
->string
);
2241 free(mdoc
->meta
.vol
);
2242 mdoc
->meta
.vol
= mandoc_strdup(cp
);
2244 cp
= mdoc_a2arch(nn
->string
);
2246 mandoc_vmsg(MANDOCERR_ARCH_BAD
, mdoc
->parse
,
2247 nn
->line
, nn
->pos
, "Dt ... %s", nn
->string
);
2248 free(mdoc
->meta
.vol
);
2249 mdoc
->meta
.vol
= mandoc_strdup(nn
->string
);
2251 mdoc
->meta
.arch
= mandoc_strdup(cp
);
2254 /* Ignore any subsequent parameters... */
2255 /* FIXME: warn about subsequent parameters. */
2261 post_prol(POST_ARGS
)
2264 * Remove prologue macros from the document after they're
2265 * processed. The final document uses mdoc_meta for these
2266 * values and discards the originals.
2269 mdoc_node_delete(mdoc
, mdoc
->last
);
2270 if (mdoc
->meta
.title
&& mdoc
->meta
.date
&& mdoc
->meta
.os
)
2271 mdoc
->flags
|= MDOC_PBODY
;
2279 struct mdoc_node
*n
;
2282 * Make `Bx's second argument always start with an uppercase
2283 * letter. Groff checks if it's an "accepted" term, but we just
2284 * uppercase blindly.
2287 n
= mdoc
->last
->child
;
2288 if (n
&& NULL
!= (n
= n
->next
))
2289 *n
->string
= (char)toupper((unsigned char)*n
->string
);
2298 struct utsname utsname
;
2299 static char *defbuf
;
2301 struct mdoc_node
*n
;
2306 * Set the operating system by way of the `Os' macro.
2307 * The order of precedence is:
2308 * 1. the argument of the `Os' macro, unless empty
2309 * 2. the -Ios=foo command line argument, if provided
2310 * 3. -DOSNAME="\"foo\"", if provided during compilation
2311 * 4. "sysname release" from uname(3)
2314 free(mdoc
->meta
.os
);
2315 mdoc
->meta
.os
= NULL
;
2316 mdoc_deroff(&mdoc
->meta
.os
, n
);
2321 mdoc
->meta
.os
= mandoc_strdup(mdoc
->defos
);
2326 mdoc
->meta
.os
= mandoc_strdup(OSNAME
);
2328 if (NULL
== defbuf
) {
2329 if (-1 == uname(&utsname
)) {
2330 mandoc_msg(MANDOCERR_OS_UNAME
, mdoc
->parse
,
2331 n
->line
, n
->pos
, "Os");
2332 defbuf
= mandoc_strdup("UNKNOWN");
2334 mandoc_asprintf(&defbuf
, "%s %s",
2335 utsname
.sysname
, utsname
.release
);
2337 mdoc
->meta
.os
= mandoc_strdup(defbuf
);
2343 * If no argument is provided,
2344 * fill in the name of the current manual page.
2349 struct mdoc_node
*n
;
2356 if (mdoc
->meta
.name
== NULL
) {
2357 mandoc_msg(MANDOCERR_EX_NONAME
, mdoc
->parse
,
2358 n
->line
, n
->pos
, "Ex");
2362 mdoc
->next
= MDOC_NEXT_CHILD
;
2364 if ( ! mdoc_word_alloc(mdoc
, n
->line
, n
->pos
, mdoc
->meta
.name
))
2371 static enum mdoc_sec
2372 a2sec(const char *p
)
2376 for (i
= 0; i
< (int)SEC__MAX
; i
++)
2377 if (secnames
[i
] && 0 == strcmp(p
, secnames
[i
]))
2378 return((enum mdoc_sec
)i
);
2384 macro2len(enum mdoct macro
)