]>
git.cameronkatri.com Git - mandoc.git/blob - mdoc_validate.c
1 /* $Id: mdoc_validate.c,v 1.249 2014/09/12 00:54:10 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 int child_an(const struct mdoc_node
*);
71 static enum mdoc_sec
a2sec(const char *);
72 static size_t macro2len(enum mdoct
);
74 static int ebool(POST_ARGS
);
75 static int berr_ge1(POST_ARGS
);
76 static int bwarn_ge1(POST_ARGS
);
77 static int ewarn_eq0(POST_ARGS
);
78 static int ewarn_eq1(POST_ARGS
);
79 static int ewarn_ge1(POST_ARGS
);
80 static int ewarn_le1(POST_ARGS
);
81 static int hwarn_eq0(POST_ARGS
);
82 static int hwarn_eq1(POST_ARGS
);
83 static int hwarn_ge1(POST_ARGS
);
85 static int post_an(POST_ARGS
);
86 static int post_at(POST_ARGS
);
87 static int post_bf(POST_ARGS
);
88 static int post_bk(POST_ARGS
);
89 static int post_bl(POST_ARGS
);
90 static int post_bl_block(POST_ARGS
);
91 static int post_bl_block_width(POST_ARGS
);
92 static int post_bl_block_tag(POST_ARGS
);
93 static int post_bl_head(POST_ARGS
);
94 static int post_bx(POST_ARGS
);
95 static int post_d1(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_fa(POST_ARGS
);
104 static int post_fo(POST_ARGS
);
105 static int post_hyph(POST_ARGS
);
106 static int post_hyphtext(POST_ARGS
);
107 static int post_ignpar(POST_ARGS
);
108 static int post_it(POST_ARGS
);
109 static int post_lb(POST_ARGS
);
110 static int post_literal(POST_ARGS
);
111 static int post_nd(POST_ARGS
);
112 static int post_nm(POST_ARGS
);
113 static int post_ns(POST_ARGS
);
114 static int post_os(POST_ARGS
);
115 static int post_par(POST_ARGS
);
116 static int post_root(POST_ARGS
);
117 static int post_rs(POST_ARGS
);
118 static int post_sh(POST_ARGS
);
119 static int post_sh_head(POST_ARGS
);
120 static int post_sh_name(POST_ARGS
);
121 static int post_sh_see_also(POST_ARGS
);
122 static int post_sh_authors(POST_ARGS
);
123 static int post_st(POST_ARGS
);
124 static int post_vt(POST_ARGS
);
125 static int pre_an(PRE_ARGS
);
126 static int pre_bd(PRE_ARGS
);
127 static int pre_bl(PRE_ARGS
);
128 static int pre_dd(PRE_ARGS
);
129 static int pre_display(PRE_ARGS
);
130 static int pre_dt(PRE_ARGS
);
131 static int pre_literal(PRE_ARGS
);
132 static int pre_obsolete(PRE_ARGS
);
133 static int pre_os(PRE_ARGS
);
134 static int pre_par(PRE_ARGS
);
135 static int pre_std(PRE_ARGS
);
137 static const struct valids mdoc_valids
[MDOC_MAX
] = {
138 { NULL
, NULL
}, /* Ap */
139 { pre_dd
, post_dd
}, /* Dd */
140 { pre_dt
, post_dt
}, /* Dt */
141 { pre_os
, post_os
}, /* Os */
142 { NULL
, post_sh
}, /* Sh */
143 { NULL
, post_ignpar
}, /* Ss */
144 { pre_par
, post_par
}, /* Pp */
145 { pre_display
, post_d1
}, /* D1 */
146 { pre_literal
, post_literal
}, /* Dl */
147 { pre_bd
, post_literal
}, /* Bd */
148 { NULL
, NULL
}, /* Ed */
149 { pre_bl
, post_bl
}, /* Bl */
150 { NULL
, NULL
}, /* El */
151 { pre_par
, post_it
}, /* It */
152 { NULL
, NULL
}, /* Ad */
153 { pre_an
, post_an
}, /* An */
154 { NULL
, post_defaults
}, /* Ar */
155 { NULL
, NULL
}, /* Cd */
156 { NULL
, NULL
}, /* Cm */
157 { NULL
, NULL
}, /* Dv */
158 { NULL
, NULL
}, /* Er */
159 { NULL
, NULL
}, /* Ev */
160 { pre_std
, post_ex
}, /* Ex */
161 { NULL
, post_fa
}, /* Fa */
162 { NULL
, ewarn_ge1
}, /* Fd */
163 { NULL
, NULL
}, /* Fl */
164 { NULL
, post_fa
}, /* Fn */
165 { NULL
, NULL
}, /* Ft */
166 { NULL
, NULL
}, /* Ic */
167 { NULL
, ewarn_eq1
}, /* In */
168 { NULL
, post_defaults
}, /* Li */
169 { NULL
, post_nd
}, /* Nd */
170 { NULL
, post_nm
}, /* Nm */
171 { NULL
, NULL
}, /* Op */
172 { pre_obsolete
, NULL
}, /* Ot */
173 { NULL
, post_defaults
}, /* Pa */
174 { pre_std
, NULL
}, /* Rv */
175 { NULL
, post_st
}, /* St */
176 { NULL
, NULL
}, /* Va */
177 { NULL
, post_vt
}, /* Vt */
178 { NULL
, ewarn_ge1
}, /* Xr */
179 { NULL
, ewarn_ge1
}, /* %A */
180 { NULL
, post_hyphtext
}, /* %B */ /* FIXME: can be used outside Rs/Re. */
181 { NULL
, ewarn_ge1
}, /* %D */
182 { NULL
, ewarn_ge1
}, /* %I */
183 { NULL
, ewarn_ge1
}, /* %J */
184 { NULL
, post_hyphtext
}, /* %N */
185 { NULL
, post_hyphtext
}, /* %O */
186 { NULL
, ewarn_ge1
}, /* %P */
187 { NULL
, post_hyphtext
}, /* %R */
188 { NULL
, post_hyphtext
}, /* %T */ /* FIXME: can be used outside Rs/Re. */
189 { NULL
, ewarn_ge1
}, /* %V */
190 { NULL
, NULL
}, /* Ac */
191 { NULL
, NULL
}, /* Ao */
192 { NULL
, NULL
}, /* Aq */
193 { NULL
, post_at
}, /* At */
194 { NULL
, NULL
}, /* Bc */
195 { NULL
, post_bf
}, /* Bf */
196 { NULL
, NULL
}, /* Bo */
197 { NULL
, NULL
}, /* Bq */
198 { NULL
, NULL
}, /* Bsx */
199 { NULL
, post_bx
}, /* Bx */
200 { NULL
, ebool
}, /* Db */
201 { NULL
, NULL
}, /* Dc */
202 { NULL
, NULL
}, /* Do */
203 { NULL
, NULL
}, /* Dq */
204 { NULL
, NULL
}, /* Ec */
205 { NULL
, NULL
}, /* Ef */
206 { NULL
, NULL
}, /* Em */
207 { NULL
, NULL
}, /* Eo */
208 { NULL
, NULL
}, /* Fx */
209 { NULL
, NULL
}, /* Ms */
210 { NULL
, ewarn_eq0
}, /* No */
211 { NULL
, post_ns
}, /* Ns */
212 { NULL
, NULL
}, /* Nx */
213 { NULL
, NULL
}, /* Ox */
214 { NULL
, NULL
}, /* Pc */
215 { NULL
, ewarn_eq1
}, /* Pf */
216 { NULL
, NULL
}, /* Po */
217 { NULL
, NULL
}, /* Pq */
218 { NULL
, NULL
}, /* Qc */
219 { NULL
, NULL
}, /* Ql */
220 { NULL
, NULL
}, /* Qo */
221 { NULL
, NULL
}, /* Qq */
222 { NULL
, NULL
}, /* Re */
223 { NULL
, post_rs
}, /* Rs */
224 { NULL
, NULL
}, /* Sc */
225 { NULL
, NULL
}, /* So */
226 { NULL
, NULL
}, /* Sq */
227 { NULL
, ebool
}, /* Sm */
228 { NULL
, post_hyph
}, /* Sx */
229 { NULL
, NULL
}, /* Sy */
230 { NULL
, NULL
}, /* Tn */
231 { NULL
, NULL
}, /* Ux */
232 { NULL
, NULL
}, /* Xc */
233 { NULL
, NULL
}, /* Xo */
234 { NULL
, post_fo
}, /* Fo */
235 { NULL
, NULL
}, /* Fc */
236 { NULL
, NULL
}, /* Oo */
237 { NULL
, NULL
}, /* Oc */
238 { NULL
, post_bk
}, /* Bk */
239 { NULL
, NULL
}, /* Ek */
240 { NULL
, post_eoln
}, /* Bt */
241 { NULL
, NULL
}, /* Hf */
242 { pre_obsolete
, NULL
}, /* Fr */
243 { NULL
, post_eoln
}, /* Ud */
244 { NULL
, post_lb
}, /* Lb */
245 { pre_par
, post_par
}, /* Lp */
246 { NULL
, NULL
}, /* Lk */
247 { NULL
, post_defaults
}, /* Mt */
248 { NULL
, NULL
}, /* Brq */
249 { NULL
, NULL
}, /* Bro */
250 { NULL
, NULL
}, /* Brc */
251 { NULL
, ewarn_ge1
}, /* %C */
252 { pre_obsolete
, post_es
}, /* Es */
253 { pre_obsolete
, post_en
}, /* En */
254 { NULL
, NULL
}, /* Dx */
255 { NULL
, ewarn_ge1
}, /* %Q */
256 { NULL
, post_par
}, /* br */
257 { NULL
, post_par
}, /* sp */
258 { NULL
, ewarn_eq1
}, /* %U */
259 { NULL
, NULL
}, /* Ta */
260 { NULL
, NULL
}, /* ll */
263 #define RSORD_MAX 14 /* Number of `Rs' blocks. */
265 static const enum mdoct rsord
[RSORD_MAX
] = {
282 static const char * const secnames
[SEC__MAX
] = {
289 "IMPLEMENTATION NOTES",
304 "SECURITY CONSIDERATIONS",
310 mdoc_valid_pre(struct mdoc
*mdoc
, struct mdoc_node
*n
)
316 check_text(mdoc
, n
->line
, n
->pos
, n
->string
);
329 p
= mdoc_valids
[n
->tok
].pre
;
330 return(*p
? (*p
)(mdoc
, n
) : 1);
334 mdoc_valid_post(struct mdoc
*mdoc
)
340 if (n
->flags
& MDOC_VALID
)
342 n
->flags
|= MDOC_VALID
;
352 return(post_root(mdoc
));
354 p
= mdoc_valids
[n
->tok
].post
;
355 return(*p
? (*p
)(mdoc
) : 1);
360 check_count(struct mdoc
*mdoc
, enum mdoc_type type
,
361 enum check_lvl lvl
, enum check_ineq ineq
, int val
)
366 if (mdoc
->last
->type
!= type
)
372 if (mdoc
->last
->nchild
< val
)
377 if (mdoc
->last
->nchild
> val
)
382 if (val
== mdoc
->last
->nchild
)
390 t
= lvl
== CHECK_WARN
? MANDOCERR_ARGCWARN
: MANDOCERR_ARGCOUNT
;
391 mandoc_vmsg(t
, mdoc
->parse
, mdoc
->last
->line
,
392 mdoc
->last
->pos
, "want %s%d children (have %d)",
393 p
, val
, mdoc
->last
->nchild
);
401 return(check_count(mdoc
, MDOC_BODY
, CHECK_ERROR
, CHECK_GT
, 0));
407 return(check_count(mdoc
, MDOC_BODY
, CHECK_WARN
, CHECK_GT
, 0));
413 return(check_count(mdoc
, MDOC_ELEM
, CHECK_WARN
, CHECK_EQ
, 0));
419 return(check_count(mdoc
, MDOC_ELEM
, CHECK_WARN
, CHECK_EQ
, 1));
425 return(check_count(mdoc
, MDOC_ELEM
, CHECK_WARN
, CHECK_GT
, 0));
431 return(check_count(mdoc
, MDOC_ELEM
, CHECK_WARN
, CHECK_LT
, 2));
437 return(check_count(mdoc
, MDOC_HEAD
, CHECK_WARN
, CHECK_EQ
, 0));
443 return(check_count(mdoc
, MDOC_HEAD
, CHECK_WARN
, CHECK_EQ
, 1));
449 return(check_count(mdoc
, MDOC_HEAD
, CHECK_WARN
, CHECK_GT
, 0));
453 check_args(struct mdoc
*mdoc
, struct mdoc_node
*n
)
460 assert(n
->args
->argc
);
461 for (i
= 0; i
< (int)n
->args
->argc
; i
++)
462 check_argv(mdoc
, n
, &n
->args
->argv
[i
]);
466 check_argv(struct mdoc
*mdoc
, struct mdoc_node
*n
, struct mdoc_argv
*v
)
470 for (i
= 0; i
< (int)v
->sz
; i
++)
471 check_text(mdoc
, v
->line
, v
->pos
, v
->value
[i
]);
475 check_text(struct mdoc
*mdoc
, int ln
, int pos
, char *p
)
479 if (MDOC_LITERAL
& mdoc
->flags
)
482 for (cp
= p
; NULL
!= (p
= strchr(p
, '\t')); p
++)
483 mandoc_msg(MANDOCERR_FI_TAB
, mdoc
->parse
,
484 ln
, pos
+ (int)(p
- cp
), NULL
);
488 pre_display(PRE_ARGS
)
490 struct mdoc_node
*node
;
492 if (MDOC_BLOCK
!= n
->type
)
495 for (node
= mdoc
->last
->parent
; node
; node
= node
->parent
)
496 if (MDOC_BLOCK
== node
->type
)
497 if (MDOC_Bd
== node
->tok
)
501 mandoc_vmsg(MANDOCERR_BD_NEST
,
502 mdoc
->parse
, n
->line
, n
->pos
,
503 "%s in Bd", mdoc_macronames
[n
->tok
]);
511 struct mdoc_node
*np
;
512 struct mdoc_argv
*argv
, *wa
;
514 enum mdocargt mdoclt
;
517 if (MDOC_BLOCK
!= n
->type
) {
518 if (ENDBODY_NOT
!= n
->end
) {
520 np
= n
->pending
->parent
;
525 assert(MDOC_BLOCK
== np
->type
);
526 assert(MDOC_Bl
== np
->tok
);
531 * First figure out which kind of list to use: bind ourselves to
532 * the first mentioned list type and warn about any remaining
533 * ones. If we find no list type, we default to LIST_item.
536 wa
= (n
->args
== NULL
) ? NULL
: n
->args
->argv
;
537 mdoclt
= MDOC_ARG_MAX
;
538 for (i
= 0; n
->args
&& i
< (int)n
->args
->argc
; i
++) {
539 argv
= n
->args
->argv
+ i
;
542 /* Set list types. */
576 /* Set list arguments. */
578 if (n
->norm
->Bl
.comp
)
579 mandoc_msg(MANDOCERR_ARG_REP
,
580 mdoc
->parse
, argv
->line
,
581 argv
->pos
, "Bl -compact");
582 n
->norm
->Bl
.comp
= 1;
587 mandoc_msg(MANDOCERR_ARG_EMPTY
,
588 mdoc
->parse
, argv
->line
,
589 argv
->pos
, "Bl -width");
590 n
->norm
->Bl
.width
= "0n";
593 if (NULL
!= n
->norm
->Bl
.width
)
594 mandoc_vmsg(MANDOCERR_ARG_REP
,
595 mdoc
->parse
, argv
->line
,
596 argv
->pos
, "Bl -width %s",
598 n
->norm
->Bl
.width
= argv
->value
[0];
602 mandoc_msg(MANDOCERR_ARG_EMPTY
,
603 mdoc
->parse
, argv
->line
,
604 argv
->pos
, "Bl -offset");
607 if (NULL
!= n
->norm
->Bl
.offs
)
608 mandoc_vmsg(MANDOCERR_ARG_REP
,
609 mdoc
->parse
, argv
->line
,
610 argv
->pos
, "Bl -offset %s",
612 n
->norm
->Bl
.offs
= argv
->value
[0];
617 if (LIST__NONE
== lt
)
621 /* Check: multiple list types. */
623 if (LIST__NONE
!= n
->norm
->Bl
.type
) {
624 mandoc_vmsg(MANDOCERR_BL_REP
,
625 mdoc
->parse
, n
->line
, n
->pos
,
626 "Bl -%s", mdoc_argnames
[argv
->arg
]);
630 /* The list type should come first. */
632 if (n
->norm
->Bl
.width
||
635 mandoc_vmsg(MANDOCERR_BL_LATETYPE
,
636 mdoc
->parse
, n
->line
, n
->pos
, "Bl -%s",
637 mdoc_argnames
[n
->args
->argv
[0].arg
]);
639 n
->norm
->Bl
.type
= lt
;
640 if (LIST_column
== lt
) {
641 n
->norm
->Bl
.ncols
= argv
->sz
;
642 n
->norm
->Bl
.cols
= (void *)argv
->value
;
646 /* Allow lists to default to LIST_item. */
648 if (LIST__NONE
== n
->norm
->Bl
.type
) {
649 mandoc_msg(MANDOCERR_BL_NOTYPE
, mdoc
->parse
,
650 n
->line
, n
->pos
, "Bl");
651 n
->norm
->Bl
.type
= LIST_item
;
655 * Validate the width field. Some list types don't need width
656 * types and should be warned about them. Others should have it
657 * and must also be warned. Yet others have a default and need
661 switch (n
->norm
->Bl
.type
) {
663 if (NULL
== n
->norm
->Bl
.width
)
664 mandoc_msg(MANDOCERR_BL_NOWIDTH
, mdoc
->parse
,
665 n
->line
, n
->pos
, "Bl -tag");
676 if (n
->norm
->Bl
.width
)
677 mandoc_vmsg(MANDOCERR_BL_SKIPW
, mdoc
->parse
,
678 wa
->line
, wa
->pos
, "Bl -%s",
679 mdoc_argnames
[mdoclt
]);
686 if (NULL
== n
->norm
->Bl
.width
)
687 n
->norm
->Bl
.width
= "2n";
690 if (NULL
== n
->norm
->Bl
.width
)
691 n
->norm
->Bl
.width
= "3n";
697 return(pre_par(mdoc
, n
));
703 struct mdoc_node
*np
;
704 struct mdoc_argv
*argv
;
708 pre_literal(mdoc
, n
);
710 if (MDOC_BLOCK
!= n
->type
) {
711 if (ENDBODY_NOT
!= n
->end
) {
713 np
= n
->pending
->parent
;
718 assert(MDOC_BLOCK
== np
->type
);
719 assert(MDOC_Bd
== np
->tok
);
723 for (i
= 0; n
->args
&& i
< (int)n
->args
->argc
; i
++) {
724 argv
= n
->args
->argv
+ i
;
744 mandoc_msg(MANDOCERR_BD_FILE
, mdoc
->parse
,
745 n
->line
, n
->pos
, NULL
);
749 mandoc_msg(MANDOCERR_ARG_EMPTY
,
750 mdoc
->parse
, argv
->line
,
751 argv
->pos
, "Bd -offset");
754 if (NULL
!= n
->norm
->Bd
.offs
)
755 mandoc_vmsg(MANDOCERR_ARG_REP
,
756 mdoc
->parse
, argv
->line
,
757 argv
->pos
, "Bd -offset %s",
759 n
->norm
->Bd
.offs
= argv
->value
[0];
762 if (n
->norm
->Bd
.comp
)
763 mandoc_msg(MANDOCERR_ARG_REP
,
764 mdoc
->parse
, argv
->line
,
765 argv
->pos
, "Bd -compact");
766 n
->norm
->Bd
.comp
= 1;
772 if (DISP__NONE
== dt
)
775 if (DISP__NONE
== n
->norm
->Bd
.type
)
776 n
->norm
->Bd
.type
= dt
;
778 mandoc_vmsg(MANDOCERR_BD_REP
,
779 mdoc
->parse
, n
->line
, n
->pos
,
780 "Bd -%s", mdoc_argnames
[argv
->arg
]);
783 if (DISP__NONE
== n
->norm
->Bd
.type
) {
784 mandoc_msg(MANDOCERR_BD_NOTYPE
, mdoc
->parse
,
785 n
->line
, n
->pos
, "Bd");
786 n
->norm
->Bd
.type
= DISP_ragged
;
789 return(pre_par(mdoc
, n
));
795 struct mdoc_argv
*argv
;
801 for (i
= 1; i
< n
->args
->argc
; i
++) {
802 argv
= n
->args
->argv
+ i
;
803 mandoc_vmsg(MANDOCERR_AN_REP
,
804 mdoc
->parse
, argv
->line
, argv
->pos
,
805 "An -%s", mdoc_argnames
[argv
->arg
]);
808 argv
= n
->args
->argv
;
809 if (argv
->arg
== MDOC_Split
)
810 n
->norm
->An
.auth
= AUTH_split
;
811 else if (argv
->arg
== MDOC_Nosplit
)
812 n
->norm
->An
.auth
= AUTH_nosplit
;
823 if (n
->args
&& 1 == n
->args
->argc
)
824 if (MDOC_Std
== n
->args
->argv
[0].arg
)
827 mandoc_msg(MANDOCERR_ARG_STD
, mdoc
->parse
,
828 n
->line
, n
->pos
, mdoc_macronames
[n
->tok
]);
833 pre_obsolete(PRE_ARGS
)
836 if (MDOC_ELEM
== n
->type
|| MDOC_BLOCK
== n
->type
)
837 mandoc_msg(MANDOCERR_MACRO_OBS
, mdoc
->parse
,
838 n
->line
, n
->pos
, mdoc_macronames
[n
->tok
]);
846 if (mdoc
->meta
.title
!= NULL
)
847 mandoc_msg(MANDOCERR_PROLOG_REP
, mdoc
->parse
,
848 n
->line
, n
->pos
, "Dt");
849 else if (mdoc
->meta
.os
!= NULL
)
850 mandoc_msg(MANDOCERR_PROLOG_ORDER
, mdoc
->parse
,
851 n
->line
, n
->pos
, "Dt after Os");
859 if (mdoc
->meta
.os
!= NULL
)
860 mandoc_msg(MANDOCERR_PROLOG_REP
, mdoc
->parse
,
861 n
->line
, n
->pos
, "Os");
862 else if (mdoc
->flags
& MDOC_PBODY
)
863 mandoc_msg(MANDOCERR_PROLOG_LATE
, mdoc
->parse
,
864 n
->line
, n
->pos
, "Os");
872 if (mdoc
->meta
.date
!= NULL
)
873 mandoc_msg(MANDOCERR_PROLOG_REP
, mdoc
->parse
,
874 n
->line
, n
->pos
, "Dd");
875 else if (mdoc
->flags
& MDOC_PBODY
)
876 mandoc_msg(MANDOCERR_PROLOG_LATE
, mdoc
->parse
,
877 n
->line
, n
->pos
, "Dd");
878 else if (mdoc
->meta
.title
!= NULL
)
879 mandoc_msg(MANDOCERR_PROLOG_ORDER
, mdoc
->parse
,
880 n
->line
, n
->pos
, "Dd after Dt");
881 else if (mdoc
->meta
.os
!= NULL
)
882 mandoc_msg(MANDOCERR_PROLOG_ORDER
, mdoc
->parse
,
883 n
->line
, n
->pos
, "Dd after Os");
890 struct mdoc_node
*np
, *nch
;
894 * Unlike other data pointers, these are "housed" by the HEAD
895 * element, which contains the goods.
898 if (MDOC_HEAD
!= mdoc
->last
->type
) {
899 if (ENDBODY_NOT
!= mdoc
->last
->end
) {
900 assert(mdoc
->last
->pending
);
901 np
= mdoc
->last
->pending
->parent
->head
;
902 } else if (MDOC_BLOCK
!= mdoc
->last
->type
) {
903 np
= mdoc
->last
->parent
->head
;
905 np
= mdoc
->last
->head
;
908 assert(MDOC_HEAD
== np
->type
);
909 assert(MDOC_Bf
== np
->tok
);
914 assert(MDOC_BLOCK
== np
->parent
->type
);
915 assert(MDOC_Bf
== np
->parent
->tok
);
917 /* Check the number of arguments. */
920 if (NULL
== np
->parent
->args
) {
922 mandoc_msg(MANDOCERR_BF_NOFONT
, mdoc
->parse
,
923 np
->line
, np
->pos
, "Bf");
929 mandoc_vmsg(MANDOCERR_ARG_EXCESS
, mdoc
->parse
,
930 nch
->line
, nch
->pos
, "Bf ... %s", nch
->string
);
932 /* Extract argument into data. */
934 if (np
->parent
->args
) {
935 arg
= np
->parent
->args
->argv
[0].arg
;
936 if (MDOC_Emphasis
== arg
)
937 np
->norm
->Bf
.font
= FONT_Em
;
938 else if (MDOC_Literal
== arg
)
939 np
->norm
->Bf
.font
= FONT_Li
;
940 else if (MDOC_Symbolic
== arg
)
941 np
->norm
->Bf
.font
= FONT_Sy
;
947 /* Extract parameter into data. */
949 if (0 == strcmp(np
->child
->string
, "Em"))
950 np
->norm
->Bf
.font
= FONT_Em
;
951 else if (0 == strcmp(np
->child
->string
, "Li"))
952 np
->norm
->Bf
.font
= FONT_Li
;
953 else if (0 == strcmp(np
->child
->string
, "Sy"))
954 np
->norm
->Bf
.font
= FONT_Sy
;
956 mandoc_vmsg(MANDOCERR_BF_BADFONT
, mdoc
->parse
,
957 np
->child
->line
, np
->child
->pos
,
958 "Bf %s", np
->child
->string
);
967 const char *stdlibname
;
970 check_count(mdoc
, MDOC_ELEM
, CHECK_WARN
, CHECK_EQ
, 1);
972 n
= mdoc
->last
->child
;
975 assert(MDOC_TEXT
== n
->type
);
977 if (NULL
== (stdlibname
= mdoc_a2lib(n
->string
)))
978 mandoc_asprintf(&libname
,
979 "library \\(lq%s\\(rq", n
->string
);
981 libname
= mandoc_strdup(stdlibname
);
991 const struct mdoc_node
*n
;
995 mandoc_vmsg(MANDOCERR_ARG_SKIP
,
996 mdoc
->parse
, n
->line
, n
->pos
,
997 "%s %s", mdoc_macronames
[n
->tok
],
1014 const struct mdoc_node
*n
;
1017 for (n
= mdoc
->last
->child
; n
!= NULL
; n
= n
->next
) {
1018 for (cp
= n
->string
; *cp
!= '\0'; cp
++) {
1019 /* Ignore callbacks and alterations. */
1020 if (*cp
== '(' || *cp
== '{')
1024 mandoc_msg(MANDOCERR_FA_COMMA
, mdoc
->parse
,
1025 n
->line
, n
->pos
+ (cp
- n
->string
),
1036 const struct mdoc_node
*n
;
1039 * The Vt macro comes in both ELEM and BLOCK form, both of which
1040 * have different syntaxes (yet more context-sensitive
1041 * behaviour). ELEM types must have a child, which is already
1042 * guaranteed by the in_line parsing routine; BLOCK types,
1043 * specifically the BODY, should only have TEXT children.
1046 if (MDOC_BODY
!= mdoc
->last
->type
)
1049 for (n
= mdoc
->last
->child
; n
; n
= n
->next
)
1050 if (MDOC_TEXT
!= n
->type
)
1051 mandoc_msg(MANDOCERR_VT_CHILD
, mdoc
->parse
,
1052 n
->line
, n
->pos
, mdoc_macronames
[n
->tok
]);
1061 if (NULL
!= mdoc
->meta
.name
)
1064 mdoc_deroff(&mdoc
->meta
.name
, mdoc
->last
);
1066 if (NULL
== mdoc
->meta
.name
)
1067 mandoc_msg(MANDOCERR_NM_NONAME
, mdoc
->parse
,
1068 mdoc
->last
->line
, mdoc
->last
->pos
, "Nm");
1077 return(post_hyph(mdoc
));
1085 return(post_hyph(mdoc
));
1089 post_literal(POST_ARGS
)
1092 if (mdoc
->last
->tok
== MDOC_Bd
)
1097 * The `Dl' (note "el" not "one") and `Bd' macros unset the
1098 * MDOC_LITERAL flag as they leave. Note that `Bd' only sets
1099 * this in literal mode, but it doesn't hurt to just switch it
1100 * off in general since displays can't be nested.
1103 if (MDOC_BODY
== mdoc
->last
->type
)
1104 mdoc
->flags
&= ~MDOC_LITERAL
;
1110 post_defaults(POST_ARGS
)
1112 struct mdoc_node
*nn
;
1115 * The `Ar' defaults to "file ..." if no value is provided as an
1116 * argument; the `Mt' and `Pa' macros use "~"; the `Li' just
1117 * gets an empty string.
1120 if (mdoc
->last
->child
)
1124 mdoc
->next
= MDOC_NEXT_CHILD
;
1128 if ( ! mdoc_word_alloc(mdoc
, nn
->line
, nn
->pos
, "file"))
1130 if ( ! mdoc_word_alloc(mdoc
, nn
->line
, nn
->pos
, "..."))
1134 if ( ! mdoc_word_alloc(mdoc
, nn
->line
, nn
->pos
, ""))
1140 if ( ! mdoc_word_alloc(mdoc
, nn
->line
, nn
->pos
, "~"))
1155 struct mdoc_node
*n
;
1156 const char *std_att
;
1160 if (n
->child
== NULL
) {
1161 mdoc
->next
= MDOC_NEXT_CHILD
;
1162 if ( ! mdoc_word_alloc(mdoc
, n
->line
, n
->pos
, "AT&T UNIX"))
1169 * If we have a child, look it up in the standard keys. If a
1170 * key exist, use that instead of the child; if it doesn't,
1171 * prefix "AT&T UNIX " to the existing data.
1175 assert(MDOC_TEXT
== n
->type
);
1176 if (NULL
== (std_att
= mdoc_a2att(n
->string
))) {
1177 mandoc_vmsg(MANDOCERR_AT_BAD
, mdoc
->parse
,
1178 n
->line
, n
->pos
, "At %s", n
->string
);
1179 mandoc_asprintf(&att
, "AT&T UNIX %s", n
->string
);
1181 att
= mandoc_strdup(std_att
);
1191 struct mdoc_node
*np
;
1194 if (AUTH__NONE
== np
->norm
->An
.auth
) {
1196 check_count(mdoc
, MDOC_ELEM
, CHECK_WARN
, CHECK_GT
, 0);
1197 } else if (np
->child
)
1198 check_count(mdoc
, MDOC_ELEM
, CHECK_WARN
, CHECK_EQ
, 0);
1207 if (MDOC_BLOCK
== mdoc
->last
->type
)
1208 mdoc
->last
->norm
->Es
= mdoc
->last_es
;
1216 mdoc
->last_es
= mdoc
->last
;
1225 struct mdoc_node
*nbl
, *nit
, *nch
;
1228 if (MDOC_BLOCK
!= nit
->type
)
1231 nbl
= nit
->parent
->parent
;
1232 lt
= nbl
->norm
->Bl
.type
;
1244 if (NULL
== nit
->head
->child
)
1245 mandoc_vmsg(MANDOCERR_IT_NOHEAD
,
1246 mdoc
->parse
, nit
->line
, nit
->pos
,
1248 mdoc_argnames
[nbl
->args
->argv
[0].arg
]);
1257 if (NULL
== nit
->body
->child
)
1258 mandoc_vmsg(MANDOCERR_IT_NOBODY
,
1259 mdoc
->parse
, nit
->line
, nit
->pos
,
1261 mdoc_argnames
[nbl
->args
->argv
[0].arg
]);
1264 if (NULL
!= nit
->head
->child
)
1265 mandoc_vmsg(MANDOCERR_ARG_SKIP
,
1266 mdoc
->parse
, nit
->line
, nit
->pos
,
1267 "It %s", nit
->head
->child
->string
);
1270 cols
= (int)nbl
->norm
->Bl
.ncols
;
1272 assert(NULL
== nit
->head
->child
);
1274 for (i
= 0, nch
= nit
->child
; nch
; nch
= nch
->next
)
1275 if (MDOC_BODY
== nch
->type
)
1278 if (i
< cols
|| i
> cols
+ 1)
1279 mandoc_vmsg(MANDOCERR_ARGCOUNT
,
1280 mdoc
->parse
, nit
->line
, nit
->pos
,
1281 "columns == %d (have %d)", cols
, i
);
1291 post_bl_block(POST_ARGS
)
1293 struct mdoc_node
*n
, *ni
, *nc
;
1296 * These are fairly complicated, so we've broken them into two
1297 * functions. post_bl_block_tag() is called when a -tag is
1298 * specified, but no -width (it must be guessed). The second
1299 * when a -width is specified (macro indicators must be
1300 * rewritten into real lengths).
1305 if (LIST_tag
== n
->norm
->Bl
.type
&&
1306 NULL
== n
->norm
->Bl
.width
) {
1307 if ( ! post_bl_block_tag(mdoc
))
1309 assert(n
->norm
->Bl
.width
);
1310 } else if (NULL
!= n
->norm
->Bl
.width
) {
1311 if ( ! post_bl_block_width(mdoc
))
1313 assert(n
->norm
->Bl
.width
);
1316 for (ni
= n
->body
->child
; ni
; ni
= ni
->next
) {
1317 if (NULL
== ni
->body
)
1319 nc
= ni
->body
->last
;
1320 while (NULL
!= nc
) {
1332 if (NULL
== ni
->next
) {
1333 mandoc_msg(MANDOCERR_PAR_MOVE
,
1334 mdoc
->parse
, nc
->line
, nc
->pos
,
1335 mdoc_macronames
[nc
->tok
]);
1336 if ( ! mdoc_node_relink(mdoc
, nc
))
1338 } else if (0 == n
->norm
->Bl
.comp
&&
1339 LIST_column
!= n
->norm
->Bl
.type
) {
1340 mandoc_vmsg(MANDOCERR_PAR_SKIP
,
1341 mdoc
->parse
, nc
->line
, nc
->pos
,
1343 mdoc_macronames
[nc
->tok
]);
1344 mdoc_node_delete(mdoc
, nc
);
1347 nc
= ni
->body
->last
;
1354 post_bl_block_width(POST_ARGS
)
1359 struct mdoc_node
*n
;
1365 * Calculate the real width of a list from the -width string,
1366 * which may contain a macro (with a known default width), a
1367 * literal string, or a scaling width.
1369 * If the value to -width is a macro, then we re-write it to be
1370 * the macro's width as set in share/tmac/mdoc/doc-common.
1373 if (0 == strcmp(n
->norm
->Bl
.width
, "Ds"))
1375 else if (MDOC_MAX
== (tok
= mdoc_hash_find(n
->norm
->Bl
.width
)))
1378 width
= macro2len(tok
);
1380 /* The value already exists: free and reallocate it. */
1384 for (i
= 0; i
< (int)n
->args
->argc
; i
++)
1385 if (MDOC_Width
== n
->args
->argv
[i
].arg
)
1388 assert(i
< (int)n
->args
->argc
);
1390 (void)snprintf(buf
, sizeof(buf
), "%un", (unsigned int)width
);
1391 free(n
->args
->argv
[i
].value
[0]);
1392 n
->args
->argv
[i
].value
[0] = mandoc_strdup(buf
);
1394 /* Set our width! */
1395 n
->norm
->Bl
.width
= n
->args
->argv
[i
].value
[0];
1400 post_bl_block_tag(POST_ARGS
)
1402 struct mdoc_node
*n
, *nn
;
1408 * Calculate the -width for a `Bl -tag' list if it hasn't been
1409 * provided. Uses the first head macro. NOTE AGAIN: this is
1410 * ONLY if the -width argument has NOT been provided. See
1411 * post_bl_block_width() for converting the -width string.
1417 for (nn
= n
->body
->child
; nn
; nn
= nn
->next
) {
1418 if (MDOC_It
!= nn
->tok
)
1421 assert(MDOC_BLOCK
== nn
->type
);
1422 nn
= nn
->head
->child
;
1427 if (MDOC_TEXT
== nn
->type
) {
1428 sz
= strlen(nn
->string
) + 1;
1432 if (0 != (ssz
= macro2len(nn
->tok
)))
1438 /* Defaults to ten ens. */
1440 (void)snprintf(buf
, sizeof(buf
), "%un", (unsigned int)sz
);
1443 * We have to dynamically add this to the macro's argument list.
1444 * We're guaranteed that a MDOC_Width doesn't already exist.
1448 i
= (int)(n
->args
->argc
)++;
1450 n
->args
->argv
= mandoc_reallocarray(n
->args
->argv
,
1451 n
->args
->argc
, sizeof(struct mdoc_argv
));
1453 n
->args
->argv
[i
].arg
= MDOC_Width
;
1454 n
->args
->argv
[i
].line
= n
->line
;
1455 n
->args
->argv
[i
].pos
= n
->pos
;
1456 n
->args
->argv
[i
].sz
= 1;
1457 n
->args
->argv
[i
].value
= mandoc_malloc(sizeof(char *));
1458 n
->args
->argv
[i
].value
[0] = mandoc_strdup(buf
);
1460 /* Set our width! */
1461 n
->norm
->Bl
.width
= n
->args
->argv
[i
].value
[0];
1466 post_bl_head(POST_ARGS
)
1468 struct mdoc_node
*np
, *nn
, *nnp
;
1469 struct mdoc_argv
*argv
;
1472 if (LIST_column
!= mdoc
->last
->norm
->Bl
.type
)
1473 /* FIXME: this should be ERROR class... */
1474 return(hwarn_eq0(mdoc
));
1477 * Append old-style lists, where the column width specifiers
1478 * trail as macro parameters, to the new-style ("normal-form")
1479 * lists where they're argument values following -column.
1482 if (mdoc
->last
->child
== NULL
)
1485 np
= mdoc
->last
->parent
;
1488 for (j
= 0; j
< (int)np
->args
->argc
; j
++)
1489 if (MDOC_Column
== np
->args
->argv
[j
].arg
)
1492 assert(j
< (int)np
->args
->argc
);
1495 * Accommodate for new-style groff column syntax. Shuffle the
1496 * child nodes, all of which must be TEXT, as arguments for the
1497 * column field. Then, delete the head children.
1500 argv
= np
->args
->argv
+ j
;
1502 argv
->sz
+= mdoc
->last
->nchild
;
1503 argv
->value
= mandoc_reallocarray(argv
->value
,
1504 argv
->sz
, sizeof(char *));
1506 mdoc
->last
->norm
->Bl
.ncols
= argv
->sz
;
1507 mdoc
->last
->norm
->Bl
.cols
= (void *)argv
->value
;
1509 for (nn
= mdoc
->last
->child
; nn
; i
++) {
1510 argv
->value
[i
] = nn
->string
;
1514 mdoc_node_delete(NULL
, nnp
);
1517 mdoc
->last
->nchild
= 0;
1518 mdoc
->last
->child
= NULL
;
1526 struct mdoc_node
*nparent
, *nprev
; /* of the Bl block */
1527 struct mdoc_node
*nblock
, *nbody
; /* of the Bl */
1528 struct mdoc_node
*nchild
, *nnext
; /* of the Bl body */
1531 switch (nbody
->type
) {
1533 return(post_bl_block(mdoc
));
1535 return(post_bl_head(mdoc
));
1544 nchild
= nbody
->child
;
1545 while (NULL
!= nchild
) {
1546 if (MDOC_It
== nchild
->tok
|| MDOC_Sm
== nchild
->tok
) {
1547 nchild
= nchild
->next
;
1551 mandoc_msg(MANDOCERR_BL_MOVE
, mdoc
->parse
,
1552 nchild
->line
, nchild
->pos
,
1553 mdoc_macronames
[nchild
->tok
]);
1556 * Move the node out of the Bl block.
1557 * First, collect all required node pointers.
1560 nblock
= nbody
->parent
;
1561 nprev
= nblock
->prev
;
1562 nparent
= nblock
->parent
;
1563 nnext
= nchild
->next
;
1566 * Unlink this child.
1569 assert(NULL
== nchild
->prev
);
1570 if (0 == --nbody
->nchild
) {
1571 nbody
->child
= NULL
;
1573 assert(NULL
== nnext
);
1575 nbody
->child
= nnext
;
1580 * Relink this child.
1583 nchild
->parent
= nparent
;
1584 nchild
->prev
= nprev
;
1585 nchild
->next
= nblock
;
1587 nblock
->prev
= nchild
;
1590 nparent
->child
= nchild
;
1592 nprev
->next
= nchild
;
1610 ebool(struct mdoc
*mdoc
)
1612 struct mdoc_node
*nch
;
1615 tok
= mdoc
->last
->tok
;
1616 nch
= mdoc
->last
->child
;
1620 mdoc
->flags
^= MDOC_SMOFF
;
1624 check_count(mdoc
, MDOC_ELEM
, CHECK_WARN
, CHECK_LT
, 2);
1626 assert(MDOC_TEXT
== nch
->type
);
1628 if (0 == strcmp(nch
->string
, "on")) {
1630 mdoc
->flags
&= ~MDOC_SMOFF
;
1633 if (0 == strcmp(nch
->string
, "off")) {
1635 mdoc
->flags
|= MDOC_SMOFF
;
1639 mandoc_vmsg(MANDOCERR_SM_BAD
,
1640 mdoc
->parse
, nch
->line
, nch
->pos
,
1641 "%s %s", mdoc_macronames
[tok
], nch
->string
);
1642 return(mdoc_node_relink(mdoc
, nch
));
1646 post_root(POST_ARGS
)
1648 struct mdoc_node
*n
;
1650 /* Add missing prologue data. */
1652 if (mdoc
->meta
.date
== NULL
)
1653 mdoc
->meta
.date
= mdoc
->quick
?
1655 mandoc_normdate(mdoc
->parse
, NULL
, 0, 0);
1657 if (mdoc
->meta
.title
== NULL
) {
1658 mandoc_msg(MANDOCERR_DT_NOTITLE
,
1659 mdoc
->parse
, 0, 0, "EOF");
1660 mdoc
->meta
.title
= mandoc_strdup("UNTITLED");
1663 if (mdoc
->meta
.vol
== NULL
)
1664 mdoc
->meta
.vol
= mandoc_strdup("LOCAL");
1666 if (mdoc
->meta
.os
== NULL
) {
1667 mandoc_msg(MANDOCERR_OS_MISSING
,
1668 mdoc
->parse
, 0, 0, NULL
);
1669 mdoc
->meta
.os
= mandoc_strdup("");
1672 /* Check that we begin with a proper `Sh'. */
1674 n
= mdoc
->first
->child
;
1675 while (n
!= NULL
&& mdoc_macros
[n
->tok
].flags
& MDOC_PROLOGUE
)
1679 mandoc_msg(MANDOCERR_DOC_EMPTY
, mdoc
->parse
, 0, 0, NULL
);
1680 else if (n
->tok
!= MDOC_Sh
)
1681 mandoc_msg(MANDOCERR_SEC_BEFORE
, mdoc
->parse
,
1682 n
->line
, n
->pos
, mdoc_macronames
[n
->tok
]);
1690 struct mdoc_node
*n
, *nch
;
1697 mandoc_msg(MANDOCERR_MACRO_EMPTY
, mdoc
->parse
,
1698 n
->line
, n
->pos
, mdoc_macronames
[n
->tok
]);
1699 mdoc_node_delete(mdoc
, n
);
1703 assert(MDOC_TEXT
== nch
->type
);
1705 if (NULL
== (p
= mdoc_a2st(nch
->string
))) {
1706 mandoc_vmsg(MANDOCERR_ST_BAD
, mdoc
->parse
,
1707 nch
->line
, nch
->pos
, "St %s", nch
->string
);
1708 mdoc_node_delete(mdoc
, n
);
1711 nch
->string
= mandoc_strdup(p
);
1720 struct mdoc_node
*nn
, *next
, *prev
;
1723 switch (mdoc
->last
->type
) {
1725 check_count(mdoc
, MDOC_HEAD
, CHECK_WARN
, CHECK_EQ
, 0);
1728 if (mdoc
->last
->child
)
1730 check_count(mdoc
, MDOC_BODY
, CHECK_WARN
, CHECK_GT
, 0);
1737 * The full `Rs' block needs special handling to order the
1738 * sub-elements according to `rsord'. Pick through each element
1739 * and correctly order it. This is an insertion sort.
1743 for (nn
= mdoc
->last
->child
->next
; nn
; nn
= next
) {
1744 /* Determine order of `nn'. */
1745 for (i
= 0; i
< RSORD_MAX
; i
++)
1746 if (rsord
[i
] == nn
->tok
)
1749 if (i
== RSORD_MAX
) {
1750 mandoc_msg(MANDOCERR_RS_BAD
,
1751 mdoc
->parse
, nn
->line
, nn
->pos
,
1752 mdoc_macronames
[nn
->tok
]);
1754 } else if (MDOC__J
== nn
->tok
|| MDOC__B
== nn
->tok
)
1755 mdoc
->last
->norm
->Rs
.quote_T
++;
1758 * Remove `nn' from the chain. This somewhat
1759 * repeats mdoc_node_unlink(), but since we're
1760 * just re-ordering, there's no need for the
1761 * full unlink process.
1764 if (NULL
!= (next
= nn
->next
))
1765 next
->prev
= nn
->prev
;
1767 if (NULL
!= (prev
= nn
->prev
))
1768 prev
->next
= nn
->next
;
1770 nn
->prev
= nn
->next
= NULL
;
1773 * Scan back until we reach a node that's
1774 * ordered before `nn'.
1777 for ( ; prev
; prev
= prev
->prev
) {
1778 /* Determine order of `prev'. */
1779 for (j
= 0; j
< RSORD_MAX
; j
++)
1780 if (rsord
[j
] == prev
->tok
)
1790 * Set `nn' back into its correct place in front
1791 * of the `prev' node.
1798 prev
->next
->prev
= nn
;
1799 nn
->next
= prev
->next
;
1802 mdoc
->last
->child
->prev
= nn
;
1803 nn
->next
= mdoc
->last
->child
;
1804 mdoc
->last
->child
= nn
;
1812 * For some arguments of some macros,
1813 * convert all breakable hyphens into ASCII_HYPH.
1816 post_hyph(POST_ARGS
)
1818 struct mdoc_node
*n
, *nch
;
1824 if (MDOC_Sh
== n
->tok
|| MDOC_Ss
== n
->tok
)
1828 if (MDOC_D1
== n
->tok
|| MDOC_Nd
== n
->tok
)
1837 for (nch
= n
->child
; nch
; nch
= nch
->next
) {
1838 if (MDOC_TEXT
!= nch
->type
)
1843 while ('\0' != *(++cp
))
1845 isalpha((unsigned char)cp
[-1]) &&
1846 isalpha((unsigned char)cp
[1]))
1853 post_hyphtext(POST_ARGS
)
1857 return(post_hyph(mdoc
));
1864 if (MDOC_LINE
& mdoc
->last
->flags
)
1865 mandoc_msg(MANDOCERR_NS_SKIP
, mdoc
->parse
,
1866 mdoc
->last
->line
, mdoc
->last
->pos
, NULL
);
1876 switch (mdoc
->last
->type
) {
1878 return(post_sh_head(mdoc
));
1880 switch (mdoc
->lastsec
) {
1882 return(post_sh_name(mdoc
));
1884 return(post_sh_see_also(mdoc
));
1886 return(post_sh_authors(mdoc
));
1899 post_sh_name(POST_ARGS
)
1901 struct mdoc_node
*n
;
1904 * Warn if the NAME section doesn't contain the `Nm' and `Nd'
1905 * macros (can have multiple `Nm' and one `Nd'). Note that the
1906 * children of the BODY declaration can also be "text".
1909 if (NULL
== (n
= mdoc
->last
->child
)) {
1910 mandoc_msg(MANDOCERR_NAMESEC_BAD
, mdoc
->parse
,
1911 mdoc
->last
->line
, mdoc
->last
->pos
, "empty");
1915 for ( ; n
&& n
->next
; n
= n
->next
) {
1916 if (MDOC_ELEM
== n
->type
&& MDOC_Nm
== n
->tok
)
1918 if (MDOC_TEXT
== n
->type
)
1920 mandoc_msg(MANDOCERR_NAMESEC_BAD
, mdoc
->parse
,
1921 n
->line
, n
->pos
, mdoc_macronames
[n
->tok
]);
1925 if (MDOC_BLOCK
== n
->type
&& MDOC_Nd
== n
->tok
)
1928 mandoc_msg(MANDOCERR_NAMESEC_BAD
, mdoc
->parse
,
1929 n
->line
, n
->pos
, mdoc_macronames
[n
->tok
]);
1934 post_sh_see_also(POST_ARGS
)
1936 const struct mdoc_node
*n
;
1937 const char *name
, *sec
;
1938 const char *lastname
, *lastsec
, *lastpunct
;
1941 n
= mdoc
->last
->child
;
1942 lastname
= lastsec
= lastpunct
= NULL
;
1944 if (n
->tok
!= MDOC_Xr
|| n
->nchild
< 2)
1947 /* Process one .Xr node. */
1949 name
= n
->child
->string
;
1950 sec
= n
->child
->next
->string
;
1951 if (lastsec
!= NULL
) {
1952 if (lastpunct
[0] != ',' || lastpunct
[1] != '\0')
1953 mandoc_vmsg(MANDOCERR_XR_PUNCT
,
1954 mdoc
->parse
, n
->line
, n
->pos
,
1955 "%s before %s(%s)", lastpunct
,
1957 cmp
= strcmp(lastsec
, sec
);
1959 mandoc_vmsg(MANDOCERR_XR_ORDER
,
1960 mdoc
->parse
, n
->line
, n
->pos
,
1961 "%s(%s) after %s(%s)", name
,
1962 sec
, lastname
, lastsec
);
1963 else if (cmp
== 0 && strcmp(lastname
, name
) > 0)
1964 mandoc_vmsg(MANDOCERR_XR_ORDER
,
1965 mdoc
->parse
, n
->line
, n
->pos
,
1966 "%s after %s", name
, lastname
);
1971 /* Process the following node. */
1976 if (n
->tok
== MDOC_Xr
) {
1980 if (n
->type
!= MDOC_TEXT
)
1982 for (name
= n
->string
; *name
!= '\0'; name
++)
1983 if (isalpha((const unsigned char)*name
))
1985 lastpunct
= n
->string
;
1986 if (n
->next
== NULL
)
1987 mandoc_vmsg(MANDOCERR_XR_PUNCT
, mdoc
->parse
,
1988 n
->line
, n
->pos
, "%s after %s(%s)",
1989 lastpunct
, lastname
, lastsec
);
1996 child_an(const struct mdoc_node
*n
)
1999 for (n
= n
->child
; n
!= NULL
; n
= n
->next
)
2000 if ((n
->tok
== MDOC_An
&& n
->nchild
) || child_an(n
))
2006 post_sh_authors(POST_ARGS
)
2009 if ( ! child_an(mdoc
->last
))
2010 mandoc_msg(MANDOCERR_AN_MISSING
, mdoc
->parse
,
2011 mdoc
->last
->line
, mdoc
->last
->pos
, NULL
);
2016 post_sh_head(POST_ARGS
)
2018 struct mdoc_node
*n
;
2019 const char *goodsec
;
2024 * Process a new section. Sections are either "named" or
2025 * "custom". Custom sections are user-defined, while named ones
2026 * follow a conventional order and may only appear in certain
2032 mdoc_deroff(&secname
, mdoc
->last
);
2033 sec
= NULL
== secname
? SEC_CUSTOM
: a2sec(secname
);
2035 /* The NAME should be first. */
2037 if (SEC_NAME
!= sec
&& SEC_NONE
== mdoc
->lastnamed
)
2038 mandoc_vmsg(MANDOCERR_NAMESEC_FIRST
, mdoc
->parse
,
2039 mdoc
->last
->line
, mdoc
->last
->pos
,
2042 /* The SYNOPSIS gets special attention in other areas. */
2044 if (SEC_SYNOPSIS
== sec
) {
2045 roff_setreg(mdoc
->roff
, "nS", 1, '=');
2046 mdoc
->flags
|= MDOC_SYNOPSIS
;
2048 roff_setreg(mdoc
->roff
, "nS", 0, '=');
2049 mdoc
->flags
&= ~MDOC_SYNOPSIS
;
2052 /* Mark our last section. */
2054 mdoc
->lastsec
= sec
;
2057 * Set the section attribute for the current HEAD, for its
2058 * parent BLOCK, and for the HEAD children; the latter can
2059 * only be TEXT nodes, so no recursion is needed.
2060 * For other blocks and elements, including .Sh BODY, this is
2061 * done when allocating the node data structures, but for .Sh
2062 * BLOCK and HEAD, the section is still unknown at that time.
2065 mdoc
->last
->parent
->sec
= sec
;
2066 mdoc
->last
->sec
= sec
;
2067 for (n
= mdoc
->last
->child
; n
; n
= n
->next
)
2070 /* We don't care about custom sections after this. */
2072 if (SEC_CUSTOM
== sec
) {
2078 * Check whether our non-custom section is being repeated or is
2082 if (sec
== mdoc
->lastnamed
)
2083 mandoc_vmsg(MANDOCERR_SEC_REP
, mdoc
->parse
,
2084 mdoc
->last
->line
, mdoc
->last
->pos
,
2087 if (sec
< mdoc
->lastnamed
)
2088 mandoc_vmsg(MANDOCERR_SEC_ORDER
, mdoc
->parse
,
2089 mdoc
->last
->line
, mdoc
->last
->pos
,
2092 /* Mark the last named section. */
2094 mdoc
->lastnamed
= sec
;
2096 /* Check particular section/manual conventions. */
2098 if (mdoc
->meta
.msec
== NULL
) {
2106 if (*mdoc
->meta
.msec
== '4')
2108 goodsec
= "2, 3, 4, 9";
2110 case SEC_RETURN_VALUES
:
2113 if (*mdoc
->meta
.msec
== '2')
2115 if (*mdoc
->meta
.msec
== '3')
2117 if (NULL
== goodsec
)
2118 goodsec
= "2, 3, 9";
2121 if (*mdoc
->meta
.msec
== '9')
2123 if (NULL
== goodsec
)
2125 mandoc_vmsg(MANDOCERR_SEC_MSEC
, mdoc
->parse
,
2126 mdoc
->last
->line
, mdoc
->last
->pos
,
2127 "Sh %s for %s only", secname
, goodsec
);
2138 post_ignpar(POST_ARGS
)
2140 struct mdoc_node
*np
;
2145 if (MDOC_BODY
!= mdoc
->last
->type
)
2148 if (NULL
!= (np
= mdoc
->last
->child
))
2149 if (MDOC_Pp
== np
->tok
|| MDOC_Lp
== np
->tok
) {
2150 mandoc_vmsg(MANDOCERR_PAR_SKIP
,
2151 mdoc
->parse
, np
->line
, np
->pos
,
2152 "%s after %s", mdoc_macronames
[np
->tok
],
2153 mdoc_macronames
[mdoc
->last
->tok
]);
2154 mdoc_node_delete(mdoc
, np
);
2157 if (NULL
!= (np
= mdoc
->last
->last
))
2158 if (MDOC_Pp
== np
->tok
|| MDOC_Lp
== np
->tok
) {
2159 mandoc_vmsg(MANDOCERR_PAR_SKIP
, mdoc
->parse
,
2160 np
->line
, np
->pos
, "%s at the end of %s",
2161 mdoc_macronames
[np
->tok
],
2162 mdoc_macronames
[mdoc
->last
->tok
]);
2163 mdoc_node_delete(mdoc
, np
);
2173 if (NULL
== mdoc
->last
)
2175 if (MDOC_ELEM
!= n
->type
&& MDOC_BLOCK
!= n
->type
)
2179 * Don't allow prior `Lp' or `Pp' prior to a paragraph-type
2180 * block: `Lp', `Pp', or non-compact `Bd' or `Bl'.
2183 if (MDOC_Pp
!= mdoc
->last
->tok
&&
2184 MDOC_Lp
!= mdoc
->last
->tok
&&
2185 MDOC_br
!= mdoc
->last
->tok
)
2187 if (MDOC_Bl
== n
->tok
&& n
->norm
->Bl
.comp
)
2189 if (MDOC_Bd
== n
->tok
&& n
->norm
->Bd
.comp
)
2191 if (MDOC_It
== n
->tok
&& n
->parent
->norm
->Bl
.comp
)
2194 mandoc_vmsg(MANDOCERR_PAR_SKIP
, mdoc
->parse
,
2195 mdoc
->last
->line
, mdoc
->last
->pos
,
2196 "%s before %s", mdoc_macronames
[mdoc
->last
->tok
],
2197 mdoc_macronames
[n
->tok
]);
2198 mdoc_node_delete(mdoc
, mdoc
->last
);
2205 struct mdoc_node
*np
;
2207 if (mdoc
->last
->tok
== MDOC_sp
)
2212 if (MDOC_ELEM
!= mdoc
->last
->type
&&
2213 MDOC_BLOCK
!= mdoc
->last
->type
)
2216 if (NULL
== (np
= mdoc
->last
->prev
)) {
2217 np
= mdoc
->last
->parent
;
2218 if (MDOC_Sh
!= np
->tok
&& MDOC_Ss
!= np
->tok
)
2221 if (MDOC_Pp
!= np
->tok
&& MDOC_Lp
!= np
->tok
&&
2222 (MDOC_br
!= mdoc
->last
->tok
||
2223 (MDOC_sp
!= np
->tok
&& MDOC_br
!= np
->tok
)))
2227 mandoc_vmsg(MANDOCERR_PAR_SKIP
, mdoc
->parse
,
2228 mdoc
->last
->line
, mdoc
->last
->pos
,
2229 "%s after %s", mdoc_macronames
[mdoc
->last
->tok
],
2230 mdoc_macronames
[np
->tok
]);
2231 mdoc_node_delete(mdoc
, mdoc
->last
);
2236 pre_literal(PRE_ARGS
)
2239 pre_display(mdoc
, n
);
2241 if (MDOC_BODY
!= n
->type
)
2245 * The `Dl' (note "el" not "one") and `Bd -literal' and `Bd
2246 * -unfilled' macros set MDOC_LITERAL on entrance to the body.
2251 mdoc
->flags
|= MDOC_LITERAL
;
2254 if (DISP_literal
== n
->norm
->Bd
.type
)
2255 mdoc
->flags
|= MDOC_LITERAL
;
2256 if (DISP_unfilled
== n
->norm
->Bd
.type
)
2257 mdoc
->flags
|= MDOC_LITERAL
;
2270 struct mdoc_node
*n
;
2273 if (mdoc
->meta
.date
)
2274 free(mdoc
->meta
.date
);
2277 if (NULL
== n
->child
|| '\0' == n
->child
->string
[0]) {
2278 mdoc
->meta
.date
= mdoc
->quick
? mandoc_strdup("") :
2279 mandoc_normdate(mdoc
->parse
, NULL
, n
->line
, n
->pos
);
2284 mdoc_deroff(&datestr
, n
);
2286 mdoc
->meta
.date
= datestr
;
2288 mdoc
->meta
.date
= mandoc_normdate(mdoc
->parse
,
2289 datestr
, n
->line
, n
->pos
);
2293 mdoc_node_delete(mdoc
, n
);
2300 struct mdoc_node
*nn
, *n
;
2306 free(mdoc
->meta
.title
);
2307 free(mdoc
->meta
.msec
);
2308 free(mdoc
->meta
.vol
);
2309 free(mdoc
->meta
.arch
);
2311 mdoc
->meta
.title
= NULL
;
2312 mdoc
->meta
.msec
= NULL
;
2313 mdoc
->meta
.vol
= NULL
;
2314 mdoc
->meta
.arch
= NULL
;
2316 /* First check that all characters are uppercase. */
2318 if (NULL
!= (nn
= n
->child
))
2319 for (p
= nn
->string
; *p
; p
++) {
2320 if (toupper((unsigned char)*p
) == *p
)
2322 mandoc_vmsg(MANDOCERR_TITLE_CASE
,
2323 mdoc
->parse
, nn
->line
,
2324 nn
->pos
+ (p
- nn
->string
),
2325 "Dt %s", nn
->string
);
2329 /* No argument: msec and arch remain NULL. */
2331 if (NULL
== (nn
= n
->child
)) {
2332 mandoc_msg(MANDOCERR_DT_NOTITLE
,
2333 mdoc
->parse
, n
->line
, n
->pos
, "Dt");
2334 mdoc
->meta
.title
= mandoc_strdup("UNTITLED");
2335 mdoc
->meta
.vol
= mandoc_strdup("LOCAL");
2339 /* One argument: msec and arch remain NULL. */
2341 mdoc
->meta
.title
= mandoc_strdup(
2342 '\0' == nn
->string
[0] ? "UNTITLED" : nn
->string
);
2344 if (NULL
== (nn
= nn
->next
)) {
2345 mandoc_vmsg(MANDOCERR_MSEC_MISSING
,
2346 mdoc
->parse
, n
->line
, n
->pos
,
2347 "Dt %s", mdoc
->meta
.title
);
2348 mdoc
->meta
.vol
= mandoc_strdup("LOCAL");
2352 /* Handles: `.Dt TITLE SEC'
2354 * volume = SEC is msec ? format(msec) : SEC,
2355 * msec = SEC is msec ? atoi(msec) : 0,
2359 cp
= mandoc_a2msec(nn
->string
);
2361 mdoc
->meta
.vol
= mandoc_strdup(cp
);
2362 mdoc
->meta
.msec
= mandoc_strdup(nn
->string
);
2364 mandoc_vmsg(MANDOCERR_MSEC_BAD
, mdoc
->parse
,
2365 nn
->line
, nn
->pos
, "Dt ... %s", nn
->string
);
2366 mdoc
->meta
.vol
= mandoc_strdup(nn
->string
);
2367 mdoc
->meta
.msec
= mandoc_strdup(nn
->string
);
2370 if (NULL
== (nn
= nn
->next
))
2373 /* Handles: `.Dt TITLE SEC VOL'
2375 * volume = VOL is vol ? format(VOL) :
2376 * VOL is arch ? format(arch) :
2380 cp
= mdoc_a2vol(nn
->string
);
2382 free(mdoc
->meta
.vol
);
2383 mdoc
->meta
.vol
= mandoc_strdup(cp
);
2385 cp
= mdoc_a2arch(nn
->string
);
2387 mandoc_vmsg(MANDOCERR_ARCH_BAD
, mdoc
->parse
,
2388 nn
->line
, nn
->pos
, "Dt ... %s", nn
->string
);
2389 free(mdoc
->meta
.vol
);
2390 mdoc
->meta
.vol
= mandoc_strdup(nn
->string
);
2392 mdoc
->meta
.arch
= mandoc_strdup(cp
);
2395 /* Ignore any subsequent parameters... */
2396 /* FIXME: warn about subsequent parameters. */
2398 mdoc_node_delete(mdoc
, n
);
2405 struct mdoc_node
*n
;
2408 * Make `Bx's second argument always start with an uppercase
2409 * letter. Groff checks if it's an "accepted" term, but we just
2410 * uppercase blindly.
2413 n
= mdoc
->last
->child
;
2414 if (n
&& NULL
!= (n
= n
->next
))
2415 *n
->string
= (char)toupper((unsigned char)*n
->string
);
2424 struct utsname utsname
;
2425 static char *defbuf
;
2427 struct mdoc_node
*n
;
2432 * Set the operating system by way of the `Os' macro.
2433 * The order of precedence is:
2434 * 1. the argument of the `Os' macro, unless empty
2435 * 2. the -Ios=foo command line argument, if provided
2436 * 3. -DOSNAME="\"foo\"", if provided during compilation
2437 * 4. "sysname release" from uname(3)
2440 free(mdoc
->meta
.os
);
2441 mdoc
->meta
.os
= NULL
;
2442 mdoc_deroff(&mdoc
->meta
.os
, n
);
2447 mdoc
->meta
.os
= mandoc_strdup(mdoc
->defos
);
2452 mdoc
->meta
.os
= mandoc_strdup(OSNAME
);
2454 if (NULL
== defbuf
) {
2455 if (-1 == uname(&utsname
)) {
2456 mandoc_msg(MANDOCERR_OS_UNAME
, mdoc
->parse
,
2457 n
->line
, n
->pos
, "Os");
2458 defbuf
= mandoc_strdup("UNKNOWN");
2460 mandoc_asprintf(&defbuf
, "%s %s",
2461 utsname
.sysname
, utsname
.release
);
2463 mdoc
->meta
.os
= mandoc_strdup(defbuf
);
2467 mdoc_node_delete(mdoc
, n
);
2472 * If no argument is provided,
2473 * fill in the name of the current manual page.
2478 struct mdoc_node
*n
;
2485 if (mdoc
->meta
.name
== NULL
) {
2486 mandoc_msg(MANDOCERR_EX_NONAME
, mdoc
->parse
,
2487 n
->line
, n
->pos
, "Ex");
2491 mdoc
->next
= MDOC_NEXT_CHILD
;
2493 if ( ! mdoc_word_alloc(mdoc
, n
->line
, n
->pos
, mdoc
->meta
.name
))
2500 static enum mdoc_sec
2501 a2sec(const char *p
)
2505 for (i
= 0; i
< (int)SEC__MAX
; i
++)
2506 if (secnames
[i
] && 0 == strcmp(p
, secnames
[i
]))
2507 return((enum mdoc_sec
)i
);
2513 macro2len(enum mdoct macro
)