]>
git.cameronkatri.com Git - mandoc.git/blob - mdoc_validate.c
1 /* $Id: mdoc_validate.c,v 1.285 2015/04/02 22:48:17 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 AUTHORS DISCLAIM ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS 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>
34 #include "mandoc_aux.h"
38 #include "libmandoc.h"
41 /* FIXME: .Bl -diag can't have non-text children in HEAD. */
43 #define PRE_ARGS struct mdoc *mdoc, struct roff_node *n
44 #define POST_ARGS struct mdoc *mdoc
52 typedef void (*v_pre
)(PRE_ARGS
);
53 typedef void (*v_post
)(POST_ARGS
);
60 static void check_text(struct mdoc
*, int, int, char *);
61 static void check_argv(struct mdoc
*,
62 struct roff_node
*, struct mdoc_argv
*);
63 static void check_args(struct mdoc
*, struct roff_node
*);
64 static int child_an(const struct roff_node
*);
65 static enum roff_sec
a2sec(const char *);
66 static size_t macro2len(int);
67 static void rewrite_macro2len(char **);
69 static void post_an(POST_ARGS
);
70 static void post_at(POST_ARGS
);
71 static void post_bf(POST_ARGS
);
72 static void post_bk(POST_ARGS
);
73 static void post_bl(POST_ARGS
);
74 static void post_bl_block(POST_ARGS
);
75 static void post_bl_block_tag(POST_ARGS
);
76 static void post_bl_head(POST_ARGS
);
77 static void post_bx(POST_ARGS
);
78 static void post_d1(POST_ARGS
);
79 static void post_defaults(POST_ARGS
);
80 static void post_dd(POST_ARGS
);
81 static void post_dt(POST_ARGS
);
82 static void post_en(POST_ARGS
);
83 static void post_es(POST_ARGS
);
84 static void post_eoln(POST_ARGS
);
85 static void post_ex(POST_ARGS
);
86 static void post_fa(POST_ARGS
);
87 static void post_fn(POST_ARGS
);
88 static void post_fname(POST_ARGS
);
89 static void post_fo(POST_ARGS
);
90 static void post_hyph(POST_ARGS
);
91 static void post_ignpar(POST_ARGS
);
92 static void post_it(POST_ARGS
);
93 static void post_lb(POST_ARGS
);
94 static void post_literal(POST_ARGS
);
95 static void post_nd(POST_ARGS
);
96 static void post_nm(POST_ARGS
);
97 static void post_ns(POST_ARGS
);
98 static void post_os(POST_ARGS
);
99 static void post_par(POST_ARGS
);
100 static void post_root(POST_ARGS
);
101 static void post_rs(POST_ARGS
);
102 static void post_sh(POST_ARGS
);
103 static void post_sh_head(POST_ARGS
);
104 static void post_sh_name(POST_ARGS
);
105 static void post_sh_see_also(POST_ARGS
);
106 static void post_sh_authors(POST_ARGS
);
107 static void post_sm(POST_ARGS
);
108 static void post_st(POST_ARGS
);
109 static void post_vt(POST_ARGS
);
111 static void pre_an(PRE_ARGS
);
112 static void pre_bd(PRE_ARGS
);
113 static void pre_bl(PRE_ARGS
);
114 static void pre_dd(PRE_ARGS
);
115 static void pre_display(PRE_ARGS
);
116 static void pre_dt(PRE_ARGS
);
117 static void pre_literal(PRE_ARGS
);
118 static void pre_obsolete(PRE_ARGS
);
119 static void pre_os(PRE_ARGS
);
120 static void pre_par(PRE_ARGS
);
121 static void pre_std(PRE_ARGS
);
123 static const struct valids mdoc_valids
[MDOC_MAX
] = {
124 { NULL
, NULL
}, /* Ap */
125 { pre_dd
, post_dd
}, /* Dd */
126 { pre_dt
, post_dt
}, /* Dt */
127 { pre_os
, post_os
}, /* Os */
128 { NULL
, post_sh
}, /* Sh */
129 { NULL
, post_ignpar
}, /* Ss */
130 { pre_par
, post_par
}, /* Pp */
131 { pre_display
, post_d1
}, /* D1 */
132 { pre_literal
, post_literal
}, /* Dl */
133 { pre_bd
, post_literal
}, /* Bd */
134 { NULL
, NULL
}, /* Ed */
135 { pre_bl
, post_bl
}, /* Bl */
136 { NULL
, NULL
}, /* El */
137 { pre_par
, post_it
}, /* It */
138 { NULL
, NULL
}, /* Ad */
139 { pre_an
, post_an
}, /* An */
140 { NULL
, post_defaults
}, /* Ar */
141 { NULL
, NULL
}, /* Cd */
142 { NULL
, NULL
}, /* Cm */
143 { NULL
, NULL
}, /* Dv */
144 { NULL
, NULL
}, /* Er */
145 { NULL
, NULL
}, /* Ev */
146 { pre_std
, post_ex
}, /* Ex */
147 { NULL
, post_fa
}, /* Fa */
148 { NULL
, NULL
}, /* Fd */
149 { NULL
, NULL
}, /* Fl */
150 { NULL
, post_fn
}, /* Fn */
151 { NULL
, NULL
}, /* Ft */
152 { NULL
, NULL
}, /* Ic */
153 { NULL
, NULL
}, /* In */
154 { NULL
, post_defaults
}, /* Li */
155 { NULL
, post_nd
}, /* Nd */
156 { NULL
, post_nm
}, /* Nm */
157 { NULL
, NULL
}, /* Op */
158 { pre_obsolete
, NULL
}, /* Ot */
159 { NULL
, post_defaults
}, /* Pa */
160 { pre_std
, NULL
}, /* Rv */
161 { NULL
, post_st
}, /* St */
162 { NULL
, NULL
}, /* Va */
163 { NULL
, post_vt
}, /* Vt */
164 { NULL
, NULL
}, /* Xr */
165 { NULL
, NULL
}, /* %A */
166 { NULL
, post_hyph
}, /* %B */ /* FIXME: can be used outside Rs/Re. */
167 { NULL
, NULL
}, /* %D */
168 { NULL
, NULL
}, /* %I */
169 { NULL
, NULL
}, /* %J */
170 { NULL
, post_hyph
}, /* %N */
171 { NULL
, post_hyph
}, /* %O */
172 { NULL
, NULL
}, /* %P */
173 { NULL
, post_hyph
}, /* %R */
174 { NULL
, post_hyph
}, /* %T */ /* FIXME: can be used outside Rs/Re. */
175 { NULL
, NULL
}, /* %V */
176 { NULL
, NULL
}, /* Ac */
177 { NULL
, NULL
}, /* Ao */
178 { NULL
, NULL
}, /* Aq */
179 { NULL
, post_at
}, /* At */
180 { NULL
, NULL
}, /* Bc */
181 { NULL
, post_bf
}, /* Bf */
182 { NULL
, NULL
}, /* Bo */
183 { NULL
, NULL
}, /* Bq */
184 { NULL
, NULL
}, /* Bsx */
185 { NULL
, post_bx
}, /* Bx */
186 { pre_obsolete
, NULL
}, /* Db */
187 { NULL
, NULL
}, /* Dc */
188 { NULL
, NULL
}, /* Do */
189 { NULL
, NULL
}, /* Dq */
190 { NULL
, NULL
}, /* Ec */
191 { NULL
, NULL
}, /* Ef */
192 { NULL
, NULL
}, /* Em */
193 { NULL
, NULL
}, /* Eo */
194 { NULL
, NULL
}, /* Fx */
195 { NULL
, NULL
}, /* Ms */
196 { NULL
, NULL
}, /* No */
197 { NULL
, post_ns
}, /* Ns */
198 { NULL
, NULL
}, /* Nx */
199 { NULL
, NULL
}, /* Ox */
200 { NULL
, NULL
}, /* Pc */
201 { NULL
, NULL
}, /* Pf */
202 { NULL
, NULL
}, /* Po */
203 { NULL
, NULL
}, /* Pq */
204 { NULL
, NULL
}, /* Qc */
205 { NULL
, NULL
}, /* Ql */
206 { NULL
, NULL
}, /* Qo */
207 { NULL
, NULL
}, /* Qq */
208 { NULL
, NULL
}, /* Re */
209 { NULL
, post_rs
}, /* Rs */
210 { NULL
, NULL
}, /* Sc */
211 { NULL
, NULL
}, /* So */
212 { NULL
, NULL
}, /* Sq */
213 { NULL
, post_sm
}, /* Sm */
214 { NULL
, post_hyph
}, /* Sx */
215 { NULL
, NULL
}, /* Sy */
216 { NULL
, NULL
}, /* Tn */
217 { NULL
, NULL
}, /* Ux */
218 { NULL
, NULL
}, /* Xc */
219 { NULL
, NULL
}, /* Xo */
220 { NULL
, post_fo
}, /* Fo */
221 { NULL
, NULL
}, /* Fc */
222 { NULL
, NULL
}, /* Oo */
223 { NULL
, NULL
}, /* Oc */
224 { NULL
, post_bk
}, /* Bk */
225 { NULL
, NULL
}, /* Ek */
226 { NULL
, post_eoln
}, /* Bt */
227 { NULL
, NULL
}, /* Hf */
228 { pre_obsolete
, NULL
}, /* Fr */
229 { NULL
, post_eoln
}, /* Ud */
230 { NULL
, post_lb
}, /* Lb */
231 { pre_par
, post_par
}, /* Lp */
232 { NULL
, NULL
}, /* Lk */
233 { NULL
, post_defaults
}, /* Mt */
234 { NULL
, NULL
}, /* Brq */
235 { NULL
, NULL
}, /* Bro */
236 { NULL
, NULL
}, /* Brc */
237 { NULL
, NULL
}, /* %C */
238 { pre_obsolete
, post_es
}, /* Es */
239 { pre_obsolete
, post_en
}, /* En */
240 { NULL
, NULL
}, /* Dx */
241 { NULL
, NULL
}, /* %Q */
242 { NULL
, post_par
}, /* br */
243 { NULL
, post_par
}, /* sp */
244 { NULL
, NULL
}, /* %U */
245 { NULL
, NULL
}, /* Ta */
246 { NULL
, NULL
}, /* ll */
249 #define RSORD_MAX 14 /* Number of `Rs' blocks. */
251 static const int rsord
[RSORD_MAX
] = {
268 static const char * const secnames
[SEC__MAX
] = {
275 "IMPLEMENTATION NOTES",
290 "SECURITY CONSIDERATIONS",
296 mdoc_valid_pre(struct mdoc
*mdoc
, struct roff_node
*n
)
302 if (n
->sec
!= SEC_SYNOPSIS
|| n
->parent
->tok
!= MDOC_Fd
)
303 check_text(mdoc
, n
->line
, n
->pos
, n
->string
);
316 p
= mdoc_valids
[n
->tok
].pre
;
322 mdoc_valid_post(struct mdoc
*mdoc
)
328 if (n
->flags
& MDOC_VALID
)
330 n
->flags
|= MDOC_VALID
| MDOC_ENDED
;
345 * Closing delimiters are not special at the
346 * beginning of a block, opening delimiters
347 * are not special at the end.
350 if (n
->child
!= NULL
)
351 n
->child
->flags
&= ~MDOC_DELIMC
;
353 n
->last
->flags
&= ~MDOC_DELIMO
;
355 /* Call the macro's postprocessor. */
357 p
= mdoc_valids
[n
->tok
].post
;
365 check_args(struct mdoc
*mdoc
, struct roff_node
*n
)
372 assert(n
->args
->argc
);
373 for (i
= 0; i
< (int)n
->args
->argc
; i
++)
374 check_argv(mdoc
, n
, &n
->args
->argv
[i
]);
378 check_argv(struct mdoc
*mdoc
, struct roff_node
*n
, struct mdoc_argv
*v
)
382 for (i
= 0; i
< (int)v
->sz
; i
++)
383 check_text(mdoc
, v
->line
, v
->pos
, v
->value
[i
]);
387 check_text(struct mdoc
*mdoc
, int ln
, int pos
, char *p
)
391 if (MDOC_LITERAL
& mdoc
->flags
)
394 for (cp
= p
; NULL
!= (p
= strchr(p
, '\t')); p
++)
395 mandoc_msg(MANDOCERR_FI_TAB
, mdoc
->parse
,
396 ln
, pos
+ (int)(p
- cp
), NULL
);
400 pre_display(PRE_ARGS
)
402 struct roff_node
*node
;
404 if (n
->type
!= ROFFT_BLOCK
)
407 for (node
= mdoc
->last
->parent
; node
; node
= node
->parent
)
408 if (node
->type
== ROFFT_BLOCK
)
409 if (MDOC_Bd
== node
->tok
)
413 mandoc_vmsg(MANDOCERR_BD_NEST
,
414 mdoc
->parse
, n
->line
, n
->pos
,
415 "%s in Bd", mdoc_macronames
[n
->tok
]);
421 struct mdoc_argv
*argv
, *wa
;
423 enum mdocargt mdoclt
;
426 if (n
->type
!= ROFFT_BLOCK
)
430 * First figure out which kind of list to use: bind ourselves to
431 * the first mentioned list type and warn about any remaining
432 * ones. If we find no list type, we default to LIST_item.
435 wa
= (n
->args
== NULL
) ? NULL
: n
->args
->argv
;
436 mdoclt
= MDOC_ARG_MAX
;
437 for (i
= 0; n
->args
&& i
< (int)n
->args
->argc
; i
++) {
438 argv
= n
->args
->argv
+ i
;
441 /* Set list types. */
475 /* Set list arguments. */
477 if (n
->norm
->Bl
.comp
)
478 mandoc_msg(MANDOCERR_ARG_REP
,
479 mdoc
->parse
, argv
->line
,
480 argv
->pos
, "Bl -compact");
481 n
->norm
->Bl
.comp
= 1;
486 mandoc_msg(MANDOCERR_ARG_EMPTY
,
487 mdoc
->parse
, argv
->line
,
488 argv
->pos
, "Bl -width");
489 n
->norm
->Bl
.width
= "0n";
492 if (NULL
!= n
->norm
->Bl
.width
)
493 mandoc_vmsg(MANDOCERR_ARG_REP
,
494 mdoc
->parse
, argv
->line
,
495 argv
->pos
, "Bl -width %s",
497 rewrite_macro2len(argv
->value
);
498 n
->norm
->Bl
.width
= argv
->value
[0];
502 mandoc_msg(MANDOCERR_ARG_EMPTY
,
503 mdoc
->parse
, argv
->line
,
504 argv
->pos
, "Bl -offset");
507 if (NULL
!= n
->norm
->Bl
.offs
)
508 mandoc_vmsg(MANDOCERR_ARG_REP
,
509 mdoc
->parse
, argv
->line
,
510 argv
->pos
, "Bl -offset %s",
512 rewrite_macro2len(argv
->value
);
513 n
->norm
->Bl
.offs
= argv
->value
[0];
518 if (LIST__NONE
== lt
)
522 /* Check: multiple list types. */
524 if (LIST__NONE
!= n
->norm
->Bl
.type
) {
525 mandoc_vmsg(MANDOCERR_BL_REP
,
526 mdoc
->parse
, n
->line
, n
->pos
,
527 "Bl -%s", mdoc_argnames
[argv
->arg
]);
531 /* The list type should come first. */
533 if (n
->norm
->Bl
.width
||
536 mandoc_vmsg(MANDOCERR_BL_LATETYPE
,
537 mdoc
->parse
, n
->line
, n
->pos
, "Bl -%s",
538 mdoc_argnames
[n
->args
->argv
[0].arg
]);
540 n
->norm
->Bl
.type
= lt
;
541 if (LIST_column
== lt
) {
542 n
->norm
->Bl
.ncols
= argv
->sz
;
543 n
->norm
->Bl
.cols
= (void *)argv
->value
;
547 /* Allow lists to default to LIST_item. */
549 if (LIST__NONE
== n
->norm
->Bl
.type
) {
550 mandoc_msg(MANDOCERR_BL_NOTYPE
, mdoc
->parse
,
551 n
->line
, n
->pos
, "Bl");
552 n
->norm
->Bl
.type
= LIST_item
;
556 * Validate the width field. Some list types don't need width
557 * types and should be warned about them. Others should have it
558 * and must also be warned. Yet others have a default and need
562 switch (n
->norm
->Bl
.type
) {
564 if (NULL
== n
->norm
->Bl
.width
)
565 mandoc_msg(MANDOCERR_BL_NOWIDTH
, mdoc
->parse
,
566 n
->line
, n
->pos
, "Bl -tag");
577 if (n
->norm
->Bl
.width
)
578 mandoc_vmsg(MANDOCERR_BL_SKIPW
, mdoc
->parse
,
579 wa
->line
, wa
->pos
, "Bl -%s",
580 mdoc_argnames
[mdoclt
]);
587 if (NULL
== n
->norm
->Bl
.width
)
588 n
->norm
->Bl
.width
= "2n";
591 if (NULL
== n
->norm
->Bl
.width
)
592 n
->norm
->Bl
.width
= "3n";
603 struct mdoc_argv
*argv
;
607 pre_literal(mdoc
, n
);
609 if (n
->type
!= ROFFT_BLOCK
)
612 for (i
= 0; n
->args
&& i
< (int)n
->args
->argc
; i
++) {
613 argv
= n
->args
->argv
+ i
;
633 mandoc_msg(MANDOCERR_BD_FILE
, mdoc
->parse
,
634 n
->line
, n
->pos
, NULL
);
638 mandoc_msg(MANDOCERR_ARG_EMPTY
,
639 mdoc
->parse
, argv
->line
,
640 argv
->pos
, "Bd -offset");
643 if (NULL
!= n
->norm
->Bd
.offs
)
644 mandoc_vmsg(MANDOCERR_ARG_REP
,
645 mdoc
->parse
, argv
->line
,
646 argv
->pos
, "Bd -offset %s",
648 rewrite_macro2len(argv
->value
);
649 n
->norm
->Bd
.offs
= argv
->value
[0];
652 if (n
->norm
->Bd
.comp
)
653 mandoc_msg(MANDOCERR_ARG_REP
,
654 mdoc
->parse
, argv
->line
,
655 argv
->pos
, "Bd -compact");
656 n
->norm
->Bd
.comp
= 1;
662 if (DISP__NONE
== dt
)
665 if (DISP__NONE
== n
->norm
->Bd
.type
)
666 n
->norm
->Bd
.type
= dt
;
668 mandoc_vmsg(MANDOCERR_BD_REP
,
669 mdoc
->parse
, n
->line
, n
->pos
,
670 "Bd -%s", mdoc_argnames
[argv
->arg
]);
673 if (DISP__NONE
== n
->norm
->Bd
.type
) {
674 mandoc_msg(MANDOCERR_BD_NOTYPE
, mdoc
->parse
,
675 n
->line
, n
->pos
, "Bd");
676 n
->norm
->Bd
.type
= DISP_ragged
;
684 struct mdoc_argv
*argv
;
690 for (i
= 1; i
< n
->args
->argc
; i
++) {
691 argv
= n
->args
->argv
+ i
;
692 mandoc_vmsg(MANDOCERR_AN_REP
,
693 mdoc
->parse
, argv
->line
, argv
->pos
,
694 "An -%s", mdoc_argnames
[argv
->arg
]);
697 argv
= n
->args
->argv
;
698 if (argv
->arg
== MDOC_Split
)
699 n
->norm
->An
.auth
= AUTH_split
;
700 else if (argv
->arg
== MDOC_Nosplit
)
701 n
->norm
->An
.auth
= AUTH_nosplit
;
710 if (n
->args
&& 1 == n
->args
->argc
)
711 if (MDOC_Std
== n
->args
->argv
[0].arg
)
714 mandoc_msg(MANDOCERR_ARG_STD
, mdoc
->parse
,
715 n
->line
, n
->pos
, mdoc_macronames
[n
->tok
]);
719 pre_obsolete(PRE_ARGS
)
722 if (n
->type
== ROFFT_ELEM
|| n
->type
== ROFFT_BLOCK
)
723 mandoc_msg(MANDOCERR_MACRO_OBS
, mdoc
->parse
,
724 n
->line
, n
->pos
, mdoc_macronames
[n
->tok
]);
731 if (mdoc
->meta
.title
!= NULL
)
732 mandoc_msg(MANDOCERR_PROLOG_REP
, mdoc
->parse
,
733 n
->line
, n
->pos
, "Dt");
734 else if (mdoc
->meta
.os
!= NULL
)
735 mandoc_msg(MANDOCERR_PROLOG_ORDER
, mdoc
->parse
,
736 n
->line
, n
->pos
, "Dt after Os");
743 if (mdoc
->meta
.os
!= NULL
)
744 mandoc_msg(MANDOCERR_PROLOG_REP
, mdoc
->parse
,
745 n
->line
, n
->pos
, "Os");
746 else if (mdoc
->flags
& MDOC_PBODY
)
747 mandoc_msg(MANDOCERR_PROLOG_LATE
, mdoc
->parse
,
748 n
->line
, n
->pos
, "Os");
755 if (mdoc
->meta
.date
!= NULL
)
756 mandoc_msg(MANDOCERR_PROLOG_REP
, mdoc
->parse
,
757 n
->line
, n
->pos
, "Dd");
758 else if (mdoc
->flags
& MDOC_PBODY
)
759 mandoc_msg(MANDOCERR_PROLOG_LATE
, mdoc
->parse
,
760 n
->line
, n
->pos
, "Dd");
761 else if (mdoc
->meta
.title
!= NULL
)
762 mandoc_msg(MANDOCERR_PROLOG_ORDER
, mdoc
->parse
,
763 n
->line
, n
->pos
, "Dd after Dt");
764 else if (mdoc
->meta
.os
!= NULL
)
765 mandoc_msg(MANDOCERR_PROLOG_ORDER
, mdoc
->parse
,
766 n
->line
, n
->pos
, "Dd after Os");
772 struct roff_node
*np
, *nch
;
776 * Unlike other data pointers, these are "housed" by the HEAD
777 * element, which contains the goods.
781 if (np
->type
!= ROFFT_HEAD
)
784 assert(np
->parent
->type
== ROFFT_BLOCK
);
785 assert(MDOC_Bf
== np
->parent
->tok
);
787 /* Check the number of arguments. */
790 if (NULL
== np
->parent
->args
) {
792 mandoc_msg(MANDOCERR_BF_NOFONT
, mdoc
->parse
,
793 np
->line
, np
->pos
, "Bf");
799 mandoc_vmsg(MANDOCERR_ARG_EXCESS
, mdoc
->parse
,
800 nch
->line
, nch
->pos
, "Bf ... %s", nch
->string
);
802 /* Extract argument into data. */
804 if (np
->parent
->args
) {
805 arg
= np
->parent
->args
->argv
[0].arg
;
806 if (MDOC_Emphasis
== arg
)
807 np
->norm
->Bf
.font
= FONT_Em
;
808 else if (MDOC_Literal
== arg
)
809 np
->norm
->Bf
.font
= FONT_Li
;
810 else if (MDOC_Symbolic
== arg
)
811 np
->norm
->Bf
.font
= FONT_Sy
;
817 /* Extract parameter into data. */
819 if (0 == strcmp(np
->child
->string
, "Em"))
820 np
->norm
->Bf
.font
= FONT_Em
;
821 else if (0 == strcmp(np
->child
->string
, "Li"))
822 np
->norm
->Bf
.font
= FONT_Li
;
823 else if (0 == strcmp(np
->child
->string
, "Sy"))
824 np
->norm
->Bf
.font
= FONT_Sy
;
826 mandoc_vmsg(MANDOCERR_BF_BADFONT
, mdoc
->parse
,
827 np
->child
->line
, np
->child
->pos
,
828 "Bf %s", np
->child
->string
);
835 const char *stdlibname
;
838 n
= mdoc
->last
->child
;
839 assert(n
->type
== ROFFT_TEXT
);
841 if (NULL
== (stdlibname
= mdoc_a2lib(n
->string
)))
842 mandoc_asprintf(&libname
,
843 "library \\(Lq%s\\(Rq", n
->string
);
845 libname
= mandoc_strdup(stdlibname
);
854 const struct roff_node
*n
;
858 mandoc_vmsg(MANDOCERR_ARG_SKIP
,
859 mdoc
->parse
, n
->line
, n
->pos
,
860 "%s %s", mdoc_macronames
[n
->tok
],
865 post_fname(POST_ARGS
)
867 const struct roff_node
*n
;
871 n
= mdoc
->last
->child
;
872 pos
= strcspn(n
->string
, "()");
873 cp
= n
->string
+ pos
;
874 if ( ! (cp
[0] == '\0' || (cp
[0] == '(' && cp
[1] == '*')))
875 mandoc_msg(MANDOCERR_FN_PAREN
, mdoc
->parse
,
876 n
->line
, n
->pos
+ pos
, n
->string
);
890 const struct roff_node
*n
;
894 if (n
->type
!= ROFFT_HEAD
)
897 if (n
->child
== NULL
) {
898 mandoc_msg(MANDOCERR_FO_NOHEAD
, mdoc
->parse
,
899 n
->line
, n
->pos
, "Fo");
902 if (n
->child
!= n
->last
) {
903 mandoc_vmsg(MANDOCERR_ARG_EXCESS
, mdoc
->parse
,
904 n
->child
->next
->line
, n
->child
->next
->pos
,
905 "Fo ... %s", n
->child
->next
->string
);
906 while (n
->child
!= n
->last
)
907 mdoc_node_delete(mdoc
, n
->last
);
916 const struct roff_node
*n
;
919 for (n
= mdoc
->last
->child
; n
!= NULL
; n
= n
->next
) {
920 for (cp
= n
->string
; *cp
!= '\0'; cp
++) {
921 /* Ignore callbacks and alterations. */
922 if (*cp
== '(' || *cp
== '{')
926 mandoc_msg(MANDOCERR_FA_COMMA
, mdoc
->parse
,
927 n
->line
, n
->pos
+ (cp
- n
->string
),
937 const struct roff_node
*n
;
940 * The Vt macro comes in both ELEM and BLOCK form, both of which
941 * have different syntaxes (yet more context-sensitive
942 * behaviour). ELEM types must have a child, which is already
943 * guaranteed by the in_line parsing routine; BLOCK types,
944 * specifically the BODY, should only have TEXT children.
947 if (mdoc
->last
->type
!= ROFFT_BODY
)
950 for (n
= mdoc
->last
->child
; n
; n
= n
->next
)
951 if (n
->type
!= ROFFT_TEXT
)
952 mandoc_msg(MANDOCERR_VT_CHILD
, mdoc
->parse
,
953 n
->line
, n
->pos
, mdoc_macronames
[n
->tok
]);
963 if (n
->last
!= NULL
&&
964 (n
->last
->tok
== MDOC_Pp
||
965 n
->last
->tok
== MDOC_Lp
))
966 mdoc_node_relink(mdoc
, n
->last
);
968 if (NULL
!= mdoc
->meta
.name
)
971 mdoc_deroff(&mdoc
->meta
.name
, n
);
973 if (NULL
== mdoc
->meta
.name
)
974 mandoc_msg(MANDOCERR_NM_NONAME
, mdoc
->parse
,
975 n
->line
, n
->pos
, "Nm");
985 if (n
->type
!= ROFFT_BODY
)
988 if (n
->child
== NULL
)
989 mandoc_msg(MANDOCERR_ND_EMPTY
, mdoc
->parse
,
990 n
->line
, n
->pos
, "Nd");
1002 if (n
->type
!= ROFFT_BODY
)
1005 if (n
->child
== NULL
)
1006 mandoc_msg(MANDOCERR_BLK_EMPTY
, mdoc
->parse
,
1007 n
->line
, n
->pos
, "D1");
1013 post_literal(POST_ARGS
)
1015 struct roff_node
*n
;
1019 if (n
->type
!= ROFFT_BODY
)
1022 if (n
->child
== NULL
)
1023 mandoc_msg(MANDOCERR_BLK_EMPTY
, mdoc
->parse
,
1024 n
->line
, n
->pos
, mdoc_macronames
[n
->tok
]);
1026 if (n
->tok
== MDOC_Bd
&&
1027 n
->norm
->Bd
.type
!= DISP_literal
&&
1028 n
->norm
->Bd
.type
!= DISP_unfilled
)
1031 mdoc
->flags
&= ~MDOC_LITERAL
;
1035 post_defaults(POST_ARGS
)
1037 struct roff_node
*nn
;
1040 * The `Ar' defaults to "file ..." if no value is provided as an
1041 * argument; the `Mt' and `Pa' macros use "~"; the `Li' just
1042 * gets an empty string.
1045 if (mdoc
->last
->child
)
1049 mdoc
->next
= MDOC_NEXT_CHILD
;
1053 mdoc_word_alloc(mdoc
, nn
->line
, nn
->pos
, "file");
1054 mdoc_word_alloc(mdoc
, nn
->line
, nn
->pos
, "...");
1059 mdoc_word_alloc(mdoc
, nn
->line
, nn
->pos
, "~");
1071 struct roff_node
*n
;
1072 const char *std_att
;
1076 if (n
->child
== NULL
) {
1077 mdoc
->next
= MDOC_NEXT_CHILD
;
1078 mdoc_word_alloc(mdoc
, n
->line
, n
->pos
, "AT&T UNIX");
1084 * If we have a child, look it up in the standard keys. If a
1085 * key exist, use that instead of the child; if it doesn't,
1086 * prefix "AT&T UNIX " to the existing data.
1090 assert(n
->type
== ROFFT_TEXT
);
1091 if (NULL
== (std_att
= mdoc_a2att(n
->string
))) {
1092 mandoc_vmsg(MANDOCERR_AT_BAD
, mdoc
->parse
,
1093 n
->line
, n
->pos
, "At %s", n
->string
);
1094 mandoc_asprintf(&att
, "AT&T UNIX %s", n
->string
);
1096 att
= mandoc_strdup(std_att
);
1105 struct roff_node
*np
, *nch
;
1109 if (np
->norm
->An
.auth
== AUTH__NONE
) {
1111 mandoc_msg(MANDOCERR_MACRO_EMPTY
, mdoc
->parse
,
1112 np
->line
, np
->pos
, "An");
1113 } else if (nch
!= NULL
)
1114 mandoc_vmsg(MANDOCERR_ARG_EXCESS
, mdoc
->parse
,
1115 nch
->line
, nch
->pos
, "An ... %s", nch
->string
);
1122 if (mdoc
->last
->type
== ROFFT_BLOCK
)
1123 mdoc
->last
->norm
->Es
= mdoc
->last_es
;
1130 mdoc
->last_es
= mdoc
->last
;
1136 struct roff_node
*nbl
, *nit
, *nch
;
1141 if (nit
->type
!= ROFFT_BLOCK
)
1144 nbl
= nit
->parent
->parent
;
1145 lt
= nbl
->norm
->Bl
.type
;
1157 if (nit
->head
->child
== NULL
)
1158 mandoc_vmsg(MANDOCERR_IT_NOHEAD
,
1159 mdoc
->parse
, nit
->line
, nit
->pos
,
1161 mdoc_argnames
[nbl
->args
->argv
[0].arg
]);
1170 if (nit
->body
== NULL
|| nit
->body
->child
== NULL
)
1171 mandoc_vmsg(MANDOCERR_IT_NOBODY
,
1172 mdoc
->parse
, nit
->line
, nit
->pos
,
1174 mdoc_argnames
[nbl
->args
->argv
[0].arg
]);
1177 if (nit
->head
->child
!= NULL
)
1178 mandoc_vmsg(MANDOCERR_ARG_SKIP
,
1179 mdoc
->parse
, nit
->line
, nit
->pos
,
1180 "It %s", nit
->head
->child
->string
);
1183 cols
= (int)nbl
->norm
->Bl
.ncols
;
1185 assert(nit
->head
->child
== NULL
);
1187 for (i
= 0, nch
= nit
->child
; nch
; nch
= nch
->next
)
1188 if (nch
->type
== ROFFT_BODY
)
1191 if (i
< cols
|| i
> cols
+ 1)
1192 mandoc_vmsg(MANDOCERR_BL_COL
,
1193 mdoc
->parse
, nit
->line
, nit
->pos
,
1194 "%d columns, %d cells", cols
, i
);
1202 post_bl_block(POST_ARGS
)
1204 struct roff_node
*n
, *ni
, *nc
;
1207 * These are fairly complicated, so we've broken them into two
1208 * functions. post_bl_block_tag() is called when a -tag is
1209 * specified, but no -width (it must be guessed). The second
1210 * when a -width is specified (macro indicators must be
1211 * rewritten into real lengths).
1216 if (LIST_tag
== n
->norm
->Bl
.type
&&
1217 NULL
== n
->norm
->Bl
.width
) {
1218 post_bl_block_tag(mdoc
);
1219 assert(n
->norm
->Bl
.width
);
1222 for (ni
= n
->body
->child
; ni
; ni
= ni
->next
) {
1223 if (NULL
== ni
->body
)
1225 nc
= ni
->body
->last
;
1226 while (NULL
!= nc
) {
1238 if (NULL
== ni
->next
) {
1239 mandoc_msg(MANDOCERR_PAR_MOVE
,
1240 mdoc
->parse
, nc
->line
, nc
->pos
,
1241 mdoc_macronames
[nc
->tok
]);
1242 mdoc_node_relink(mdoc
, nc
);
1243 } else if (0 == n
->norm
->Bl
.comp
&&
1244 LIST_column
!= n
->norm
->Bl
.type
) {
1245 mandoc_vmsg(MANDOCERR_PAR_SKIP
,
1246 mdoc
->parse
, nc
->line
, nc
->pos
,
1248 mdoc_macronames
[nc
->tok
]);
1249 mdoc_node_delete(mdoc
, nc
);
1252 nc
= ni
->body
->last
;
1258 * If the argument of -offset or -width is a macro,
1259 * replace it with the associated default width.
1262 rewrite_macro2len(char **arg
)
1269 else if ( ! strcmp(*arg
, "Ds"))
1271 else if ((tok
= mdoc_hash_find(*arg
)) == MDOC_MAX
)
1274 width
= macro2len(tok
);
1277 mandoc_asprintf(arg
, "%zun", width
);
1281 post_bl_block_tag(POST_ARGS
)
1283 struct roff_node
*n
, *nn
;
1289 * Calculate the -width for a `Bl -tag' list if it hasn't been
1290 * provided. Uses the first head macro. NOTE AGAIN: this is
1291 * ONLY if the -width argument has NOT been provided. See
1292 * rewrite_macro2len() for converting the -width string.
1298 for (nn
= n
->body
->child
; nn
; nn
= nn
->next
) {
1299 if (MDOC_It
!= nn
->tok
)
1302 assert(nn
->type
== ROFFT_BLOCK
);
1303 nn
= nn
->head
->child
;
1308 if (nn
->type
== ROFFT_TEXT
) {
1309 sz
= strlen(nn
->string
) + 1;
1313 if (0 != (ssz
= macro2len(nn
->tok
)))
1319 /* Defaults to ten ens. */
1321 (void)snprintf(buf
, sizeof(buf
), "%un", (unsigned int)sz
);
1324 * We have to dynamically add this to the macro's argument list.
1325 * We're guaranteed that a MDOC_Width doesn't already exist.
1329 i
= (int)(n
->args
->argc
)++;
1331 n
->args
->argv
= mandoc_reallocarray(n
->args
->argv
,
1332 n
->args
->argc
, sizeof(struct mdoc_argv
));
1334 n
->args
->argv
[i
].arg
= MDOC_Width
;
1335 n
->args
->argv
[i
].line
= n
->line
;
1336 n
->args
->argv
[i
].pos
= n
->pos
;
1337 n
->args
->argv
[i
].sz
= 1;
1338 n
->args
->argv
[i
].value
= mandoc_malloc(sizeof(char *));
1339 n
->args
->argv
[i
].value
[0] = mandoc_strdup(buf
);
1341 /* Set our width! */
1342 n
->norm
->Bl
.width
= n
->args
->argv
[i
].value
[0];
1346 post_bl_head(POST_ARGS
)
1348 struct roff_node
*nbl
, *nh
, *nch
, *nnext
;
1349 struct mdoc_argv
*argv
;
1354 if (nh
->norm
->Bl
.type
!= LIST_column
) {
1355 if ((nch
= nh
->child
) == NULL
)
1357 mandoc_vmsg(MANDOCERR_ARG_EXCESS
, mdoc
->parse
,
1358 nch
->line
, nch
->pos
, "Bl ... %s", nch
->string
);
1359 while (nch
!= NULL
) {
1360 mdoc_node_delete(mdoc
, nch
);
1367 * Append old-style lists, where the column width specifiers
1368 * trail as macro parameters, to the new-style ("normal-form")
1369 * lists where they're argument values following -column.
1372 if (nh
->child
== NULL
)
1376 for (j
= 0; j
< (int)nbl
->args
->argc
; j
++)
1377 if (nbl
->args
->argv
[j
].arg
== MDOC_Column
)
1380 assert(j
< (int)nbl
->args
->argc
);
1383 * Accommodate for new-style groff column syntax. Shuffle the
1384 * child nodes, all of which must be TEXT, as arguments for the
1385 * column field. Then, delete the head children.
1388 argv
= nbl
->args
->argv
+ j
;
1390 argv
->sz
+= nh
->nchild
;
1391 argv
->value
= mandoc_reallocarray(argv
->value
,
1392 argv
->sz
, sizeof(char *));
1394 nh
->norm
->Bl
.ncols
= argv
->sz
;
1395 nh
->norm
->Bl
.cols
= (void *)argv
->value
;
1397 for (nch
= nh
->child
; nch
!= NULL
; nch
= nnext
) {
1398 argv
->value
[i
++] = nch
->string
;
1401 mdoc_node_delete(NULL
, nch
);
1410 struct roff_node
*nparent
, *nprev
; /* of the Bl block */
1411 struct roff_node
*nblock
, *nbody
; /* of the Bl */
1412 struct roff_node
*nchild
, *nnext
; /* of the Bl body */
1415 switch (nbody
->type
) {
1417 post_bl_block(mdoc
);
1428 nchild
= nbody
->child
;
1429 if (nchild
== NULL
) {
1430 mandoc_msg(MANDOCERR_BLK_EMPTY
, mdoc
->parse
,
1431 nbody
->line
, nbody
->pos
, "Bl");
1434 while (nchild
!= NULL
) {
1435 if (nchild
->tok
== MDOC_It
||
1436 (nchild
->tok
== MDOC_Sm
&&
1437 nchild
->next
!= NULL
&&
1438 nchild
->next
->tok
== MDOC_It
)) {
1439 nchild
= nchild
->next
;
1443 mandoc_msg(MANDOCERR_BL_MOVE
, mdoc
->parse
,
1444 nchild
->line
, nchild
->pos
,
1445 mdoc_macronames
[nchild
->tok
]);
1448 * Move the node out of the Bl block.
1449 * First, collect all required node pointers.
1452 nblock
= nbody
->parent
;
1453 nprev
= nblock
->prev
;
1454 nparent
= nblock
->parent
;
1455 nnext
= nchild
->next
;
1458 * Unlink this child.
1461 assert(NULL
== nchild
->prev
);
1462 if (0 == --nbody
->nchild
) {
1463 nbody
->child
= NULL
;
1465 assert(NULL
== nnext
);
1467 nbody
->child
= nnext
;
1472 * Relink this child.
1475 nchild
->parent
= nparent
;
1476 nchild
->prev
= nprev
;
1477 nchild
->next
= nblock
;
1479 nblock
->prev
= nchild
;
1482 nparent
->child
= nchild
;
1484 nprev
->next
= nchild
;
1493 struct roff_node
*n
;
1497 if (n
->type
== ROFFT_BLOCK
&& n
->body
->child
== NULL
) {
1498 mandoc_msg(MANDOCERR_BLK_EMPTY
,
1499 mdoc
->parse
, n
->line
, n
->pos
, "Bk");
1500 mdoc_node_delete(mdoc
, n
);
1505 post_sm(struct mdoc
*mdoc
)
1507 struct roff_node
*nch
;
1509 nch
= mdoc
->last
->child
;
1512 mdoc
->flags
^= MDOC_SMOFF
;
1516 assert(nch
->type
== ROFFT_TEXT
);
1518 if ( ! strcmp(nch
->string
, "on")) {
1519 mdoc
->flags
&= ~MDOC_SMOFF
;
1522 if ( ! strcmp(nch
->string
, "off")) {
1523 mdoc
->flags
|= MDOC_SMOFF
;
1527 mandoc_vmsg(MANDOCERR_SM_BAD
,
1528 mdoc
->parse
, nch
->line
, nch
->pos
,
1529 "%s %s", mdoc_macronames
[mdoc
->last
->tok
], nch
->string
);
1530 mdoc_node_relink(mdoc
, nch
);
1535 post_root(POST_ARGS
)
1537 struct roff_node
*n
;
1539 /* Add missing prologue data. */
1541 if (mdoc
->meta
.date
== NULL
)
1542 mdoc
->meta
.date
= mdoc
->quick
?
1544 mandoc_normdate(mdoc
->parse
, NULL
, 0, 0);
1546 if (mdoc
->meta
.title
== NULL
) {
1547 mandoc_msg(MANDOCERR_DT_NOTITLE
,
1548 mdoc
->parse
, 0, 0, "EOF");
1549 mdoc
->meta
.title
= mandoc_strdup("UNTITLED");
1552 if (mdoc
->meta
.vol
== NULL
)
1553 mdoc
->meta
.vol
= mandoc_strdup("LOCAL");
1555 if (mdoc
->meta
.os
== NULL
) {
1556 mandoc_msg(MANDOCERR_OS_MISSING
,
1557 mdoc
->parse
, 0, 0, NULL
);
1558 mdoc
->meta
.os
= mandoc_strdup("");
1561 /* Check that we begin with a proper `Sh'. */
1563 n
= mdoc
->first
->child
;
1564 while (n
!= NULL
&& mdoc_macros
[n
->tok
].flags
& MDOC_PROLOGUE
)
1568 mandoc_msg(MANDOCERR_DOC_EMPTY
, mdoc
->parse
, 0, 0, NULL
);
1569 else if (n
->tok
!= MDOC_Sh
)
1570 mandoc_msg(MANDOCERR_SEC_BEFORE
, mdoc
->parse
,
1571 n
->line
, n
->pos
, mdoc_macronames
[n
->tok
]);
1577 struct roff_node
*n
, *nch
;
1583 assert(nch
->type
== ROFFT_TEXT
);
1585 if (NULL
== (p
= mdoc_a2st(nch
->string
))) {
1586 mandoc_vmsg(MANDOCERR_ST_BAD
, mdoc
->parse
,
1587 nch
->line
, nch
->pos
, "St %s", nch
->string
);
1588 mdoc_node_delete(mdoc
, n
);
1591 nch
->string
= mandoc_strdup(p
);
1598 struct roff_node
*np
, *nch
, *next
, *prev
;
1603 if (np
->type
!= ROFFT_BODY
)
1606 if (np
->child
== NULL
) {
1607 mandoc_msg(MANDOCERR_RS_EMPTY
, mdoc
->parse
,
1608 np
->line
, np
->pos
, "Rs");
1613 * The full `Rs' block needs special handling to order the
1614 * sub-elements according to `rsord'. Pick through each element
1615 * and correctly order it. This is an insertion sort.
1619 for (nch
= np
->child
->next
; nch
!= NULL
; nch
= next
) {
1620 /* Determine order number of this child. */
1621 for (i
= 0; i
< RSORD_MAX
; i
++)
1622 if (rsord
[i
] == nch
->tok
)
1625 if (i
== RSORD_MAX
) {
1626 mandoc_msg(MANDOCERR_RS_BAD
,
1627 mdoc
->parse
, nch
->line
, nch
->pos
,
1628 mdoc_macronames
[nch
->tok
]);
1630 } else if (nch
->tok
== MDOC__J
|| nch
->tok
== MDOC__B
)
1631 np
->norm
->Rs
.quote_T
++;
1634 * Remove this child from the chain. This somewhat
1635 * repeats mdoc_node_unlink(), but since we're
1636 * just re-ordering, there's no need for the
1637 * full unlink process.
1640 if ((next
= nch
->next
) != NULL
)
1641 next
->prev
= nch
->prev
;
1643 if ((prev
= nch
->prev
) != NULL
)
1644 prev
->next
= nch
->next
;
1646 nch
->prev
= nch
->next
= NULL
;
1649 * Scan back until we reach a node that's
1650 * to be ordered before this child.
1653 for ( ; prev
; prev
= prev
->prev
) {
1654 /* Determine order of `prev'. */
1655 for (j
= 0; j
< RSORD_MAX
; j
++)
1656 if (rsord
[j
] == prev
->tok
)
1666 * Set this child back into its correct place
1667 * in front of the `prev' node.
1673 np
->child
->prev
= nch
;
1674 nch
->next
= np
->child
;
1678 prev
->next
->prev
= nch
;
1679 nch
->next
= prev
->next
;
1686 * For some arguments of some macros,
1687 * convert all breakable hyphens into ASCII_HYPH.
1690 post_hyph(POST_ARGS
)
1692 struct roff_node
*nch
;
1695 for (nch
= mdoc
->last
->child
; nch
!= NULL
; nch
= nch
->next
) {
1696 if (nch
->type
!= ROFFT_TEXT
)
1701 while (*(++cp
) != '\0')
1703 isalpha((unsigned char)cp
[-1]) &&
1704 isalpha((unsigned char)cp
[1]))
1713 if (MDOC_LINE
& mdoc
->last
->flags
)
1714 mandoc_msg(MANDOCERR_NS_SKIP
, mdoc
->parse
,
1715 mdoc
->last
->line
, mdoc
->last
->pos
, NULL
);
1724 switch (mdoc
->last
->type
) {
1729 switch (mdoc
->lastsec
) {
1734 post_sh_see_also(mdoc
);
1737 post_sh_authors(mdoc
);
1749 post_sh_name(POST_ARGS
)
1751 struct roff_node
*n
;
1756 for (n
= mdoc
->last
->child
; n
!= NULL
; n
= n
->next
) {
1763 if (n
->next
!= NULL
)
1764 mandoc_msg(MANDOCERR_NAMESEC_ND
,
1765 mdoc
->parse
, n
->line
, n
->pos
, NULL
);
1772 mandoc_msg(MANDOCERR_NAMESEC_BAD
, mdoc
->parse
,
1773 n
->line
, n
->pos
, mdoc_macronames
[n
->tok
]);
1779 mandoc_msg(MANDOCERR_NAMESEC_NONM
, mdoc
->parse
,
1780 mdoc
->last
->line
, mdoc
->last
->pos
, NULL
);
1782 mandoc_msg(MANDOCERR_NAMESEC_NOND
, mdoc
->parse
,
1783 mdoc
->last
->line
, mdoc
->last
->pos
, NULL
);
1787 post_sh_see_also(POST_ARGS
)
1789 const struct roff_node
*n
;
1790 const char *name
, *sec
;
1791 const char *lastname
, *lastsec
, *lastpunct
;
1794 n
= mdoc
->last
->child
;
1795 lastname
= lastsec
= lastpunct
= NULL
;
1797 if (n
->tok
!= MDOC_Xr
|| n
->nchild
< 2)
1800 /* Process one .Xr node. */
1802 name
= n
->child
->string
;
1803 sec
= n
->child
->next
->string
;
1804 if (lastsec
!= NULL
) {
1805 if (lastpunct
[0] != ',' || lastpunct
[1] != '\0')
1806 mandoc_vmsg(MANDOCERR_XR_PUNCT
,
1807 mdoc
->parse
, n
->line
, n
->pos
,
1808 "%s before %s(%s)", lastpunct
,
1810 cmp
= strcmp(lastsec
, sec
);
1812 mandoc_vmsg(MANDOCERR_XR_ORDER
,
1813 mdoc
->parse
, n
->line
, n
->pos
,
1814 "%s(%s) after %s(%s)", name
,
1815 sec
, lastname
, lastsec
);
1816 else if (cmp
== 0 &&
1817 strcasecmp(lastname
, name
) > 0)
1818 mandoc_vmsg(MANDOCERR_XR_ORDER
,
1819 mdoc
->parse
, n
->line
, n
->pos
,
1820 "%s after %s", name
, lastname
);
1825 /* Process the following node. */
1830 if (n
->tok
== MDOC_Xr
) {
1834 if (n
->type
!= ROFFT_TEXT
)
1836 for (name
= n
->string
; *name
!= '\0'; name
++)
1837 if (isalpha((const unsigned char)*name
))
1839 lastpunct
= n
->string
;
1840 if (n
->next
== NULL
)
1841 mandoc_vmsg(MANDOCERR_XR_PUNCT
, mdoc
->parse
,
1842 n
->line
, n
->pos
, "%s after %s(%s)",
1843 lastpunct
, lastname
, lastsec
);
1849 child_an(const struct roff_node
*n
)
1852 for (n
= n
->child
; n
!= NULL
; n
= n
->next
)
1853 if ((n
->tok
== MDOC_An
&& n
->nchild
) || child_an(n
))
1859 post_sh_authors(POST_ARGS
)
1862 if ( ! child_an(mdoc
->last
))
1863 mandoc_msg(MANDOCERR_AN_MISSING
, mdoc
->parse
,
1864 mdoc
->last
->line
, mdoc
->last
->pos
, NULL
);
1868 post_sh_head(POST_ARGS
)
1870 struct roff_node
*n
;
1871 const char *goodsec
;
1876 * Process a new section. Sections are either "named" or
1877 * "custom". Custom sections are user-defined, while named ones
1878 * follow a conventional order and may only appear in certain
1884 mdoc_deroff(&secname
, mdoc
->last
);
1885 sec
= NULL
== secname
? SEC_CUSTOM
: a2sec(secname
);
1887 /* The NAME should be first. */
1889 if (SEC_NAME
!= sec
&& SEC_NONE
== mdoc
->lastnamed
)
1890 mandoc_vmsg(MANDOCERR_NAMESEC_FIRST
, mdoc
->parse
,
1891 mdoc
->last
->line
, mdoc
->last
->pos
,
1894 /* The SYNOPSIS gets special attention in other areas. */
1896 if (SEC_SYNOPSIS
== sec
) {
1897 roff_setreg(mdoc
->roff
, "nS", 1, '=');
1898 mdoc
->flags
|= MDOC_SYNOPSIS
;
1900 roff_setreg(mdoc
->roff
, "nS", 0, '=');
1901 mdoc
->flags
&= ~MDOC_SYNOPSIS
;
1904 /* Mark our last section. */
1906 mdoc
->lastsec
= sec
;
1909 * Set the section attribute for the current HEAD, for its
1910 * parent BLOCK, and for the HEAD children; the latter can
1911 * only be TEXT nodes, so no recursion is needed.
1912 * For other blocks and elements, including .Sh BODY, this is
1913 * done when allocating the node data structures, but for .Sh
1914 * BLOCK and HEAD, the section is still unknown at that time.
1917 mdoc
->last
->parent
->sec
= sec
;
1918 mdoc
->last
->sec
= sec
;
1919 for (n
= mdoc
->last
->child
; n
; n
= n
->next
)
1922 /* We don't care about custom sections after this. */
1924 if (SEC_CUSTOM
== sec
) {
1930 * Check whether our non-custom section is being repeated or is
1934 if (sec
== mdoc
->lastnamed
)
1935 mandoc_vmsg(MANDOCERR_SEC_REP
, mdoc
->parse
,
1936 mdoc
->last
->line
, mdoc
->last
->pos
,
1939 if (sec
< mdoc
->lastnamed
)
1940 mandoc_vmsg(MANDOCERR_SEC_ORDER
, mdoc
->parse
,
1941 mdoc
->last
->line
, mdoc
->last
->pos
,
1944 /* Mark the last named section. */
1946 mdoc
->lastnamed
= sec
;
1948 /* Check particular section/manual conventions. */
1950 if (mdoc
->meta
.msec
== NULL
) {
1958 if (*mdoc
->meta
.msec
== '4')
1960 goodsec
= "2, 3, 4, 9";
1962 case SEC_RETURN_VALUES
:
1965 if (*mdoc
->meta
.msec
== '2')
1967 if (*mdoc
->meta
.msec
== '3')
1969 if (NULL
== goodsec
)
1970 goodsec
= "2, 3, 9";
1973 if (*mdoc
->meta
.msec
== '9')
1975 if (NULL
== goodsec
)
1977 mandoc_vmsg(MANDOCERR_SEC_MSEC
, mdoc
->parse
,
1978 mdoc
->last
->line
, mdoc
->last
->pos
,
1979 "Sh %s for %s only", secname
, goodsec
);
1988 post_ignpar(POST_ARGS
)
1990 struct roff_node
*np
;
1992 switch (mdoc
->last
->type
) {
2002 if (NULL
!= (np
= mdoc
->last
->child
))
2003 if (MDOC_Pp
== np
->tok
|| MDOC_Lp
== np
->tok
) {
2004 mandoc_vmsg(MANDOCERR_PAR_SKIP
,
2005 mdoc
->parse
, np
->line
, np
->pos
,
2006 "%s after %s", mdoc_macronames
[np
->tok
],
2007 mdoc_macronames
[mdoc
->last
->tok
]);
2008 mdoc_node_delete(mdoc
, np
);
2011 if (NULL
!= (np
= mdoc
->last
->last
))
2012 if (MDOC_Pp
== np
->tok
|| MDOC_Lp
== np
->tok
) {
2013 mandoc_vmsg(MANDOCERR_PAR_SKIP
, mdoc
->parse
,
2014 np
->line
, np
->pos
, "%s at the end of %s",
2015 mdoc_macronames
[np
->tok
],
2016 mdoc_macronames
[mdoc
->last
->tok
]);
2017 mdoc_node_delete(mdoc
, np
);
2025 if (NULL
== mdoc
->last
)
2027 if (n
->type
!= ROFFT_ELEM
&& n
->type
!= ROFFT_BLOCK
)
2031 * Don't allow prior `Lp' or `Pp' prior to a paragraph-type
2032 * block: `Lp', `Pp', or non-compact `Bd' or `Bl'.
2035 if (MDOC_Pp
!= mdoc
->last
->tok
&&
2036 MDOC_Lp
!= mdoc
->last
->tok
&&
2037 MDOC_br
!= mdoc
->last
->tok
)
2039 if (MDOC_Bl
== n
->tok
&& n
->norm
->Bl
.comp
)
2041 if (MDOC_Bd
== n
->tok
&& n
->norm
->Bd
.comp
)
2043 if (MDOC_It
== n
->tok
&& n
->parent
->norm
->Bl
.comp
)
2046 mandoc_vmsg(MANDOCERR_PAR_SKIP
, mdoc
->parse
,
2047 mdoc
->last
->line
, mdoc
->last
->pos
,
2048 "%s before %s", mdoc_macronames
[mdoc
->last
->tok
],
2049 mdoc_macronames
[n
->tok
]);
2050 mdoc_node_delete(mdoc
, mdoc
->last
);
2056 struct roff_node
*np
;
2060 if (np
->tok
== MDOC_sp
) {
2062 mandoc_vmsg(MANDOCERR_ARG_EXCESS
, mdoc
->parse
,
2063 np
->child
->next
->line
, np
->child
->next
->pos
,
2064 "sp ... %s", np
->child
->next
->string
);
2065 } else if (np
->child
!= NULL
)
2066 mandoc_vmsg(MANDOCERR_ARG_SKIP
,
2067 mdoc
->parse
, np
->line
, np
->pos
, "%s %s",
2068 mdoc_macronames
[np
->tok
], np
->child
->string
);
2070 if (NULL
== (np
= mdoc
->last
->prev
)) {
2071 np
= mdoc
->last
->parent
;
2072 if (MDOC_Sh
!= np
->tok
&& MDOC_Ss
!= np
->tok
)
2074 } else if (MDOC_Pp
!= np
->tok
&& MDOC_Lp
!= np
->tok
&&
2075 (MDOC_br
!= mdoc
->last
->tok
||
2076 (MDOC_sp
!= np
->tok
&& MDOC_br
!= np
->tok
)))
2079 mandoc_vmsg(MANDOCERR_PAR_SKIP
, mdoc
->parse
,
2080 mdoc
->last
->line
, mdoc
->last
->pos
,
2081 "%s after %s", mdoc_macronames
[mdoc
->last
->tok
],
2082 mdoc_macronames
[np
->tok
]);
2083 mdoc_node_delete(mdoc
, mdoc
->last
);
2087 pre_literal(PRE_ARGS
)
2090 pre_display(mdoc
, n
);
2092 if (n
->type
!= ROFFT_BODY
)
2096 * The `Dl' (note "el" not "one") and `Bd -literal' and `Bd
2097 * -unfilled' macros set MDOC_LITERAL on entrance to the body.
2102 mdoc
->flags
|= MDOC_LITERAL
;
2105 if (DISP_literal
== n
->norm
->Bd
.type
)
2106 mdoc
->flags
|= MDOC_LITERAL
;
2107 if (DISP_unfilled
== n
->norm
->Bd
.type
)
2108 mdoc
->flags
|= MDOC_LITERAL
;
2119 struct roff_node
*n
;
2122 if (mdoc
->meta
.date
)
2123 free(mdoc
->meta
.date
);
2126 if (NULL
== n
->child
|| '\0' == n
->child
->string
[0]) {
2127 mdoc
->meta
.date
= mdoc
->quick
? mandoc_strdup("") :
2128 mandoc_normdate(mdoc
->parse
, NULL
, n
->line
, n
->pos
);
2133 mdoc_deroff(&datestr
, n
);
2135 mdoc
->meta
.date
= datestr
;
2137 mdoc
->meta
.date
= mandoc_normdate(mdoc
->parse
,
2138 datestr
, n
->line
, n
->pos
);
2142 mdoc_node_delete(mdoc
, n
);
2148 struct roff_node
*nn
, *n
;
2154 free(mdoc
->meta
.title
);
2155 free(mdoc
->meta
.msec
);
2156 free(mdoc
->meta
.vol
);
2157 free(mdoc
->meta
.arch
);
2159 mdoc
->meta
.title
= NULL
;
2160 mdoc
->meta
.msec
= NULL
;
2161 mdoc
->meta
.vol
= NULL
;
2162 mdoc
->meta
.arch
= NULL
;
2164 /* Mandatory first argument: title. */
2167 if (nn
== NULL
|| *nn
->string
== '\0') {
2168 mandoc_msg(MANDOCERR_DT_NOTITLE
,
2169 mdoc
->parse
, n
->line
, n
->pos
, "Dt");
2170 mdoc
->meta
.title
= mandoc_strdup("UNTITLED");
2172 mdoc
->meta
.title
= mandoc_strdup(nn
->string
);
2174 /* Check that all characters are uppercase. */
2176 for (p
= nn
->string
; *p
!= '\0'; p
++)
2177 if (islower((unsigned char)*p
)) {
2178 mandoc_vmsg(MANDOCERR_TITLE_CASE
,
2179 mdoc
->parse
, nn
->line
,
2180 nn
->pos
+ (p
- nn
->string
),
2181 "Dt %s", nn
->string
);
2186 /* Mandatory second argument: section. */
2192 mandoc_vmsg(MANDOCERR_MSEC_MISSING
,
2193 mdoc
->parse
, n
->line
, n
->pos
,
2194 "Dt %s", mdoc
->meta
.title
);
2195 mdoc
->meta
.vol
= mandoc_strdup("LOCAL");
2196 goto out
; /* msec and arch remain NULL. */
2199 mdoc
->meta
.msec
= mandoc_strdup(nn
->string
);
2201 /* Infer volume title from section number. */
2203 cp
= mandoc_a2msec(nn
->string
);
2205 mandoc_vmsg(MANDOCERR_MSEC_BAD
, mdoc
->parse
,
2206 nn
->line
, nn
->pos
, "Dt ... %s", nn
->string
);
2207 mdoc
->meta
.vol
= mandoc_strdup(nn
->string
);
2209 mdoc
->meta
.vol
= mandoc_strdup(cp
);
2211 /* Optional third argument: architecture. */
2213 if ((nn
= nn
->next
) == NULL
)
2216 for (p
= nn
->string
; *p
!= '\0'; p
++)
2217 *p
= tolower((unsigned char)*p
);
2218 mdoc
->meta
.arch
= mandoc_strdup(nn
->string
);
2220 /* Ignore fourth and later arguments. */
2222 if ((nn
= nn
->next
) != NULL
)
2223 mandoc_vmsg(MANDOCERR_ARG_EXCESS
, mdoc
->parse
,
2224 nn
->line
, nn
->pos
, "Dt ... %s", nn
->string
);
2227 mdoc_node_delete(mdoc
, n
);
2233 struct roff_node
*n
;
2236 * Make `Bx's second argument always start with an uppercase
2237 * letter. Groff checks if it's an "accepted" term, but we just
2238 * uppercase blindly.
2241 n
= mdoc
->last
->child
;
2242 if (n
&& NULL
!= (n
= n
->next
))
2243 *n
->string
= (char)toupper((unsigned char)*n
->string
);
2250 struct utsname utsname
;
2251 static char *defbuf
;
2253 struct roff_node
*n
;
2258 * Set the operating system by way of the `Os' macro.
2259 * The order of precedence is:
2260 * 1. the argument of the `Os' macro, unless empty
2261 * 2. the -Ios=foo command line argument, if provided
2262 * 3. -DOSNAME="\"foo\"", if provided during compilation
2263 * 4. "sysname release" from uname(3)
2266 free(mdoc
->meta
.os
);
2267 mdoc
->meta
.os
= NULL
;
2268 mdoc_deroff(&mdoc
->meta
.os
, n
);
2273 mdoc
->meta
.os
= mandoc_strdup(mdoc
->defos
);
2278 mdoc
->meta
.os
= mandoc_strdup(OSNAME
);
2280 if (NULL
== defbuf
) {
2281 if (-1 == uname(&utsname
)) {
2282 mandoc_msg(MANDOCERR_OS_UNAME
, mdoc
->parse
,
2283 n
->line
, n
->pos
, "Os");
2284 defbuf
= mandoc_strdup("UNKNOWN");
2286 mandoc_asprintf(&defbuf
, "%s %s",
2287 utsname
.sysname
, utsname
.release
);
2289 mdoc
->meta
.os
= mandoc_strdup(defbuf
);
2293 mdoc_node_delete(mdoc
, n
);
2297 * If no argument is provided,
2298 * fill in the name of the current manual page.
2303 struct roff_node
*n
;
2310 if (mdoc
->meta
.name
== NULL
) {
2311 mandoc_msg(MANDOCERR_EX_NONAME
, mdoc
->parse
,
2312 n
->line
, n
->pos
, "Ex");
2316 mdoc
->next
= MDOC_NEXT_CHILD
;
2317 mdoc_word_alloc(mdoc
, n
->line
, n
->pos
, mdoc
->meta
.name
);
2321 static enum roff_sec
2322 a2sec(const char *p
)
2326 for (i
= 0; i
< (int)SEC__MAX
; i
++)
2327 if (secnames
[i
] && 0 == strcmp(p
, secnames
[i
]))
2328 return((enum roff_sec
)i
);
2334 macro2len(int macro
)