]>
git.cameronkatri.com Git - mandoc.git/blob - mdoc_validate.c
1 /* $Id: mdoc_validate.c,v 1.244 2014/08/10 23:54:41 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.
21 #include <sys/types.h>
23 #include <sys/utsname.h>
36 #include "mandoc_aux.h"
38 #include "libmandoc.h"
40 /* FIXME: .Bl -diag can't have non-text children in HEAD. */
42 #define PRE_ARGS struct mdoc *mdoc, struct mdoc_node *n
43 #define POST_ARGS struct mdoc *mdoc
56 typedef int (*v_pre
)(PRE_ARGS
);
57 typedef int (*v_post
)(POST_ARGS
);
64 static int check_count(struct mdoc
*, enum mdoc_type
,
65 enum check_lvl
, enum check_ineq
, int);
66 static void check_text(struct mdoc
*, int, int, char *);
67 static void check_argv(struct mdoc
*,
68 struct mdoc_node
*, struct mdoc_argv
*);
69 static void check_args(struct mdoc
*, struct mdoc_node
*);
70 static enum mdoc_sec
a2sec(const char *);
71 static size_t macro2len(enum mdoct
);
73 static int ebool(POST_ARGS
);
74 static int berr_ge1(POST_ARGS
);
75 static int bwarn_ge1(POST_ARGS
);
76 static int ewarn_eq0(POST_ARGS
);
77 static int ewarn_eq1(POST_ARGS
);
78 static int ewarn_ge1(POST_ARGS
);
79 static int ewarn_le1(POST_ARGS
);
80 static int hwarn_eq0(POST_ARGS
);
81 static int hwarn_eq1(POST_ARGS
);
82 static int hwarn_ge1(POST_ARGS
);
84 static int post_an(POST_ARGS
);
85 static int post_at(POST_ARGS
);
86 static int post_bf(POST_ARGS
);
87 static int post_bk(POST_ARGS
);
88 static int post_bl(POST_ARGS
);
89 static int post_bl_block(POST_ARGS
);
90 static int post_bl_block_width(POST_ARGS
);
91 static int post_bl_block_tag(POST_ARGS
);
92 static int post_bl_head(POST_ARGS
);
93 static int post_bx(POST_ARGS
);
94 static int post_d1(POST_ARGS
);
95 static int post_defaults(POST_ARGS
);
96 static int post_dd(POST_ARGS
);
97 static int post_dt(POST_ARGS
);
98 static int post_en(POST_ARGS
);
99 static int post_es(POST_ARGS
);
100 static int post_eoln(POST_ARGS
);
101 static int post_ex(POST_ARGS
);
102 static int post_fo(POST_ARGS
);
103 static int post_hyph(POST_ARGS
);
104 static int post_hyphtext(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_nd(POST_ARGS
);
110 static int post_nm(POST_ARGS
);
111 static int post_ns(POST_ARGS
);
112 static int post_os(POST_ARGS
);
113 static int post_par(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_vt(POST_ARGS
);
121 static int pre_an(PRE_ARGS
);
122 static int pre_bd(PRE_ARGS
);
123 static int pre_bl(PRE_ARGS
);
124 static int pre_dd(PRE_ARGS
);
125 static int pre_display(PRE_ARGS
);
126 static int pre_dt(PRE_ARGS
);
127 static int pre_literal(PRE_ARGS
);
128 static int pre_obsolete(PRE_ARGS
);
129 static int pre_os(PRE_ARGS
);
130 static int pre_par(PRE_ARGS
);
131 static int pre_std(PRE_ARGS
);
133 static const struct valids mdoc_valids
[MDOC_MAX
] = {
134 { NULL
, NULL
}, /* Ap */
135 { pre_dd
, post_dd
}, /* Dd */
136 { pre_dt
, post_dt
}, /* Dt */
137 { pre_os
, post_os
}, /* Os */
138 { NULL
, post_sh
}, /* Sh */
139 { NULL
, post_ignpar
}, /* Ss */
140 { pre_par
, post_par
}, /* Pp */
141 { pre_display
, post_d1
}, /* D1 */
142 { pre_literal
, post_literal
}, /* Dl */
143 { pre_bd
, post_literal
}, /* Bd */
144 { NULL
, NULL
}, /* Ed */
145 { pre_bl
, post_bl
}, /* Bl */
146 { NULL
, NULL
}, /* El */
147 { pre_par
, post_it
}, /* It */
148 { NULL
, NULL
}, /* Ad */
149 { pre_an
, post_an
}, /* An */
150 { NULL
, post_defaults
}, /* Ar */
151 { NULL
, NULL
}, /* Cd */
152 { NULL
, NULL
}, /* Cm */
153 { NULL
, NULL
}, /* Dv */
154 { NULL
, NULL
}, /* Er */
155 { NULL
, NULL
}, /* Ev */
156 { pre_std
, post_ex
}, /* Ex */
157 { NULL
, NULL
}, /* Fa */
158 { NULL
, ewarn_ge1
}, /* Fd */
159 { NULL
, NULL
}, /* Fl */
160 { NULL
, NULL
}, /* Fn */
161 { NULL
, NULL
}, /* Ft */
162 { NULL
, NULL
}, /* Ic */
163 { NULL
, ewarn_eq1
}, /* In */
164 { NULL
, post_defaults
}, /* Li */
165 { NULL
, post_nd
}, /* Nd */
166 { NULL
, post_nm
}, /* Nm */
167 { NULL
, NULL
}, /* Op */
168 { pre_obsolete
, NULL
}, /* Ot */
169 { NULL
, post_defaults
}, /* Pa */
170 { pre_std
, NULL
}, /* Rv */
171 { NULL
, post_st
}, /* St */
172 { NULL
, NULL
}, /* Va */
173 { NULL
, post_vt
}, /* Vt */
174 { NULL
, ewarn_ge1
}, /* Xr */
175 { NULL
, ewarn_ge1
}, /* %A */
176 { NULL
, post_hyphtext
}, /* %B */ /* FIXME: can be used outside Rs/Re. */
177 { NULL
, ewarn_ge1
}, /* %D */
178 { NULL
, ewarn_ge1
}, /* %I */
179 { NULL
, ewarn_ge1
}, /* %J */
180 { NULL
, post_hyphtext
}, /* %N */
181 { NULL
, post_hyphtext
}, /* %O */
182 { NULL
, ewarn_ge1
}, /* %P */
183 { NULL
, post_hyphtext
}, /* %R */
184 { NULL
, post_hyphtext
}, /* %T */ /* FIXME: can be used outside Rs/Re. */
185 { NULL
, ewarn_ge1
}, /* %V */
186 { NULL
, NULL
}, /* Ac */
187 { NULL
, NULL
}, /* Ao */
188 { NULL
, NULL
}, /* Aq */
189 { NULL
, post_at
}, /* At */
190 { NULL
, NULL
}, /* Bc */
191 { NULL
, post_bf
}, /* Bf */
192 { NULL
, NULL
}, /* Bo */
193 { NULL
, NULL
}, /* Bq */
194 { NULL
, NULL
}, /* Bsx */
195 { NULL
, post_bx
}, /* Bx */
196 { NULL
, ebool
}, /* Db */
197 { NULL
, NULL
}, /* Dc */
198 { NULL
, NULL
}, /* Do */
199 { NULL
, NULL
}, /* Dq */
200 { NULL
, NULL
}, /* Ec */
201 { NULL
, NULL
}, /* Ef */
202 { NULL
, NULL
}, /* Em */
203 { NULL
, NULL
}, /* Eo */
204 { NULL
, NULL
}, /* Fx */
205 { NULL
, NULL
}, /* Ms */
206 { NULL
, ewarn_eq0
}, /* No */
207 { NULL
, post_ns
}, /* Ns */
208 { NULL
, NULL
}, /* Nx */
209 { NULL
, NULL
}, /* Ox */
210 { NULL
, NULL
}, /* Pc */
211 { NULL
, ewarn_eq1
}, /* Pf */
212 { NULL
, NULL
}, /* Po */
213 { NULL
, NULL
}, /* Pq */
214 { NULL
, NULL
}, /* Qc */
215 { NULL
, NULL
}, /* Ql */
216 { NULL
, NULL
}, /* Qo */
217 { NULL
, NULL
}, /* Qq */
218 { NULL
, NULL
}, /* Re */
219 { NULL
, post_rs
}, /* Rs */
220 { NULL
, NULL
}, /* Sc */
221 { NULL
, NULL
}, /* So */
222 { NULL
, NULL
}, /* Sq */
223 { NULL
, ebool
}, /* Sm */
224 { NULL
, post_hyph
}, /* Sx */
225 { NULL
, NULL
}, /* Sy */
226 { NULL
, NULL
}, /* Tn */
227 { NULL
, NULL
}, /* Ux */
228 { NULL
, NULL
}, /* Xc */
229 { NULL
, NULL
}, /* Xo */
230 { NULL
, post_fo
}, /* Fo */
231 { NULL
, NULL
}, /* Fc */
232 { NULL
, NULL
}, /* Oo */
233 { NULL
, NULL
}, /* Oc */
234 { NULL
, post_bk
}, /* Bk */
235 { NULL
, NULL
}, /* Ek */
236 { NULL
, post_eoln
}, /* Bt */
237 { NULL
, NULL
}, /* Hf */
238 { pre_obsolete
, NULL
}, /* Fr */
239 { NULL
, post_eoln
}, /* Ud */
240 { NULL
, post_lb
}, /* Lb */
241 { pre_par
, post_par
}, /* Lp */
242 { NULL
, NULL
}, /* Lk */
243 { NULL
, post_defaults
}, /* Mt */
244 { NULL
, NULL
}, /* Brq */
245 { NULL
, NULL
}, /* Bro */
246 { NULL
, NULL
}, /* Brc */
247 { NULL
, ewarn_ge1
}, /* %C */
248 { pre_obsolete
, post_es
}, /* Es */
249 { pre_obsolete
, post_en
}, /* En */
250 { NULL
, NULL
}, /* Dx */
251 { NULL
, ewarn_ge1
}, /* %Q */
252 { NULL
, post_par
}, /* br */
253 { NULL
, post_par
}, /* sp */
254 { NULL
, ewarn_eq1
}, /* %U */
255 { NULL
, NULL
}, /* Ta */
256 { NULL
, NULL
}, /* ll */
259 #define RSORD_MAX 14 /* Number of `Rs' blocks. */
261 static const enum mdoct rsord
[RSORD_MAX
] = {
278 static const char * const secnames
[SEC__MAX
] = {
285 "IMPLEMENTATION NOTES",
300 "SECURITY CONSIDERATIONS",
306 mdoc_valid_pre(struct mdoc
*mdoc
, struct mdoc_node
*n
)
312 check_text(mdoc
, n
->line
, n
->pos
, n
->string
);
325 p
= mdoc_valids
[n
->tok
].pre
;
326 return(*p
? (*p
)(mdoc
, n
) : 1);
330 mdoc_valid_post(struct mdoc
*mdoc
)
336 if (n
->flags
& MDOC_VALID
)
338 n
->flags
|= MDOC_VALID
;
348 return(post_root(mdoc
));
350 p
= mdoc_valids
[n
->tok
].post
;
351 return(*p
? (*p
)(mdoc
) : 1);
356 check_count(struct mdoc
*mdoc
, enum mdoc_type type
,
357 enum check_lvl lvl
, enum check_ineq ineq
, int val
)
362 if (mdoc
->last
->type
!= type
)
368 if (mdoc
->last
->nchild
< val
)
373 if (mdoc
->last
->nchild
> val
)
378 if (val
== mdoc
->last
->nchild
)
386 t
= lvl
== CHECK_WARN
? MANDOCERR_ARGCWARN
: MANDOCERR_ARGCOUNT
;
387 mandoc_vmsg(t
, mdoc
->parse
, mdoc
->last
->line
,
388 mdoc
->last
->pos
, "want %s%d children (have %d)",
389 p
, val
, mdoc
->last
->nchild
);
397 return(check_count(mdoc
, MDOC_BODY
, CHECK_ERROR
, CHECK_GT
, 0));
403 return(check_count(mdoc
, MDOC_BODY
, CHECK_WARN
, CHECK_GT
, 0));
409 return(check_count(mdoc
, MDOC_ELEM
, CHECK_WARN
, CHECK_EQ
, 0));
415 return(check_count(mdoc
, MDOC_ELEM
, CHECK_WARN
, CHECK_EQ
, 1));
421 return(check_count(mdoc
, MDOC_ELEM
, CHECK_WARN
, CHECK_GT
, 0));
427 return(check_count(mdoc
, MDOC_ELEM
, CHECK_WARN
, CHECK_LT
, 2));
433 return(check_count(mdoc
, MDOC_HEAD
, CHECK_WARN
, CHECK_EQ
, 0));
439 return(check_count(mdoc
, MDOC_HEAD
, CHECK_WARN
, CHECK_EQ
, 1));
445 return(check_count(mdoc
, MDOC_HEAD
, CHECK_WARN
, CHECK_GT
, 0));
449 check_args(struct mdoc
*mdoc
, struct mdoc_node
*n
)
456 assert(n
->args
->argc
);
457 for (i
= 0; i
< (int)n
->args
->argc
; i
++)
458 check_argv(mdoc
, n
, &n
->args
->argv
[i
]);
462 check_argv(struct mdoc
*mdoc
, struct mdoc_node
*n
, struct mdoc_argv
*v
)
466 for (i
= 0; i
< (int)v
->sz
; i
++)
467 check_text(mdoc
, v
->line
, v
->pos
, v
->value
[i
]);
471 check_text(struct mdoc
*mdoc
, int ln
, int pos
, char *p
)
475 if (MDOC_LITERAL
& mdoc
->flags
)
478 for (cp
= p
; NULL
!= (p
= strchr(p
, '\t')); p
++)
479 mandoc_msg(MANDOCERR_FI_TAB
, mdoc
->parse
,
480 ln
, pos
+ (int)(p
- cp
), NULL
);
484 pre_display(PRE_ARGS
)
486 struct mdoc_node
*node
;
488 if (MDOC_BLOCK
!= n
->type
)
491 for (node
= mdoc
->last
->parent
; node
; node
= node
->parent
)
492 if (MDOC_BLOCK
== node
->type
)
493 if (MDOC_Bd
== node
->tok
)
497 mandoc_vmsg(MANDOCERR_BD_NEST
,
498 mdoc
->parse
, n
->line
, n
->pos
,
499 "%s in Bd", mdoc_macronames
[n
->tok
]);
507 struct mdoc_node
*np
;
508 struct mdoc_argv
*argv
, *wa
;
510 enum mdocargt mdoclt
;
513 if (MDOC_BLOCK
!= n
->type
) {
514 if (ENDBODY_NOT
!= n
->end
) {
516 np
= n
->pending
->parent
;
521 assert(MDOC_BLOCK
== np
->type
);
522 assert(MDOC_Bl
== np
->tok
);
527 * First figure out which kind of list to use: bind ourselves to
528 * the first mentioned list type and warn about any remaining
529 * ones. If we find no list type, we default to LIST_item.
533 mdoclt
= MDOC_ARG_MAX
;
534 for (i
= 0; n
->args
&& i
< (int)n
->args
->argc
; i
++) {
535 argv
= n
->args
->argv
+ i
;
538 /* Set list types. */
572 /* Set list arguments. */
574 if (n
->norm
->Bl
.comp
)
575 mandoc_msg(MANDOCERR_ARG_REP
,
576 mdoc
->parse
, argv
->line
,
577 argv
->pos
, "Bl -compact");
578 n
->norm
->Bl
.comp
= 1;
583 mandoc_msg(MANDOCERR_ARG_EMPTY
,
584 mdoc
->parse
, argv
->line
,
585 argv
->pos
, "Bl -width");
586 n
->norm
->Bl
.width
= "0n";
589 if (NULL
!= n
->norm
->Bl
.width
)
590 mandoc_vmsg(MANDOCERR_ARG_REP
,
591 mdoc
->parse
, argv
->line
,
592 argv
->pos
, "Bl -width %s",
594 n
->norm
->Bl
.width
= argv
->value
[0];
598 mandoc_msg(MANDOCERR_ARG_EMPTY
,
599 mdoc
->parse
, argv
->line
,
600 argv
->pos
, "Bl -offset");
603 if (NULL
!= n
->norm
->Bl
.offs
)
604 mandoc_vmsg(MANDOCERR_ARG_REP
,
605 mdoc
->parse
, argv
->line
,
606 argv
->pos
, "Bl -offset %s",
608 n
->norm
->Bl
.offs
= argv
->value
[0];
613 if (LIST__NONE
== lt
)
617 /* Check: multiple list types. */
619 if (LIST__NONE
!= n
->norm
->Bl
.type
) {
620 mandoc_vmsg(MANDOCERR_BL_REP
,
621 mdoc
->parse
, n
->line
, n
->pos
,
622 "Bl -%s", mdoc_argnames
[argv
->arg
]);
626 /* The list type should come first. */
628 if (n
->norm
->Bl
.width
||
631 mandoc_vmsg(MANDOCERR_BL_LATETYPE
,
632 mdoc
->parse
, n
->line
, n
->pos
, "Bl -%s",
633 mdoc_argnames
[n
->args
->argv
[0].arg
]);
635 n
->norm
->Bl
.type
= lt
;
636 if (LIST_column
== lt
) {
637 n
->norm
->Bl
.ncols
= argv
->sz
;
638 n
->norm
->Bl
.cols
= (void *)argv
->value
;
642 /* Allow lists to default to LIST_item. */
644 if (LIST__NONE
== n
->norm
->Bl
.type
) {
645 mandoc_msg(MANDOCERR_BL_NOTYPE
, mdoc
->parse
,
646 n
->line
, n
->pos
, "Bl");
647 n
->norm
->Bl
.type
= LIST_item
;
651 * Validate the width field. Some list types don't need width
652 * types and should be warned about them. Others should have it
653 * and must also be warned. Yet others have a default and need
657 switch (n
->norm
->Bl
.type
) {
659 if (NULL
== n
->norm
->Bl
.width
)
660 mandoc_msg(MANDOCERR_BL_NOWIDTH
, mdoc
->parse
,
661 n
->line
, n
->pos
, "Bl -tag");
672 if (n
->norm
->Bl
.width
)
673 mandoc_vmsg(MANDOCERR_BL_SKIPW
, mdoc
->parse
,
674 wa
->line
, wa
->pos
, "Bl -%s",
675 mdoc_argnames
[mdoclt
]);
682 if (NULL
== n
->norm
->Bl
.width
)
683 n
->norm
->Bl
.width
= "2n";
686 if (NULL
== n
->norm
->Bl
.width
)
687 n
->norm
->Bl
.width
= "3n";
693 return(pre_par(mdoc
, n
));
699 struct mdoc_node
*np
;
700 struct mdoc_argv
*argv
;
704 pre_literal(mdoc
, n
);
706 if (MDOC_BLOCK
!= n
->type
) {
707 if (ENDBODY_NOT
!= n
->end
) {
709 np
= n
->pending
->parent
;
714 assert(MDOC_BLOCK
== np
->type
);
715 assert(MDOC_Bd
== np
->tok
);
719 for (i
= 0; n
->args
&& i
< (int)n
->args
->argc
; i
++) {
720 argv
= n
->args
->argv
+ i
;
740 mandoc_msg(MANDOCERR_BD_FILE
, mdoc
->parse
,
741 n
->line
, n
->pos
, NULL
);
745 mandoc_msg(MANDOCERR_ARG_EMPTY
,
746 mdoc
->parse
, argv
->line
,
747 argv
->pos
, "Bd -offset");
750 if (NULL
!= n
->norm
->Bd
.offs
)
751 mandoc_vmsg(MANDOCERR_ARG_REP
,
752 mdoc
->parse
, argv
->line
,
753 argv
->pos
, "Bd -offset %s",
755 n
->norm
->Bd
.offs
= argv
->value
[0];
758 if (n
->norm
->Bd
.comp
)
759 mandoc_msg(MANDOCERR_ARG_REP
,
760 mdoc
->parse
, argv
->line
,
761 argv
->pos
, "Bd -compact");
762 n
->norm
->Bd
.comp
= 1;
768 if (DISP__NONE
== dt
)
771 if (DISP__NONE
== n
->norm
->Bd
.type
)
772 n
->norm
->Bd
.type
= dt
;
774 mandoc_vmsg(MANDOCERR_BD_REP
,
775 mdoc
->parse
, n
->line
, n
->pos
,
776 "Bd -%s", mdoc_argnames
[argv
->arg
]);
779 if (DISP__NONE
== n
->norm
->Bd
.type
) {
780 mandoc_msg(MANDOCERR_BD_NOTYPE
, mdoc
->parse
,
781 n
->line
, n
->pos
, "Bd");
782 n
->norm
->Bd
.type
= DISP_ragged
;
785 return(pre_par(mdoc
, n
));
791 struct mdoc_argv
*argv
;
797 for (i
= 1; i
< n
->args
->argc
; i
++) {
798 argv
= n
->args
->argv
+ i
;
799 mandoc_vmsg(MANDOCERR_AN_REP
,
800 mdoc
->parse
, argv
->line
, argv
->pos
,
801 "An -%s", mdoc_argnames
[argv
->arg
]);
804 argv
= n
->args
->argv
;
805 if (argv
->arg
== MDOC_Split
)
806 n
->norm
->An
.auth
= AUTH_split
;
807 else if (argv
->arg
== MDOC_Nosplit
)
808 n
->norm
->An
.auth
= AUTH_nosplit
;
819 if (n
->args
&& 1 == n
->args
->argc
)
820 if (MDOC_Std
== n
->args
->argv
[0].arg
)
823 mandoc_msg(MANDOCERR_ARG_STD
, mdoc
->parse
,
824 n
->line
, n
->pos
, mdoc_macronames
[n
->tok
]);
829 pre_obsolete(PRE_ARGS
)
832 if (MDOC_ELEM
== n
->type
|| MDOC_BLOCK
== n
->type
)
833 mandoc_msg(MANDOCERR_MACRO_OBS
, mdoc
->parse
,
834 n
->line
, n
->pos
, mdoc_macronames
[n
->tok
]);
842 if (mdoc
->meta
.title
!= NULL
)
843 mandoc_msg(MANDOCERR_PROLOG_REP
, mdoc
->parse
,
844 n
->line
, n
->pos
, "Dt");
845 else if (mdoc
->meta
.os
!= NULL
)
846 mandoc_msg(MANDOCERR_PROLOG_ORDER
, mdoc
->parse
,
847 n
->line
, n
->pos
, "Dt after Os");
855 if (mdoc
->meta
.os
!= NULL
)
856 mandoc_msg(MANDOCERR_PROLOG_REP
, mdoc
->parse
,
857 n
->line
, n
->pos
, "Os");
858 else if (mdoc
->flags
& MDOC_PBODY
)
859 mandoc_msg(MANDOCERR_PROLOG_LATE
, mdoc
->parse
,
860 n
->line
, n
->pos
, "Os");
868 if (mdoc
->meta
.date
!= NULL
)
869 mandoc_msg(MANDOCERR_PROLOG_REP
, mdoc
->parse
,
870 n
->line
, n
->pos
, "Dd");
871 else if (mdoc
->flags
& MDOC_PBODY
)
872 mandoc_msg(MANDOCERR_PROLOG_LATE
, mdoc
->parse
,
873 n
->line
, n
->pos
, "Dd");
874 else if (mdoc
->meta
.title
!= NULL
)
875 mandoc_msg(MANDOCERR_PROLOG_ORDER
, mdoc
->parse
,
876 n
->line
, n
->pos
, "Dd after Dt");
877 else if (mdoc
->meta
.os
!= NULL
)
878 mandoc_msg(MANDOCERR_PROLOG_ORDER
, mdoc
->parse
,
879 n
->line
, n
->pos
, "Dd after Os");
886 struct mdoc_node
*np
, *nch
;
890 * Unlike other data pointers, these are "housed" by the HEAD
891 * element, which contains the goods.
894 if (MDOC_HEAD
!= mdoc
->last
->type
) {
895 if (ENDBODY_NOT
!= mdoc
->last
->end
) {
896 assert(mdoc
->last
->pending
);
897 np
= mdoc
->last
->pending
->parent
->head
;
898 } else if (MDOC_BLOCK
!= mdoc
->last
->type
) {
899 np
= mdoc
->last
->parent
->head
;
901 np
= mdoc
->last
->head
;
904 assert(MDOC_HEAD
== np
->type
);
905 assert(MDOC_Bf
== np
->tok
);
910 assert(MDOC_BLOCK
== np
->parent
->type
);
911 assert(MDOC_Bf
== np
->parent
->tok
);
913 /* Check the number of arguments. */
916 if (NULL
== np
->parent
->args
) {
918 mandoc_msg(MANDOCERR_BF_NOFONT
, mdoc
->parse
,
919 np
->line
, np
->pos
, "Bf");
925 mandoc_vmsg(MANDOCERR_ARG_EXCESS
, mdoc
->parse
,
926 nch
->line
, nch
->pos
, "Bf ... %s", nch
->string
);
928 /* Extract argument into data. */
930 if (np
->parent
->args
) {
931 arg
= np
->parent
->args
->argv
[0].arg
;
932 if (MDOC_Emphasis
== arg
)
933 np
->norm
->Bf
.font
= FONT_Em
;
934 else if (MDOC_Literal
== arg
)
935 np
->norm
->Bf
.font
= FONT_Li
;
936 else if (MDOC_Symbolic
== arg
)
937 np
->norm
->Bf
.font
= FONT_Sy
;
943 /* Extract parameter into data. */
945 if (0 == strcmp(np
->child
->string
, "Em"))
946 np
->norm
->Bf
.font
= FONT_Em
;
947 else if (0 == strcmp(np
->child
->string
, "Li"))
948 np
->norm
->Bf
.font
= FONT_Li
;
949 else if (0 == strcmp(np
->child
->string
, "Sy"))
950 np
->norm
->Bf
.font
= FONT_Sy
;
952 mandoc_vmsg(MANDOCERR_BF_BADFONT
, mdoc
->parse
,
953 np
->child
->line
, np
->child
->pos
,
954 "Bf %s", np
->child
->string
);
963 const char *stdlibname
;
966 check_count(mdoc
, MDOC_ELEM
, CHECK_WARN
, CHECK_EQ
, 1);
968 n
= mdoc
->last
->child
;
971 assert(MDOC_TEXT
== n
->type
);
973 if (NULL
== (stdlibname
= mdoc_a2lib(n
->string
)))
974 mandoc_asprintf(&libname
,
975 "library \\(lq%s\\(rq", n
->string
);
977 libname
= mandoc_strdup(stdlibname
);
987 const struct mdoc_node
*n
;
991 mandoc_vmsg(MANDOCERR_ARG_SKIP
,
992 mdoc
->parse
, n
->line
, n
->pos
,
993 "%s %s", mdoc_macronames
[n
->tok
],
1010 const struct mdoc_node
*n
;
1013 * The Vt macro comes in both ELEM and BLOCK form, both of which
1014 * have different syntaxes (yet more context-sensitive
1015 * behaviour). ELEM types must have a child, which is already
1016 * guaranteed by the in_line parsing routine; BLOCK types,
1017 * specifically the BODY, should only have TEXT children.
1020 if (MDOC_BODY
!= mdoc
->last
->type
)
1023 for (n
= mdoc
->last
->child
; n
; n
= n
->next
)
1024 if (MDOC_TEXT
!= n
->type
)
1025 mandoc_msg(MANDOCERR_VT_CHILD
, mdoc
->parse
,
1026 n
->line
, n
->pos
, mdoc_macronames
[n
->tok
]);
1035 if (NULL
!= mdoc
->meta
.name
)
1038 mdoc_deroff(&mdoc
->meta
.name
, mdoc
->last
);
1040 if (NULL
== mdoc
->meta
.name
)
1041 mandoc_msg(MANDOCERR_NM_NONAME
, mdoc
->parse
,
1042 mdoc
->last
->line
, mdoc
->last
->pos
, "Nm");
1051 return(post_hyph(mdoc
));
1059 return(post_hyph(mdoc
));
1063 post_literal(POST_ARGS
)
1066 if (mdoc
->last
->tok
== MDOC_Bd
)
1071 * The `Dl' (note "el" not "one") and `Bd' macros unset the
1072 * MDOC_LITERAL flag as they leave. Note that `Bd' only sets
1073 * this in literal mode, but it doesn't hurt to just switch it
1074 * off in general since displays can't be nested.
1077 if (MDOC_BODY
== mdoc
->last
->type
)
1078 mdoc
->flags
&= ~MDOC_LITERAL
;
1084 post_defaults(POST_ARGS
)
1086 struct mdoc_node
*nn
;
1089 * The `Ar' defaults to "file ..." if no value is provided as an
1090 * argument; the `Mt' and `Pa' macros use "~"; the `Li' just
1091 * gets an empty string.
1094 if (mdoc
->last
->child
)
1098 mdoc
->next
= MDOC_NEXT_CHILD
;
1102 if ( ! mdoc_word_alloc(mdoc
, nn
->line
, nn
->pos
, "file"))
1104 if ( ! mdoc_word_alloc(mdoc
, nn
->line
, nn
->pos
, "..."))
1108 if ( ! mdoc_word_alloc(mdoc
, nn
->line
, nn
->pos
, ""))
1114 if ( ! mdoc_word_alloc(mdoc
, nn
->line
, nn
->pos
, "~"))
1129 struct mdoc_node
*n
;
1130 const char *std_att
;
1134 if (n
->child
== NULL
) {
1135 mdoc
->next
= MDOC_NEXT_CHILD
;
1136 if ( ! mdoc_word_alloc(mdoc
, n
->line
, n
->pos
, "AT&T UNIX"))
1143 * If we have a child, look it up in the standard keys. If a
1144 * key exist, use that instead of the child; if it doesn't,
1145 * prefix "AT&T UNIX " to the existing data.
1149 assert(MDOC_TEXT
== n
->type
);
1150 if (NULL
== (std_att
= mdoc_a2att(n
->string
))) {
1151 mandoc_vmsg(MANDOCERR_AT_BAD
, mdoc
->parse
,
1152 n
->line
, n
->pos
, "At %s", n
->string
);
1153 mandoc_asprintf(&att
, "AT&T UNIX %s", n
->string
);
1155 att
= mandoc_strdup(std_att
);
1165 struct mdoc_node
*np
;
1168 if (AUTH__NONE
== np
->norm
->An
.auth
) {
1170 check_count(mdoc
, MDOC_ELEM
, CHECK_WARN
, CHECK_GT
, 0);
1171 } else if (np
->child
)
1172 check_count(mdoc
, MDOC_ELEM
, CHECK_WARN
, CHECK_EQ
, 0);
1181 if (MDOC_BLOCK
== mdoc
->last
->type
)
1182 mdoc
->last
->norm
->Es
= mdoc
->last_es
;
1190 mdoc
->last_es
= mdoc
->last
;
1199 struct mdoc_node
*nbl
, *nit
, *nch
;
1202 if (MDOC_BLOCK
!= nit
->type
)
1205 nbl
= nit
->parent
->parent
;
1206 lt
= nbl
->norm
->Bl
.type
;
1218 if (NULL
== nit
->head
->child
)
1219 mandoc_vmsg(MANDOCERR_IT_NOHEAD
,
1220 mdoc
->parse
, nit
->line
, nit
->pos
,
1222 mdoc_argnames
[nbl
->args
->argv
[0].arg
]);
1231 if (NULL
== nit
->body
->child
)
1232 mandoc_vmsg(MANDOCERR_IT_NOBODY
,
1233 mdoc
->parse
, nit
->line
, nit
->pos
,
1235 mdoc_argnames
[nbl
->args
->argv
[0].arg
]);
1238 if (NULL
!= nit
->head
->child
)
1239 mandoc_vmsg(MANDOCERR_ARG_SKIP
,
1240 mdoc
->parse
, nit
->line
, nit
->pos
,
1241 "It %s", nit
->head
->child
->string
);
1244 cols
= (int)nbl
->norm
->Bl
.ncols
;
1246 assert(NULL
== nit
->head
->child
);
1248 for (i
= 0, nch
= nit
->child
; nch
; nch
= nch
->next
)
1249 if (MDOC_BODY
== nch
->type
)
1252 if (i
< cols
|| i
> cols
+ 1)
1253 mandoc_vmsg(MANDOCERR_ARGCOUNT
,
1254 mdoc
->parse
, nit
->line
, nit
->pos
,
1255 "columns == %d (have %d)", cols
, i
);
1265 post_bl_block(POST_ARGS
)
1267 struct mdoc_node
*n
, *ni
, *nc
;
1270 * These are fairly complicated, so we've broken them into two
1271 * functions. post_bl_block_tag() is called when a -tag is
1272 * specified, but no -width (it must be guessed). The second
1273 * when a -width is specified (macro indicators must be
1274 * rewritten into real lengths).
1279 if (LIST_tag
== n
->norm
->Bl
.type
&&
1280 NULL
== n
->norm
->Bl
.width
) {
1281 if ( ! post_bl_block_tag(mdoc
))
1283 assert(n
->norm
->Bl
.width
);
1284 } else if (NULL
!= n
->norm
->Bl
.width
) {
1285 if ( ! post_bl_block_width(mdoc
))
1287 assert(n
->norm
->Bl
.width
);
1290 for (ni
= n
->body
->child
; ni
; ni
= ni
->next
) {
1291 if (NULL
== ni
->body
)
1293 nc
= ni
->body
->last
;
1294 while (NULL
!= nc
) {
1306 if (NULL
== ni
->next
) {
1307 mandoc_msg(MANDOCERR_PAR_MOVE
,
1308 mdoc
->parse
, nc
->line
, nc
->pos
,
1309 mdoc_macronames
[nc
->tok
]);
1310 if ( ! mdoc_node_relink(mdoc
, nc
))
1312 } else if (0 == n
->norm
->Bl
.comp
&&
1313 LIST_column
!= n
->norm
->Bl
.type
) {
1314 mandoc_vmsg(MANDOCERR_PAR_SKIP
,
1315 mdoc
->parse
, nc
->line
, nc
->pos
,
1317 mdoc_macronames
[nc
->tok
]);
1318 mdoc_node_delete(mdoc
, nc
);
1321 nc
= ni
->body
->last
;
1328 post_bl_block_width(POST_ARGS
)
1333 struct mdoc_node
*n
;
1339 * Calculate the real width of a list from the -width string,
1340 * which may contain a macro (with a known default width), a
1341 * literal string, or a scaling width.
1343 * If the value to -width is a macro, then we re-write it to be
1344 * the macro's width as set in share/tmac/mdoc/doc-common.
1347 if (0 == strcmp(n
->norm
->Bl
.width
, "Ds"))
1349 else if (MDOC_MAX
== (tok
= mdoc_hash_find(n
->norm
->Bl
.width
)))
1352 width
= macro2len(tok
);
1354 /* The value already exists: free and reallocate it. */
1358 for (i
= 0; i
< (int)n
->args
->argc
; i
++)
1359 if (MDOC_Width
== n
->args
->argv
[i
].arg
)
1362 assert(i
< (int)n
->args
->argc
);
1364 (void)snprintf(buf
, sizeof(buf
), "%un", (unsigned int)width
);
1365 free(n
->args
->argv
[i
].value
[0]);
1366 n
->args
->argv
[i
].value
[0] = mandoc_strdup(buf
);
1368 /* Set our width! */
1369 n
->norm
->Bl
.width
= n
->args
->argv
[i
].value
[0];
1374 post_bl_block_tag(POST_ARGS
)
1376 struct mdoc_node
*n
, *nn
;
1382 * Calculate the -width for a `Bl -tag' list if it hasn't been
1383 * provided. Uses the first head macro. NOTE AGAIN: this is
1384 * ONLY if the -width argument has NOT been provided. See
1385 * post_bl_block_width() for converting the -width string.
1391 for (nn
= n
->body
->child
; nn
; nn
= nn
->next
) {
1392 if (MDOC_It
!= nn
->tok
)
1395 assert(MDOC_BLOCK
== nn
->type
);
1396 nn
= nn
->head
->child
;
1401 if (MDOC_TEXT
== nn
->type
) {
1402 sz
= strlen(nn
->string
) + 1;
1406 if (0 != (ssz
= macro2len(nn
->tok
)))
1412 /* Defaults to ten ens. */
1414 (void)snprintf(buf
, sizeof(buf
), "%un", (unsigned int)sz
);
1417 * We have to dynamically add this to the macro's argument list.
1418 * We're guaranteed that a MDOC_Width doesn't already exist.
1422 i
= (int)(n
->args
->argc
)++;
1424 n
->args
->argv
= mandoc_reallocarray(n
->args
->argv
,
1425 n
->args
->argc
, sizeof(struct mdoc_argv
));
1427 n
->args
->argv
[i
].arg
= MDOC_Width
;
1428 n
->args
->argv
[i
].line
= n
->line
;
1429 n
->args
->argv
[i
].pos
= n
->pos
;
1430 n
->args
->argv
[i
].sz
= 1;
1431 n
->args
->argv
[i
].value
= mandoc_malloc(sizeof(char *));
1432 n
->args
->argv
[i
].value
[0] = mandoc_strdup(buf
);
1434 /* Set our width! */
1435 n
->norm
->Bl
.width
= n
->args
->argv
[i
].value
[0];
1440 post_bl_head(POST_ARGS
)
1442 struct mdoc_node
*np
, *nn
, *nnp
;
1443 struct mdoc_argv
*argv
;
1446 if (LIST_column
!= mdoc
->last
->norm
->Bl
.type
)
1447 /* FIXME: this should be ERROR class... */
1448 return(hwarn_eq0(mdoc
));
1451 * Append old-style lists, where the column width specifiers
1452 * trail as macro parameters, to the new-style ("normal-form")
1453 * lists where they're argument values following -column.
1456 if (mdoc
->last
->child
== NULL
)
1459 np
= mdoc
->last
->parent
;
1462 for (j
= 0; j
< (int)np
->args
->argc
; j
++)
1463 if (MDOC_Column
== np
->args
->argv
[j
].arg
)
1466 assert(j
< (int)np
->args
->argc
);
1469 * Accommodate for new-style groff column syntax. Shuffle the
1470 * child nodes, all of which must be TEXT, as arguments for the
1471 * column field. Then, delete the head children.
1474 argv
= np
->args
->argv
+ j
;
1476 argv
->sz
+= mdoc
->last
->nchild
;
1477 argv
->value
= mandoc_reallocarray(argv
->value
,
1478 argv
->sz
, sizeof(char *));
1480 mdoc
->last
->norm
->Bl
.ncols
= argv
->sz
;
1481 mdoc
->last
->norm
->Bl
.cols
= (void *)argv
->value
;
1483 for (nn
= mdoc
->last
->child
; nn
; i
++) {
1484 argv
->value
[i
] = nn
->string
;
1488 mdoc_node_delete(NULL
, nnp
);
1491 mdoc
->last
->nchild
= 0;
1492 mdoc
->last
->child
= NULL
;
1500 struct mdoc_node
*nparent
, *nprev
; /* of the Bl block */
1501 struct mdoc_node
*nblock
, *nbody
; /* of the Bl */
1502 struct mdoc_node
*nchild
, *nnext
; /* of the Bl body */
1505 switch (nbody
->type
) {
1507 return(post_bl_block(mdoc
));
1509 return(post_bl_head(mdoc
));
1518 nchild
= nbody
->child
;
1519 while (NULL
!= nchild
) {
1520 if (MDOC_It
== nchild
->tok
|| MDOC_Sm
== nchild
->tok
) {
1521 nchild
= nchild
->next
;
1525 mandoc_msg(MANDOCERR_BL_MOVE
, mdoc
->parse
,
1526 nchild
->line
, nchild
->pos
,
1527 mdoc_macronames
[nchild
->tok
]);
1530 * Move the node out of the Bl block.
1531 * First, collect all required node pointers.
1534 nblock
= nbody
->parent
;
1535 nprev
= nblock
->prev
;
1536 nparent
= nblock
->parent
;
1537 nnext
= nchild
->next
;
1540 * Unlink this child.
1543 assert(NULL
== nchild
->prev
);
1544 if (0 == --nbody
->nchild
) {
1545 nbody
->child
= NULL
;
1547 assert(NULL
== nnext
);
1549 nbody
->child
= nnext
;
1554 * Relink this child.
1557 nchild
->parent
= nparent
;
1558 nchild
->prev
= nprev
;
1559 nchild
->next
= nblock
;
1561 nblock
->prev
= nchild
;
1564 nparent
->child
= nchild
;
1566 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
->meta
.date
== NULL
)
1627 mdoc
->meta
.date
= mdoc
->quick
?
1629 mandoc_normdate(mdoc
->parse
, NULL
, 0, 0);
1631 if (mdoc
->meta
.title
== NULL
) {
1632 mandoc_msg(MANDOCERR_DT_NOTITLE
,
1633 mdoc
->parse
, 0, 0, "EOF");
1634 mdoc
->meta
.title
= mandoc_strdup("UNTITLED");
1637 if (mdoc
->meta
.vol
== NULL
)
1638 mdoc
->meta
.vol
= mandoc_strdup("LOCAL");
1640 if (mdoc
->meta
.os
== NULL
) {
1641 mandoc_msg(MANDOCERR_OS_MISSING
,
1642 mdoc
->parse
, 0, 0, NULL
);
1643 mdoc
->meta
.os
= mandoc_strdup("");
1649 /* Check that we begin with a proper `Sh'. */
1651 if (NULL
== n
->child
)
1652 mandoc_msg(MANDOCERR_DOC_EMPTY
, mdoc
->parse
,
1653 n
->line
, n
->pos
, NULL
);
1654 else if (MDOC_Sh
!= n
->child
->tok
)
1655 mandoc_msg(MANDOCERR_SEC_BEFORE
, mdoc
->parse
,
1656 n
->child
->line
, n
->child
->pos
,
1657 mdoc_macronames
[n
->child
->tok
]);
1665 struct mdoc_node
*n
, *nch
;
1672 mandoc_msg(MANDOCERR_MACRO_EMPTY
, mdoc
->parse
,
1673 n
->line
, n
->pos
, mdoc_macronames
[n
->tok
]);
1674 mdoc_node_delete(mdoc
, n
);
1678 assert(MDOC_TEXT
== nch
->type
);
1680 if (NULL
== (p
= mdoc_a2st(nch
->string
))) {
1681 mandoc_vmsg(MANDOCERR_ST_BAD
, mdoc
->parse
,
1682 nch
->line
, nch
->pos
, "St %s", nch
->string
);
1683 mdoc_node_delete(mdoc
, n
);
1686 nch
->string
= mandoc_strdup(p
);
1695 struct mdoc_node
*nn
, *next
, *prev
;
1698 switch (mdoc
->last
->type
) {
1700 check_count(mdoc
, MDOC_HEAD
, CHECK_WARN
, CHECK_EQ
, 0);
1703 if (mdoc
->last
->child
)
1705 check_count(mdoc
, MDOC_BODY
, CHECK_WARN
, CHECK_GT
, 0);
1712 * The full `Rs' block needs special handling to order the
1713 * sub-elements according to `rsord'. Pick through each element
1714 * and correctly order it. This is an insertion sort.
1718 for (nn
= mdoc
->last
->child
->next
; nn
; nn
= next
) {
1719 /* Determine order of `nn'. */
1720 for (i
= 0; i
< RSORD_MAX
; i
++)
1721 if (rsord
[i
] == nn
->tok
)
1724 if (i
== RSORD_MAX
) {
1725 mandoc_msg(MANDOCERR_RS_BAD
,
1726 mdoc
->parse
, nn
->line
, nn
->pos
,
1727 mdoc_macronames
[nn
->tok
]);
1729 } else if (MDOC__J
== nn
->tok
|| MDOC__B
== nn
->tok
)
1730 mdoc
->last
->norm
->Rs
.quote_T
++;
1733 * Remove `nn' from the chain. This somewhat
1734 * repeats mdoc_node_unlink(), but since we're
1735 * just re-ordering, there's no need for the
1736 * full unlink process.
1739 if (NULL
!= (next
= nn
->next
))
1740 next
->prev
= nn
->prev
;
1742 if (NULL
!= (prev
= nn
->prev
))
1743 prev
->next
= nn
->next
;
1745 nn
->prev
= nn
->next
= NULL
;
1748 * Scan back until we reach a node that's
1749 * ordered before `nn'.
1752 for ( ; prev
; prev
= prev
->prev
) {
1753 /* Determine order of `prev'. */
1754 for (j
= 0; j
< RSORD_MAX
; j
++)
1755 if (rsord
[j
] == prev
->tok
)
1765 * Set `nn' back into its correct place in front
1766 * of the `prev' node.
1773 prev
->next
->prev
= nn
;
1774 nn
->next
= prev
->next
;
1777 mdoc
->last
->child
->prev
= nn
;
1778 nn
->next
= mdoc
->last
->child
;
1779 mdoc
->last
->child
= nn
;
1787 * For some arguments of some macros,
1788 * convert all breakable hyphens into ASCII_HYPH.
1791 post_hyph(POST_ARGS
)
1793 struct mdoc_node
*n
, *nch
;
1799 if (MDOC_Sh
== n
->tok
|| MDOC_Ss
== n
->tok
)
1803 if (MDOC_D1
== n
->tok
|| MDOC_Nd
== n
->tok
)
1812 for (nch
= n
->child
; nch
; nch
= nch
->next
) {
1813 if (MDOC_TEXT
!= nch
->type
)
1818 while ('\0' != *(++cp
))
1820 isalpha((unsigned char)cp
[-1]) &&
1821 isalpha((unsigned char)cp
[1]))
1828 post_hyphtext(POST_ARGS
)
1832 return(post_hyph(mdoc
));
1839 if (MDOC_LINE
& mdoc
->last
->flags
)
1840 mandoc_msg(MANDOCERR_NS_SKIP
, mdoc
->parse
,
1841 mdoc
->last
->line
, mdoc
->last
->pos
, NULL
);
1851 if (MDOC_HEAD
== mdoc
->last
->type
)
1852 return(post_sh_head(mdoc
));
1853 if (MDOC_BODY
== mdoc
->last
->type
)
1854 return(post_sh_body(mdoc
));
1860 post_sh_body(POST_ARGS
)
1862 struct mdoc_node
*n
;
1864 if (SEC_NAME
!= mdoc
->lastsec
)
1868 * Warn if the NAME section doesn't contain the `Nm' and `Nd'
1869 * macros (can have multiple `Nm' and one `Nd'). Note that the
1870 * children of the BODY declaration can also be "text".
1873 if (NULL
== (n
= mdoc
->last
->child
)) {
1874 mandoc_msg(MANDOCERR_NAMESEC_BAD
, mdoc
->parse
,
1875 mdoc
->last
->line
, mdoc
->last
->pos
, "empty");
1879 for ( ; n
&& n
->next
; n
= n
->next
) {
1880 if (MDOC_ELEM
== n
->type
&& MDOC_Nm
== n
->tok
)
1882 if (MDOC_TEXT
== n
->type
)
1884 mandoc_msg(MANDOCERR_NAMESEC_BAD
, mdoc
->parse
,
1885 n
->line
, n
->pos
, mdoc_macronames
[n
->tok
]);
1889 if (MDOC_BLOCK
== n
->type
&& MDOC_Nd
== n
->tok
)
1892 mandoc_msg(MANDOCERR_NAMESEC_BAD
, mdoc
->parse
,
1893 n
->line
, n
->pos
, mdoc_macronames
[n
->tok
]);
1898 post_sh_head(POST_ARGS
)
1900 struct mdoc_node
*n
;
1901 const char *goodsec
;
1906 * Process a new section. Sections are either "named" or
1907 * "custom". Custom sections are user-defined, while named ones
1908 * follow a conventional order and may only appear in certain
1914 mdoc_deroff(&secname
, mdoc
->last
);
1915 sec
= NULL
== secname
? SEC_CUSTOM
: a2sec(secname
);
1917 /* The NAME should be first. */
1919 if (SEC_NAME
!= sec
&& SEC_NONE
== mdoc
->lastnamed
)
1920 mandoc_vmsg(MANDOCERR_NAMESEC_FIRST
, mdoc
->parse
,
1921 mdoc
->last
->line
, mdoc
->last
->pos
,
1924 /* The SYNOPSIS gets special attention in other areas. */
1926 if (SEC_SYNOPSIS
== sec
) {
1927 roff_setreg(mdoc
->roff
, "nS", 1, '=');
1928 mdoc
->flags
|= MDOC_SYNOPSIS
;
1930 roff_setreg(mdoc
->roff
, "nS", 0, '=');
1931 mdoc
->flags
&= ~MDOC_SYNOPSIS
;
1934 /* Mark our last section. */
1936 mdoc
->lastsec
= sec
;
1939 * Set the section attribute for the current HEAD, for its
1940 * parent BLOCK, and for the HEAD children; the latter can
1941 * only be TEXT nodes, so no recursion is needed.
1942 * For other blocks and elements, including .Sh BODY, this is
1943 * done when allocating the node data structures, but for .Sh
1944 * BLOCK and HEAD, the section is still unknown at that time.
1947 mdoc
->last
->parent
->sec
= sec
;
1948 mdoc
->last
->sec
= sec
;
1949 for (n
= mdoc
->last
->child
; n
; n
= n
->next
)
1952 /* We don't care about custom sections after this. */
1954 if (SEC_CUSTOM
== sec
) {
1960 * Check whether our non-custom section is being repeated or is
1964 if (sec
== mdoc
->lastnamed
)
1965 mandoc_vmsg(MANDOCERR_SEC_REP
, mdoc
->parse
,
1966 mdoc
->last
->line
, mdoc
->last
->pos
,
1969 if (sec
< mdoc
->lastnamed
)
1970 mandoc_vmsg(MANDOCERR_SEC_ORDER
, mdoc
->parse
,
1971 mdoc
->last
->line
, mdoc
->last
->pos
,
1974 /* Mark the last named section. */
1976 mdoc
->lastnamed
= sec
;
1978 /* Check particular section/manual conventions. */
1980 if (mdoc
->meta
.msec
== NULL
) {
1988 if (*mdoc
->meta
.msec
== '4')
1990 goodsec
= "2, 3, 4, 9";
1992 case SEC_RETURN_VALUES
:
1995 if (*mdoc
->meta
.msec
== '2')
1997 if (*mdoc
->meta
.msec
== '3')
1999 if (NULL
== goodsec
)
2000 goodsec
= "2, 3, 9";
2003 if (*mdoc
->meta
.msec
== '9')
2005 if (NULL
== goodsec
)
2007 mandoc_vmsg(MANDOCERR_SEC_MSEC
, mdoc
->parse
,
2008 mdoc
->last
->line
, mdoc
->last
->pos
,
2009 "Sh %s for %s only", secname
, goodsec
);
2020 post_ignpar(POST_ARGS
)
2022 struct mdoc_node
*np
;
2027 if (MDOC_BODY
!= mdoc
->last
->type
)
2030 if (NULL
!= (np
= mdoc
->last
->child
))
2031 if (MDOC_Pp
== np
->tok
|| MDOC_Lp
== np
->tok
) {
2032 mandoc_vmsg(MANDOCERR_PAR_SKIP
,
2033 mdoc
->parse
, np
->line
, np
->pos
,
2034 "%s after %s", mdoc_macronames
[np
->tok
],
2035 mdoc_macronames
[mdoc
->last
->tok
]);
2036 mdoc_node_delete(mdoc
, np
);
2039 if (NULL
!= (np
= mdoc
->last
->last
))
2040 if (MDOC_Pp
== np
->tok
|| MDOC_Lp
== np
->tok
) {
2041 mandoc_vmsg(MANDOCERR_PAR_SKIP
, mdoc
->parse
,
2042 np
->line
, np
->pos
, "%s at the end of %s",
2043 mdoc_macronames
[np
->tok
],
2044 mdoc_macronames
[mdoc
->last
->tok
]);
2045 mdoc_node_delete(mdoc
, np
);
2055 if (NULL
== mdoc
->last
)
2057 if (MDOC_ELEM
!= n
->type
&& MDOC_BLOCK
!= n
->type
)
2061 * Don't allow prior `Lp' or `Pp' prior to a paragraph-type
2062 * block: `Lp', `Pp', or non-compact `Bd' or `Bl'.
2065 if (MDOC_Pp
!= mdoc
->last
->tok
&&
2066 MDOC_Lp
!= mdoc
->last
->tok
&&
2067 MDOC_br
!= mdoc
->last
->tok
)
2069 if (MDOC_Bl
== n
->tok
&& n
->norm
->Bl
.comp
)
2071 if (MDOC_Bd
== n
->tok
&& n
->norm
->Bd
.comp
)
2073 if (MDOC_It
== n
->tok
&& n
->parent
->norm
->Bl
.comp
)
2076 mandoc_vmsg(MANDOCERR_PAR_SKIP
, mdoc
->parse
,
2077 mdoc
->last
->line
, mdoc
->last
->pos
,
2078 "%s before %s", mdoc_macronames
[mdoc
->last
->tok
],
2079 mdoc_macronames
[n
->tok
]);
2080 mdoc_node_delete(mdoc
, mdoc
->last
);
2087 struct mdoc_node
*np
;
2089 if (mdoc
->last
->tok
== MDOC_sp
)
2094 if (MDOC_ELEM
!= mdoc
->last
->type
&&
2095 MDOC_BLOCK
!= mdoc
->last
->type
)
2098 if (NULL
== (np
= mdoc
->last
->prev
)) {
2099 np
= mdoc
->last
->parent
;
2100 if (MDOC_Sh
!= np
->tok
&& MDOC_Ss
!= np
->tok
)
2103 if (MDOC_Pp
!= np
->tok
&& MDOC_Lp
!= np
->tok
&&
2104 (MDOC_br
!= mdoc
->last
->tok
||
2105 (MDOC_sp
!= np
->tok
&& MDOC_br
!= np
->tok
)))
2109 mandoc_vmsg(MANDOCERR_PAR_SKIP
, mdoc
->parse
,
2110 mdoc
->last
->line
, mdoc
->last
->pos
,
2111 "%s after %s", mdoc_macronames
[mdoc
->last
->tok
],
2112 mdoc_macronames
[np
->tok
]);
2113 mdoc_node_delete(mdoc
, mdoc
->last
);
2118 pre_literal(PRE_ARGS
)
2121 pre_display(mdoc
, n
);
2123 if (MDOC_BODY
!= n
->type
)
2127 * The `Dl' (note "el" not "one") and `Bd -literal' and `Bd
2128 * -unfilled' macros set MDOC_LITERAL on entrance to the body.
2133 mdoc
->flags
|= MDOC_LITERAL
;
2136 if (DISP_literal
== n
->norm
->Bd
.type
)
2137 mdoc
->flags
|= MDOC_LITERAL
;
2138 if (DISP_unfilled
== n
->norm
->Bd
.type
)
2139 mdoc
->flags
|= MDOC_LITERAL
;
2152 struct mdoc_node
*n
;
2155 if (mdoc
->meta
.date
)
2156 free(mdoc
->meta
.date
);
2159 if (NULL
== n
->child
|| '\0' == n
->child
->string
[0]) {
2160 mdoc
->meta
.date
= mdoc
->quick
? mandoc_strdup("") :
2161 mandoc_normdate(mdoc
->parse
, NULL
, n
->line
, n
->pos
);
2166 mdoc_deroff(&datestr
, n
);
2168 mdoc
->meta
.date
= datestr
;
2170 mdoc
->meta
.date
= mandoc_normdate(mdoc
->parse
,
2171 datestr
, n
->line
, n
->pos
);
2175 mdoc_node_delete(mdoc
, n
);
2182 struct mdoc_node
*nn
, *n
;
2188 free(mdoc
->meta
.title
);
2189 free(mdoc
->meta
.msec
);
2190 free(mdoc
->meta
.vol
);
2191 free(mdoc
->meta
.arch
);
2193 mdoc
->meta
.title
= NULL
;
2194 mdoc
->meta
.msec
= NULL
;
2195 mdoc
->meta
.vol
= NULL
;
2196 mdoc
->meta
.arch
= NULL
;
2198 /* First check that all characters are uppercase. */
2200 if (NULL
!= (nn
= n
->child
))
2201 for (p
= nn
->string
; *p
; p
++) {
2202 if (toupper((unsigned char)*p
) == *p
)
2204 mandoc_vmsg(MANDOCERR_TITLE_CASE
,
2205 mdoc
->parse
, nn
->line
,
2206 nn
->pos
+ (p
- nn
->string
),
2207 "Dt %s", nn
->string
);
2211 /* No argument: msec and arch remain NULL. */
2213 if (NULL
== (nn
= n
->child
)) {
2214 mandoc_msg(MANDOCERR_DT_NOTITLE
,
2215 mdoc
->parse
, n
->line
, n
->pos
, "Dt");
2216 mdoc
->meta
.title
= mandoc_strdup("UNTITLED");
2217 mdoc
->meta
.vol
= mandoc_strdup("LOCAL");
2221 /* One argument: msec and arch remain NULL. */
2223 mdoc
->meta
.title
= mandoc_strdup(
2224 '\0' == nn
->string
[0] ? "UNTITLED" : nn
->string
);
2226 if (NULL
== (nn
= nn
->next
)) {
2227 mandoc_vmsg(MANDOCERR_MSEC_MISSING
,
2228 mdoc
->parse
, n
->line
, n
->pos
,
2229 "Dt %s", mdoc
->meta
.title
);
2230 mdoc
->meta
.vol
= mandoc_strdup("LOCAL");
2234 /* Handles: `.Dt TITLE SEC'
2236 * volume = SEC is msec ? format(msec) : SEC,
2237 * msec = SEC is msec ? atoi(msec) : 0,
2241 cp
= mandoc_a2msec(nn
->string
);
2243 mdoc
->meta
.vol
= mandoc_strdup(cp
);
2244 mdoc
->meta
.msec
= mandoc_strdup(nn
->string
);
2246 mandoc_vmsg(MANDOCERR_MSEC_BAD
, mdoc
->parse
,
2247 nn
->line
, nn
->pos
, "Dt ... %s", nn
->string
);
2248 mdoc
->meta
.vol
= mandoc_strdup(nn
->string
);
2249 mdoc
->meta
.msec
= mandoc_strdup(nn
->string
);
2252 if (NULL
== (nn
= nn
->next
))
2255 /* Handles: `.Dt TITLE SEC VOL'
2257 * volume = VOL is vol ? format(VOL) :
2258 * VOL is arch ? format(arch) :
2262 cp
= mdoc_a2vol(nn
->string
);
2264 free(mdoc
->meta
.vol
);
2265 mdoc
->meta
.vol
= mandoc_strdup(cp
);
2267 cp
= mdoc_a2arch(nn
->string
);
2269 mandoc_vmsg(MANDOCERR_ARCH_BAD
, mdoc
->parse
,
2270 nn
->line
, nn
->pos
, "Dt ... %s", nn
->string
);
2271 free(mdoc
->meta
.vol
);
2272 mdoc
->meta
.vol
= mandoc_strdup(nn
->string
);
2274 mdoc
->meta
.arch
= mandoc_strdup(cp
);
2277 /* Ignore any subsequent parameters... */
2278 /* FIXME: warn about subsequent parameters. */
2280 mdoc_node_delete(mdoc
, n
);
2287 struct mdoc_node
*n
;
2290 * Make `Bx's second argument always start with an uppercase
2291 * letter. Groff checks if it's an "accepted" term, but we just
2292 * uppercase blindly.
2295 n
= mdoc
->last
->child
;
2296 if (n
&& NULL
!= (n
= n
->next
))
2297 *n
->string
= (char)toupper((unsigned char)*n
->string
);
2306 struct utsname utsname
;
2307 static char *defbuf
;
2309 struct mdoc_node
*n
;
2314 * Set the operating system by way of the `Os' macro.
2315 * The order of precedence is:
2316 * 1. the argument of the `Os' macro, unless empty
2317 * 2. the -Ios=foo command line argument, if provided
2318 * 3. -DOSNAME="\"foo\"", if provided during compilation
2319 * 4. "sysname release" from uname(3)
2322 free(mdoc
->meta
.os
);
2323 mdoc
->meta
.os
= NULL
;
2324 mdoc_deroff(&mdoc
->meta
.os
, n
);
2329 mdoc
->meta
.os
= mandoc_strdup(mdoc
->defos
);
2334 mdoc
->meta
.os
= mandoc_strdup(OSNAME
);
2336 if (NULL
== defbuf
) {
2337 if (-1 == uname(&utsname
)) {
2338 mandoc_msg(MANDOCERR_OS_UNAME
, mdoc
->parse
,
2339 n
->line
, n
->pos
, "Os");
2340 defbuf
= mandoc_strdup("UNKNOWN");
2342 mandoc_asprintf(&defbuf
, "%s %s",
2343 utsname
.sysname
, utsname
.release
);
2345 mdoc
->meta
.os
= mandoc_strdup(defbuf
);
2349 mdoc_node_delete(mdoc
, n
);
2354 * If no argument is provided,
2355 * fill in the name of the current manual page.
2360 struct mdoc_node
*n
;
2367 if (mdoc
->meta
.name
== NULL
) {
2368 mandoc_msg(MANDOCERR_EX_NONAME
, mdoc
->parse
,
2369 n
->line
, n
->pos
, "Ex");
2373 mdoc
->next
= MDOC_NEXT_CHILD
;
2375 if ( ! mdoc_word_alloc(mdoc
, n
->line
, n
->pos
, mdoc
->meta
.name
))
2382 static enum mdoc_sec
2383 a2sec(const char *p
)
2387 for (i
= 0; i
< (int)SEC__MAX
; i
++)
2388 if (secnames
[i
] && 0 == strcmp(p
, secnames
[i
]))
2389 return((enum mdoc_sec
)i
);
2395 macro2len(enum mdoct macro
)