]>
git.cameronkatri.com Git - mandoc.git/blob - mdoc_validate.c
1 /* $Id: mdoc_validate.c,v 1.251 2014/10/11 21:34:04 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
;
1010 n
= mdoc
->last
->child
;
1011 pos
= strcspn(n
->string
, "()");
1012 if (n
->string
[pos
] != '\0')
1013 mandoc_msg(MANDOCERR_FN_PAREN
, mdoc
->parse
,
1014 n
->line
, n
->pos
+ pos
, n
->string
);
1033 if (mdoc
->last
->type
== MDOC_HEAD
&& mdoc
->last
->nchild
)
1041 const struct mdoc_node
*n
;
1044 for (n
= mdoc
->last
->child
; n
!= NULL
; n
= n
->next
) {
1045 for (cp
= n
->string
; *cp
!= '\0'; cp
++) {
1046 /* Ignore callbacks and alterations. */
1047 if (*cp
== '(' || *cp
== '{')
1051 mandoc_msg(MANDOCERR_FA_COMMA
, mdoc
->parse
,
1052 n
->line
, n
->pos
+ (cp
- n
->string
),
1063 const struct mdoc_node
*n
;
1066 * The Vt macro comes in both ELEM and BLOCK form, both of which
1067 * have different syntaxes (yet more context-sensitive
1068 * behaviour). ELEM types must have a child, which is already
1069 * guaranteed by the in_line parsing routine; BLOCK types,
1070 * specifically the BODY, should only have TEXT children.
1073 if (MDOC_BODY
!= mdoc
->last
->type
)
1076 for (n
= mdoc
->last
->child
; n
; n
= n
->next
)
1077 if (MDOC_TEXT
!= n
->type
)
1078 mandoc_msg(MANDOCERR_VT_CHILD
, mdoc
->parse
,
1079 n
->line
, n
->pos
, mdoc_macronames
[n
->tok
]);
1088 if (NULL
!= mdoc
->meta
.name
)
1091 mdoc_deroff(&mdoc
->meta
.name
, mdoc
->last
);
1093 if (NULL
== mdoc
->meta
.name
)
1094 mandoc_msg(MANDOCERR_NM_NONAME
, mdoc
->parse
,
1095 mdoc
->last
->line
, mdoc
->last
->pos
, "Nm");
1104 return(post_hyph(mdoc
));
1112 return(post_hyph(mdoc
));
1116 post_literal(POST_ARGS
)
1119 if (mdoc
->last
->tok
== MDOC_Bd
)
1124 * The `Dl' (note "el" not "one") and `Bd' macros unset the
1125 * MDOC_LITERAL flag as they leave. Note that `Bd' only sets
1126 * this in literal mode, but it doesn't hurt to just switch it
1127 * off in general since displays can't be nested.
1130 if (MDOC_BODY
== mdoc
->last
->type
)
1131 mdoc
->flags
&= ~MDOC_LITERAL
;
1137 post_defaults(POST_ARGS
)
1139 struct mdoc_node
*nn
;
1142 * The `Ar' defaults to "file ..." if no value is provided as an
1143 * argument; the `Mt' and `Pa' macros use "~"; the `Li' just
1144 * gets an empty string.
1147 if (mdoc
->last
->child
)
1151 mdoc
->next
= MDOC_NEXT_CHILD
;
1155 if ( ! mdoc_word_alloc(mdoc
, nn
->line
, nn
->pos
, "file"))
1157 if ( ! mdoc_word_alloc(mdoc
, nn
->line
, nn
->pos
, "..."))
1161 if ( ! mdoc_word_alloc(mdoc
, nn
->line
, nn
->pos
, ""))
1167 if ( ! mdoc_word_alloc(mdoc
, nn
->line
, nn
->pos
, "~"))
1182 struct mdoc_node
*n
;
1183 const char *std_att
;
1187 if (n
->child
== NULL
) {
1188 mdoc
->next
= MDOC_NEXT_CHILD
;
1189 if ( ! mdoc_word_alloc(mdoc
, n
->line
, n
->pos
, "AT&T UNIX"))
1196 * If we have a child, look it up in the standard keys. If a
1197 * key exist, use that instead of the child; if it doesn't,
1198 * prefix "AT&T UNIX " to the existing data.
1202 assert(MDOC_TEXT
== n
->type
);
1203 if (NULL
== (std_att
= mdoc_a2att(n
->string
))) {
1204 mandoc_vmsg(MANDOCERR_AT_BAD
, mdoc
->parse
,
1205 n
->line
, n
->pos
, "At %s", n
->string
);
1206 mandoc_asprintf(&att
, "AT&T UNIX %s", n
->string
);
1208 att
= mandoc_strdup(std_att
);
1218 struct mdoc_node
*np
;
1221 if (AUTH__NONE
== np
->norm
->An
.auth
) {
1223 check_count(mdoc
, MDOC_ELEM
, CHECK_WARN
, CHECK_GT
, 0);
1224 } else if (np
->child
)
1225 check_count(mdoc
, MDOC_ELEM
, CHECK_WARN
, CHECK_EQ
, 0);
1234 if (MDOC_BLOCK
== mdoc
->last
->type
)
1235 mdoc
->last
->norm
->Es
= mdoc
->last_es
;
1243 mdoc
->last_es
= mdoc
->last
;
1252 struct mdoc_node
*nbl
, *nit
, *nch
;
1255 if (MDOC_BLOCK
!= nit
->type
)
1258 nbl
= nit
->parent
->parent
;
1259 lt
= nbl
->norm
->Bl
.type
;
1271 if (NULL
== nit
->head
->child
)
1272 mandoc_vmsg(MANDOCERR_IT_NOHEAD
,
1273 mdoc
->parse
, nit
->line
, nit
->pos
,
1275 mdoc_argnames
[nbl
->args
->argv
[0].arg
]);
1284 if (NULL
== nit
->body
->child
)
1285 mandoc_vmsg(MANDOCERR_IT_NOBODY
,
1286 mdoc
->parse
, nit
->line
, nit
->pos
,
1288 mdoc_argnames
[nbl
->args
->argv
[0].arg
]);
1291 if (NULL
!= nit
->head
->child
)
1292 mandoc_vmsg(MANDOCERR_ARG_SKIP
,
1293 mdoc
->parse
, nit
->line
, nit
->pos
,
1294 "It %s", nit
->head
->child
->string
);
1297 cols
= (int)nbl
->norm
->Bl
.ncols
;
1299 assert(NULL
== nit
->head
->child
);
1301 for (i
= 0, nch
= nit
->child
; nch
; nch
= nch
->next
)
1302 if (MDOC_BODY
== nch
->type
)
1305 if (i
< cols
|| i
> cols
+ 1)
1306 mandoc_vmsg(MANDOCERR_ARGCOUNT
,
1307 mdoc
->parse
, nit
->line
, nit
->pos
,
1308 "columns == %d (have %d)", cols
, i
);
1318 post_bl_block(POST_ARGS
)
1320 struct mdoc_node
*n
, *ni
, *nc
;
1323 * These are fairly complicated, so we've broken them into two
1324 * functions. post_bl_block_tag() is called when a -tag is
1325 * specified, but no -width (it must be guessed). The second
1326 * when a -width is specified (macro indicators must be
1327 * rewritten into real lengths).
1332 if (LIST_tag
== n
->norm
->Bl
.type
&&
1333 NULL
== n
->norm
->Bl
.width
) {
1334 if ( ! post_bl_block_tag(mdoc
))
1336 assert(n
->norm
->Bl
.width
);
1337 } else if (NULL
!= n
->norm
->Bl
.width
) {
1338 if ( ! post_bl_block_width(mdoc
))
1340 assert(n
->norm
->Bl
.width
);
1343 for (ni
= n
->body
->child
; ni
; ni
= ni
->next
) {
1344 if (NULL
== ni
->body
)
1346 nc
= ni
->body
->last
;
1347 while (NULL
!= nc
) {
1359 if (NULL
== ni
->next
) {
1360 mandoc_msg(MANDOCERR_PAR_MOVE
,
1361 mdoc
->parse
, nc
->line
, nc
->pos
,
1362 mdoc_macronames
[nc
->tok
]);
1363 if ( ! mdoc_node_relink(mdoc
, nc
))
1365 } else if (0 == n
->norm
->Bl
.comp
&&
1366 LIST_column
!= n
->norm
->Bl
.type
) {
1367 mandoc_vmsg(MANDOCERR_PAR_SKIP
,
1368 mdoc
->parse
, nc
->line
, nc
->pos
,
1370 mdoc_macronames
[nc
->tok
]);
1371 mdoc_node_delete(mdoc
, nc
);
1374 nc
= ni
->body
->last
;
1381 post_bl_block_width(POST_ARGS
)
1386 struct mdoc_node
*n
;
1392 * Calculate the real width of a list from the -width string,
1393 * which may contain a macro (with a known default width), a
1394 * literal string, or a scaling width.
1396 * If the value to -width is a macro, then we re-write it to be
1397 * the macro's width as set in share/tmac/mdoc/doc-common.
1400 if (0 == strcmp(n
->norm
->Bl
.width
, "Ds"))
1402 else if (MDOC_MAX
== (tok
= mdoc_hash_find(n
->norm
->Bl
.width
)))
1405 width
= macro2len(tok
);
1407 /* The value already exists: free and reallocate it. */
1411 for (i
= 0; i
< (int)n
->args
->argc
; i
++)
1412 if (MDOC_Width
== n
->args
->argv
[i
].arg
)
1415 assert(i
< (int)n
->args
->argc
);
1417 (void)snprintf(buf
, sizeof(buf
), "%un", (unsigned int)width
);
1418 free(n
->args
->argv
[i
].value
[0]);
1419 n
->args
->argv
[i
].value
[0] = mandoc_strdup(buf
);
1421 /* Set our width! */
1422 n
->norm
->Bl
.width
= n
->args
->argv
[i
].value
[0];
1427 post_bl_block_tag(POST_ARGS
)
1429 struct mdoc_node
*n
, *nn
;
1435 * Calculate the -width for a `Bl -tag' list if it hasn't been
1436 * provided. Uses the first head macro. NOTE AGAIN: this is
1437 * ONLY if the -width argument has NOT been provided. See
1438 * post_bl_block_width() for converting the -width string.
1444 for (nn
= n
->body
->child
; nn
; nn
= nn
->next
) {
1445 if (MDOC_It
!= nn
->tok
)
1448 assert(MDOC_BLOCK
== nn
->type
);
1449 nn
= nn
->head
->child
;
1454 if (MDOC_TEXT
== nn
->type
) {
1455 sz
= strlen(nn
->string
) + 1;
1459 if (0 != (ssz
= macro2len(nn
->tok
)))
1465 /* Defaults to ten ens. */
1467 (void)snprintf(buf
, sizeof(buf
), "%un", (unsigned int)sz
);
1470 * We have to dynamically add this to the macro's argument list.
1471 * We're guaranteed that a MDOC_Width doesn't already exist.
1475 i
= (int)(n
->args
->argc
)++;
1477 n
->args
->argv
= mandoc_reallocarray(n
->args
->argv
,
1478 n
->args
->argc
, sizeof(struct mdoc_argv
));
1480 n
->args
->argv
[i
].arg
= MDOC_Width
;
1481 n
->args
->argv
[i
].line
= n
->line
;
1482 n
->args
->argv
[i
].pos
= n
->pos
;
1483 n
->args
->argv
[i
].sz
= 1;
1484 n
->args
->argv
[i
].value
= mandoc_malloc(sizeof(char *));
1485 n
->args
->argv
[i
].value
[0] = mandoc_strdup(buf
);
1487 /* Set our width! */
1488 n
->norm
->Bl
.width
= n
->args
->argv
[i
].value
[0];
1493 post_bl_head(POST_ARGS
)
1495 struct mdoc_node
*np
, *nn
, *nnp
;
1496 struct mdoc_argv
*argv
;
1499 if (LIST_column
!= mdoc
->last
->norm
->Bl
.type
)
1500 /* FIXME: this should be ERROR class... */
1501 return(hwarn_eq0(mdoc
));
1504 * Append old-style lists, where the column width specifiers
1505 * trail as macro parameters, to the new-style ("normal-form")
1506 * lists where they're argument values following -column.
1509 if (mdoc
->last
->child
== NULL
)
1512 np
= mdoc
->last
->parent
;
1515 for (j
= 0; j
< (int)np
->args
->argc
; j
++)
1516 if (MDOC_Column
== np
->args
->argv
[j
].arg
)
1519 assert(j
< (int)np
->args
->argc
);
1522 * Accommodate for new-style groff column syntax. Shuffle the
1523 * child nodes, all of which must be TEXT, as arguments for the
1524 * column field. Then, delete the head children.
1527 argv
= np
->args
->argv
+ j
;
1529 argv
->sz
+= mdoc
->last
->nchild
;
1530 argv
->value
= mandoc_reallocarray(argv
->value
,
1531 argv
->sz
, sizeof(char *));
1533 mdoc
->last
->norm
->Bl
.ncols
= argv
->sz
;
1534 mdoc
->last
->norm
->Bl
.cols
= (void *)argv
->value
;
1536 for (nn
= mdoc
->last
->child
; nn
; i
++) {
1537 argv
->value
[i
] = nn
->string
;
1541 mdoc_node_delete(NULL
, nnp
);
1544 mdoc
->last
->nchild
= 0;
1545 mdoc
->last
->child
= NULL
;
1553 struct mdoc_node
*nparent
, *nprev
; /* of the Bl block */
1554 struct mdoc_node
*nblock
, *nbody
; /* of the Bl */
1555 struct mdoc_node
*nchild
, *nnext
; /* of the Bl body */
1558 switch (nbody
->type
) {
1560 return(post_bl_block(mdoc
));
1562 return(post_bl_head(mdoc
));
1571 nchild
= nbody
->child
;
1572 while (NULL
!= nchild
) {
1573 if (MDOC_It
== nchild
->tok
|| MDOC_Sm
== nchild
->tok
) {
1574 nchild
= nchild
->next
;
1578 mandoc_msg(MANDOCERR_BL_MOVE
, mdoc
->parse
,
1579 nchild
->line
, nchild
->pos
,
1580 mdoc_macronames
[nchild
->tok
]);
1583 * Move the node out of the Bl block.
1584 * First, collect all required node pointers.
1587 nblock
= nbody
->parent
;
1588 nprev
= nblock
->prev
;
1589 nparent
= nblock
->parent
;
1590 nnext
= nchild
->next
;
1593 * Unlink this child.
1596 assert(NULL
== nchild
->prev
);
1597 if (0 == --nbody
->nchild
) {
1598 nbody
->child
= NULL
;
1600 assert(NULL
== nnext
);
1602 nbody
->child
= nnext
;
1607 * Relink this child.
1610 nchild
->parent
= nparent
;
1611 nchild
->prev
= nprev
;
1612 nchild
->next
= nblock
;
1614 nblock
->prev
= nchild
;
1617 nparent
->child
= nchild
;
1619 nprev
->next
= nchild
;
1637 ebool(struct mdoc
*mdoc
)
1639 struct mdoc_node
*nch
;
1642 tok
= mdoc
->last
->tok
;
1643 nch
= mdoc
->last
->child
;
1647 mdoc
->flags
^= MDOC_SMOFF
;
1651 check_count(mdoc
, MDOC_ELEM
, CHECK_WARN
, CHECK_LT
, 2);
1653 assert(MDOC_TEXT
== nch
->type
);
1655 if (0 == strcmp(nch
->string
, "on")) {
1657 mdoc
->flags
&= ~MDOC_SMOFF
;
1660 if (0 == strcmp(nch
->string
, "off")) {
1662 mdoc
->flags
|= MDOC_SMOFF
;
1666 mandoc_vmsg(MANDOCERR_SM_BAD
,
1667 mdoc
->parse
, nch
->line
, nch
->pos
,
1668 "%s %s", mdoc_macronames
[tok
], nch
->string
);
1669 return(mdoc_node_relink(mdoc
, nch
));
1673 post_root(POST_ARGS
)
1675 struct mdoc_node
*n
;
1677 /* Add missing prologue data. */
1679 if (mdoc
->meta
.date
== NULL
)
1680 mdoc
->meta
.date
= mdoc
->quick
?
1682 mandoc_normdate(mdoc
->parse
, NULL
, 0, 0);
1684 if (mdoc
->meta
.title
== NULL
) {
1685 mandoc_msg(MANDOCERR_DT_NOTITLE
,
1686 mdoc
->parse
, 0, 0, "EOF");
1687 mdoc
->meta
.title
= mandoc_strdup("UNTITLED");
1690 if (mdoc
->meta
.vol
== NULL
)
1691 mdoc
->meta
.vol
= mandoc_strdup("LOCAL");
1693 if (mdoc
->meta
.os
== NULL
) {
1694 mandoc_msg(MANDOCERR_OS_MISSING
,
1695 mdoc
->parse
, 0, 0, NULL
);
1696 mdoc
->meta
.os
= mandoc_strdup("");
1699 /* Check that we begin with a proper `Sh'. */
1701 n
= mdoc
->first
->child
;
1702 while (n
!= NULL
&& mdoc_macros
[n
->tok
].flags
& MDOC_PROLOGUE
)
1706 mandoc_msg(MANDOCERR_DOC_EMPTY
, mdoc
->parse
, 0, 0, NULL
);
1707 else if (n
->tok
!= MDOC_Sh
)
1708 mandoc_msg(MANDOCERR_SEC_BEFORE
, mdoc
->parse
,
1709 n
->line
, n
->pos
, mdoc_macronames
[n
->tok
]);
1717 struct mdoc_node
*n
, *nch
;
1724 mandoc_msg(MANDOCERR_MACRO_EMPTY
, mdoc
->parse
,
1725 n
->line
, n
->pos
, mdoc_macronames
[n
->tok
]);
1726 mdoc_node_delete(mdoc
, n
);
1730 assert(MDOC_TEXT
== nch
->type
);
1732 if (NULL
== (p
= mdoc_a2st(nch
->string
))) {
1733 mandoc_vmsg(MANDOCERR_ST_BAD
, mdoc
->parse
,
1734 nch
->line
, nch
->pos
, "St %s", nch
->string
);
1735 mdoc_node_delete(mdoc
, n
);
1738 nch
->string
= mandoc_strdup(p
);
1747 struct mdoc_node
*nn
, *next
, *prev
;
1750 switch (mdoc
->last
->type
) {
1752 check_count(mdoc
, MDOC_HEAD
, CHECK_WARN
, CHECK_EQ
, 0);
1755 if (mdoc
->last
->child
)
1757 check_count(mdoc
, MDOC_BODY
, CHECK_WARN
, CHECK_GT
, 0);
1764 * The full `Rs' block needs special handling to order the
1765 * sub-elements according to `rsord'. Pick through each element
1766 * and correctly order it. This is an insertion sort.
1770 for (nn
= mdoc
->last
->child
->next
; nn
; nn
= next
) {
1771 /* Determine order of `nn'. */
1772 for (i
= 0; i
< RSORD_MAX
; i
++)
1773 if (rsord
[i
] == nn
->tok
)
1776 if (i
== RSORD_MAX
) {
1777 mandoc_msg(MANDOCERR_RS_BAD
,
1778 mdoc
->parse
, nn
->line
, nn
->pos
,
1779 mdoc_macronames
[nn
->tok
]);
1781 } else if (MDOC__J
== nn
->tok
|| MDOC__B
== nn
->tok
)
1782 mdoc
->last
->norm
->Rs
.quote_T
++;
1785 * Remove `nn' from the chain. This somewhat
1786 * repeats mdoc_node_unlink(), but since we're
1787 * just re-ordering, there's no need for the
1788 * full unlink process.
1791 if (NULL
!= (next
= nn
->next
))
1792 next
->prev
= nn
->prev
;
1794 if (NULL
!= (prev
= nn
->prev
))
1795 prev
->next
= nn
->next
;
1797 nn
->prev
= nn
->next
= NULL
;
1800 * Scan back until we reach a node that's
1801 * ordered before `nn'.
1804 for ( ; prev
; prev
= prev
->prev
) {
1805 /* Determine order of `prev'. */
1806 for (j
= 0; j
< RSORD_MAX
; j
++)
1807 if (rsord
[j
] == prev
->tok
)
1817 * Set `nn' back into its correct place in front
1818 * of the `prev' node.
1825 prev
->next
->prev
= nn
;
1826 nn
->next
= prev
->next
;
1829 mdoc
->last
->child
->prev
= nn
;
1830 nn
->next
= mdoc
->last
->child
;
1831 mdoc
->last
->child
= nn
;
1839 * For some arguments of some macros,
1840 * convert all breakable hyphens into ASCII_HYPH.
1843 post_hyph(POST_ARGS
)
1845 struct mdoc_node
*n
, *nch
;
1851 if (MDOC_Sh
== n
->tok
|| MDOC_Ss
== n
->tok
)
1855 if (MDOC_D1
== n
->tok
|| MDOC_Nd
== n
->tok
)
1864 for (nch
= n
->child
; nch
; nch
= nch
->next
) {
1865 if (MDOC_TEXT
!= nch
->type
)
1870 while ('\0' != *(++cp
))
1872 isalpha((unsigned char)cp
[-1]) &&
1873 isalpha((unsigned char)cp
[1]))
1880 post_hyphtext(POST_ARGS
)
1884 return(post_hyph(mdoc
));
1891 if (MDOC_LINE
& mdoc
->last
->flags
)
1892 mandoc_msg(MANDOCERR_NS_SKIP
, mdoc
->parse
,
1893 mdoc
->last
->line
, mdoc
->last
->pos
, NULL
);
1903 switch (mdoc
->last
->type
) {
1905 return(post_sh_head(mdoc
));
1907 switch (mdoc
->lastsec
) {
1909 return(post_sh_name(mdoc
));
1911 return(post_sh_see_also(mdoc
));
1913 return(post_sh_authors(mdoc
));
1926 post_sh_name(POST_ARGS
)
1928 struct mdoc_node
*n
;
1931 * Warn if the NAME section doesn't contain the `Nm' and `Nd'
1932 * macros (can have multiple `Nm' and one `Nd'). Note that the
1933 * children of the BODY declaration can also be "text".
1936 if (NULL
== (n
= mdoc
->last
->child
)) {
1937 mandoc_msg(MANDOCERR_NAMESEC_BAD
, mdoc
->parse
,
1938 mdoc
->last
->line
, mdoc
->last
->pos
, "empty");
1942 for ( ; n
&& n
->next
; n
= n
->next
) {
1943 if (MDOC_ELEM
== n
->type
&& MDOC_Nm
== n
->tok
)
1945 if (MDOC_TEXT
== n
->type
)
1947 mandoc_msg(MANDOCERR_NAMESEC_BAD
, mdoc
->parse
,
1948 n
->line
, n
->pos
, mdoc_macronames
[n
->tok
]);
1952 if (MDOC_BLOCK
== n
->type
&& MDOC_Nd
== n
->tok
)
1955 mandoc_msg(MANDOCERR_NAMESEC_BAD
, mdoc
->parse
,
1956 n
->line
, n
->pos
, mdoc_macronames
[n
->tok
]);
1961 post_sh_see_also(POST_ARGS
)
1963 const struct mdoc_node
*n
;
1964 const char *name
, *sec
;
1965 const char *lastname
, *lastsec
, *lastpunct
;
1968 n
= mdoc
->last
->child
;
1969 lastname
= lastsec
= lastpunct
= NULL
;
1971 if (n
->tok
!= MDOC_Xr
|| n
->nchild
< 2)
1974 /* Process one .Xr node. */
1976 name
= n
->child
->string
;
1977 sec
= n
->child
->next
->string
;
1978 if (lastsec
!= NULL
) {
1979 if (lastpunct
[0] != ',' || lastpunct
[1] != '\0')
1980 mandoc_vmsg(MANDOCERR_XR_PUNCT
,
1981 mdoc
->parse
, n
->line
, n
->pos
,
1982 "%s before %s(%s)", lastpunct
,
1984 cmp
= strcmp(lastsec
, sec
);
1986 mandoc_vmsg(MANDOCERR_XR_ORDER
,
1987 mdoc
->parse
, n
->line
, n
->pos
,
1988 "%s(%s) after %s(%s)", name
,
1989 sec
, lastname
, lastsec
);
1990 else if (cmp
== 0 && strcmp(lastname
, name
) > 0)
1991 mandoc_vmsg(MANDOCERR_XR_ORDER
,
1992 mdoc
->parse
, n
->line
, n
->pos
,
1993 "%s after %s", name
, lastname
);
1998 /* Process the following node. */
2003 if (n
->tok
== MDOC_Xr
) {
2007 if (n
->type
!= MDOC_TEXT
)
2009 for (name
= n
->string
; *name
!= '\0'; name
++)
2010 if (isalpha((const unsigned char)*name
))
2012 lastpunct
= n
->string
;
2013 if (n
->next
== NULL
)
2014 mandoc_vmsg(MANDOCERR_XR_PUNCT
, mdoc
->parse
,
2015 n
->line
, n
->pos
, "%s after %s(%s)",
2016 lastpunct
, lastname
, lastsec
);
2023 child_an(const struct mdoc_node
*n
)
2026 for (n
= n
->child
; n
!= NULL
; n
= n
->next
)
2027 if ((n
->tok
== MDOC_An
&& n
->nchild
) || child_an(n
))
2033 post_sh_authors(POST_ARGS
)
2036 if ( ! child_an(mdoc
->last
))
2037 mandoc_msg(MANDOCERR_AN_MISSING
, mdoc
->parse
,
2038 mdoc
->last
->line
, mdoc
->last
->pos
, NULL
);
2043 post_sh_head(POST_ARGS
)
2045 struct mdoc_node
*n
;
2046 const char *goodsec
;
2051 * Process a new section. Sections are either "named" or
2052 * "custom". Custom sections are user-defined, while named ones
2053 * follow a conventional order and may only appear in certain
2059 mdoc_deroff(&secname
, mdoc
->last
);
2060 sec
= NULL
== secname
? SEC_CUSTOM
: a2sec(secname
);
2062 /* The NAME should be first. */
2064 if (SEC_NAME
!= sec
&& SEC_NONE
== mdoc
->lastnamed
)
2065 mandoc_vmsg(MANDOCERR_NAMESEC_FIRST
, mdoc
->parse
,
2066 mdoc
->last
->line
, mdoc
->last
->pos
,
2069 /* The SYNOPSIS gets special attention in other areas. */
2071 if (SEC_SYNOPSIS
== sec
) {
2072 roff_setreg(mdoc
->roff
, "nS", 1, '=');
2073 mdoc
->flags
|= MDOC_SYNOPSIS
;
2075 roff_setreg(mdoc
->roff
, "nS", 0, '=');
2076 mdoc
->flags
&= ~MDOC_SYNOPSIS
;
2079 /* Mark our last section. */
2081 mdoc
->lastsec
= sec
;
2084 * Set the section attribute for the current HEAD, for its
2085 * parent BLOCK, and for the HEAD children; the latter can
2086 * only be TEXT nodes, so no recursion is needed.
2087 * For other blocks and elements, including .Sh BODY, this is
2088 * done when allocating the node data structures, but for .Sh
2089 * BLOCK and HEAD, the section is still unknown at that time.
2092 mdoc
->last
->parent
->sec
= sec
;
2093 mdoc
->last
->sec
= sec
;
2094 for (n
= mdoc
->last
->child
; n
; n
= n
->next
)
2097 /* We don't care about custom sections after this. */
2099 if (SEC_CUSTOM
== sec
) {
2105 * Check whether our non-custom section is being repeated or is
2109 if (sec
== mdoc
->lastnamed
)
2110 mandoc_vmsg(MANDOCERR_SEC_REP
, mdoc
->parse
,
2111 mdoc
->last
->line
, mdoc
->last
->pos
,
2114 if (sec
< mdoc
->lastnamed
)
2115 mandoc_vmsg(MANDOCERR_SEC_ORDER
, mdoc
->parse
,
2116 mdoc
->last
->line
, mdoc
->last
->pos
,
2119 /* Mark the last named section. */
2121 mdoc
->lastnamed
= sec
;
2123 /* Check particular section/manual conventions. */
2125 if (mdoc
->meta
.msec
== NULL
) {
2133 if (*mdoc
->meta
.msec
== '4')
2135 goodsec
= "2, 3, 4, 9";
2137 case SEC_RETURN_VALUES
:
2140 if (*mdoc
->meta
.msec
== '2')
2142 if (*mdoc
->meta
.msec
== '3')
2144 if (NULL
== goodsec
)
2145 goodsec
= "2, 3, 9";
2148 if (*mdoc
->meta
.msec
== '9')
2150 if (NULL
== goodsec
)
2152 mandoc_vmsg(MANDOCERR_SEC_MSEC
, mdoc
->parse
,
2153 mdoc
->last
->line
, mdoc
->last
->pos
,
2154 "Sh %s for %s only", secname
, goodsec
);
2165 post_ignpar(POST_ARGS
)
2167 struct mdoc_node
*np
;
2172 if (MDOC_BODY
!= mdoc
->last
->type
)
2175 if (NULL
!= (np
= mdoc
->last
->child
))
2176 if (MDOC_Pp
== np
->tok
|| MDOC_Lp
== np
->tok
) {
2177 mandoc_vmsg(MANDOCERR_PAR_SKIP
,
2178 mdoc
->parse
, np
->line
, np
->pos
,
2179 "%s after %s", mdoc_macronames
[np
->tok
],
2180 mdoc_macronames
[mdoc
->last
->tok
]);
2181 mdoc_node_delete(mdoc
, np
);
2184 if (NULL
!= (np
= mdoc
->last
->last
))
2185 if (MDOC_Pp
== np
->tok
|| MDOC_Lp
== np
->tok
) {
2186 mandoc_vmsg(MANDOCERR_PAR_SKIP
, mdoc
->parse
,
2187 np
->line
, np
->pos
, "%s at the end of %s",
2188 mdoc_macronames
[np
->tok
],
2189 mdoc_macronames
[mdoc
->last
->tok
]);
2190 mdoc_node_delete(mdoc
, np
);
2200 if (NULL
== mdoc
->last
)
2202 if (MDOC_ELEM
!= n
->type
&& MDOC_BLOCK
!= n
->type
)
2206 * Don't allow prior `Lp' or `Pp' prior to a paragraph-type
2207 * block: `Lp', `Pp', or non-compact `Bd' or `Bl'.
2210 if (MDOC_Pp
!= mdoc
->last
->tok
&&
2211 MDOC_Lp
!= mdoc
->last
->tok
&&
2212 MDOC_br
!= mdoc
->last
->tok
)
2214 if (MDOC_Bl
== n
->tok
&& n
->norm
->Bl
.comp
)
2216 if (MDOC_Bd
== n
->tok
&& n
->norm
->Bd
.comp
)
2218 if (MDOC_It
== n
->tok
&& n
->parent
->norm
->Bl
.comp
)
2221 mandoc_vmsg(MANDOCERR_PAR_SKIP
, mdoc
->parse
,
2222 mdoc
->last
->line
, mdoc
->last
->pos
,
2223 "%s before %s", mdoc_macronames
[mdoc
->last
->tok
],
2224 mdoc_macronames
[n
->tok
]);
2225 mdoc_node_delete(mdoc
, mdoc
->last
);
2232 struct mdoc_node
*np
;
2234 if (mdoc
->last
->tok
== MDOC_sp
)
2239 if (MDOC_ELEM
!= mdoc
->last
->type
&&
2240 MDOC_BLOCK
!= mdoc
->last
->type
)
2243 if (NULL
== (np
= mdoc
->last
->prev
)) {
2244 np
= mdoc
->last
->parent
;
2245 if (MDOC_Sh
!= np
->tok
&& MDOC_Ss
!= np
->tok
)
2248 if (MDOC_Pp
!= np
->tok
&& MDOC_Lp
!= np
->tok
&&
2249 (MDOC_br
!= mdoc
->last
->tok
||
2250 (MDOC_sp
!= np
->tok
&& MDOC_br
!= np
->tok
)))
2254 mandoc_vmsg(MANDOCERR_PAR_SKIP
, mdoc
->parse
,
2255 mdoc
->last
->line
, mdoc
->last
->pos
,
2256 "%s after %s", mdoc_macronames
[mdoc
->last
->tok
],
2257 mdoc_macronames
[np
->tok
]);
2258 mdoc_node_delete(mdoc
, mdoc
->last
);
2263 pre_literal(PRE_ARGS
)
2266 pre_display(mdoc
, n
);
2268 if (MDOC_BODY
!= n
->type
)
2272 * The `Dl' (note "el" not "one") and `Bd -literal' and `Bd
2273 * -unfilled' macros set MDOC_LITERAL on entrance to the body.
2278 mdoc
->flags
|= MDOC_LITERAL
;
2281 if (DISP_literal
== n
->norm
->Bd
.type
)
2282 mdoc
->flags
|= MDOC_LITERAL
;
2283 if (DISP_unfilled
== n
->norm
->Bd
.type
)
2284 mdoc
->flags
|= MDOC_LITERAL
;
2297 struct mdoc_node
*n
;
2300 if (mdoc
->meta
.date
)
2301 free(mdoc
->meta
.date
);
2304 if (NULL
== n
->child
|| '\0' == n
->child
->string
[0]) {
2305 mdoc
->meta
.date
= mdoc
->quick
? mandoc_strdup("") :
2306 mandoc_normdate(mdoc
->parse
, NULL
, n
->line
, n
->pos
);
2311 mdoc_deroff(&datestr
, n
);
2313 mdoc
->meta
.date
= datestr
;
2315 mdoc
->meta
.date
= mandoc_normdate(mdoc
->parse
,
2316 datestr
, n
->line
, n
->pos
);
2320 mdoc_node_delete(mdoc
, n
);
2327 struct mdoc_node
*nn
, *n
;
2333 free(mdoc
->meta
.title
);
2334 free(mdoc
->meta
.msec
);
2335 free(mdoc
->meta
.vol
);
2336 free(mdoc
->meta
.arch
);
2338 mdoc
->meta
.title
= NULL
;
2339 mdoc
->meta
.msec
= NULL
;
2340 mdoc
->meta
.vol
= NULL
;
2341 mdoc
->meta
.arch
= NULL
;
2343 /* First check that all characters are uppercase. */
2345 if (NULL
!= (nn
= n
->child
))
2346 for (p
= nn
->string
; *p
; p
++) {
2347 if (toupper((unsigned char)*p
) == *p
)
2349 mandoc_vmsg(MANDOCERR_TITLE_CASE
,
2350 mdoc
->parse
, nn
->line
,
2351 nn
->pos
+ (p
- nn
->string
),
2352 "Dt %s", nn
->string
);
2356 /* No argument: msec and arch remain NULL. */
2358 if (NULL
== (nn
= n
->child
)) {
2359 mandoc_msg(MANDOCERR_DT_NOTITLE
,
2360 mdoc
->parse
, n
->line
, n
->pos
, "Dt");
2361 mdoc
->meta
.title
= mandoc_strdup("UNTITLED");
2362 mdoc
->meta
.vol
= mandoc_strdup("LOCAL");
2366 /* One argument: msec and arch remain NULL. */
2368 mdoc
->meta
.title
= mandoc_strdup(
2369 '\0' == nn
->string
[0] ? "UNTITLED" : nn
->string
);
2371 if (NULL
== (nn
= nn
->next
)) {
2372 mandoc_vmsg(MANDOCERR_MSEC_MISSING
,
2373 mdoc
->parse
, n
->line
, n
->pos
,
2374 "Dt %s", mdoc
->meta
.title
);
2375 mdoc
->meta
.vol
= mandoc_strdup("LOCAL");
2379 /* Handles: `.Dt TITLE SEC'
2381 * volume = SEC is msec ? format(msec) : SEC,
2382 * msec = SEC is msec ? atoi(msec) : 0,
2386 cp
= mandoc_a2msec(nn
->string
);
2388 mdoc
->meta
.vol
= mandoc_strdup(cp
);
2389 mdoc
->meta
.msec
= mandoc_strdup(nn
->string
);
2391 mandoc_vmsg(MANDOCERR_MSEC_BAD
, mdoc
->parse
,
2392 nn
->line
, nn
->pos
, "Dt ... %s", nn
->string
);
2393 mdoc
->meta
.vol
= mandoc_strdup(nn
->string
);
2394 mdoc
->meta
.msec
= mandoc_strdup(nn
->string
);
2397 if (NULL
== (nn
= nn
->next
))
2400 /* Handles: `.Dt TITLE SEC VOL'
2402 * volume = VOL is vol ? format(VOL) :
2403 * VOL is arch ? format(arch) :
2407 cp
= mdoc_a2vol(nn
->string
);
2409 free(mdoc
->meta
.vol
);
2410 mdoc
->meta
.vol
= mandoc_strdup(cp
);
2412 cp
= mdoc_a2arch(nn
->string
);
2414 mandoc_vmsg(MANDOCERR_ARCH_BAD
, mdoc
->parse
,
2415 nn
->line
, nn
->pos
, "Dt ... %s", nn
->string
);
2416 free(mdoc
->meta
.vol
);
2417 mdoc
->meta
.vol
= mandoc_strdup(nn
->string
);
2419 mdoc
->meta
.arch
= mandoc_strdup(cp
);
2422 /* Ignore any subsequent parameters... */
2423 /* FIXME: warn about subsequent parameters. */
2425 mdoc_node_delete(mdoc
, n
);
2432 struct mdoc_node
*n
;
2435 * Make `Bx's second argument always start with an uppercase
2436 * letter. Groff checks if it's an "accepted" term, but we just
2437 * uppercase blindly.
2440 n
= mdoc
->last
->child
;
2441 if (n
&& NULL
!= (n
= n
->next
))
2442 *n
->string
= (char)toupper((unsigned char)*n
->string
);
2451 struct utsname utsname
;
2452 static char *defbuf
;
2454 struct mdoc_node
*n
;
2459 * Set the operating system by way of the `Os' macro.
2460 * The order of precedence is:
2461 * 1. the argument of the `Os' macro, unless empty
2462 * 2. the -Ios=foo command line argument, if provided
2463 * 3. -DOSNAME="\"foo\"", if provided during compilation
2464 * 4. "sysname release" from uname(3)
2467 free(mdoc
->meta
.os
);
2468 mdoc
->meta
.os
= NULL
;
2469 mdoc_deroff(&mdoc
->meta
.os
, n
);
2474 mdoc
->meta
.os
= mandoc_strdup(mdoc
->defos
);
2479 mdoc
->meta
.os
= mandoc_strdup(OSNAME
);
2481 if (NULL
== defbuf
) {
2482 if (-1 == uname(&utsname
)) {
2483 mandoc_msg(MANDOCERR_OS_UNAME
, mdoc
->parse
,
2484 n
->line
, n
->pos
, "Os");
2485 defbuf
= mandoc_strdup("UNKNOWN");
2487 mandoc_asprintf(&defbuf
, "%s %s",
2488 utsname
.sysname
, utsname
.release
);
2490 mdoc
->meta
.os
= mandoc_strdup(defbuf
);
2494 mdoc_node_delete(mdoc
, n
);
2499 * If no argument is provided,
2500 * fill in the name of the current manual page.
2505 struct mdoc_node
*n
;
2512 if (mdoc
->meta
.name
== NULL
) {
2513 mandoc_msg(MANDOCERR_EX_NONAME
, mdoc
->parse
,
2514 n
->line
, n
->pos
, "Ex");
2518 mdoc
->next
= MDOC_NEXT_CHILD
;
2520 if ( ! mdoc_word_alloc(mdoc
, n
->line
, n
->pos
, mdoc
->meta
.name
))
2527 static enum mdoc_sec
2528 a2sec(const char *p
)
2532 for (i
= 0; i
< (int)SEC__MAX
; i
++)
2533 if (secnames
[i
] && 0 == strcmp(p
, secnames
[i
]))
2534 return((enum mdoc_sec
)i
);
2540 macro2len(enum mdoct macro
)