]>
git.cameronkatri.com Git - mandoc.git/blob - mdoc_validate.c
1 /* $Id: mdoc_validate.c,v 1.253 2014/10/13 14:05:32 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_fn(POST_ARGS
);
105 static int post_fname(POST_ARGS
);
106 static int post_fo(POST_ARGS
);
107 static int post_hyph(POST_ARGS
);
108 static int post_hyphtext(POST_ARGS
);
109 static int post_ignpar(POST_ARGS
);
110 static int post_it(POST_ARGS
);
111 static int post_lb(POST_ARGS
);
112 static int post_literal(POST_ARGS
);
113 static int post_nd(POST_ARGS
);
114 static int post_nm(POST_ARGS
);
115 static int post_ns(POST_ARGS
);
116 static int post_os(POST_ARGS
);
117 static int post_par(POST_ARGS
);
118 static int post_root(POST_ARGS
);
119 static int post_rs(POST_ARGS
);
120 static int post_sh(POST_ARGS
);
121 static int post_sh_head(POST_ARGS
);
122 static int post_sh_name(POST_ARGS
);
123 static int post_sh_see_also(POST_ARGS
);
124 static int post_sh_authors(POST_ARGS
);
125 static int post_st(POST_ARGS
);
126 static int post_vt(POST_ARGS
);
127 static int pre_an(PRE_ARGS
);
128 static int pre_bd(PRE_ARGS
);
129 static int pre_bl(PRE_ARGS
);
130 static int pre_dd(PRE_ARGS
);
131 static int pre_display(PRE_ARGS
);
132 static int pre_dt(PRE_ARGS
);
133 static int pre_literal(PRE_ARGS
);
134 static int pre_obsolete(PRE_ARGS
);
135 static int pre_os(PRE_ARGS
);
136 static int pre_par(PRE_ARGS
);
137 static int pre_std(PRE_ARGS
);
139 static const struct valids mdoc_valids
[MDOC_MAX
] = {
140 { NULL
, NULL
}, /* Ap */
141 { pre_dd
, post_dd
}, /* Dd */
142 { pre_dt
, post_dt
}, /* Dt */
143 { pre_os
, post_os
}, /* Os */
144 { NULL
, post_sh
}, /* Sh */
145 { NULL
, post_ignpar
}, /* Ss */
146 { pre_par
, post_par
}, /* Pp */
147 { pre_display
, post_d1
}, /* D1 */
148 { pre_literal
, post_literal
}, /* Dl */
149 { pre_bd
, post_literal
}, /* Bd */
150 { NULL
, NULL
}, /* Ed */
151 { pre_bl
, post_bl
}, /* Bl */
152 { NULL
, NULL
}, /* El */
153 { pre_par
, post_it
}, /* It */
154 { NULL
, NULL
}, /* Ad */
155 { pre_an
, post_an
}, /* An */
156 { NULL
, post_defaults
}, /* Ar */
157 { NULL
, NULL
}, /* Cd */
158 { NULL
, NULL
}, /* Cm */
159 { NULL
, NULL
}, /* Dv */
160 { NULL
, NULL
}, /* Er */
161 { NULL
, NULL
}, /* Ev */
162 { pre_std
, post_ex
}, /* Ex */
163 { NULL
, post_fa
}, /* Fa */
164 { NULL
, ewarn_ge1
}, /* Fd */
165 { NULL
, NULL
}, /* Fl */
166 { NULL
, post_fn
}, /* Fn */
167 { NULL
, NULL
}, /* Ft */
168 { NULL
, NULL
}, /* Ic */
169 { NULL
, ewarn_eq1
}, /* In */
170 { NULL
, post_defaults
}, /* Li */
171 { NULL
, post_nd
}, /* Nd */
172 { NULL
, post_nm
}, /* Nm */
173 { NULL
, NULL
}, /* Op */
174 { pre_obsolete
, NULL
}, /* Ot */
175 { NULL
, post_defaults
}, /* Pa */
176 { pre_std
, NULL
}, /* Rv */
177 { NULL
, post_st
}, /* St */
178 { NULL
, NULL
}, /* Va */
179 { NULL
, post_vt
}, /* Vt */
180 { NULL
, ewarn_ge1
}, /* Xr */
181 { NULL
, ewarn_ge1
}, /* %A */
182 { NULL
, post_hyphtext
}, /* %B */ /* FIXME: can be used outside Rs/Re. */
183 { NULL
, ewarn_ge1
}, /* %D */
184 { NULL
, ewarn_ge1
}, /* %I */
185 { NULL
, ewarn_ge1
}, /* %J */
186 { NULL
, post_hyphtext
}, /* %N */
187 { NULL
, post_hyphtext
}, /* %O */
188 { NULL
, ewarn_ge1
}, /* %P */
189 { NULL
, post_hyphtext
}, /* %R */
190 { NULL
, post_hyphtext
}, /* %T */ /* FIXME: can be used outside Rs/Re. */
191 { NULL
, ewarn_ge1
}, /* %V */
192 { NULL
, NULL
}, /* Ac */
193 { NULL
, NULL
}, /* Ao */
194 { NULL
, NULL
}, /* Aq */
195 { NULL
, post_at
}, /* At */
196 { NULL
, NULL
}, /* Bc */
197 { NULL
, post_bf
}, /* Bf */
198 { NULL
, NULL
}, /* Bo */
199 { NULL
, NULL
}, /* Bq */
200 { NULL
, NULL
}, /* Bsx */
201 { NULL
, post_bx
}, /* Bx */
202 { NULL
, ebool
}, /* Db */
203 { NULL
, NULL
}, /* Dc */
204 { NULL
, NULL
}, /* Do */
205 { NULL
, NULL
}, /* Dq */
206 { NULL
, NULL
}, /* Ec */
207 { NULL
, NULL
}, /* Ef */
208 { NULL
, NULL
}, /* Em */
209 { NULL
, NULL
}, /* Eo */
210 { NULL
, NULL
}, /* Fx */
211 { NULL
, NULL
}, /* Ms */
212 { NULL
, ewarn_eq0
}, /* No */
213 { NULL
, post_ns
}, /* Ns */
214 { NULL
, NULL
}, /* Nx */
215 { NULL
, NULL
}, /* Ox */
216 { NULL
, NULL
}, /* Pc */
217 { NULL
, ewarn_eq1
}, /* Pf */
218 { NULL
, NULL
}, /* Po */
219 { NULL
, NULL
}, /* Pq */
220 { NULL
, NULL
}, /* Qc */
221 { NULL
, NULL
}, /* Ql */
222 { NULL
, NULL
}, /* Qo */
223 { NULL
, NULL
}, /* Qq */
224 { NULL
, NULL
}, /* Re */
225 { NULL
, post_rs
}, /* Rs */
226 { NULL
, NULL
}, /* Sc */
227 { NULL
, NULL
}, /* So */
228 { NULL
, NULL
}, /* Sq */
229 { NULL
, ebool
}, /* Sm */
230 { NULL
, post_hyph
}, /* Sx */
231 { NULL
, NULL
}, /* Sy */
232 { NULL
, NULL
}, /* Tn */
233 { NULL
, NULL
}, /* Ux */
234 { NULL
, NULL
}, /* Xc */
235 { NULL
, NULL
}, /* Xo */
236 { NULL
, post_fo
}, /* Fo */
237 { NULL
, NULL
}, /* Fc */
238 { NULL
, NULL
}, /* Oo */
239 { NULL
, NULL
}, /* Oc */
240 { NULL
, post_bk
}, /* Bk */
241 { NULL
, NULL
}, /* Ek */
242 { NULL
, post_eoln
}, /* Bt */
243 { NULL
, NULL
}, /* Hf */
244 { pre_obsolete
, NULL
}, /* Fr */
245 { NULL
, post_eoln
}, /* Ud */
246 { NULL
, post_lb
}, /* Lb */
247 { pre_par
, post_par
}, /* Lp */
248 { NULL
, NULL
}, /* Lk */
249 { NULL
, post_defaults
}, /* Mt */
250 { NULL
, NULL
}, /* Brq */
251 { NULL
, NULL
}, /* Bro */
252 { NULL
, NULL
}, /* Brc */
253 { NULL
, ewarn_ge1
}, /* %C */
254 { pre_obsolete
, post_es
}, /* Es */
255 { pre_obsolete
, post_en
}, /* En */
256 { NULL
, NULL
}, /* Dx */
257 { NULL
, ewarn_ge1
}, /* %Q */
258 { NULL
, post_par
}, /* br */
259 { NULL
, post_par
}, /* sp */
260 { NULL
, ewarn_eq1
}, /* %U */
261 { NULL
, NULL
}, /* Ta */
262 { NULL
, NULL
}, /* ll */
265 #define RSORD_MAX 14 /* Number of `Rs' blocks. */
267 static const enum mdoct rsord
[RSORD_MAX
] = {
284 static const char * const secnames
[SEC__MAX
] = {
291 "IMPLEMENTATION NOTES",
306 "SECURITY CONSIDERATIONS",
312 mdoc_valid_pre(struct mdoc
*mdoc
, struct mdoc_node
*n
)
318 check_text(mdoc
, n
->line
, n
->pos
, n
->string
);
331 p
= mdoc_valids
[n
->tok
].pre
;
332 return(*p
? (*p
)(mdoc
, n
) : 1);
336 mdoc_valid_post(struct mdoc
*mdoc
)
342 if (n
->flags
& MDOC_VALID
)
344 n
->flags
|= MDOC_VALID
;
354 return(post_root(mdoc
));
356 p
= mdoc_valids
[n
->tok
].post
;
357 return(*p
? (*p
)(mdoc
) : 1);
362 check_count(struct mdoc
*mdoc
, enum mdoc_type type
,
363 enum check_lvl lvl
, enum check_ineq ineq
, int val
)
368 if (mdoc
->last
->type
!= type
)
374 if (mdoc
->last
->nchild
< val
)
379 if (mdoc
->last
->nchild
> val
)
384 if (val
== mdoc
->last
->nchild
)
392 t
= lvl
== CHECK_WARN
? MANDOCERR_ARGCWARN
: MANDOCERR_ARGCOUNT
;
393 mandoc_vmsg(t
, mdoc
->parse
, mdoc
->last
->line
,
394 mdoc
->last
->pos
, "want %s%d children (have %d)",
395 p
, val
, mdoc
->last
->nchild
);
403 return(check_count(mdoc
, MDOC_BODY
, CHECK_ERROR
, CHECK_GT
, 0));
409 return(check_count(mdoc
, MDOC_BODY
, CHECK_WARN
, CHECK_GT
, 0));
415 return(check_count(mdoc
, MDOC_ELEM
, CHECK_WARN
, CHECK_EQ
, 0));
421 return(check_count(mdoc
, MDOC_ELEM
, CHECK_WARN
, CHECK_EQ
, 1));
427 return(check_count(mdoc
, MDOC_ELEM
, CHECK_WARN
, CHECK_GT
, 0));
433 return(check_count(mdoc
, MDOC_ELEM
, CHECK_WARN
, CHECK_LT
, 2));
439 return(check_count(mdoc
, MDOC_HEAD
, CHECK_WARN
, CHECK_EQ
, 0));
445 return(check_count(mdoc
, MDOC_HEAD
, CHECK_WARN
, CHECK_EQ
, 1));
451 return(check_count(mdoc
, MDOC_HEAD
, CHECK_WARN
, CHECK_GT
, 0));
455 check_args(struct mdoc
*mdoc
, struct mdoc_node
*n
)
462 assert(n
->args
->argc
);
463 for (i
= 0; i
< (int)n
->args
->argc
; i
++)
464 check_argv(mdoc
, n
, &n
->args
->argv
[i
]);
468 check_argv(struct mdoc
*mdoc
, struct mdoc_node
*n
, struct mdoc_argv
*v
)
472 for (i
= 0; i
< (int)v
->sz
; i
++)
473 check_text(mdoc
, v
->line
, v
->pos
, v
->value
[i
]);
477 check_text(struct mdoc
*mdoc
, int ln
, int pos
, char *p
)
481 if (MDOC_LITERAL
& mdoc
->flags
)
484 for (cp
= p
; NULL
!= (p
= strchr(p
, '\t')); p
++)
485 mandoc_msg(MANDOCERR_FI_TAB
, mdoc
->parse
,
486 ln
, pos
+ (int)(p
- cp
), NULL
);
490 pre_display(PRE_ARGS
)
492 struct mdoc_node
*node
;
494 if (MDOC_BLOCK
!= n
->type
)
497 for (node
= mdoc
->last
->parent
; node
; node
= node
->parent
)
498 if (MDOC_BLOCK
== node
->type
)
499 if (MDOC_Bd
== node
->tok
)
503 mandoc_vmsg(MANDOCERR_BD_NEST
,
504 mdoc
->parse
, n
->line
, n
->pos
,
505 "%s in Bd", mdoc_macronames
[n
->tok
]);
513 struct mdoc_node
*np
;
514 struct mdoc_argv
*argv
, *wa
;
516 enum mdocargt mdoclt
;
519 if (MDOC_BLOCK
!= n
->type
) {
520 if (ENDBODY_NOT
!= n
->end
) {
522 np
= n
->pending
->parent
;
527 assert(MDOC_BLOCK
== np
->type
);
528 assert(MDOC_Bl
== np
->tok
);
533 * First figure out which kind of list to use: bind ourselves to
534 * the first mentioned list type and warn about any remaining
535 * ones. If we find no list type, we default to LIST_item.
538 wa
= (n
->args
== NULL
) ? NULL
: n
->args
->argv
;
539 mdoclt
= MDOC_ARG_MAX
;
540 for (i
= 0; n
->args
&& i
< (int)n
->args
->argc
; i
++) {
541 argv
= n
->args
->argv
+ i
;
544 /* Set list types. */
578 /* Set list arguments. */
580 if (n
->norm
->Bl
.comp
)
581 mandoc_msg(MANDOCERR_ARG_REP
,
582 mdoc
->parse
, argv
->line
,
583 argv
->pos
, "Bl -compact");
584 n
->norm
->Bl
.comp
= 1;
589 mandoc_msg(MANDOCERR_ARG_EMPTY
,
590 mdoc
->parse
, argv
->line
,
591 argv
->pos
, "Bl -width");
592 n
->norm
->Bl
.width
= "0n";
595 if (NULL
!= n
->norm
->Bl
.width
)
596 mandoc_vmsg(MANDOCERR_ARG_REP
,
597 mdoc
->parse
, argv
->line
,
598 argv
->pos
, "Bl -width %s",
600 n
->norm
->Bl
.width
= argv
->value
[0];
604 mandoc_msg(MANDOCERR_ARG_EMPTY
,
605 mdoc
->parse
, argv
->line
,
606 argv
->pos
, "Bl -offset");
609 if (NULL
!= n
->norm
->Bl
.offs
)
610 mandoc_vmsg(MANDOCERR_ARG_REP
,
611 mdoc
->parse
, argv
->line
,
612 argv
->pos
, "Bl -offset %s",
614 n
->norm
->Bl
.offs
= argv
->value
[0];
619 if (LIST__NONE
== lt
)
623 /* Check: multiple list types. */
625 if (LIST__NONE
!= n
->norm
->Bl
.type
) {
626 mandoc_vmsg(MANDOCERR_BL_REP
,
627 mdoc
->parse
, n
->line
, n
->pos
,
628 "Bl -%s", mdoc_argnames
[argv
->arg
]);
632 /* The list type should come first. */
634 if (n
->norm
->Bl
.width
||
637 mandoc_vmsg(MANDOCERR_BL_LATETYPE
,
638 mdoc
->parse
, n
->line
, n
->pos
, "Bl -%s",
639 mdoc_argnames
[n
->args
->argv
[0].arg
]);
641 n
->norm
->Bl
.type
= lt
;
642 if (LIST_column
== lt
) {
643 n
->norm
->Bl
.ncols
= argv
->sz
;
644 n
->norm
->Bl
.cols
= (void *)argv
->value
;
648 /* Allow lists to default to LIST_item. */
650 if (LIST__NONE
== n
->norm
->Bl
.type
) {
651 mandoc_msg(MANDOCERR_BL_NOTYPE
, mdoc
->parse
,
652 n
->line
, n
->pos
, "Bl");
653 n
->norm
->Bl
.type
= LIST_item
;
657 * Validate the width field. Some list types don't need width
658 * types and should be warned about them. Others should have it
659 * and must also be warned. Yet others have a default and need
663 switch (n
->norm
->Bl
.type
) {
665 if (NULL
== n
->norm
->Bl
.width
)
666 mandoc_msg(MANDOCERR_BL_NOWIDTH
, mdoc
->parse
,
667 n
->line
, n
->pos
, "Bl -tag");
678 if (n
->norm
->Bl
.width
)
679 mandoc_vmsg(MANDOCERR_BL_SKIPW
, mdoc
->parse
,
680 wa
->line
, wa
->pos
, "Bl -%s",
681 mdoc_argnames
[mdoclt
]);
688 if (NULL
== n
->norm
->Bl
.width
)
689 n
->norm
->Bl
.width
= "2n";
692 if (NULL
== n
->norm
->Bl
.width
)
693 n
->norm
->Bl
.width
= "3n";
699 return(pre_par(mdoc
, n
));
705 struct mdoc_node
*np
;
706 struct mdoc_argv
*argv
;
710 pre_literal(mdoc
, n
);
712 if (MDOC_BLOCK
!= n
->type
) {
713 if (ENDBODY_NOT
!= n
->end
) {
715 np
= n
->pending
->parent
;
720 assert(MDOC_BLOCK
== np
->type
);
721 assert(MDOC_Bd
== np
->tok
);
725 for (i
= 0; n
->args
&& i
< (int)n
->args
->argc
; i
++) {
726 argv
= n
->args
->argv
+ i
;
746 mandoc_msg(MANDOCERR_BD_FILE
, mdoc
->parse
,
747 n
->line
, n
->pos
, NULL
);
751 mandoc_msg(MANDOCERR_ARG_EMPTY
,
752 mdoc
->parse
, argv
->line
,
753 argv
->pos
, "Bd -offset");
756 if (NULL
!= n
->norm
->Bd
.offs
)
757 mandoc_vmsg(MANDOCERR_ARG_REP
,
758 mdoc
->parse
, argv
->line
,
759 argv
->pos
, "Bd -offset %s",
761 n
->norm
->Bd
.offs
= argv
->value
[0];
764 if (n
->norm
->Bd
.comp
)
765 mandoc_msg(MANDOCERR_ARG_REP
,
766 mdoc
->parse
, argv
->line
,
767 argv
->pos
, "Bd -compact");
768 n
->norm
->Bd
.comp
= 1;
774 if (DISP__NONE
== dt
)
777 if (DISP__NONE
== n
->norm
->Bd
.type
)
778 n
->norm
->Bd
.type
= dt
;
780 mandoc_vmsg(MANDOCERR_BD_REP
,
781 mdoc
->parse
, n
->line
, n
->pos
,
782 "Bd -%s", mdoc_argnames
[argv
->arg
]);
785 if (DISP__NONE
== n
->norm
->Bd
.type
) {
786 mandoc_msg(MANDOCERR_BD_NOTYPE
, mdoc
->parse
,
787 n
->line
, n
->pos
, "Bd");
788 n
->norm
->Bd
.type
= DISP_ragged
;
791 return(pre_par(mdoc
, n
));
797 struct mdoc_argv
*argv
;
803 for (i
= 1; i
< n
->args
->argc
; i
++) {
804 argv
= n
->args
->argv
+ i
;
805 mandoc_vmsg(MANDOCERR_AN_REP
,
806 mdoc
->parse
, argv
->line
, argv
->pos
,
807 "An -%s", mdoc_argnames
[argv
->arg
]);
810 argv
= n
->args
->argv
;
811 if (argv
->arg
== MDOC_Split
)
812 n
->norm
->An
.auth
= AUTH_split
;
813 else if (argv
->arg
== MDOC_Nosplit
)
814 n
->norm
->An
.auth
= AUTH_nosplit
;
825 if (n
->args
&& 1 == n
->args
->argc
)
826 if (MDOC_Std
== n
->args
->argv
[0].arg
)
829 mandoc_msg(MANDOCERR_ARG_STD
, mdoc
->parse
,
830 n
->line
, n
->pos
, mdoc_macronames
[n
->tok
]);
835 pre_obsolete(PRE_ARGS
)
838 if (MDOC_ELEM
== n
->type
|| MDOC_BLOCK
== n
->type
)
839 mandoc_msg(MANDOCERR_MACRO_OBS
, mdoc
->parse
,
840 n
->line
, n
->pos
, mdoc_macronames
[n
->tok
]);
848 if (mdoc
->meta
.title
!= NULL
)
849 mandoc_msg(MANDOCERR_PROLOG_REP
, mdoc
->parse
,
850 n
->line
, n
->pos
, "Dt");
851 else if (mdoc
->meta
.os
!= NULL
)
852 mandoc_msg(MANDOCERR_PROLOG_ORDER
, mdoc
->parse
,
853 n
->line
, n
->pos
, "Dt after Os");
861 if (mdoc
->meta
.os
!= NULL
)
862 mandoc_msg(MANDOCERR_PROLOG_REP
, mdoc
->parse
,
863 n
->line
, n
->pos
, "Os");
864 else if (mdoc
->flags
& MDOC_PBODY
)
865 mandoc_msg(MANDOCERR_PROLOG_LATE
, mdoc
->parse
,
866 n
->line
, n
->pos
, "Os");
874 if (mdoc
->meta
.date
!= NULL
)
875 mandoc_msg(MANDOCERR_PROLOG_REP
, mdoc
->parse
,
876 n
->line
, n
->pos
, "Dd");
877 else if (mdoc
->flags
& MDOC_PBODY
)
878 mandoc_msg(MANDOCERR_PROLOG_LATE
, mdoc
->parse
,
879 n
->line
, n
->pos
, "Dd");
880 else if (mdoc
->meta
.title
!= NULL
)
881 mandoc_msg(MANDOCERR_PROLOG_ORDER
, mdoc
->parse
,
882 n
->line
, n
->pos
, "Dd after Dt");
883 else if (mdoc
->meta
.os
!= NULL
)
884 mandoc_msg(MANDOCERR_PROLOG_ORDER
, mdoc
->parse
,
885 n
->line
, n
->pos
, "Dd after Os");
892 struct mdoc_node
*np
, *nch
;
896 * Unlike other data pointers, these are "housed" by the HEAD
897 * element, which contains the goods.
900 if (MDOC_HEAD
!= mdoc
->last
->type
) {
901 if (ENDBODY_NOT
!= mdoc
->last
->end
) {
902 assert(mdoc
->last
->pending
);
903 np
= mdoc
->last
->pending
->parent
->head
;
904 } else if (MDOC_BLOCK
!= mdoc
->last
->type
) {
905 np
= mdoc
->last
->parent
->head
;
907 np
= mdoc
->last
->head
;
910 assert(MDOC_HEAD
== np
->type
);
911 assert(MDOC_Bf
== np
->tok
);
916 assert(MDOC_BLOCK
== np
->parent
->type
);
917 assert(MDOC_Bf
== np
->parent
->tok
);
919 /* Check the number of arguments. */
922 if (NULL
== np
->parent
->args
) {
924 mandoc_msg(MANDOCERR_BF_NOFONT
, mdoc
->parse
,
925 np
->line
, np
->pos
, "Bf");
931 mandoc_vmsg(MANDOCERR_ARG_EXCESS
, mdoc
->parse
,
932 nch
->line
, nch
->pos
, "Bf ... %s", nch
->string
);
934 /* Extract argument into data. */
936 if (np
->parent
->args
) {
937 arg
= np
->parent
->args
->argv
[0].arg
;
938 if (MDOC_Emphasis
== arg
)
939 np
->norm
->Bf
.font
= FONT_Em
;
940 else if (MDOC_Literal
== arg
)
941 np
->norm
->Bf
.font
= FONT_Li
;
942 else if (MDOC_Symbolic
== arg
)
943 np
->norm
->Bf
.font
= FONT_Sy
;
949 /* Extract parameter into data. */
951 if (0 == strcmp(np
->child
->string
, "Em"))
952 np
->norm
->Bf
.font
= FONT_Em
;
953 else if (0 == strcmp(np
->child
->string
, "Li"))
954 np
->norm
->Bf
.font
= FONT_Li
;
955 else if (0 == strcmp(np
->child
->string
, "Sy"))
956 np
->norm
->Bf
.font
= FONT_Sy
;
958 mandoc_vmsg(MANDOCERR_BF_BADFONT
, mdoc
->parse
,
959 np
->child
->line
, np
->child
->pos
,
960 "Bf %s", np
->child
->string
);
969 const char *stdlibname
;
972 check_count(mdoc
, MDOC_ELEM
, CHECK_WARN
, CHECK_EQ
, 1);
974 n
= mdoc
->last
->child
;
977 assert(MDOC_TEXT
== n
->type
);
979 if (NULL
== (stdlibname
= mdoc_a2lib(n
->string
)))
980 mandoc_asprintf(&libname
,
981 "library \\(lq%s\\(rq", n
->string
);
983 libname
= mandoc_strdup(stdlibname
);
993 const struct mdoc_node
*n
;
997 mandoc_vmsg(MANDOCERR_ARG_SKIP
,
998 mdoc
->parse
, n
->line
, n
->pos
,
999 "%s %s", mdoc_macronames
[n
->tok
],
1005 post_fname(POST_ARGS
)
1007 const struct mdoc_node
*n
;
1011 n
= mdoc
->last
->child
;
1012 pos
= strcspn(n
->string
, "()");
1013 cp
= n
->string
+ pos
;
1014 if ( ! (cp
[0] == '\0' || (cp
[0] == '(' && cp
[1] == '*')))
1015 mandoc_msg(MANDOCERR_FN_PAREN
, mdoc
->parse
,
1016 n
->line
, n
->pos
+ pos
, n
->string
);
1035 if (mdoc
->last
->type
== MDOC_HEAD
&& mdoc
->last
->nchild
)
1043 const struct mdoc_node
*n
;
1046 for (n
= mdoc
->last
->child
; n
!= NULL
; n
= n
->next
) {
1047 for (cp
= n
->string
; *cp
!= '\0'; cp
++) {
1048 /* Ignore callbacks and alterations. */
1049 if (*cp
== '(' || *cp
== '{')
1053 mandoc_msg(MANDOCERR_FA_COMMA
, mdoc
->parse
,
1054 n
->line
, n
->pos
+ (cp
- n
->string
),
1065 const struct mdoc_node
*n
;
1068 * The Vt macro comes in both ELEM and BLOCK form, both of which
1069 * have different syntaxes (yet more context-sensitive
1070 * behaviour). ELEM types must have a child, which is already
1071 * guaranteed by the in_line parsing routine; BLOCK types,
1072 * specifically the BODY, should only have TEXT children.
1075 if (MDOC_BODY
!= mdoc
->last
->type
)
1078 for (n
= mdoc
->last
->child
; n
; n
= n
->next
)
1079 if (MDOC_TEXT
!= n
->type
)
1080 mandoc_msg(MANDOCERR_VT_CHILD
, mdoc
->parse
,
1081 n
->line
, n
->pos
, mdoc_macronames
[n
->tok
]);
1090 if (NULL
!= mdoc
->meta
.name
)
1093 mdoc_deroff(&mdoc
->meta
.name
, mdoc
->last
);
1095 if (NULL
== mdoc
->meta
.name
)
1096 mandoc_msg(MANDOCERR_NM_NONAME
, mdoc
->parse
,
1097 mdoc
->last
->line
, mdoc
->last
->pos
, "Nm");
1106 return(post_hyph(mdoc
));
1114 return(post_hyph(mdoc
));
1118 post_literal(POST_ARGS
)
1121 if (mdoc
->last
->tok
== MDOC_Bd
)
1126 * The `Dl' (note "el" not "one") and `Bd' macros unset the
1127 * MDOC_LITERAL flag as they leave. Note that `Bd' only sets
1128 * this in literal mode, but it doesn't hurt to just switch it
1129 * off in general since displays can't be nested.
1132 if (MDOC_BODY
== mdoc
->last
->type
)
1133 mdoc
->flags
&= ~MDOC_LITERAL
;
1139 post_defaults(POST_ARGS
)
1141 struct mdoc_node
*nn
;
1144 * The `Ar' defaults to "file ..." if no value is provided as an
1145 * argument; the `Mt' and `Pa' macros use "~"; the `Li' just
1146 * gets an empty string.
1149 if (mdoc
->last
->child
)
1153 mdoc
->next
= MDOC_NEXT_CHILD
;
1157 if ( ! mdoc_word_alloc(mdoc
, nn
->line
, nn
->pos
, "file"))
1159 if ( ! mdoc_word_alloc(mdoc
, nn
->line
, nn
->pos
, "..."))
1163 if ( ! mdoc_word_alloc(mdoc
, nn
->line
, nn
->pos
, ""))
1169 if ( ! mdoc_word_alloc(mdoc
, nn
->line
, nn
->pos
, "~"))
1184 struct mdoc_node
*n
;
1185 const char *std_att
;
1189 if (n
->child
== NULL
) {
1190 mdoc
->next
= MDOC_NEXT_CHILD
;
1191 if ( ! mdoc_word_alloc(mdoc
, n
->line
, n
->pos
, "AT&T UNIX"))
1198 * If we have a child, look it up in the standard keys. If a
1199 * key exist, use that instead of the child; if it doesn't,
1200 * prefix "AT&T UNIX " to the existing data.
1204 assert(MDOC_TEXT
== n
->type
);
1205 if (NULL
== (std_att
= mdoc_a2att(n
->string
))) {
1206 mandoc_vmsg(MANDOCERR_AT_BAD
, mdoc
->parse
,
1207 n
->line
, n
->pos
, "At %s", n
->string
);
1208 mandoc_asprintf(&att
, "AT&T UNIX %s", n
->string
);
1210 att
= mandoc_strdup(std_att
);
1220 struct mdoc_node
*np
;
1223 if (AUTH__NONE
== np
->norm
->An
.auth
) {
1225 check_count(mdoc
, MDOC_ELEM
, CHECK_WARN
, CHECK_GT
, 0);
1226 } else if (np
->child
)
1227 check_count(mdoc
, MDOC_ELEM
, CHECK_WARN
, CHECK_EQ
, 0);
1236 if (MDOC_BLOCK
== mdoc
->last
->type
)
1237 mdoc
->last
->norm
->Es
= mdoc
->last_es
;
1245 mdoc
->last_es
= mdoc
->last
;
1254 struct mdoc_node
*nbl
, *nit
, *nch
;
1257 if (MDOC_BLOCK
!= nit
->type
)
1260 nbl
= nit
->parent
->parent
;
1261 lt
= nbl
->norm
->Bl
.type
;
1273 if (NULL
== nit
->head
->child
)
1274 mandoc_vmsg(MANDOCERR_IT_NOHEAD
,
1275 mdoc
->parse
, nit
->line
, nit
->pos
,
1277 mdoc_argnames
[nbl
->args
->argv
[0].arg
]);
1286 if (NULL
== nit
->body
->child
)
1287 mandoc_vmsg(MANDOCERR_IT_NOBODY
,
1288 mdoc
->parse
, nit
->line
, nit
->pos
,
1290 mdoc_argnames
[nbl
->args
->argv
[0].arg
]);
1293 if (NULL
!= nit
->head
->child
)
1294 mandoc_vmsg(MANDOCERR_ARG_SKIP
,
1295 mdoc
->parse
, nit
->line
, nit
->pos
,
1296 "It %s", nit
->head
->child
->string
);
1299 cols
= (int)nbl
->norm
->Bl
.ncols
;
1301 assert(NULL
== nit
->head
->child
);
1303 for (i
= 0, nch
= nit
->child
; nch
; nch
= nch
->next
)
1304 if (MDOC_BODY
== nch
->type
)
1307 if (i
< cols
|| i
> cols
+ 1)
1308 mandoc_vmsg(MANDOCERR_ARGCOUNT
,
1309 mdoc
->parse
, nit
->line
, nit
->pos
,
1310 "columns == %d (have %d)", cols
, i
);
1320 post_bl_block(POST_ARGS
)
1322 struct mdoc_node
*n
, *ni
, *nc
;
1325 * These are fairly complicated, so we've broken them into two
1326 * functions. post_bl_block_tag() is called when a -tag is
1327 * specified, but no -width (it must be guessed). The second
1328 * when a -width is specified (macro indicators must be
1329 * rewritten into real lengths).
1334 if (LIST_tag
== n
->norm
->Bl
.type
&&
1335 NULL
== n
->norm
->Bl
.width
) {
1336 if ( ! post_bl_block_tag(mdoc
))
1338 assert(n
->norm
->Bl
.width
);
1339 } else if (NULL
!= n
->norm
->Bl
.width
) {
1340 if ( ! post_bl_block_width(mdoc
))
1342 assert(n
->norm
->Bl
.width
);
1345 for (ni
= n
->body
->child
; ni
; ni
= ni
->next
) {
1346 if (NULL
== ni
->body
)
1348 nc
= ni
->body
->last
;
1349 while (NULL
!= nc
) {
1361 if (NULL
== ni
->next
) {
1362 mandoc_msg(MANDOCERR_PAR_MOVE
,
1363 mdoc
->parse
, nc
->line
, nc
->pos
,
1364 mdoc_macronames
[nc
->tok
]);
1365 if ( ! mdoc_node_relink(mdoc
, nc
))
1367 } else if (0 == n
->norm
->Bl
.comp
&&
1368 LIST_column
!= n
->norm
->Bl
.type
) {
1369 mandoc_vmsg(MANDOCERR_PAR_SKIP
,
1370 mdoc
->parse
, nc
->line
, nc
->pos
,
1372 mdoc_macronames
[nc
->tok
]);
1373 mdoc_node_delete(mdoc
, nc
);
1376 nc
= ni
->body
->last
;
1383 post_bl_block_width(POST_ARGS
)
1388 struct mdoc_node
*n
;
1394 * Calculate the real width of a list from the -width string,
1395 * which may contain a macro (with a known default width), a
1396 * literal string, or a scaling width.
1398 * If the value to -width is a macro, then we re-write it to be
1399 * the macro's width as set in share/tmac/mdoc/doc-common.
1402 if (0 == strcmp(n
->norm
->Bl
.width
, "Ds"))
1404 else if (MDOC_MAX
== (tok
= mdoc_hash_find(n
->norm
->Bl
.width
)))
1407 width
= macro2len(tok
);
1409 /* The value already exists: free and reallocate it. */
1413 for (i
= 0; i
< (int)n
->args
->argc
; i
++)
1414 if (MDOC_Width
== n
->args
->argv
[i
].arg
)
1417 assert(i
< (int)n
->args
->argc
);
1419 (void)snprintf(buf
, sizeof(buf
), "%un", (unsigned int)width
);
1420 free(n
->args
->argv
[i
].value
[0]);
1421 n
->args
->argv
[i
].value
[0] = mandoc_strdup(buf
);
1423 /* Set our width! */
1424 n
->norm
->Bl
.width
= n
->args
->argv
[i
].value
[0];
1429 post_bl_block_tag(POST_ARGS
)
1431 struct mdoc_node
*n
, *nn
;
1437 * Calculate the -width for a `Bl -tag' list if it hasn't been
1438 * provided. Uses the first head macro. NOTE AGAIN: this is
1439 * ONLY if the -width argument has NOT been provided. See
1440 * post_bl_block_width() for converting the -width string.
1446 for (nn
= n
->body
->child
; nn
; nn
= nn
->next
) {
1447 if (MDOC_It
!= nn
->tok
)
1450 assert(MDOC_BLOCK
== nn
->type
);
1451 nn
= nn
->head
->child
;
1456 if (MDOC_TEXT
== nn
->type
) {
1457 sz
= strlen(nn
->string
) + 1;
1461 if (0 != (ssz
= macro2len(nn
->tok
)))
1467 /* Defaults to ten ens. */
1469 (void)snprintf(buf
, sizeof(buf
), "%un", (unsigned int)sz
);
1472 * We have to dynamically add this to the macro's argument list.
1473 * We're guaranteed that a MDOC_Width doesn't already exist.
1477 i
= (int)(n
->args
->argc
)++;
1479 n
->args
->argv
= mandoc_reallocarray(n
->args
->argv
,
1480 n
->args
->argc
, sizeof(struct mdoc_argv
));
1482 n
->args
->argv
[i
].arg
= MDOC_Width
;
1483 n
->args
->argv
[i
].line
= n
->line
;
1484 n
->args
->argv
[i
].pos
= n
->pos
;
1485 n
->args
->argv
[i
].sz
= 1;
1486 n
->args
->argv
[i
].value
= mandoc_malloc(sizeof(char *));
1487 n
->args
->argv
[i
].value
[0] = mandoc_strdup(buf
);
1489 /* Set our width! */
1490 n
->norm
->Bl
.width
= n
->args
->argv
[i
].value
[0];
1495 post_bl_head(POST_ARGS
)
1497 struct mdoc_node
*np
, *nn
, *nnp
;
1498 struct mdoc_argv
*argv
;
1501 if (LIST_column
!= mdoc
->last
->norm
->Bl
.type
)
1502 /* FIXME: this should be ERROR class... */
1503 return(hwarn_eq0(mdoc
));
1506 * Append old-style lists, where the column width specifiers
1507 * trail as macro parameters, to the new-style ("normal-form")
1508 * lists where they're argument values following -column.
1511 if (mdoc
->last
->child
== NULL
)
1514 np
= mdoc
->last
->parent
;
1517 for (j
= 0; j
< (int)np
->args
->argc
; j
++)
1518 if (MDOC_Column
== np
->args
->argv
[j
].arg
)
1521 assert(j
< (int)np
->args
->argc
);
1524 * Accommodate for new-style groff column syntax. Shuffle the
1525 * child nodes, all of which must be TEXT, as arguments for the
1526 * column field. Then, delete the head children.
1529 argv
= np
->args
->argv
+ j
;
1531 argv
->sz
+= mdoc
->last
->nchild
;
1532 argv
->value
= mandoc_reallocarray(argv
->value
,
1533 argv
->sz
, sizeof(char *));
1535 mdoc
->last
->norm
->Bl
.ncols
= argv
->sz
;
1536 mdoc
->last
->norm
->Bl
.cols
= (void *)argv
->value
;
1538 for (nn
= mdoc
->last
->child
; nn
; i
++) {
1539 argv
->value
[i
] = nn
->string
;
1543 mdoc_node_delete(NULL
, nnp
);
1546 mdoc
->last
->nchild
= 0;
1547 mdoc
->last
->child
= NULL
;
1555 struct mdoc_node
*nparent
, *nprev
; /* of the Bl block */
1556 struct mdoc_node
*nblock
, *nbody
; /* of the Bl */
1557 struct mdoc_node
*nchild
, *nnext
; /* of the Bl body */
1560 switch (nbody
->type
) {
1562 return(post_bl_block(mdoc
));
1564 return(post_bl_head(mdoc
));
1573 nchild
= nbody
->child
;
1574 while (NULL
!= nchild
) {
1575 if (MDOC_It
== nchild
->tok
|| MDOC_Sm
== nchild
->tok
) {
1576 nchild
= nchild
->next
;
1580 mandoc_msg(MANDOCERR_BL_MOVE
, mdoc
->parse
,
1581 nchild
->line
, nchild
->pos
,
1582 mdoc_macronames
[nchild
->tok
]);
1585 * Move the node out of the Bl block.
1586 * First, collect all required node pointers.
1589 nblock
= nbody
->parent
;
1590 nprev
= nblock
->prev
;
1591 nparent
= nblock
->parent
;
1592 nnext
= nchild
->next
;
1595 * Unlink this child.
1598 assert(NULL
== nchild
->prev
);
1599 if (0 == --nbody
->nchild
) {
1600 nbody
->child
= NULL
;
1602 assert(NULL
== nnext
);
1604 nbody
->child
= nnext
;
1609 * Relink this child.
1612 nchild
->parent
= nparent
;
1613 nchild
->prev
= nprev
;
1614 nchild
->next
= nblock
;
1616 nblock
->prev
= nchild
;
1619 nparent
->child
= nchild
;
1621 nprev
->next
= nchild
;
1639 ebool(struct mdoc
*mdoc
)
1641 struct mdoc_node
*nch
;
1644 tok
= mdoc
->last
->tok
;
1645 nch
= mdoc
->last
->child
;
1649 mdoc
->flags
^= MDOC_SMOFF
;
1653 check_count(mdoc
, MDOC_ELEM
, CHECK_WARN
, CHECK_LT
, 2);
1655 assert(MDOC_TEXT
== nch
->type
);
1657 if (0 == strcmp(nch
->string
, "on")) {
1659 mdoc
->flags
&= ~MDOC_SMOFF
;
1662 if (0 == strcmp(nch
->string
, "off")) {
1664 mdoc
->flags
|= MDOC_SMOFF
;
1668 mandoc_vmsg(MANDOCERR_SM_BAD
,
1669 mdoc
->parse
, nch
->line
, nch
->pos
,
1670 "%s %s", mdoc_macronames
[tok
], nch
->string
);
1671 return(mdoc_node_relink(mdoc
, nch
));
1675 post_root(POST_ARGS
)
1677 struct mdoc_node
*n
;
1679 /* Add missing prologue data. */
1681 if (mdoc
->meta
.date
== NULL
)
1682 mdoc
->meta
.date
= mdoc
->quick
?
1684 mandoc_normdate(mdoc
->parse
, NULL
, 0, 0);
1686 if (mdoc
->meta
.title
== NULL
) {
1687 mandoc_msg(MANDOCERR_DT_NOTITLE
,
1688 mdoc
->parse
, 0, 0, "EOF");
1689 mdoc
->meta
.title
= mandoc_strdup("UNTITLED");
1692 if (mdoc
->meta
.vol
== NULL
)
1693 mdoc
->meta
.vol
= mandoc_strdup("LOCAL");
1695 if (mdoc
->meta
.os
== NULL
) {
1696 mandoc_msg(MANDOCERR_OS_MISSING
,
1697 mdoc
->parse
, 0, 0, NULL
);
1698 mdoc
->meta
.os
= mandoc_strdup("");
1701 /* Check that we begin with a proper `Sh'. */
1703 n
= mdoc
->first
->child
;
1704 while (n
!= NULL
&& mdoc_macros
[n
->tok
].flags
& MDOC_PROLOGUE
)
1708 mandoc_msg(MANDOCERR_DOC_EMPTY
, mdoc
->parse
, 0, 0, NULL
);
1709 else if (n
->tok
!= MDOC_Sh
)
1710 mandoc_msg(MANDOCERR_SEC_BEFORE
, mdoc
->parse
,
1711 n
->line
, n
->pos
, mdoc_macronames
[n
->tok
]);
1719 struct mdoc_node
*n
, *nch
;
1726 mandoc_msg(MANDOCERR_MACRO_EMPTY
, mdoc
->parse
,
1727 n
->line
, n
->pos
, mdoc_macronames
[n
->tok
]);
1728 mdoc_node_delete(mdoc
, n
);
1732 assert(MDOC_TEXT
== nch
->type
);
1734 if (NULL
== (p
= mdoc_a2st(nch
->string
))) {
1735 mandoc_vmsg(MANDOCERR_ST_BAD
, mdoc
->parse
,
1736 nch
->line
, nch
->pos
, "St %s", nch
->string
);
1737 mdoc_node_delete(mdoc
, n
);
1740 nch
->string
= mandoc_strdup(p
);
1749 struct mdoc_node
*nn
, *next
, *prev
;
1752 switch (mdoc
->last
->type
) {
1754 check_count(mdoc
, MDOC_HEAD
, CHECK_WARN
, CHECK_EQ
, 0);
1757 if (mdoc
->last
->child
)
1759 check_count(mdoc
, MDOC_BODY
, CHECK_WARN
, CHECK_GT
, 0);
1766 * The full `Rs' block needs special handling to order the
1767 * sub-elements according to `rsord'. Pick through each element
1768 * and correctly order it. This is an insertion sort.
1772 for (nn
= mdoc
->last
->child
->next
; nn
; nn
= next
) {
1773 /* Determine order of `nn'. */
1774 for (i
= 0; i
< RSORD_MAX
; i
++)
1775 if (rsord
[i
] == nn
->tok
)
1778 if (i
== RSORD_MAX
) {
1779 mandoc_msg(MANDOCERR_RS_BAD
,
1780 mdoc
->parse
, nn
->line
, nn
->pos
,
1781 mdoc_macronames
[nn
->tok
]);
1783 } else if (MDOC__J
== nn
->tok
|| MDOC__B
== nn
->tok
)
1784 mdoc
->last
->norm
->Rs
.quote_T
++;
1787 * Remove `nn' from the chain. This somewhat
1788 * repeats mdoc_node_unlink(), but since we're
1789 * just re-ordering, there's no need for the
1790 * full unlink process.
1793 if (NULL
!= (next
= nn
->next
))
1794 next
->prev
= nn
->prev
;
1796 if (NULL
!= (prev
= nn
->prev
))
1797 prev
->next
= nn
->next
;
1799 nn
->prev
= nn
->next
= NULL
;
1802 * Scan back until we reach a node that's
1803 * ordered before `nn'.
1806 for ( ; prev
; prev
= prev
->prev
) {
1807 /* Determine order of `prev'. */
1808 for (j
= 0; j
< RSORD_MAX
; j
++)
1809 if (rsord
[j
] == prev
->tok
)
1819 * Set `nn' back into its correct place in front
1820 * of the `prev' node.
1827 prev
->next
->prev
= nn
;
1828 nn
->next
= prev
->next
;
1831 mdoc
->last
->child
->prev
= nn
;
1832 nn
->next
= mdoc
->last
->child
;
1833 mdoc
->last
->child
= nn
;
1841 * For some arguments of some macros,
1842 * convert all breakable hyphens into ASCII_HYPH.
1845 post_hyph(POST_ARGS
)
1847 struct mdoc_node
*n
, *nch
;
1853 if (MDOC_Sh
== n
->tok
|| MDOC_Ss
== n
->tok
)
1857 if (MDOC_D1
== n
->tok
|| MDOC_Nd
== n
->tok
)
1866 for (nch
= n
->child
; nch
; nch
= nch
->next
) {
1867 if (MDOC_TEXT
!= nch
->type
)
1872 while ('\0' != *(++cp
))
1874 isalpha((unsigned char)cp
[-1]) &&
1875 isalpha((unsigned char)cp
[1]))
1882 post_hyphtext(POST_ARGS
)
1886 return(post_hyph(mdoc
));
1893 if (MDOC_LINE
& mdoc
->last
->flags
)
1894 mandoc_msg(MANDOCERR_NS_SKIP
, mdoc
->parse
,
1895 mdoc
->last
->line
, mdoc
->last
->pos
, NULL
);
1905 switch (mdoc
->last
->type
) {
1907 return(post_sh_head(mdoc
));
1909 switch (mdoc
->lastsec
) {
1911 return(post_sh_name(mdoc
));
1913 return(post_sh_see_also(mdoc
));
1915 return(post_sh_authors(mdoc
));
1928 post_sh_name(POST_ARGS
)
1930 struct mdoc_node
*n
;
1933 * Warn if the NAME section doesn't contain the `Nm' and `Nd'
1934 * macros (can have multiple `Nm' and one `Nd'). Note that the
1935 * children of the BODY declaration can also be "text".
1938 if (NULL
== (n
= mdoc
->last
->child
)) {
1939 mandoc_msg(MANDOCERR_NAMESEC_BAD
, mdoc
->parse
,
1940 mdoc
->last
->line
, mdoc
->last
->pos
, "empty");
1944 for ( ; n
&& n
->next
; n
= n
->next
) {
1945 if (MDOC_ELEM
== n
->type
&& MDOC_Nm
== n
->tok
)
1947 if (MDOC_TEXT
== n
->type
)
1949 mandoc_msg(MANDOCERR_NAMESEC_BAD
, mdoc
->parse
,
1950 n
->line
, n
->pos
, mdoc_macronames
[n
->tok
]);
1954 if (MDOC_BLOCK
== n
->type
&& MDOC_Nd
== n
->tok
)
1957 mandoc_msg(MANDOCERR_NAMESEC_BAD
, mdoc
->parse
,
1958 n
->line
, n
->pos
, mdoc_macronames
[n
->tok
]);
1963 post_sh_see_also(POST_ARGS
)
1965 const struct mdoc_node
*n
;
1966 const char *name
, *sec
;
1967 const char *lastname
, *lastsec
, *lastpunct
;
1970 n
= mdoc
->last
->child
;
1971 lastname
= lastsec
= lastpunct
= NULL
;
1973 if (n
->tok
!= MDOC_Xr
|| n
->nchild
< 2)
1976 /* Process one .Xr node. */
1978 name
= n
->child
->string
;
1979 sec
= n
->child
->next
->string
;
1980 if (lastsec
!= NULL
) {
1981 if (lastpunct
[0] != ',' || lastpunct
[1] != '\0')
1982 mandoc_vmsg(MANDOCERR_XR_PUNCT
,
1983 mdoc
->parse
, n
->line
, n
->pos
,
1984 "%s before %s(%s)", lastpunct
,
1986 cmp
= strcmp(lastsec
, sec
);
1988 mandoc_vmsg(MANDOCERR_XR_ORDER
,
1989 mdoc
->parse
, n
->line
, n
->pos
,
1990 "%s(%s) after %s(%s)", name
,
1991 sec
, lastname
, lastsec
);
1992 else if (cmp
== 0 &&
1993 strcasecmp(lastname
, name
) > 0)
1994 mandoc_vmsg(MANDOCERR_XR_ORDER
,
1995 mdoc
->parse
, n
->line
, n
->pos
,
1996 "%s after %s", name
, lastname
);
2001 /* Process the following node. */
2006 if (n
->tok
== MDOC_Xr
) {
2010 if (n
->type
!= MDOC_TEXT
)
2012 for (name
= n
->string
; *name
!= '\0'; name
++)
2013 if (isalpha((const unsigned char)*name
))
2015 lastpunct
= n
->string
;
2016 if (n
->next
== NULL
)
2017 mandoc_vmsg(MANDOCERR_XR_PUNCT
, mdoc
->parse
,
2018 n
->line
, n
->pos
, "%s after %s(%s)",
2019 lastpunct
, lastname
, lastsec
);
2026 child_an(const struct mdoc_node
*n
)
2029 for (n
= n
->child
; n
!= NULL
; n
= n
->next
)
2030 if ((n
->tok
== MDOC_An
&& n
->nchild
) || child_an(n
))
2036 post_sh_authors(POST_ARGS
)
2039 if ( ! child_an(mdoc
->last
))
2040 mandoc_msg(MANDOCERR_AN_MISSING
, mdoc
->parse
,
2041 mdoc
->last
->line
, mdoc
->last
->pos
, NULL
);
2046 post_sh_head(POST_ARGS
)
2048 struct mdoc_node
*n
;
2049 const char *goodsec
;
2054 * Process a new section. Sections are either "named" or
2055 * "custom". Custom sections are user-defined, while named ones
2056 * follow a conventional order and may only appear in certain
2062 mdoc_deroff(&secname
, mdoc
->last
);
2063 sec
= NULL
== secname
? SEC_CUSTOM
: a2sec(secname
);
2065 /* The NAME should be first. */
2067 if (SEC_NAME
!= sec
&& SEC_NONE
== mdoc
->lastnamed
)
2068 mandoc_vmsg(MANDOCERR_NAMESEC_FIRST
, mdoc
->parse
,
2069 mdoc
->last
->line
, mdoc
->last
->pos
,
2072 /* The SYNOPSIS gets special attention in other areas. */
2074 if (SEC_SYNOPSIS
== sec
) {
2075 roff_setreg(mdoc
->roff
, "nS", 1, '=');
2076 mdoc
->flags
|= MDOC_SYNOPSIS
;
2078 roff_setreg(mdoc
->roff
, "nS", 0, '=');
2079 mdoc
->flags
&= ~MDOC_SYNOPSIS
;
2082 /* Mark our last section. */
2084 mdoc
->lastsec
= sec
;
2087 * Set the section attribute for the current HEAD, for its
2088 * parent BLOCK, and for the HEAD children; the latter can
2089 * only be TEXT nodes, so no recursion is needed.
2090 * For other blocks and elements, including .Sh BODY, this is
2091 * done when allocating the node data structures, but for .Sh
2092 * BLOCK and HEAD, the section is still unknown at that time.
2095 mdoc
->last
->parent
->sec
= sec
;
2096 mdoc
->last
->sec
= sec
;
2097 for (n
= mdoc
->last
->child
; n
; n
= n
->next
)
2100 /* We don't care about custom sections after this. */
2102 if (SEC_CUSTOM
== sec
) {
2108 * Check whether our non-custom section is being repeated or is
2112 if (sec
== mdoc
->lastnamed
)
2113 mandoc_vmsg(MANDOCERR_SEC_REP
, mdoc
->parse
,
2114 mdoc
->last
->line
, mdoc
->last
->pos
,
2117 if (sec
< mdoc
->lastnamed
)
2118 mandoc_vmsg(MANDOCERR_SEC_ORDER
, mdoc
->parse
,
2119 mdoc
->last
->line
, mdoc
->last
->pos
,
2122 /* Mark the last named section. */
2124 mdoc
->lastnamed
= sec
;
2126 /* Check particular section/manual conventions. */
2128 if (mdoc
->meta
.msec
== NULL
) {
2136 if (*mdoc
->meta
.msec
== '4')
2138 goodsec
= "2, 3, 4, 9";
2140 case SEC_RETURN_VALUES
:
2143 if (*mdoc
->meta
.msec
== '2')
2145 if (*mdoc
->meta
.msec
== '3')
2147 if (NULL
== goodsec
)
2148 goodsec
= "2, 3, 9";
2151 if (*mdoc
->meta
.msec
== '9')
2153 if (NULL
== goodsec
)
2155 mandoc_vmsg(MANDOCERR_SEC_MSEC
, mdoc
->parse
,
2156 mdoc
->last
->line
, mdoc
->last
->pos
,
2157 "Sh %s for %s only", secname
, goodsec
);
2168 post_ignpar(POST_ARGS
)
2170 struct mdoc_node
*np
;
2175 if (MDOC_BODY
!= mdoc
->last
->type
)
2178 if (NULL
!= (np
= mdoc
->last
->child
))
2179 if (MDOC_Pp
== np
->tok
|| MDOC_Lp
== np
->tok
) {
2180 mandoc_vmsg(MANDOCERR_PAR_SKIP
,
2181 mdoc
->parse
, np
->line
, np
->pos
,
2182 "%s after %s", mdoc_macronames
[np
->tok
],
2183 mdoc_macronames
[mdoc
->last
->tok
]);
2184 mdoc_node_delete(mdoc
, np
);
2187 if (NULL
!= (np
= mdoc
->last
->last
))
2188 if (MDOC_Pp
== np
->tok
|| MDOC_Lp
== np
->tok
) {
2189 mandoc_vmsg(MANDOCERR_PAR_SKIP
, mdoc
->parse
,
2190 np
->line
, np
->pos
, "%s at the end of %s",
2191 mdoc_macronames
[np
->tok
],
2192 mdoc_macronames
[mdoc
->last
->tok
]);
2193 mdoc_node_delete(mdoc
, np
);
2203 if (NULL
== mdoc
->last
)
2205 if (MDOC_ELEM
!= n
->type
&& MDOC_BLOCK
!= n
->type
)
2209 * Don't allow prior `Lp' or `Pp' prior to a paragraph-type
2210 * block: `Lp', `Pp', or non-compact `Bd' or `Bl'.
2213 if (MDOC_Pp
!= mdoc
->last
->tok
&&
2214 MDOC_Lp
!= mdoc
->last
->tok
&&
2215 MDOC_br
!= mdoc
->last
->tok
)
2217 if (MDOC_Bl
== n
->tok
&& n
->norm
->Bl
.comp
)
2219 if (MDOC_Bd
== n
->tok
&& n
->norm
->Bd
.comp
)
2221 if (MDOC_It
== n
->tok
&& n
->parent
->norm
->Bl
.comp
)
2224 mandoc_vmsg(MANDOCERR_PAR_SKIP
, mdoc
->parse
,
2225 mdoc
->last
->line
, mdoc
->last
->pos
,
2226 "%s before %s", mdoc_macronames
[mdoc
->last
->tok
],
2227 mdoc_macronames
[n
->tok
]);
2228 mdoc_node_delete(mdoc
, mdoc
->last
);
2235 struct mdoc_node
*np
;
2237 if (mdoc
->last
->tok
== MDOC_sp
)
2242 if (MDOC_ELEM
!= mdoc
->last
->type
&&
2243 MDOC_BLOCK
!= mdoc
->last
->type
)
2246 if (NULL
== (np
= mdoc
->last
->prev
)) {
2247 np
= mdoc
->last
->parent
;
2248 if (MDOC_Sh
!= np
->tok
&& MDOC_Ss
!= np
->tok
)
2251 if (MDOC_Pp
!= np
->tok
&& MDOC_Lp
!= np
->tok
&&
2252 (MDOC_br
!= mdoc
->last
->tok
||
2253 (MDOC_sp
!= np
->tok
&& MDOC_br
!= np
->tok
)))
2257 mandoc_vmsg(MANDOCERR_PAR_SKIP
, mdoc
->parse
,
2258 mdoc
->last
->line
, mdoc
->last
->pos
,
2259 "%s after %s", mdoc_macronames
[mdoc
->last
->tok
],
2260 mdoc_macronames
[np
->tok
]);
2261 mdoc_node_delete(mdoc
, mdoc
->last
);
2266 pre_literal(PRE_ARGS
)
2269 pre_display(mdoc
, n
);
2271 if (MDOC_BODY
!= n
->type
)
2275 * The `Dl' (note "el" not "one") and `Bd -literal' and `Bd
2276 * -unfilled' macros set MDOC_LITERAL on entrance to the body.
2281 mdoc
->flags
|= MDOC_LITERAL
;
2284 if (DISP_literal
== n
->norm
->Bd
.type
)
2285 mdoc
->flags
|= MDOC_LITERAL
;
2286 if (DISP_unfilled
== n
->norm
->Bd
.type
)
2287 mdoc
->flags
|= MDOC_LITERAL
;
2300 struct mdoc_node
*n
;
2303 if (mdoc
->meta
.date
)
2304 free(mdoc
->meta
.date
);
2307 if (NULL
== n
->child
|| '\0' == n
->child
->string
[0]) {
2308 mdoc
->meta
.date
= mdoc
->quick
? mandoc_strdup("") :
2309 mandoc_normdate(mdoc
->parse
, NULL
, n
->line
, n
->pos
);
2314 mdoc_deroff(&datestr
, n
);
2316 mdoc
->meta
.date
= datestr
;
2318 mdoc
->meta
.date
= mandoc_normdate(mdoc
->parse
,
2319 datestr
, n
->line
, n
->pos
);
2323 mdoc_node_delete(mdoc
, n
);
2330 struct mdoc_node
*nn
, *n
;
2336 free(mdoc
->meta
.title
);
2337 free(mdoc
->meta
.msec
);
2338 free(mdoc
->meta
.vol
);
2339 free(mdoc
->meta
.arch
);
2341 mdoc
->meta
.title
= NULL
;
2342 mdoc
->meta
.msec
= NULL
;
2343 mdoc
->meta
.vol
= NULL
;
2344 mdoc
->meta
.arch
= NULL
;
2346 /* First check that all characters are uppercase. */
2348 if (NULL
!= (nn
= n
->child
))
2349 for (p
= nn
->string
; *p
; p
++) {
2350 if (toupper((unsigned char)*p
) == *p
)
2352 mandoc_vmsg(MANDOCERR_TITLE_CASE
,
2353 mdoc
->parse
, nn
->line
,
2354 nn
->pos
+ (p
- nn
->string
),
2355 "Dt %s", nn
->string
);
2359 /* No argument: msec and arch remain NULL. */
2361 if (NULL
== (nn
= n
->child
)) {
2362 mandoc_msg(MANDOCERR_DT_NOTITLE
,
2363 mdoc
->parse
, n
->line
, n
->pos
, "Dt");
2364 mdoc
->meta
.title
= mandoc_strdup("UNTITLED");
2365 mdoc
->meta
.vol
= mandoc_strdup("LOCAL");
2369 /* One argument: msec and arch remain NULL. */
2371 mdoc
->meta
.title
= mandoc_strdup(
2372 '\0' == nn
->string
[0] ? "UNTITLED" : nn
->string
);
2374 if (NULL
== (nn
= nn
->next
)) {
2375 mandoc_vmsg(MANDOCERR_MSEC_MISSING
,
2376 mdoc
->parse
, n
->line
, n
->pos
,
2377 "Dt %s", mdoc
->meta
.title
);
2378 mdoc
->meta
.vol
= mandoc_strdup("LOCAL");
2382 /* Handles: `.Dt TITLE SEC'
2384 * volume = SEC is msec ? format(msec) : SEC,
2385 * msec = SEC is msec ? atoi(msec) : 0,
2389 cp
= mandoc_a2msec(nn
->string
);
2391 mdoc
->meta
.vol
= mandoc_strdup(cp
);
2392 mdoc
->meta
.msec
= mandoc_strdup(nn
->string
);
2394 mandoc_vmsg(MANDOCERR_MSEC_BAD
, mdoc
->parse
,
2395 nn
->line
, nn
->pos
, "Dt ... %s", nn
->string
);
2396 mdoc
->meta
.vol
= mandoc_strdup(nn
->string
);
2397 mdoc
->meta
.msec
= mandoc_strdup(nn
->string
);
2400 if (NULL
== (nn
= nn
->next
))
2403 /* Handles: `.Dt TITLE SEC VOL'
2405 * volume = VOL is vol ? format(VOL) :
2406 * VOL is arch ? format(arch) :
2410 cp
= mdoc_a2vol(nn
->string
);
2412 free(mdoc
->meta
.vol
);
2413 mdoc
->meta
.vol
= mandoc_strdup(cp
);
2415 cp
= mdoc_a2arch(nn
->string
);
2417 mandoc_vmsg(MANDOCERR_ARCH_BAD
, mdoc
->parse
,
2418 nn
->line
, nn
->pos
, "Dt ... %s", nn
->string
);
2419 free(mdoc
->meta
.vol
);
2420 mdoc
->meta
.vol
= mandoc_strdup(nn
->string
);
2422 mdoc
->meta
.arch
= mandoc_strdup(cp
);
2425 /* Ignore any subsequent parameters... */
2426 /* FIXME: warn about subsequent parameters. */
2428 mdoc_node_delete(mdoc
, n
);
2435 struct mdoc_node
*n
;
2438 * Make `Bx's second argument always start with an uppercase
2439 * letter. Groff checks if it's an "accepted" term, but we just
2440 * uppercase blindly.
2443 n
= mdoc
->last
->child
;
2444 if (n
&& NULL
!= (n
= n
->next
))
2445 *n
->string
= (char)toupper((unsigned char)*n
->string
);
2454 struct utsname utsname
;
2455 static char *defbuf
;
2457 struct mdoc_node
*n
;
2462 * Set the operating system by way of the `Os' macro.
2463 * The order of precedence is:
2464 * 1. the argument of the `Os' macro, unless empty
2465 * 2. the -Ios=foo command line argument, if provided
2466 * 3. -DOSNAME="\"foo\"", if provided during compilation
2467 * 4. "sysname release" from uname(3)
2470 free(mdoc
->meta
.os
);
2471 mdoc
->meta
.os
= NULL
;
2472 mdoc_deroff(&mdoc
->meta
.os
, n
);
2477 mdoc
->meta
.os
= mandoc_strdup(mdoc
->defos
);
2482 mdoc
->meta
.os
= mandoc_strdup(OSNAME
);
2484 if (NULL
== defbuf
) {
2485 if (-1 == uname(&utsname
)) {
2486 mandoc_msg(MANDOCERR_OS_UNAME
, mdoc
->parse
,
2487 n
->line
, n
->pos
, "Os");
2488 defbuf
= mandoc_strdup("UNKNOWN");
2490 mandoc_asprintf(&defbuf
, "%s %s",
2491 utsname
.sysname
, utsname
.release
);
2493 mdoc
->meta
.os
= mandoc_strdup(defbuf
);
2497 mdoc_node_delete(mdoc
, n
);
2502 * If no argument is provided,
2503 * fill in the name of the current manual page.
2508 struct mdoc_node
*n
;
2515 if (mdoc
->meta
.name
== NULL
) {
2516 mandoc_msg(MANDOCERR_EX_NONAME
, mdoc
->parse
,
2517 n
->line
, n
->pos
, "Ex");
2521 mdoc
->next
= MDOC_NEXT_CHILD
;
2523 if ( ! mdoc_word_alloc(mdoc
, n
->line
, n
->pos
, mdoc
->meta
.name
))
2530 static enum mdoc_sec
2531 a2sec(const char *p
)
2535 for (i
= 0; i
< (int)SEC__MAX
; i
++)
2536 if (secnames
[i
] && 0 == strcmp(p
, secnames
[i
]))
2537 return((enum mdoc_sec
)i
);
2543 macro2len(enum mdoct macro
)