]>
git.cameronkatri.com Git - mandoc.git/blob - mdoc_validate.c
1 /* $Id: mdoc_validate.c,v 1.271 2015/02/05 01:46:56 schwarze Exp $ */
3 * Copyright (c) 2008-2012 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2010-2015 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
51 typedef void (*v_pre
)(PRE_ARGS
);
52 typedef void (*v_post
)(POST_ARGS
);
59 static void check_count(struct mdoc
*, enum mdoc_type
,
60 enum check_ineq
, int);
61 static void check_text(struct mdoc
*, int, int, char *);
62 static void check_argv(struct mdoc
*,
63 struct mdoc_node
*, struct mdoc_argv
*);
64 static void check_args(struct mdoc
*, struct mdoc_node
*);
65 static int child_an(const struct mdoc_node
*);
66 static enum mdoc_sec
a2sec(const char *);
67 static size_t macro2len(enum mdoct
);
68 static void rewrite_macro2len(char **);
70 static void bwarn_ge1(POST_ARGS
);
71 static void ewarn_eq1(POST_ARGS
);
72 static void ewarn_ge1(POST_ARGS
);
74 static void post_an(POST_ARGS
);
75 static void post_at(POST_ARGS
);
76 static void post_bf(POST_ARGS
);
77 static void post_bk(POST_ARGS
);
78 static void post_bl(POST_ARGS
);
79 static void post_bl_block(POST_ARGS
);
80 static void post_bl_block_tag(POST_ARGS
);
81 static void post_bl_head(POST_ARGS
);
82 static void post_bx(POST_ARGS
);
83 static void post_d1(POST_ARGS
);
84 static void post_defaults(POST_ARGS
);
85 static void post_dd(POST_ARGS
);
86 static void post_dt(POST_ARGS
);
87 static void post_en(POST_ARGS
);
88 static void post_es(POST_ARGS
);
89 static void post_eoln(POST_ARGS
);
90 static void post_ex(POST_ARGS
);
91 static void post_fa(POST_ARGS
);
92 static void post_fn(POST_ARGS
);
93 static void post_fname(POST_ARGS
);
94 static void post_fo(POST_ARGS
);
95 static void post_hyph(POST_ARGS
);
96 static void post_hyphtext(POST_ARGS
);
97 static void post_ignpar(POST_ARGS
);
98 static void post_it(POST_ARGS
);
99 static void post_lb(POST_ARGS
);
100 static void post_literal(POST_ARGS
);
101 static void post_nd(POST_ARGS
);
102 static void post_nm(POST_ARGS
);
103 static void post_ns(POST_ARGS
);
104 static void post_os(POST_ARGS
);
105 static void post_par(POST_ARGS
);
106 static void post_root(POST_ARGS
);
107 static void post_rs(POST_ARGS
);
108 static void post_sh(POST_ARGS
);
109 static void post_sh_head(POST_ARGS
);
110 static void post_sh_name(POST_ARGS
);
111 static void post_sh_see_also(POST_ARGS
);
112 static void post_sh_authors(POST_ARGS
);
113 static void post_sm(POST_ARGS
);
114 static void post_st(POST_ARGS
);
115 static void post_vt(POST_ARGS
);
117 static void pre_an(PRE_ARGS
);
118 static void pre_bd(PRE_ARGS
);
119 static void pre_bl(PRE_ARGS
);
120 static void pre_dd(PRE_ARGS
);
121 static void pre_display(PRE_ARGS
);
122 static void pre_dt(PRE_ARGS
);
123 static void pre_literal(PRE_ARGS
);
124 static void pre_obsolete(PRE_ARGS
);
125 static void pre_os(PRE_ARGS
);
126 static void pre_par(PRE_ARGS
);
127 static void pre_std(PRE_ARGS
);
129 static const struct valids mdoc_valids
[MDOC_MAX
] = {
130 { NULL
, NULL
}, /* Ap */
131 { pre_dd
, post_dd
}, /* Dd */
132 { pre_dt
, post_dt
}, /* Dt */
133 { pre_os
, post_os
}, /* Os */
134 { NULL
, post_sh
}, /* Sh */
135 { NULL
, post_ignpar
}, /* Ss */
136 { pre_par
, post_par
}, /* Pp */
137 { pre_display
, post_d1
}, /* D1 */
138 { pre_literal
, post_literal
}, /* Dl */
139 { pre_bd
, post_literal
}, /* Bd */
140 { NULL
, NULL
}, /* Ed */
141 { pre_bl
, post_bl
}, /* Bl */
142 { NULL
, NULL
}, /* El */
143 { pre_par
, post_it
}, /* It */
144 { NULL
, NULL
}, /* Ad */
145 { pre_an
, post_an
}, /* An */
146 { NULL
, post_defaults
}, /* Ar */
147 { NULL
, NULL
}, /* Cd */
148 { NULL
, NULL
}, /* Cm */
149 { NULL
, NULL
}, /* Dv */
150 { NULL
, NULL
}, /* Er */
151 { NULL
, NULL
}, /* Ev */
152 { pre_std
, post_ex
}, /* Ex */
153 { NULL
, post_fa
}, /* Fa */
154 { NULL
, ewarn_ge1
}, /* Fd */
155 { NULL
, NULL
}, /* Fl */
156 { NULL
, post_fn
}, /* Fn */
157 { NULL
, NULL
}, /* Ft */
158 { NULL
, NULL
}, /* Ic */
159 { NULL
, ewarn_eq1
}, /* In */
160 { NULL
, post_defaults
}, /* Li */
161 { NULL
, post_nd
}, /* Nd */
162 { NULL
, post_nm
}, /* Nm */
163 { NULL
, NULL
}, /* Op */
164 { pre_obsolete
, NULL
}, /* Ot */
165 { NULL
, post_defaults
}, /* Pa */
166 { pre_std
, NULL
}, /* Rv */
167 { NULL
, post_st
}, /* St */
168 { NULL
, NULL
}, /* Va */
169 { NULL
, post_vt
}, /* Vt */
170 { NULL
, ewarn_ge1
}, /* Xr */
171 { NULL
, ewarn_ge1
}, /* %A */
172 { NULL
, post_hyphtext
}, /* %B */ /* FIXME: can be used outside Rs/Re. */
173 { NULL
, ewarn_ge1
}, /* %D */
174 { NULL
, ewarn_ge1
}, /* %I */
175 { NULL
, ewarn_ge1
}, /* %J */
176 { NULL
, post_hyphtext
}, /* %N */
177 { NULL
, post_hyphtext
}, /* %O */
178 { NULL
, ewarn_ge1
}, /* %P */
179 { NULL
, post_hyphtext
}, /* %R */
180 { NULL
, post_hyphtext
}, /* %T */ /* FIXME: can be used outside Rs/Re. */
181 { NULL
, ewarn_ge1
}, /* %V */
182 { NULL
, NULL
}, /* Ac */
183 { NULL
, NULL
}, /* Ao */
184 { NULL
, NULL
}, /* Aq */
185 { NULL
, post_at
}, /* At */
186 { NULL
, NULL
}, /* Bc */
187 { NULL
, post_bf
}, /* Bf */
188 { NULL
, NULL
}, /* Bo */
189 { NULL
, NULL
}, /* Bq */
190 { NULL
, NULL
}, /* Bsx */
191 { NULL
, post_bx
}, /* Bx */
192 { pre_obsolete
, NULL
}, /* Db */
193 { NULL
, NULL
}, /* Dc */
194 { NULL
, NULL
}, /* Do */
195 { NULL
, NULL
}, /* Dq */
196 { NULL
, NULL
}, /* Ec */
197 { NULL
, NULL
}, /* Ef */
198 { NULL
, NULL
}, /* Em */
199 { NULL
, NULL
}, /* Eo */
200 { NULL
, NULL
}, /* Fx */
201 { NULL
, NULL
}, /* Ms */
202 { NULL
, NULL
}, /* No */
203 { NULL
, post_ns
}, /* Ns */
204 { NULL
, NULL
}, /* Nx */
205 { NULL
, NULL
}, /* Ox */
206 { NULL
, NULL
}, /* Pc */
207 { NULL
, NULL
}, /* Pf */
208 { NULL
, NULL
}, /* Po */
209 { NULL
, NULL
}, /* Pq */
210 { NULL
, NULL
}, /* Qc */
211 { NULL
, NULL
}, /* Ql */
212 { NULL
, NULL
}, /* Qo */
213 { NULL
, NULL
}, /* Qq */
214 { NULL
, NULL
}, /* Re */
215 { NULL
, post_rs
}, /* Rs */
216 { NULL
, NULL
}, /* Sc */
217 { NULL
, NULL
}, /* So */
218 { NULL
, NULL
}, /* Sq */
219 { NULL
, post_sm
}, /* Sm */
220 { NULL
, post_hyph
}, /* Sx */
221 { NULL
, NULL
}, /* Sy */
222 { NULL
, NULL
}, /* Tn */
223 { NULL
, NULL
}, /* Ux */
224 { NULL
, NULL
}, /* Xc */
225 { NULL
, NULL
}, /* Xo */
226 { NULL
, post_fo
}, /* Fo */
227 { NULL
, NULL
}, /* Fc */
228 { NULL
, NULL
}, /* Oo */
229 { NULL
, NULL
}, /* Oc */
230 { NULL
, post_bk
}, /* Bk */
231 { NULL
, NULL
}, /* Ek */
232 { NULL
, post_eoln
}, /* Bt */
233 { NULL
, NULL
}, /* Hf */
234 { pre_obsolete
, NULL
}, /* Fr */
235 { NULL
, post_eoln
}, /* Ud */
236 { NULL
, post_lb
}, /* Lb */
237 { pre_par
, post_par
}, /* Lp */
238 { NULL
, NULL
}, /* Lk */
239 { NULL
, post_defaults
}, /* Mt */
240 { NULL
, NULL
}, /* Brq */
241 { NULL
, NULL
}, /* Bro */
242 { NULL
, NULL
}, /* Brc */
243 { NULL
, ewarn_ge1
}, /* %C */
244 { pre_obsolete
, post_es
}, /* Es */
245 { pre_obsolete
, post_en
}, /* En */
246 { NULL
, NULL
}, /* Dx */
247 { NULL
, ewarn_ge1
}, /* %Q */
248 { NULL
, post_par
}, /* br */
249 { NULL
, post_par
}, /* sp */
250 { NULL
, ewarn_eq1
}, /* %U */
251 { NULL
, NULL
}, /* Ta */
252 { NULL
, NULL
}, /* ll */
255 #define RSORD_MAX 14 /* Number of `Rs' blocks. */
257 static const enum mdoct rsord
[RSORD_MAX
] = {
274 static const char * const secnames
[SEC__MAX
] = {
281 "IMPLEMENTATION NOTES",
296 "SECURITY CONSIDERATIONS",
302 mdoc_valid_pre(struct mdoc
*mdoc
, struct mdoc_node
*n
)
308 check_text(mdoc
, n
->line
, n
->pos
, n
->string
);
321 p
= mdoc_valids
[n
->tok
].pre
;
327 mdoc_valid_post(struct mdoc
*mdoc
)
333 if (n
->flags
& MDOC_VALID
)
335 n
->flags
|= MDOC_VALID
;
350 * Closing delimiters are not special at the
351 * beginning of a block, opening delimiters
352 * are not special at the end.
355 if (n
->child
!= NULL
)
356 n
->child
->flags
&= ~MDOC_DELIMC
;
358 n
->last
->flags
&= ~MDOC_DELIMO
;
360 /* Call the macro's postprocessor. */
362 p
= mdoc_valids
[n
->tok
].post
;
370 check_count(struct mdoc
*mdoc
, enum mdoc_type type
,
371 enum check_ineq ineq
, int val
)
375 if (mdoc
->last
->type
!= type
)
381 if (mdoc
->last
->nchild
< val
)
386 if (mdoc
->last
->nchild
> val
)
391 if (val
== mdoc
->last
->nchild
)
399 mandoc_vmsg(MANDOCERR_ARGCWARN
, mdoc
->parse
, mdoc
->last
->line
,
400 mdoc
->last
->pos
, "want %s%d children (have %d)",
401 p
, val
, mdoc
->last
->nchild
);
407 check_count(mdoc
, MDOC_BODY
, CHECK_GT
, 0);
413 check_count(mdoc
, MDOC_ELEM
, CHECK_EQ
, 1);
419 check_count(mdoc
, MDOC_ELEM
, CHECK_GT
, 0);
423 check_args(struct mdoc
*mdoc
, struct mdoc_node
*n
)
430 assert(n
->args
->argc
);
431 for (i
= 0; i
< (int)n
->args
->argc
; i
++)
432 check_argv(mdoc
, n
, &n
->args
->argv
[i
]);
436 check_argv(struct mdoc
*mdoc
, struct mdoc_node
*n
, struct mdoc_argv
*v
)
440 for (i
= 0; i
< (int)v
->sz
; i
++)
441 check_text(mdoc
, v
->line
, v
->pos
, v
->value
[i
]);
445 check_text(struct mdoc
*mdoc
, int ln
, int pos
, char *p
)
449 if (MDOC_LITERAL
& mdoc
->flags
)
452 for (cp
= p
; NULL
!= (p
= strchr(p
, '\t')); p
++)
453 mandoc_msg(MANDOCERR_FI_TAB
, mdoc
->parse
,
454 ln
, pos
+ (int)(p
- cp
), NULL
);
458 pre_display(PRE_ARGS
)
460 struct mdoc_node
*node
;
462 if (MDOC_BLOCK
!= n
->type
)
465 for (node
= mdoc
->last
->parent
; node
; node
= node
->parent
)
466 if (MDOC_BLOCK
== node
->type
)
467 if (MDOC_Bd
== node
->tok
)
471 mandoc_vmsg(MANDOCERR_BD_NEST
,
472 mdoc
->parse
, n
->line
, n
->pos
,
473 "%s in Bd", mdoc_macronames
[n
->tok
]);
479 struct mdoc_node
*np
;
480 struct mdoc_argv
*argv
, *wa
;
482 enum mdocargt mdoclt
;
485 if (MDOC_BLOCK
!= n
->type
) {
486 if (ENDBODY_NOT
!= n
->end
) {
488 np
= n
->pending
->parent
;
493 assert(MDOC_BLOCK
== np
->type
);
494 assert(MDOC_Bl
== np
->tok
);
499 * First figure out which kind of list to use: bind ourselves to
500 * the first mentioned list type and warn about any remaining
501 * ones. If we find no list type, we default to LIST_item.
504 wa
= (n
->args
== NULL
) ? NULL
: n
->args
->argv
;
505 mdoclt
= MDOC_ARG_MAX
;
506 for (i
= 0; n
->args
&& i
< (int)n
->args
->argc
; i
++) {
507 argv
= n
->args
->argv
+ i
;
510 /* Set list types. */
544 /* Set list arguments. */
546 if (n
->norm
->Bl
.comp
)
547 mandoc_msg(MANDOCERR_ARG_REP
,
548 mdoc
->parse
, argv
->line
,
549 argv
->pos
, "Bl -compact");
550 n
->norm
->Bl
.comp
= 1;
555 mandoc_msg(MANDOCERR_ARG_EMPTY
,
556 mdoc
->parse
, argv
->line
,
557 argv
->pos
, "Bl -width");
558 n
->norm
->Bl
.width
= "0n";
561 if (NULL
!= n
->norm
->Bl
.width
)
562 mandoc_vmsg(MANDOCERR_ARG_REP
,
563 mdoc
->parse
, argv
->line
,
564 argv
->pos
, "Bl -width %s",
566 rewrite_macro2len(argv
->value
);
567 n
->norm
->Bl
.width
= argv
->value
[0];
571 mandoc_msg(MANDOCERR_ARG_EMPTY
,
572 mdoc
->parse
, argv
->line
,
573 argv
->pos
, "Bl -offset");
576 if (NULL
!= n
->norm
->Bl
.offs
)
577 mandoc_vmsg(MANDOCERR_ARG_REP
,
578 mdoc
->parse
, argv
->line
,
579 argv
->pos
, "Bl -offset %s",
581 rewrite_macro2len(argv
->value
);
582 n
->norm
->Bl
.offs
= argv
->value
[0];
587 if (LIST__NONE
== lt
)
591 /* Check: multiple list types. */
593 if (LIST__NONE
!= n
->norm
->Bl
.type
) {
594 mandoc_vmsg(MANDOCERR_BL_REP
,
595 mdoc
->parse
, n
->line
, n
->pos
,
596 "Bl -%s", mdoc_argnames
[argv
->arg
]);
600 /* The list type should come first. */
602 if (n
->norm
->Bl
.width
||
605 mandoc_vmsg(MANDOCERR_BL_LATETYPE
,
606 mdoc
->parse
, n
->line
, n
->pos
, "Bl -%s",
607 mdoc_argnames
[n
->args
->argv
[0].arg
]);
609 n
->norm
->Bl
.type
= lt
;
610 if (LIST_column
== lt
) {
611 n
->norm
->Bl
.ncols
= argv
->sz
;
612 n
->norm
->Bl
.cols
= (void *)argv
->value
;
616 /* Allow lists to default to LIST_item. */
618 if (LIST__NONE
== n
->norm
->Bl
.type
) {
619 mandoc_msg(MANDOCERR_BL_NOTYPE
, mdoc
->parse
,
620 n
->line
, n
->pos
, "Bl");
621 n
->norm
->Bl
.type
= LIST_item
;
625 * Validate the width field. Some list types don't need width
626 * types and should be warned about them. Others should have it
627 * and must also be warned. Yet others have a default and need
631 switch (n
->norm
->Bl
.type
) {
633 if (NULL
== n
->norm
->Bl
.width
)
634 mandoc_msg(MANDOCERR_BL_NOWIDTH
, mdoc
->parse
,
635 n
->line
, n
->pos
, "Bl -tag");
646 if (n
->norm
->Bl
.width
)
647 mandoc_vmsg(MANDOCERR_BL_SKIPW
, mdoc
->parse
,
648 wa
->line
, wa
->pos
, "Bl -%s",
649 mdoc_argnames
[mdoclt
]);
656 if (NULL
== n
->norm
->Bl
.width
)
657 n
->norm
->Bl
.width
= "2n";
660 if (NULL
== n
->norm
->Bl
.width
)
661 n
->norm
->Bl
.width
= "3n";
672 struct mdoc_node
*np
;
673 struct mdoc_argv
*argv
;
677 pre_literal(mdoc
, n
);
679 if (MDOC_BLOCK
!= n
->type
) {
680 if (ENDBODY_NOT
!= n
->end
) {
682 np
= n
->pending
->parent
;
687 assert(MDOC_BLOCK
== np
->type
);
688 assert(MDOC_Bd
== np
->tok
);
692 for (i
= 0; n
->args
&& i
< (int)n
->args
->argc
; i
++) {
693 argv
= n
->args
->argv
+ i
;
713 mandoc_msg(MANDOCERR_BD_FILE
, mdoc
->parse
,
714 n
->line
, n
->pos
, NULL
);
718 mandoc_msg(MANDOCERR_ARG_EMPTY
,
719 mdoc
->parse
, argv
->line
,
720 argv
->pos
, "Bd -offset");
723 if (NULL
!= n
->norm
->Bd
.offs
)
724 mandoc_vmsg(MANDOCERR_ARG_REP
,
725 mdoc
->parse
, argv
->line
,
726 argv
->pos
, "Bd -offset %s",
728 rewrite_macro2len(argv
->value
);
729 n
->norm
->Bd
.offs
= argv
->value
[0];
732 if (n
->norm
->Bd
.comp
)
733 mandoc_msg(MANDOCERR_ARG_REP
,
734 mdoc
->parse
, argv
->line
,
735 argv
->pos
, "Bd -compact");
736 n
->norm
->Bd
.comp
= 1;
742 if (DISP__NONE
== dt
)
745 if (DISP__NONE
== n
->norm
->Bd
.type
)
746 n
->norm
->Bd
.type
= dt
;
748 mandoc_vmsg(MANDOCERR_BD_REP
,
749 mdoc
->parse
, n
->line
, n
->pos
,
750 "Bd -%s", mdoc_argnames
[argv
->arg
]);
753 if (DISP__NONE
== n
->norm
->Bd
.type
) {
754 mandoc_msg(MANDOCERR_BD_NOTYPE
, mdoc
->parse
,
755 n
->line
, n
->pos
, "Bd");
756 n
->norm
->Bd
.type
= DISP_ragged
;
764 struct mdoc_argv
*argv
;
770 for (i
= 1; i
< n
->args
->argc
; i
++) {
771 argv
= n
->args
->argv
+ i
;
772 mandoc_vmsg(MANDOCERR_AN_REP
,
773 mdoc
->parse
, argv
->line
, argv
->pos
,
774 "An -%s", mdoc_argnames
[argv
->arg
]);
777 argv
= n
->args
->argv
;
778 if (argv
->arg
== MDOC_Split
)
779 n
->norm
->An
.auth
= AUTH_split
;
780 else if (argv
->arg
== MDOC_Nosplit
)
781 n
->norm
->An
.auth
= AUTH_nosplit
;
790 if (n
->args
&& 1 == n
->args
->argc
)
791 if (MDOC_Std
== n
->args
->argv
[0].arg
)
794 mandoc_msg(MANDOCERR_ARG_STD
, mdoc
->parse
,
795 n
->line
, n
->pos
, mdoc_macronames
[n
->tok
]);
799 pre_obsolete(PRE_ARGS
)
802 if (MDOC_ELEM
== n
->type
|| MDOC_BLOCK
== n
->type
)
803 mandoc_msg(MANDOCERR_MACRO_OBS
, mdoc
->parse
,
804 n
->line
, n
->pos
, mdoc_macronames
[n
->tok
]);
811 if (mdoc
->meta
.title
!= NULL
)
812 mandoc_msg(MANDOCERR_PROLOG_REP
, mdoc
->parse
,
813 n
->line
, n
->pos
, "Dt");
814 else if (mdoc
->meta
.os
!= NULL
)
815 mandoc_msg(MANDOCERR_PROLOG_ORDER
, mdoc
->parse
,
816 n
->line
, n
->pos
, "Dt after Os");
823 if (mdoc
->meta
.os
!= NULL
)
824 mandoc_msg(MANDOCERR_PROLOG_REP
, mdoc
->parse
,
825 n
->line
, n
->pos
, "Os");
826 else if (mdoc
->flags
& MDOC_PBODY
)
827 mandoc_msg(MANDOCERR_PROLOG_LATE
, mdoc
->parse
,
828 n
->line
, n
->pos
, "Os");
835 if (mdoc
->meta
.date
!= NULL
)
836 mandoc_msg(MANDOCERR_PROLOG_REP
, mdoc
->parse
,
837 n
->line
, n
->pos
, "Dd");
838 else if (mdoc
->flags
& MDOC_PBODY
)
839 mandoc_msg(MANDOCERR_PROLOG_LATE
, mdoc
->parse
,
840 n
->line
, n
->pos
, "Dd");
841 else if (mdoc
->meta
.title
!= NULL
)
842 mandoc_msg(MANDOCERR_PROLOG_ORDER
, mdoc
->parse
,
843 n
->line
, n
->pos
, "Dd after Dt");
844 else if (mdoc
->meta
.os
!= NULL
)
845 mandoc_msg(MANDOCERR_PROLOG_ORDER
, mdoc
->parse
,
846 n
->line
, n
->pos
, "Dd after Os");
852 struct mdoc_node
*np
, *nch
;
856 * Unlike other data pointers, these are "housed" by the HEAD
857 * element, which contains the goods.
860 if (MDOC_HEAD
!= mdoc
->last
->type
) {
861 if (ENDBODY_NOT
!= mdoc
->last
->end
) {
862 assert(mdoc
->last
->pending
);
863 np
= mdoc
->last
->pending
->parent
->head
;
864 } else if (MDOC_BLOCK
!= mdoc
->last
->type
) {
865 np
= mdoc
->last
->parent
->head
;
867 np
= mdoc
->last
->head
;
870 assert(MDOC_HEAD
== np
->type
);
871 assert(MDOC_Bf
== np
->tok
);
876 assert(MDOC_BLOCK
== np
->parent
->type
);
877 assert(MDOC_Bf
== np
->parent
->tok
);
879 /* Check the number of arguments. */
882 if (NULL
== np
->parent
->args
) {
884 mandoc_msg(MANDOCERR_BF_NOFONT
, mdoc
->parse
,
885 np
->line
, np
->pos
, "Bf");
891 mandoc_vmsg(MANDOCERR_ARG_EXCESS
, mdoc
->parse
,
892 nch
->line
, nch
->pos
, "Bf ... %s", nch
->string
);
894 /* Extract argument into data. */
896 if (np
->parent
->args
) {
897 arg
= np
->parent
->args
->argv
[0].arg
;
898 if (MDOC_Emphasis
== arg
)
899 np
->norm
->Bf
.font
= FONT_Em
;
900 else if (MDOC_Literal
== arg
)
901 np
->norm
->Bf
.font
= FONT_Li
;
902 else if (MDOC_Symbolic
== arg
)
903 np
->norm
->Bf
.font
= FONT_Sy
;
909 /* Extract parameter into data. */
911 if (0 == strcmp(np
->child
->string
, "Em"))
912 np
->norm
->Bf
.font
= FONT_Em
;
913 else if (0 == strcmp(np
->child
->string
, "Li"))
914 np
->norm
->Bf
.font
= FONT_Li
;
915 else if (0 == strcmp(np
->child
->string
, "Sy"))
916 np
->norm
->Bf
.font
= FONT_Sy
;
918 mandoc_vmsg(MANDOCERR_BF_BADFONT
, mdoc
->parse
,
919 np
->child
->line
, np
->child
->pos
,
920 "Bf %s", np
->child
->string
);
927 const char *stdlibname
;
930 check_count(mdoc
, MDOC_ELEM
, CHECK_EQ
, 1);
931 n
= mdoc
->last
->child
;
932 assert(MDOC_TEXT
== n
->type
);
934 if (NULL
== (stdlibname
= mdoc_a2lib(n
->string
)))
935 mandoc_asprintf(&libname
,
936 "library \\(lq%s\\(rq", n
->string
);
938 libname
= mandoc_strdup(stdlibname
);
947 const struct mdoc_node
*n
;
951 mandoc_vmsg(MANDOCERR_ARG_SKIP
,
952 mdoc
->parse
, n
->line
, n
->pos
,
953 "%s %s", mdoc_macronames
[n
->tok
],
958 post_fname(POST_ARGS
)
960 const struct mdoc_node
*n
;
964 n
= mdoc
->last
->child
;
965 pos
= strcspn(n
->string
, "()");
966 cp
= n
->string
+ pos
;
967 if ( ! (cp
[0] == '\0' || (cp
[0] == '(' && cp
[1] == '*')))
968 mandoc_msg(MANDOCERR_FN_PAREN
, mdoc
->parse
,
969 n
->line
, n
->pos
+ pos
, n
->string
);
984 check_count(mdoc
, MDOC_HEAD
, CHECK_EQ
, 1);
986 if (mdoc
->last
->type
== MDOC_HEAD
&& mdoc
->last
->nchild
)
993 const struct mdoc_node
*n
;
996 for (n
= mdoc
->last
->child
; n
!= NULL
; n
= n
->next
) {
997 for (cp
= n
->string
; *cp
!= '\0'; cp
++) {
998 /* Ignore callbacks and alterations. */
999 if (*cp
== '(' || *cp
== '{')
1003 mandoc_msg(MANDOCERR_FA_COMMA
, mdoc
->parse
,
1004 n
->line
, n
->pos
+ (cp
- n
->string
),
1014 const struct mdoc_node
*n
;
1017 * The Vt macro comes in both ELEM and BLOCK form, both of which
1018 * have different syntaxes (yet more context-sensitive
1019 * behaviour). ELEM types must have a child, which is already
1020 * guaranteed by the in_line parsing routine; BLOCK types,
1021 * specifically the BODY, should only have TEXT children.
1024 if (MDOC_BODY
!= mdoc
->last
->type
)
1027 for (n
= mdoc
->last
->child
; n
; n
= n
->next
)
1028 if (MDOC_TEXT
!= n
->type
)
1029 mandoc_msg(MANDOCERR_VT_CHILD
, mdoc
->parse
,
1030 n
->line
, n
->pos
, mdoc_macronames
[n
->tok
]);
1036 struct mdoc_node
*n
;
1040 if (n
->last
!= NULL
&&
1041 (n
->last
->tok
== MDOC_Pp
||
1042 n
->last
->tok
== MDOC_Lp
))
1043 mdoc_node_relink(mdoc
, n
->last
);
1045 if (NULL
!= mdoc
->meta
.name
)
1048 mdoc_deroff(&mdoc
->meta
.name
, n
);
1050 if (NULL
== mdoc
->meta
.name
)
1051 mandoc_msg(MANDOCERR_NM_NONAME
, mdoc
->parse
,
1052 n
->line
, n
->pos
, "Nm");
1058 struct mdoc_node
*n
;
1062 if (n
->type
!= MDOC_BODY
)
1065 if (n
->child
== NULL
)
1066 mandoc_msg(MANDOCERR_ND_EMPTY
, mdoc
->parse
,
1067 n
->line
, n
->pos
, "Nd");
1081 post_literal(POST_ARGS
)
1087 * The `Dl' (note "el" not "one") and `Bd' macros unset the
1088 * MDOC_LITERAL flag as they leave. Note that `Bd' only sets
1089 * this in literal mode, but it doesn't hurt to just switch it
1090 * off in general since displays can't be nested.
1093 if (MDOC_BODY
== mdoc
->last
->type
)
1094 mdoc
->flags
&= ~MDOC_LITERAL
;
1098 post_defaults(POST_ARGS
)
1100 struct mdoc_node
*nn
;
1103 * The `Ar' defaults to "file ..." if no value is provided as an
1104 * argument; the `Mt' and `Pa' macros use "~"; the `Li' just
1105 * gets an empty string.
1108 if (mdoc
->last
->child
)
1112 mdoc
->next
= MDOC_NEXT_CHILD
;
1116 mdoc_word_alloc(mdoc
, nn
->line
, nn
->pos
, "file");
1117 mdoc_word_alloc(mdoc
, nn
->line
, nn
->pos
, "...");
1122 mdoc_word_alloc(mdoc
, nn
->line
, nn
->pos
, "~");
1134 struct mdoc_node
*n
;
1135 const char *std_att
;
1139 if (n
->child
== NULL
) {
1140 mdoc
->next
= MDOC_NEXT_CHILD
;
1141 mdoc_word_alloc(mdoc
, n
->line
, n
->pos
, "AT&T UNIX");
1147 * If we have a child, look it up in the standard keys. If a
1148 * key exist, use that instead of the child; if it doesn't,
1149 * prefix "AT&T UNIX " to the existing data.
1153 assert(MDOC_TEXT
== n
->type
);
1154 if (NULL
== (std_att
= mdoc_a2att(n
->string
))) {
1155 mandoc_vmsg(MANDOCERR_AT_BAD
, mdoc
->parse
,
1156 n
->line
, n
->pos
, "At %s", n
->string
);
1157 mandoc_asprintf(&att
, "AT&T UNIX %s", n
->string
);
1159 att
= mandoc_strdup(std_att
);
1168 struct mdoc_node
*np
, *nch
;
1172 if (np
->norm
->An
.auth
== AUTH__NONE
) {
1174 mandoc_msg(MANDOCERR_MACRO_EMPTY
, mdoc
->parse
,
1175 np
->line
, np
->pos
, "An");
1176 } else if (nch
!= NULL
)
1177 mandoc_vmsg(MANDOCERR_ARG_EXCESS
, mdoc
->parse
,
1178 nch
->line
, nch
->pos
, "An ... %s", nch
->string
);
1185 if (MDOC_BLOCK
== mdoc
->last
->type
)
1186 mdoc
->last
->norm
->Es
= mdoc
->last_es
;
1193 mdoc
->last_es
= mdoc
->last
;
1201 struct mdoc_node
*nbl
, *nit
, *nch
;
1204 if (nit
->type
!= MDOC_BLOCK
)
1207 nbl
= nit
->parent
->parent
;
1208 lt
= nbl
->norm
->Bl
.type
;
1220 if (nit
->head
->child
== NULL
)
1221 mandoc_vmsg(MANDOCERR_IT_NOHEAD
,
1222 mdoc
->parse
, nit
->line
, nit
->pos
,
1224 mdoc_argnames
[nbl
->args
->argv
[0].arg
]);
1233 if (nit
->body
== NULL
|| nit
->body
->child
== NULL
)
1234 mandoc_vmsg(MANDOCERR_IT_NOBODY
,
1235 mdoc
->parse
, nit
->line
, nit
->pos
,
1237 mdoc_argnames
[nbl
->args
->argv
[0].arg
]);
1240 if (nit
->head
->child
!= NULL
)
1241 mandoc_vmsg(MANDOCERR_ARG_SKIP
,
1242 mdoc
->parse
, nit
->line
, nit
->pos
,
1243 "It %s", nit
->head
->child
->string
);
1246 cols
= (int)nbl
->norm
->Bl
.ncols
;
1248 assert(nit
->head
->child
== NULL
);
1250 for (i
= 0, nch
= nit
->child
; nch
; nch
= nch
->next
)
1251 if (nch
->type
== MDOC_BODY
)
1254 if (i
< cols
|| i
> cols
+ 1)
1255 mandoc_vmsg(MANDOCERR_ARGCOUNT
,
1256 mdoc
->parse
, nit
->line
, nit
->pos
,
1257 "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 post_bl_block_tag(mdoc
);
1282 assert(n
->norm
->Bl
.width
);
1285 for (ni
= n
->body
->child
; ni
; ni
= ni
->next
) {
1286 if (NULL
== ni
->body
)
1288 nc
= ni
->body
->last
;
1289 while (NULL
!= nc
) {
1301 if (NULL
== ni
->next
) {
1302 mandoc_msg(MANDOCERR_PAR_MOVE
,
1303 mdoc
->parse
, nc
->line
, nc
->pos
,
1304 mdoc_macronames
[nc
->tok
]);
1305 mdoc_node_relink(mdoc
, nc
);
1306 } else if (0 == n
->norm
->Bl
.comp
&&
1307 LIST_column
!= n
->norm
->Bl
.type
) {
1308 mandoc_vmsg(MANDOCERR_PAR_SKIP
,
1309 mdoc
->parse
, nc
->line
, nc
->pos
,
1311 mdoc_macronames
[nc
->tok
]);
1312 mdoc_node_delete(mdoc
, nc
);
1315 nc
= ni
->body
->last
;
1321 * If the argument of -offset or -width is a macro,
1322 * replace it with the associated default width.
1325 rewrite_macro2len(char **arg
)
1332 else if ( ! strcmp(*arg
, "Ds"))
1334 else if ((tok
= mdoc_hash_find(*arg
)) == MDOC_MAX
)
1337 width
= macro2len(tok
);
1340 mandoc_asprintf(arg
, "%zun", width
);
1344 post_bl_block_tag(POST_ARGS
)
1346 struct mdoc_node
*n
, *nn
;
1352 * Calculate the -width for a `Bl -tag' list if it hasn't been
1353 * provided. Uses the first head macro. NOTE AGAIN: this is
1354 * ONLY if the -width argument has NOT been provided. See
1355 * rewrite_macro2len() for converting the -width string.
1361 for (nn
= n
->body
->child
; nn
; nn
= nn
->next
) {
1362 if (MDOC_It
!= nn
->tok
)
1365 assert(MDOC_BLOCK
== nn
->type
);
1366 nn
= nn
->head
->child
;
1371 if (MDOC_TEXT
== nn
->type
) {
1372 sz
= strlen(nn
->string
) + 1;
1376 if (0 != (ssz
= macro2len(nn
->tok
)))
1382 /* Defaults to ten ens. */
1384 (void)snprintf(buf
, sizeof(buf
), "%un", (unsigned int)sz
);
1387 * We have to dynamically add this to the macro's argument list.
1388 * We're guaranteed that a MDOC_Width doesn't already exist.
1392 i
= (int)(n
->args
->argc
)++;
1394 n
->args
->argv
= mandoc_reallocarray(n
->args
->argv
,
1395 n
->args
->argc
, sizeof(struct mdoc_argv
));
1397 n
->args
->argv
[i
].arg
= MDOC_Width
;
1398 n
->args
->argv
[i
].line
= n
->line
;
1399 n
->args
->argv
[i
].pos
= n
->pos
;
1400 n
->args
->argv
[i
].sz
= 1;
1401 n
->args
->argv
[i
].value
= mandoc_malloc(sizeof(char *));
1402 n
->args
->argv
[i
].value
[0] = mandoc_strdup(buf
);
1404 /* Set our width! */
1405 n
->norm
->Bl
.width
= n
->args
->argv
[i
].value
[0];
1409 post_bl_head(POST_ARGS
)
1411 struct mdoc_node
*nbl
, *nh
, *nch
, *nnext
;
1412 struct mdoc_argv
*argv
;
1417 if (nh
->norm
->Bl
.type
!= LIST_column
) {
1418 if ((nch
= nh
->child
) == NULL
)
1420 mandoc_vmsg(MANDOCERR_ARG_EXCESS
, mdoc
->parse
,
1421 nch
->line
, nch
->pos
, "Bl ... %s", nch
->string
);
1422 while (nch
!= NULL
) {
1423 mdoc_node_delete(mdoc
, nch
);
1430 * Append old-style lists, where the column width specifiers
1431 * trail as macro parameters, to the new-style ("normal-form")
1432 * lists where they're argument values following -column.
1435 if (nh
->child
== NULL
)
1439 for (j
= 0; j
< (int)nbl
->args
->argc
; j
++)
1440 if (nbl
->args
->argv
[j
].arg
== MDOC_Column
)
1443 assert(j
< (int)nbl
->args
->argc
);
1446 * Accommodate for new-style groff column syntax. Shuffle the
1447 * child nodes, all of which must be TEXT, as arguments for the
1448 * column field. Then, delete the head children.
1451 argv
= nbl
->args
->argv
+ j
;
1453 argv
->sz
+= nh
->nchild
;
1454 argv
->value
= mandoc_reallocarray(argv
->value
,
1455 argv
->sz
, sizeof(char *));
1457 nh
->norm
->Bl
.ncols
= argv
->sz
;
1458 nh
->norm
->Bl
.cols
= (void *)argv
->value
;
1460 for (nch
= nh
->child
; nch
!= NULL
; nch
= nnext
) {
1461 argv
->value
[i
++] = nch
->string
;
1464 mdoc_node_delete(NULL
, nch
);
1473 struct mdoc_node
*nparent
, *nprev
; /* of the Bl block */
1474 struct mdoc_node
*nblock
, *nbody
; /* of the Bl */
1475 struct mdoc_node
*nchild
, *nnext
; /* of the Bl body */
1478 switch (nbody
->type
) {
1480 post_bl_block(mdoc
);
1493 nchild
= nbody
->child
;
1494 while (NULL
!= nchild
) {
1495 if (nchild
->tok
== MDOC_It
||
1496 (nchild
->tok
== MDOC_Sm
&&
1497 nchild
->next
!= NULL
&&
1498 nchild
->next
->tok
== MDOC_It
)) {
1499 nchild
= nchild
->next
;
1503 mandoc_msg(MANDOCERR_BL_MOVE
, mdoc
->parse
,
1504 nchild
->line
, nchild
->pos
,
1505 mdoc_macronames
[nchild
->tok
]);
1508 * Move the node out of the Bl block.
1509 * First, collect all required node pointers.
1512 nblock
= nbody
->parent
;
1513 nprev
= nblock
->prev
;
1514 nparent
= nblock
->parent
;
1515 nnext
= nchild
->next
;
1518 * Unlink this child.
1521 assert(NULL
== nchild
->prev
);
1522 if (0 == --nbody
->nchild
) {
1523 nbody
->child
= NULL
;
1525 assert(NULL
== nnext
);
1527 nbody
->child
= nnext
;
1532 * Relink this child.
1535 nchild
->parent
= nparent
;
1536 nchild
->prev
= nprev
;
1537 nchild
->next
= nblock
;
1539 nblock
->prev
= nchild
;
1542 nparent
->child
= nchild
;
1544 nprev
->next
= nchild
;
1553 struct mdoc_node
*n
;
1557 if (n
->type
== MDOC_BLOCK
&& n
->body
->child
== NULL
) {
1558 mandoc_msg(MANDOCERR_MACRO_EMPTY
,
1559 mdoc
->parse
, n
->line
, n
->pos
, "Bk");
1560 mdoc_node_delete(mdoc
, n
);
1565 post_sm(struct mdoc
*mdoc
)
1567 struct mdoc_node
*nch
;
1569 nch
= mdoc
->last
->child
;
1572 mdoc
->flags
^= MDOC_SMOFF
;
1576 assert(nch
->type
== MDOC_TEXT
);
1578 if ( ! strcmp(nch
->string
, "on")) {
1579 mdoc
->flags
&= ~MDOC_SMOFF
;
1582 if ( ! strcmp(nch
->string
, "off")) {
1583 mdoc
->flags
|= MDOC_SMOFF
;
1587 mandoc_vmsg(MANDOCERR_SM_BAD
,
1588 mdoc
->parse
, nch
->line
, nch
->pos
,
1589 "%s %s", mdoc_macronames
[mdoc
->last
->tok
], nch
->string
);
1590 mdoc_node_relink(mdoc
, nch
);
1595 post_root(POST_ARGS
)
1597 struct mdoc_node
*n
;
1599 /* Add missing prologue data. */
1601 if (mdoc
->meta
.date
== NULL
)
1602 mdoc
->meta
.date
= mdoc
->quick
?
1604 mandoc_normdate(mdoc
->parse
, NULL
, 0, 0);
1606 if (mdoc
->meta
.title
== NULL
) {
1607 mandoc_msg(MANDOCERR_DT_NOTITLE
,
1608 mdoc
->parse
, 0, 0, "EOF");
1609 mdoc
->meta
.title
= mandoc_strdup("UNTITLED");
1612 if (mdoc
->meta
.vol
== NULL
)
1613 mdoc
->meta
.vol
= mandoc_strdup("LOCAL");
1615 if (mdoc
->meta
.os
== NULL
) {
1616 mandoc_msg(MANDOCERR_OS_MISSING
,
1617 mdoc
->parse
, 0, 0, NULL
);
1618 mdoc
->meta
.os
= mandoc_strdup("");
1621 /* Check that we begin with a proper `Sh'. */
1623 n
= mdoc
->first
->child
;
1624 while (n
!= NULL
&& mdoc_macros
[n
->tok
].flags
& MDOC_PROLOGUE
)
1628 mandoc_msg(MANDOCERR_DOC_EMPTY
, mdoc
->parse
, 0, 0, NULL
);
1629 else if (n
->tok
!= MDOC_Sh
)
1630 mandoc_msg(MANDOCERR_SEC_BEFORE
, mdoc
->parse
,
1631 n
->line
, n
->pos
, mdoc_macronames
[n
->tok
]);
1637 struct mdoc_node
*n
, *nch
;
1644 mandoc_msg(MANDOCERR_MACRO_EMPTY
, mdoc
->parse
,
1645 n
->line
, n
->pos
, mdoc_macronames
[n
->tok
]);
1646 mdoc_node_delete(mdoc
, n
);
1650 assert(MDOC_TEXT
== nch
->type
);
1652 if (NULL
== (p
= mdoc_a2st(nch
->string
))) {
1653 mandoc_vmsg(MANDOCERR_ST_BAD
, mdoc
->parse
,
1654 nch
->line
, nch
->pos
, "St %s", nch
->string
);
1655 mdoc_node_delete(mdoc
, n
);
1658 nch
->string
= mandoc_strdup(p
);
1665 struct mdoc_node
*np
, *nch
, *next
, *prev
;
1670 if (np
->type
!= MDOC_BODY
)
1673 if (np
->child
== NULL
) {
1674 mandoc_msg(MANDOCERR_RS_EMPTY
, mdoc
->parse
,
1675 np
->line
, np
->pos
, "Rs");
1680 * The full `Rs' block needs special handling to order the
1681 * sub-elements according to `rsord'. Pick through each element
1682 * and correctly order it. This is an insertion sort.
1686 for (nch
= np
->child
->next
; nch
!= NULL
; nch
= next
) {
1687 /* Determine order number of this child. */
1688 for (i
= 0; i
< RSORD_MAX
; i
++)
1689 if (rsord
[i
] == nch
->tok
)
1692 if (i
== RSORD_MAX
) {
1693 mandoc_msg(MANDOCERR_RS_BAD
,
1694 mdoc
->parse
, nch
->line
, nch
->pos
,
1695 mdoc_macronames
[nch
->tok
]);
1697 } else if (nch
->tok
== MDOC__J
|| nch
->tok
== MDOC__B
)
1698 np
->norm
->Rs
.quote_T
++;
1701 * Remove this child from the chain. This somewhat
1702 * repeats mdoc_node_unlink(), but since we're
1703 * just re-ordering, there's no need for the
1704 * full unlink process.
1707 if ((next
= nch
->next
) != NULL
)
1708 next
->prev
= nch
->prev
;
1710 if ((prev
= nch
->prev
) != NULL
)
1711 prev
->next
= nch
->next
;
1713 nch
->prev
= nch
->next
= NULL
;
1716 * Scan back until we reach a node that's
1717 * to be ordered before this child.
1720 for ( ; prev
; prev
= prev
->prev
) {
1721 /* Determine order of `prev'. */
1722 for (j
= 0; j
< RSORD_MAX
; j
++)
1723 if (rsord
[j
] == prev
->tok
)
1733 * Set this child back into its correct place
1734 * in front of the `prev' node.
1740 np
->child
->prev
= nch
;
1741 nch
->next
= np
->child
;
1745 prev
->next
->prev
= nch
;
1746 nch
->next
= prev
->next
;
1753 * For some arguments of some macros,
1754 * convert all breakable hyphens into ASCII_HYPH.
1757 post_hyph(POST_ARGS
)
1759 struct mdoc_node
*n
, *nch
;
1765 if (MDOC_Sh
== n
->tok
|| MDOC_Ss
== n
->tok
)
1769 if (MDOC_D1
== n
->tok
|| MDOC_Nd
== n
->tok
)
1778 for (nch
= n
->child
; nch
; nch
= nch
->next
) {
1779 if (MDOC_TEXT
!= nch
->type
)
1784 while ('\0' != *(++cp
))
1786 isalpha((unsigned char)cp
[-1]) &&
1787 isalpha((unsigned char)cp
[1]))
1793 post_hyphtext(POST_ARGS
)
1804 if (MDOC_LINE
& mdoc
->last
->flags
)
1805 mandoc_msg(MANDOCERR_NS_SKIP
, mdoc
->parse
,
1806 mdoc
->last
->line
, mdoc
->last
->pos
, NULL
);
1815 switch (mdoc
->last
->type
) {
1820 switch (mdoc
->lastsec
) {
1825 post_sh_see_also(mdoc
);
1828 post_sh_authors(mdoc
);
1840 post_sh_name(POST_ARGS
)
1842 struct mdoc_node
*n
;
1845 * Warn if the NAME section doesn't contain the `Nm' and `Nd'
1846 * macros (can have multiple `Nm' and one `Nd'). Note that the
1847 * children of the BODY declaration can also be "text".
1850 if (NULL
== (n
= mdoc
->last
->child
)) {
1851 mandoc_msg(MANDOCERR_NAMESEC_BAD
, mdoc
->parse
,
1852 mdoc
->last
->line
, mdoc
->last
->pos
, "empty");
1856 for ( ; n
&& n
->next
; n
= n
->next
) {
1857 if (MDOC_ELEM
== n
->type
&& MDOC_Nm
== n
->tok
)
1859 if (MDOC_TEXT
== n
->type
)
1861 mandoc_msg(MANDOCERR_NAMESEC_BAD
, mdoc
->parse
,
1862 n
->line
, n
->pos
, mdoc_macronames
[n
->tok
]);
1866 if (MDOC_BLOCK
== n
->type
&& MDOC_Nd
== n
->tok
)
1869 mandoc_msg(MANDOCERR_NAMESEC_BAD
, mdoc
->parse
,
1870 n
->line
, n
->pos
, mdoc_macronames
[n
->tok
]);
1874 post_sh_see_also(POST_ARGS
)
1876 const struct mdoc_node
*n
;
1877 const char *name
, *sec
;
1878 const char *lastname
, *lastsec
, *lastpunct
;
1881 n
= mdoc
->last
->child
;
1882 lastname
= lastsec
= lastpunct
= NULL
;
1884 if (n
->tok
!= MDOC_Xr
|| n
->nchild
< 2)
1887 /* Process one .Xr node. */
1889 name
= n
->child
->string
;
1890 sec
= n
->child
->next
->string
;
1891 if (lastsec
!= NULL
) {
1892 if (lastpunct
[0] != ',' || lastpunct
[1] != '\0')
1893 mandoc_vmsg(MANDOCERR_XR_PUNCT
,
1894 mdoc
->parse
, n
->line
, n
->pos
,
1895 "%s before %s(%s)", lastpunct
,
1897 cmp
= strcmp(lastsec
, sec
);
1899 mandoc_vmsg(MANDOCERR_XR_ORDER
,
1900 mdoc
->parse
, n
->line
, n
->pos
,
1901 "%s(%s) after %s(%s)", name
,
1902 sec
, lastname
, lastsec
);
1903 else if (cmp
== 0 &&
1904 strcasecmp(lastname
, name
) > 0)
1905 mandoc_vmsg(MANDOCERR_XR_ORDER
,
1906 mdoc
->parse
, n
->line
, n
->pos
,
1907 "%s after %s", name
, lastname
);
1912 /* Process the following node. */
1917 if (n
->tok
== MDOC_Xr
) {
1921 if (n
->type
!= MDOC_TEXT
)
1923 for (name
= n
->string
; *name
!= '\0'; name
++)
1924 if (isalpha((const unsigned char)*name
))
1926 lastpunct
= n
->string
;
1927 if (n
->next
== NULL
)
1928 mandoc_vmsg(MANDOCERR_XR_PUNCT
, mdoc
->parse
,
1929 n
->line
, n
->pos
, "%s after %s(%s)",
1930 lastpunct
, lastname
, lastsec
);
1936 child_an(const struct mdoc_node
*n
)
1939 for (n
= n
->child
; n
!= NULL
; n
= n
->next
)
1940 if ((n
->tok
== MDOC_An
&& n
->nchild
) || child_an(n
))
1946 post_sh_authors(POST_ARGS
)
1949 if ( ! child_an(mdoc
->last
))
1950 mandoc_msg(MANDOCERR_AN_MISSING
, mdoc
->parse
,
1951 mdoc
->last
->line
, mdoc
->last
->pos
, NULL
);
1955 post_sh_head(POST_ARGS
)
1957 struct mdoc_node
*n
;
1958 const char *goodsec
;
1963 * Process a new section. Sections are either "named" or
1964 * "custom". Custom sections are user-defined, while named ones
1965 * follow a conventional order and may only appear in certain
1971 mdoc_deroff(&secname
, mdoc
->last
);
1972 sec
= NULL
== secname
? SEC_CUSTOM
: a2sec(secname
);
1974 /* The NAME should be first. */
1976 if (SEC_NAME
!= sec
&& SEC_NONE
== mdoc
->lastnamed
)
1977 mandoc_vmsg(MANDOCERR_NAMESEC_FIRST
, mdoc
->parse
,
1978 mdoc
->last
->line
, mdoc
->last
->pos
,
1981 /* The SYNOPSIS gets special attention in other areas. */
1983 if (SEC_SYNOPSIS
== sec
) {
1984 roff_setreg(mdoc
->roff
, "nS", 1, '=');
1985 mdoc
->flags
|= MDOC_SYNOPSIS
;
1987 roff_setreg(mdoc
->roff
, "nS", 0, '=');
1988 mdoc
->flags
&= ~MDOC_SYNOPSIS
;
1991 /* Mark our last section. */
1993 mdoc
->lastsec
= sec
;
1996 * Set the section attribute for the current HEAD, for its
1997 * parent BLOCK, and for the HEAD children; the latter can
1998 * only be TEXT nodes, so no recursion is needed.
1999 * For other blocks and elements, including .Sh BODY, this is
2000 * done when allocating the node data structures, but for .Sh
2001 * BLOCK and HEAD, the section is still unknown at that time.
2004 mdoc
->last
->parent
->sec
= sec
;
2005 mdoc
->last
->sec
= sec
;
2006 for (n
= mdoc
->last
->child
; n
; n
= n
->next
)
2009 /* We don't care about custom sections after this. */
2011 if (SEC_CUSTOM
== sec
) {
2017 * Check whether our non-custom section is being repeated or is
2021 if (sec
== mdoc
->lastnamed
)
2022 mandoc_vmsg(MANDOCERR_SEC_REP
, mdoc
->parse
,
2023 mdoc
->last
->line
, mdoc
->last
->pos
,
2026 if (sec
< mdoc
->lastnamed
)
2027 mandoc_vmsg(MANDOCERR_SEC_ORDER
, mdoc
->parse
,
2028 mdoc
->last
->line
, mdoc
->last
->pos
,
2031 /* Mark the last named section. */
2033 mdoc
->lastnamed
= sec
;
2035 /* Check particular section/manual conventions. */
2037 if (mdoc
->meta
.msec
== NULL
) {
2045 if (*mdoc
->meta
.msec
== '4')
2047 goodsec
= "2, 3, 4, 9";
2049 case SEC_RETURN_VALUES
:
2052 if (*mdoc
->meta
.msec
== '2')
2054 if (*mdoc
->meta
.msec
== '3')
2056 if (NULL
== goodsec
)
2057 goodsec
= "2, 3, 9";
2060 if (*mdoc
->meta
.msec
== '9')
2062 if (NULL
== goodsec
)
2064 mandoc_vmsg(MANDOCERR_SEC_MSEC
, mdoc
->parse
,
2065 mdoc
->last
->line
, mdoc
->last
->pos
,
2066 "Sh %s for %s only", secname
, goodsec
);
2075 post_ignpar(POST_ARGS
)
2077 struct mdoc_node
*np
;
2079 check_count(mdoc
, MDOC_HEAD
, CHECK_GT
, 0);
2082 if (MDOC_BODY
!= mdoc
->last
->type
)
2085 if (NULL
!= (np
= mdoc
->last
->child
))
2086 if (MDOC_Pp
== np
->tok
|| MDOC_Lp
== np
->tok
) {
2087 mandoc_vmsg(MANDOCERR_PAR_SKIP
,
2088 mdoc
->parse
, np
->line
, np
->pos
,
2089 "%s after %s", mdoc_macronames
[np
->tok
],
2090 mdoc_macronames
[mdoc
->last
->tok
]);
2091 mdoc_node_delete(mdoc
, np
);
2094 if (NULL
!= (np
= mdoc
->last
->last
))
2095 if (MDOC_Pp
== np
->tok
|| MDOC_Lp
== np
->tok
) {
2096 mandoc_vmsg(MANDOCERR_PAR_SKIP
, mdoc
->parse
,
2097 np
->line
, np
->pos
, "%s at the end of %s",
2098 mdoc_macronames
[np
->tok
],
2099 mdoc_macronames
[mdoc
->last
->tok
]);
2100 mdoc_node_delete(mdoc
, np
);
2108 if (NULL
== mdoc
->last
)
2110 if (MDOC_ELEM
!= n
->type
&& MDOC_BLOCK
!= n
->type
)
2114 * Don't allow prior `Lp' or `Pp' prior to a paragraph-type
2115 * block: `Lp', `Pp', or non-compact `Bd' or `Bl'.
2118 if (MDOC_Pp
!= mdoc
->last
->tok
&&
2119 MDOC_Lp
!= mdoc
->last
->tok
&&
2120 MDOC_br
!= mdoc
->last
->tok
)
2122 if (MDOC_Bl
== n
->tok
&& n
->norm
->Bl
.comp
)
2124 if (MDOC_Bd
== n
->tok
&& n
->norm
->Bd
.comp
)
2126 if (MDOC_It
== n
->tok
&& n
->parent
->norm
->Bl
.comp
)
2129 mandoc_vmsg(MANDOCERR_PAR_SKIP
, mdoc
->parse
,
2130 mdoc
->last
->line
, mdoc
->last
->pos
,
2131 "%s before %s", mdoc_macronames
[mdoc
->last
->tok
],
2132 mdoc_macronames
[n
->tok
]);
2133 mdoc_node_delete(mdoc
, mdoc
->last
);
2139 struct mdoc_node
*np
;
2143 if (np
->tok
== MDOC_sp
) {
2145 mandoc_vmsg(MANDOCERR_ARG_EXCESS
, mdoc
->parse
,
2146 np
->child
->next
->line
, np
->child
->next
->pos
,
2147 "sp ... %s", np
->child
->next
->string
);
2148 } else if (np
->child
!= NULL
)
2149 mandoc_vmsg(MANDOCERR_ARG_SKIP
,
2150 mdoc
->parse
, np
->line
, np
->pos
, "%s %s",
2151 mdoc_macronames
[np
->tok
], np
->child
->string
);
2153 if (NULL
== (np
= mdoc
->last
->prev
)) {
2154 np
= mdoc
->last
->parent
;
2155 if (MDOC_Sh
!= np
->tok
&& MDOC_Ss
!= np
->tok
)
2157 } else if (MDOC_Pp
!= np
->tok
&& MDOC_Lp
!= np
->tok
&&
2158 (MDOC_br
!= mdoc
->last
->tok
||
2159 (MDOC_sp
!= np
->tok
&& MDOC_br
!= np
->tok
)))
2162 mandoc_vmsg(MANDOCERR_PAR_SKIP
, mdoc
->parse
,
2163 mdoc
->last
->line
, mdoc
->last
->pos
,
2164 "%s after %s", mdoc_macronames
[mdoc
->last
->tok
],
2165 mdoc_macronames
[np
->tok
]);
2166 mdoc_node_delete(mdoc
, mdoc
->last
);
2170 pre_literal(PRE_ARGS
)
2173 pre_display(mdoc
, n
);
2175 if (MDOC_BODY
!= n
->type
)
2179 * The `Dl' (note "el" not "one") and `Bd -literal' and `Bd
2180 * -unfilled' macros set MDOC_LITERAL on entrance to the body.
2185 mdoc
->flags
|= MDOC_LITERAL
;
2188 if (DISP_literal
== n
->norm
->Bd
.type
)
2189 mdoc
->flags
|= MDOC_LITERAL
;
2190 if (DISP_unfilled
== n
->norm
->Bd
.type
)
2191 mdoc
->flags
|= MDOC_LITERAL
;
2202 struct mdoc_node
*n
;
2205 if (mdoc
->meta
.date
)
2206 free(mdoc
->meta
.date
);
2209 if (NULL
== n
->child
|| '\0' == n
->child
->string
[0]) {
2210 mdoc
->meta
.date
= mdoc
->quick
? mandoc_strdup("") :
2211 mandoc_normdate(mdoc
->parse
, NULL
, n
->line
, n
->pos
);
2216 mdoc_deroff(&datestr
, n
);
2218 mdoc
->meta
.date
= datestr
;
2220 mdoc
->meta
.date
= mandoc_normdate(mdoc
->parse
,
2221 datestr
, n
->line
, n
->pos
);
2225 mdoc_node_delete(mdoc
, n
);
2231 struct mdoc_node
*nn
, *n
;
2237 free(mdoc
->meta
.title
);
2238 free(mdoc
->meta
.msec
);
2239 free(mdoc
->meta
.vol
);
2240 free(mdoc
->meta
.arch
);
2242 mdoc
->meta
.title
= NULL
;
2243 mdoc
->meta
.msec
= NULL
;
2244 mdoc
->meta
.vol
= NULL
;
2245 mdoc
->meta
.arch
= NULL
;
2247 /* First check that all characters are uppercase. */
2249 if (NULL
!= (nn
= n
->child
))
2250 for (p
= nn
->string
; *p
; p
++) {
2251 if (toupper((unsigned char)*p
) == *p
)
2253 mandoc_vmsg(MANDOCERR_TITLE_CASE
,
2254 mdoc
->parse
, nn
->line
,
2255 nn
->pos
+ (p
- nn
->string
),
2256 "Dt %s", nn
->string
);
2260 /* No argument: msec and arch remain NULL. */
2262 if (NULL
== (nn
= n
->child
)) {
2263 mandoc_msg(MANDOCERR_DT_NOTITLE
,
2264 mdoc
->parse
, n
->line
, n
->pos
, "Dt");
2265 mdoc
->meta
.title
= mandoc_strdup("UNTITLED");
2266 mdoc
->meta
.vol
= mandoc_strdup("LOCAL");
2270 /* One argument: msec and arch remain NULL. */
2272 mdoc
->meta
.title
= mandoc_strdup(
2273 '\0' == nn
->string
[0] ? "UNTITLED" : nn
->string
);
2275 if (NULL
== (nn
= nn
->next
)) {
2276 mandoc_vmsg(MANDOCERR_MSEC_MISSING
,
2277 mdoc
->parse
, n
->line
, n
->pos
,
2278 "Dt %s", mdoc
->meta
.title
);
2279 mdoc
->meta
.vol
= mandoc_strdup("LOCAL");
2283 /* Handles: `.Dt TITLE SEC'
2285 * volume = SEC is msec ? format(msec) : SEC,
2286 * msec = SEC is msec ? atoi(msec) : 0,
2290 cp
= mandoc_a2msec(nn
->string
);
2292 mdoc
->meta
.vol
= mandoc_strdup(cp
);
2293 mdoc
->meta
.msec
= mandoc_strdup(nn
->string
);
2295 mandoc_vmsg(MANDOCERR_MSEC_BAD
, mdoc
->parse
,
2296 nn
->line
, nn
->pos
, "Dt ... %s", nn
->string
);
2297 mdoc
->meta
.vol
= mandoc_strdup(nn
->string
);
2298 mdoc
->meta
.msec
= mandoc_strdup(nn
->string
);
2301 /* Handle an optional architecture */
2303 if ((nn
= nn
->next
) != NULL
) {
2304 for (p
= nn
->string
; *p
; p
++)
2305 *p
= tolower((unsigned char)*p
);
2306 mdoc
->meta
.arch
= mandoc_strdup(nn
->string
);
2309 /* Ignore any subsequent parameters... */
2310 /* FIXME: warn about subsequent parameters. */
2312 mdoc_node_delete(mdoc
, n
);
2318 struct mdoc_node
*n
;
2321 * Make `Bx's second argument always start with an uppercase
2322 * letter. Groff checks if it's an "accepted" term, but we just
2323 * uppercase blindly.
2326 n
= mdoc
->last
->child
;
2327 if (n
&& NULL
!= (n
= n
->next
))
2328 *n
->string
= (char)toupper((unsigned char)*n
->string
);
2335 struct utsname utsname
;
2336 static char *defbuf
;
2338 struct mdoc_node
*n
;
2343 * Set the operating system by way of the `Os' macro.
2344 * The order of precedence is:
2345 * 1. the argument of the `Os' macro, unless empty
2346 * 2. the -Ios=foo command line argument, if provided
2347 * 3. -DOSNAME="\"foo\"", if provided during compilation
2348 * 4. "sysname release" from uname(3)
2351 free(mdoc
->meta
.os
);
2352 mdoc
->meta
.os
= NULL
;
2353 mdoc_deroff(&mdoc
->meta
.os
, n
);
2358 mdoc
->meta
.os
= mandoc_strdup(mdoc
->defos
);
2363 mdoc
->meta
.os
= mandoc_strdup(OSNAME
);
2365 if (NULL
== defbuf
) {
2366 if (-1 == uname(&utsname
)) {
2367 mandoc_msg(MANDOCERR_OS_UNAME
, mdoc
->parse
,
2368 n
->line
, n
->pos
, "Os");
2369 defbuf
= mandoc_strdup("UNKNOWN");
2371 mandoc_asprintf(&defbuf
, "%s %s",
2372 utsname
.sysname
, utsname
.release
);
2374 mdoc
->meta
.os
= mandoc_strdup(defbuf
);
2378 mdoc_node_delete(mdoc
, n
);
2382 * If no argument is provided,
2383 * fill in the name of the current manual page.
2388 struct mdoc_node
*n
;
2395 if (mdoc
->meta
.name
== NULL
) {
2396 mandoc_msg(MANDOCERR_EX_NONAME
, mdoc
->parse
,
2397 n
->line
, n
->pos
, "Ex");
2401 mdoc
->next
= MDOC_NEXT_CHILD
;
2402 mdoc_word_alloc(mdoc
, n
->line
, n
->pos
, mdoc
->meta
.name
);
2406 static enum mdoc_sec
2407 a2sec(const char *p
)
2411 for (i
= 0; i
< (int)SEC__MAX
; i
++)
2412 if (secnames
[i
] && 0 == strcmp(p
, secnames
[i
]))
2413 return((enum mdoc_sec
)i
);
2419 macro2len(enum mdoct macro
)