]>
git.cameronkatri.com Git - mandoc.git/blob - mdoc_validate.c
1 /* $Id: mdoc_validate.c,v 1.242 2014/08/02 00:02:42 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.
24 #include <sys/utsname.h>
27 #include <sys/types.h>
39 #include "mandoc_aux.h"
41 #include "libmandoc.h"
43 /* FIXME: .Bl -diag can't have non-text children in HEAD. */
45 #define PRE_ARGS struct mdoc *mdoc, struct mdoc_node *n
46 #define POST_ARGS struct mdoc *mdoc
59 typedef int (*v_pre
)(PRE_ARGS
);
60 typedef int (*v_post
)(POST_ARGS
);
67 static int check_count(struct mdoc
*, enum mdoc_type
,
68 enum check_lvl
, enum check_ineq
, int);
69 static void check_text(struct mdoc
*, int, int, char *);
70 static void check_argv(struct mdoc
*,
71 struct mdoc_node
*, struct mdoc_argv
*);
72 static void check_args(struct mdoc
*, struct mdoc_node
*);
73 static enum mdoc_sec
a2sec(const char *);
74 static size_t macro2len(enum mdoct
);
76 static int ebool(POST_ARGS
);
77 static int berr_ge1(POST_ARGS
);
78 static int bwarn_ge1(POST_ARGS
);
79 static int ewarn_eq0(POST_ARGS
);
80 static int ewarn_eq1(POST_ARGS
);
81 static int ewarn_ge1(POST_ARGS
);
82 static int ewarn_le1(POST_ARGS
);
83 static int hwarn_eq0(POST_ARGS
);
84 static int hwarn_eq1(POST_ARGS
);
85 static int hwarn_ge1(POST_ARGS
);
87 static int post_an(POST_ARGS
);
88 static int post_at(POST_ARGS
);
89 static int post_bf(POST_ARGS
);
90 static int post_bk(POST_ARGS
);
91 static int post_bl(POST_ARGS
);
92 static int post_bl_block(POST_ARGS
);
93 static int post_bl_block_width(POST_ARGS
);
94 static int post_bl_block_tag(POST_ARGS
);
95 static int post_bl_head(POST_ARGS
);
96 static int post_bx(POST_ARGS
);
97 static int post_d1(POST_ARGS
);
98 static int post_defaults(POST_ARGS
);
99 static int post_dd(POST_ARGS
);
100 static int post_dt(POST_ARGS
);
101 static int post_en(POST_ARGS
);
102 static int post_es(POST_ARGS
);
103 static int post_eoln(POST_ARGS
);
104 static int post_ex(POST_ARGS
);
105 static int post_fo(POST_ARGS
);
106 static int post_hyph(POST_ARGS
);
107 static int post_hyphtext(POST_ARGS
);
108 static int post_ignpar(POST_ARGS
);
109 static int post_it(POST_ARGS
);
110 static int post_lb(POST_ARGS
);
111 static int post_literal(POST_ARGS
);
112 static int post_nd(POST_ARGS
);
113 static int post_nm(POST_ARGS
);
114 static int post_ns(POST_ARGS
);
115 static int post_os(POST_ARGS
);
116 static int post_par(POST_ARGS
);
117 static int post_prol(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_body(POST_ARGS
);
122 static int post_sh_head(POST_ARGS
);
123 static int post_st(POST_ARGS
);
124 static int post_vt(POST_ARGS
);
125 static int pre_an(PRE_ARGS
);
126 static int pre_bd(PRE_ARGS
);
127 static int pre_bl(PRE_ARGS
);
128 static int pre_dd(PRE_ARGS
);
129 static int pre_display(PRE_ARGS
);
130 static int pre_dt(PRE_ARGS
);
131 static int pre_literal(PRE_ARGS
);
132 static int pre_obsolete(PRE_ARGS
);
133 static int pre_os(PRE_ARGS
);
134 static int pre_par(PRE_ARGS
);
135 static int pre_std(PRE_ARGS
);
137 static const struct valids mdoc_valids
[MDOC_MAX
] = {
138 { NULL
, NULL
}, /* Ap */
139 { pre_dd
, post_dd
}, /* Dd */
140 { pre_dt
, post_dt
}, /* Dt */
141 { pre_os
, post_os
}, /* Os */
142 { NULL
, post_sh
}, /* Sh */
143 { NULL
, post_ignpar
}, /* Ss */
144 { pre_par
, post_par
}, /* Pp */
145 { pre_display
, post_d1
}, /* D1 */
146 { pre_literal
, post_literal
}, /* Dl */
147 { pre_bd
, post_literal
}, /* Bd */
148 { NULL
, NULL
}, /* Ed */
149 { pre_bl
, post_bl
}, /* Bl */
150 { NULL
, NULL
}, /* El */
151 { pre_par
, post_it
}, /* It */
152 { NULL
, NULL
}, /* Ad */
153 { pre_an
, post_an
}, /* An */
154 { NULL
, post_defaults
}, /* Ar */
155 { NULL
, NULL
}, /* Cd */
156 { NULL
, NULL
}, /* Cm */
157 { NULL
, NULL
}, /* Dv */
158 { NULL
, NULL
}, /* Er */
159 { NULL
, NULL
}, /* Ev */
160 { pre_std
, post_ex
}, /* Ex */
161 { NULL
, NULL
}, /* Fa */
162 { NULL
, ewarn_ge1
}, /* Fd */
163 { NULL
, NULL
}, /* Fl */
164 { NULL
, NULL
}, /* Fn */
165 { NULL
, NULL
}, /* Ft */
166 { NULL
, NULL
}, /* Ic */
167 { NULL
, ewarn_eq1
}, /* In */
168 { NULL
, post_defaults
}, /* Li */
169 { NULL
, post_nd
}, /* Nd */
170 { NULL
, post_nm
}, /* Nm */
171 { NULL
, NULL
}, /* Op */
172 { pre_obsolete
, NULL
}, /* Ot */
173 { NULL
, post_defaults
}, /* Pa */
174 { pre_std
, NULL
}, /* Rv */
175 { NULL
, post_st
}, /* St */
176 { NULL
, NULL
}, /* Va */
177 { NULL
, post_vt
}, /* Vt */
178 { NULL
, ewarn_ge1
}, /* Xr */
179 { NULL
, ewarn_ge1
}, /* %A */
180 { NULL
, post_hyphtext
}, /* %B */ /* FIXME: can be used outside Rs/Re. */
181 { NULL
, ewarn_ge1
}, /* %D */
182 { NULL
, ewarn_ge1
}, /* %I */
183 { NULL
, ewarn_ge1
}, /* %J */
184 { NULL
, post_hyphtext
}, /* %N */
185 { NULL
, post_hyphtext
}, /* %O */
186 { NULL
, ewarn_ge1
}, /* %P */
187 { NULL
, post_hyphtext
}, /* %R */
188 { NULL
, post_hyphtext
}, /* %T */ /* FIXME: can be used outside Rs/Re. */
189 { NULL
, ewarn_ge1
}, /* %V */
190 { NULL
, NULL
}, /* Ac */
191 { NULL
, NULL
}, /* Ao */
192 { NULL
, NULL
}, /* Aq */
193 { NULL
, post_at
}, /* At */
194 { NULL
, NULL
}, /* Bc */
195 { NULL
, post_bf
}, /* Bf */
196 { NULL
, NULL
}, /* Bo */
197 { NULL
, NULL
}, /* Bq */
198 { NULL
, NULL
}, /* Bsx */
199 { NULL
, post_bx
}, /* Bx */
200 { NULL
, ebool
}, /* Db */
201 { NULL
, NULL
}, /* Dc */
202 { NULL
, NULL
}, /* Do */
203 { NULL
, NULL
}, /* Dq */
204 { NULL
, NULL
}, /* Ec */
205 { NULL
, NULL
}, /* Ef */
206 { NULL
, NULL
}, /* Em */
207 { NULL
, NULL
}, /* Eo */
208 { NULL
, NULL
}, /* Fx */
209 { NULL
, NULL
}, /* Ms */
210 { NULL
, ewarn_eq0
}, /* No */
211 { NULL
, post_ns
}, /* Ns */
212 { NULL
, NULL
}, /* Nx */
213 { NULL
, NULL
}, /* Ox */
214 { NULL
, NULL
}, /* Pc */
215 { NULL
, ewarn_eq1
}, /* Pf */
216 { NULL
, NULL
}, /* Po */
217 { NULL
, NULL
}, /* Pq */
218 { NULL
, NULL
}, /* Qc */
219 { NULL
, NULL
}, /* Ql */
220 { NULL
, NULL
}, /* Qo */
221 { NULL
, NULL
}, /* Qq */
222 { NULL
, NULL
}, /* Re */
223 { NULL
, post_rs
}, /* Rs */
224 { NULL
, NULL
}, /* Sc */
225 { NULL
, NULL
}, /* So */
226 { NULL
, NULL
}, /* Sq */
227 { NULL
, ebool
}, /* Sm */
228 { NULL
, post_hyph
}, /* Sx */
229 { NULL
, NULL
}, /* Sy */
230 { NULL
, NULL
}, /* Tn */
231 { NULL
, NULL
}, /* Ux */
232 { NULL
, NULL
}, /* Xc */
233 { NULL
, NULL
}, /* Xo */
234 { NULL
, post_fo
}, /* Fo */
235 { NULL
, NULL
}, /* Fc */
236 { NULL
, NULL
}, /* Oo */
237 { NULL
, NULL
}, /* Oc */
238 { NULL
, post_bk
}, /* Bk */
239 { NULL
, NULL
}, /* Ek */
240 { NULL
, post_eoln
}, /* Bt */
241 { NULL
, NULL
}, /* Hf */
242 { pre_obsolete
, NULL
}, /* Fr */
243 { NULL
, post_eoln
}, /* Ud */
244 { NULL
, post_lb
}, /* Lb */
245 { pre_par
, post_par
}, /* Lp */
246 { NULL
, NULL
}, /* Lk */
247 { NULL
, post_defaults
}, /* Mt */
248 { NULL
, NULL
}, /* Brq */
249 { NULL
, NULL
}, /* Bro */
250 { NULL
, NULL
}, /* Brc */
251 { NULL
, ewarn_ge1
}, /* %C */
252 { pre_obsolete
, post_es
}, /* Es */
253 { pre_obsolete
, post_en
}, /* En */
254 { NULL
, NULL
}, /* Dx */
255 { NULL
, ewarn_ge1
}, /* %Q */
256 { NULL
, post_par
}, /* br */
257 { NULL
, post_par
}, /* sp */
258 { NULL
, ewarn_eq1
}, /* %U */
259 { NULL
, NULL
}, /* Ta */
260 { NULL
, NULL
}, /* ll */
263 #define RSORD_MAX 14 /* Number of `Rs' blocks. */
265 static const enum mdoct rsord
[RSORD_MAX
] = {
282 static const char * const secnames
[SEC__MAX
] = {
289 "IMPLEMENTATION NOTES",
304 "SECURITY CONSIDERATIONS",
310 mdoc_valid_pre(struct mdoc
*mdoc
, struct mdoc_node
*n
)
316 check_text(mdoc
, n
->line
, n
->pos
, n
->string
);
329 p
= mdoc_valids
[n
->tok
].pre
;
330 return(*p
? (*p
)(mdoc
, n
) : 1);
334 mdoc_valid_post(struct mdoc
*mdoc
)
340 if (n
->flags
& MDOC_VALID
)
342 n
->flags
|= MDOC_VALID
;
352 return(post_root(mdoc
));
354 p
= mdoc_valids
[n
->tok
].post
;
355 return(*p
? (*p
)(mdoc
) : 1);
360 check_count(struct mdoc
*mdoc
, enum mdoc_type type
,
361 enum check_lvl lvl
, enum check_ineq ineq
, int val
)
366 if (mdoc
->last
->type
!= type
)
372 if (mdoc
->last
->nchild
< val
)
377 if (mdoc
->last
->nchild
> val
)
382 if (val
== mdoc
->last
->nchild
)
390 t
= lvl
== CHECK_WARN
? MANDOCERR_ARGCWARN
: MANDOCERR_ARGCOUNT
;
391 mandoc_vmsg(t
, mdoc
->parse
, mdoc
->last
->line
,
392 mdoc
->last
->pos
, "want %s%d children (have %d)",
393 p
, val
, mdoc
->last
->nchild
);
401 return(check_count(mdoc
, MDOC_BODY
, CHECK_ERROR
, CHECK_GT
, 0));
407 return(check_count(mdoc
, MDOC_BODY
, CHECK_WARN
, CHECK_GT
, 0));
413 return(check_count(mdoc
, MDOC_ELEM
, CHECK_WARN
, CHECK_EQ
, 0));
419 return(check_count(mdoc
, MDOC_ELEM
, CHECK_WARN
, CHECK_EQ
, 1));
425 return(check_count(mdoc
, MDOC_ELEM
, CHECK_WARN
, CHECK_GT
, 0));
431 return(check_count(mdoc
, MDOC_ELEM
, CHECK_WARN
, CHECK_LT
, 2));
437 return(check_count(mdoc
, MDOC_HEAD
, CHECK_WARN
, CHECK_EQ
, 0));
443 return(check_count(mdoc
, MDOC_HEAD
, CHECK_WARN
, CHECK_EQ
, 1));
449 return(check_count(mdoc
, MDOC_HEAD
, CHECK_WARN
, CHECK_GT
, 0));
453 check_args(struct mdoc
*mdoc
, struct mdoc_node
*n
)
460 assert(n
->args
->argc
);
461 for (i
= 0; i
< (int)n
->args
->argc
; i
++)
462 check_argv(mdoc
, n
, &n
->args
->argv
[i
]);
466 check_argv(struct mdoc
*mdoc
, struct mdoc_node
*n
, struct mdoc_argv
*v
)
470 for (i
= 0; i
< (int)v
->sz
; i
++)
471 check_text(mdoc
, v
->line
, v
->pos
, v
->value
[i
]);
475 check_text(struct mdoc
*mdoc
, int ln
, int pos
, char *p
)
479 if (MDOC_LITERAL
& mdoc
->flags
)
482 for (cp
= p
; NULL
!= (p
= strchr(p
, '\t')); p
++)
483 mandoc_msg(MANDOCERR_FI_TAB
, mdoc
->parse
,
484 ln
, pos
+ (int)(p
- cp
), NULL
);
488 pre_display(PRE_ARGS
)
490 struct mdoc_node
*node
;
492 if (MDOC_BLOCK
!= n
->type
)
495 for (node
= mdoc
->last
->parent
; node
; node
= node
->parent
)
496 if (MDOC_BLOCK
== node
->type
)
497 if (MDOC_Bd
== node
->tok
)
501 mandoc_vmsg(MANDOCERR_BD_NEST
,
502 mdoc
->parse
, n
->line
, n
->pos
,
503 "%s in Bd", mdoc_macronames
[n
->tok
]);
511 struct mdoc_node
*np
;
512 struct mdoc_argv
*argv
, *wa
;
514 enum mdocargt mdoclt
;
517 if (MDOC_BLOCK
!= n
->type
) {
518 if (ENDBODY_NOT
!= n
->end
) {
520 np
= n
->pending
->parent
;
525 assert(MDOC_BLOCK
== np
->type
);
526 assert(MDOC_Bl
== np
->tok
);
531 * First figure out which kind of list to use: bind ourselves to
532 * the first mentioned list type and warn about any remaining
533 * ones. If we find no list type, we default to LIST_item.
537 mdoclt
= MDOC_ARG_MAX
;
538 for (i
= 0; n
->args
&& i
< (int)n
->args
->argc
; i
++) {
539 argv
= n
->args
->argv
+ i
;
542 /* Set list types. */
576 /* Set list arguments. */
578 if (n
->norm
->Bl
.comp
)
579 mandoc_msg(MANDOCERR_ARG_REP
,
580 mdoc
->parse
, argv
->line
,
581 argv
->pos
, "Bl -compact");
582 n
->norm
->Bl
.comp
= 1;
587 mandoc_msg(MANDOCERR_ARG_EMPTY
,
588 mdoc
->parse
, argv
->line
,
589 argv
->pos
, "Bl -width");
590 n
->norm
->Bl
.width
= "0n";
593 if (NULL
!= n
->norm
->Bl
.width
)
594 mandoc_vmsg(MANDOCERR_ARG_REP
,
595 mdoc
->parse
, argv
->line
,
596 argv
->pos
, "Bl -width %s",
598 n
->norm
->Bl
.width
= argv
->value
[0];
602 mandoc_msg(MANDOCERR_ARG_EMPTY
,
603 mdoc
->parse
, argv
->line
,
604 argv
->pos
, "Bl -offset");
607 if (NULL
!= n
->norm
->Bl
.offs
)
608 mandoc_vmsg(MANDOCERR_ARG_REP
,
609 mdoc
->parse
, argv
->line
,
610 argv
->pos
, "Bl -offset %s",
612 n
->norm
->Bl
.offs
= argv
->value
[0];
617 if (LIST__NONE
== lt
)
621 /* Check: multiple list types. */
623 if (LIST__NONE
!= n
->norm
->Bl
.type
) {
624 mandoc_vmsg(MANDOCERR_BL_REP
,
625 mdoc
->parse
, n
->line
, n
->pos
,
626 "Bl -%s", mdoc_argnames
[argv
->arg
]);
630 /* The list type should come first. */
632 if (n
->norm
->Bl
.width
||
635 mandoc_vmsg(MANDOCERR_BL_LATETYPE
,
636 mdoc
->parse
, n
->line
, n
->pos
, "Bl -%s",
637 mdoc_argnames
[n
->args
->argv
[0].arg
]);
639 n
->norm
->Bl
.type
= lt
;
640 if (LIST_column
== lt
) {
641 n
->norm
->Bl
.ncols
= argv
->sz
;
642 n
->norm
->Bl
.cols
= (void *)argv
->value
;
646 /* Allow lists to default to LIST_item. */
648 if (LIST__NONE
== n
->norm
->Bl
.type
) {
649 mandoc_msg(MANDOCERR_BL_NOTYPE
, mdoc
->parse
,
650 n
->line
, n
->pos
, "Bl");
651 n
->norm
->Bl
.type
= LIST_item
;
655 * Validate the width field. Some list types don't need width
656 * types and should be warned about them. Others should have it
657 * and must also be warned. Yet others have a default and need
661 switch (n
->norm
->Bl
.type
) {
663 if (NULL
== n
->norm
->Bl
.width
)
664 mandoc_msg(MANDOCERR_BL_NOWIDTH
, mdoc
->parse
,
665 n
->line
, n
->pos
, "Bl -tag");
676 if (n
->norm
->Bl
.width
)
677 mandoc_vmsg(MANDOCERR_BL_SKIPW
, mdoc
->parse
,
678 wa
->line
, wa
->pos
, "Bl -%s",
679 mdoc_argnames
[mdoclt
]);
686 if (NULL
== n
->norm
->Bl
.width
)
687 n
->norm
->Bl
.width
= "2n";
690 if (NULL
== n
->norm
->Bl
.width
)
691 n
->norm
->Bl
.width
= "3n";
697 return(pre_par(mdoc
, n
));
703 struct mdoc_node
*np
;
704 struct mdoc_argv
*argv
;
708 pre_literal(mdoc
, n
);
710 if (MDOC_BLOCK
!= n
->type
) {
711 if (ENDBODY_NOT
!= n
->end
) {
713 np
= n
->pending
->parent
;
718 assert(MDOC_BLOCK
== np
->type
);
719 assert(MDOC_Bd
== np
->tok
);
723 for (i
= 0; n
->args
&& i
< (int)n
->args
->argc
; i
++) {
724 argv
= n
->args
->argv
+ i
;
744 mandoc_msg(MANDOCERR_BD_FILE
, mdoc
->parse
,
745 n
->line
, n
->pos
, NULL
);
749 mandoc_msg(MANDOCERR_ARG_EMPTY
,
750 mdoc
->parse
, argv
->line
,
751 argv
->pos
, "Bd -offset");
754 if (NULL
!= n
->norm
->Bd
.offs
)
755 mandoc_vmsg(MANDOCERR_ARG_REP
,
756 mdoc
->parse
, argv
->line
,
757 argv
->pos
, "Bd -offset %s",
759 n
->norm
->Bd
.offs
= argv
->value
[0];
762 if (n
->norm
->Bd
.comp
)
763 mandoc_msg(MANDOCERR_ARG_REP
,
764 mdoc
->parse
, argv
->line
,
765 argv
->pos
, "Bd -compact");
766 n
->norm
->Bd
.comp
= 1;
772 if (DISP__NONE
== dt
)
775 if (DISP__NONE
== n
->norm
->Bd
.type
)
776 n
->norm
->Bd
.type
= dt
;
778 mandoc_vmsg(MANDOCERR_BD_REP
,
779 mdoc
->parse
, n
->line
, n
->pos
,
780 "Bd -%s", mdoc_argnames
[argv
->arg
]);
783 if (DISP__NONE
== n
->norm
->Bd
.type
) {
784 mandoc_msg(MANDOCERR_BD_NOTYPE
, mdoc
->parse
,
785 n
->line
, n
->pos
, "Bd");
786 n
->norm
->Bd
.type
= DISP_ragged
;
789 return(pre_par(mdoc
, n
));
795 struct mdoc_argv
*argv
;
801 for (i
= 1; i
< n
->args
->argc
; i
++) {
802 argv
= n
->args
->argv
+ i
;
803 mandoc_vmsg(MANDOCERR_AN_REP
,
804 mdoc
->parse
, argv
->line
, argv
->pos
,
805 "An -%s", mdoc_argnames
[argv
->arg
]);
808 argv
= n
->args
->argv
;
809 if (argv
->arg
== MDOC_Split
)
810 n
->norm
->An
.auth
= AUTH_split
;
811 else if (argv
->arg
== MDOC_Nosplit
)
812 n
->norm
->An
.auth
= AUTH_nosplit
;
823 if (n
->args
&& 1 == n
->args
->argc
)
824 if (MDOC_Std
== n
->args
->argv
[0].arg
)
827 mandoc_msg(MANDOCERR_ARG_STD
, mdoc
->parse
,
828 n
->line
, n
->pos
, mdoc_macronames
[n
->tok
]);
833 pre_obsolete(PRE_ARGS
)
836 if (MDOC_ELEM
== n
->type
|| MDOC_BLOCK
== n
->type
)
837 mandoc_msg(MANDOCERR_MACRO_OBS
, mdoc
->parse
,
838 n
->line
, n
->pos
, mdoc_macronames
[n
->tok
]);
846 if (NULL
== mdoc
->meta
.date
|| mdoc
->meta
.os
)
847 mandoc_msg(MANDOCERR_PROLOG_ORDER
, mdoc
->parse
,
848 n
->line
, n
->pos
, "Dt");
850 if (mdoc
->meta
.title
)
851 mandoc_msg(MANDOCERR_PROLOG_REP
, mdoc
->parse
,
852 n
->line
, n
->pos
, "Dt");
861 if (NULL
== mdoc
->meta
.title
|| NULL
== mdoc
->meta
.date
)
862 mandoc_msg(MANDOCERR_PROLOG_ORDER
, mdoc
->parse
,
863 n
->line
, n
->pos
, "Os");
866 mandoc_msg(MANDOCERR_PROLOG_REP
, mdoc
->parse
,
867 n
->line
, n
->pos
, "Os");
876 if (mdoc
->meta
.title
|| mdoc
->meta
.os
)
877 mandoc_msg(MANDOCERR_PROLOG_ORDER
, mdoc
->parse
,
878 n
->line
, n
->pos
, "Dd");
881 mandoc_msg(MANDOCERR_PROLOG_REP
, mdoc
->parse
,
882 n
->line
, n
->pos
, "Dd");
890 struct mdoc_node
*np
, *nch
;
894 * Unlike other data pointers, these are "housed" by the HEAD
895 * element, which contains the goods.
898 if (MDOC_HEAD
!= mdoc
->last
->type
) {
899 if (ENDBODY_NOT
!= mdoc
->last
->end
) {
900 assert(mdoc
->last
->pending
);
901 np
= mdoc
->last
->pending
->parent
->head
;
902 } else if (MDOC_BLOCK
!= mdoc
->last
->type
) {
903 np
= mdoc
->last
->parent
->head
;
905 np
= mdoc
->last
->head
;
908 assert(MDOC_HEAD
== np
->type
);
909 assert(MDOC_Bf
== np
->tok
);
914 assert(MDOC_BLOCK
== np
->parent
->type
);
915 assert(MDOC_Bf
== np
->parent
->tok
);
917 /* Check the number of arguments. */
920 if (NULL
== np
->parent
->args
) {
922 mandoc_msg(MANDOCERR_BF_NOFONT
, mdoc
->parse
,
923 np
->line
, np
->pos
, "Bf");
929 mandoc_vmsg(MANDOCERR_ARG_EXCESS
, mdoc
->parse
,
930 nch
->line
, nch
->pos
, "Bf ... %s", nch
->string
);
932 /* Extract argument into data. */
934 if (np
->parent
->args
) {
935 arg
= np
->parent
->args
->argv
[0].arg
;
936 if (MDOC_Emphasis
== arg
)
937 np
->norm
->Bf
.font
= FONT_Em
;
938 else if (MDOC_Literal
== arg
)
939 np
->norm
->Bf
.font
= FONT_Li
;
940 else if (MDOC_Symbolic
== arg
)
941 np
->norm
->Bf
.font
= FONT_Sy
;
947 /* Extract parameter into data. */
949 if (0 == strcmp(np
->child
->string
, "Em"))
950 np
->norm
->Bf
.font
= FONT_Em
;
951 else if (0 == strcmp(np
->child
->string
, "Li"))
952 np
->norm
->Bf
.font
= FONT_Li
;
953 else if (0 == strcmp(np
->child
->string
, "Sy"))
954 np
->norm
->Bf
.font
= FONT_Sy
;
956 mandoc_vmsg(MANDOCERR_BF_BADFONT
, mdoc
->parse
,
957 np
->child
->line
, np
->child
->pos
,
958 "Bf %s", np
->child
->string
);
967 const char *stdlibname
;
970 check_count(mdoc
, MDOC_ELEM
, CHECK_WARN
, CHECK_EQ
, 1);
972 n
= mdoc
->last
->child
;
975 assert(MDOC_TEXT
== n
->type
);
977 if (NULL
== (stdlibname
= mdoc_a2lib(n
->string
)))
978 mandoc_asprintf(&libname
,
979 "library \\(lq%s\\(rq", n
->string
);
981 libname
= mandoc_strdup(stdlibname
);
991 const struct mdoc_node
*n
;
995 mandoc_vmsg(MANDOCERR_ARG_SKIP
,
996 mdoc
->parse
, n
->line
, n
->pos
,
997 "%s %s", mdoc_macronames
[n
->tok
],
1014 const struct mdoc_node
*n
;
1017 * The Vt macro comes in both ELEM and BLOCK form, both of which
1018 * have different syntaxes (yet more context-sensitive
1019 * behaviour). ELEM types must have a child, which is already
1020 * guaranteed by the in_line parsing routine; BLOCK types,
1021 * specifically the BODY, should only have TEXT children.
1024 if (MDOC_BODY
!= mdoc
->last
->type
)
1027 for (n
= mdoc
->last
->child
; n
; n
= n
->next
)
1028 if (MDOC_TEXT
!= n
->type
)
1029 mandoc_msg(MANDOCERR_VT_CHILD
, mdoc
->parse
,
1030 n
->line
, n
->pos
, mdoc_macronames
[n
->tok
]);
1039 if (NULL
!= mdoc
->meta
.name
)
1042 mdoc_deroff(&mdoc
->meta
.name
, mdoc
->last
);
1044 if (NULL
== mdoc
->meta
.name
)
1045 mandoc_msg(MANDOCERR_NM_NONAME
, mdoc
->parse
,
1046 mdoc
->last
->line
, mdoc
->last
->pos
, "Nm");
1055 return(post_hyph(mdoc
));
1063 return(post_hyph(mdoc
));
1067 post_literal(POST_ARGS
)
1070 if (mdoc
->last
->tok
== MDOC_Bd
)
1075 * The `Dl' (note "el" not "one") and `Bd' macros unset the
1076 * MDOC_LITERAL flag as they leave. Note that `Bd' only sets
1077 * this in literal mode, but it doesn't hurt to just switch it
1078 * off in general since displays can't be nested.
1081 if (MDOC_BODY
== mdoc
->last
->type
)
1082 mdoc
->flags
&= ~MDOC_LITERAL
;
1088 post_defaults(POST_ARGS
)
1090 struct mdoc_node
*nn
;
1093 * The `Ar' defaults to "file ..." if no value is provided as an
1094 * argument; the `Mt' and `Pa' macros use "~"; the `Li' just
1095 * gets an empty string.
1098 if (mdoc
->last
->child
)
1102 mdoc
->next
= MDOC_NEXT_CHILD
;
1106 if ( ! mdoc_word_alloc(mdoc
, nn
->line
, nn
->pos
, "file"))
1108 if ( ! mdoc_word_alloc(mdoc
, nn
->line
, nn
->pos
, "..."))
1112 if ( ! mdoc_word_alloc(mdoc
, nn
->line
, nn
->pos
, ""))
1118 if ( ! mdoc_word_alloc(mdoc
, nn
->line
, nn
->pos
, "~"))
1133 struct mdoc_node
*n
;
1134 const char *std_att
;
1138 if (n
->child
== NULL
) {
1139 mdoc
->next
= MDOC_NEXT_CHILD
;
1140 if ( ! mdoc_word_alloc(mdoc
, n
->line
, n
->pos
, "AT&T UNIX"))
1147 * If we have a child, look it up in the standard keys. If a
1148 * key exist, use that instead of the child; if it doesn't,
1149 * prefix "AT&T UNIX " to the existing data.
1153 assert(MDOC_TEXT
== n
->type
);
1154 if (NULL
== (std_att
= mdoc_a2att(n
->string
))) {
1155 mandoc_vmsg(MANDOCERR_AT_BAD
, mdoc
->parse
,
1156 n
->line
, n
->pos
, "At %s", n
->string
);
1157 mandoc_asprintf(&att
, "AT&T UNIX %s", n
->string
);
1159 att
= mandoc_strdup(std_att
);
1169 struct mdoc_node
*np
;
1172 if (AUTH__NONE
== np
->norm
->An
.auth
) {
1174 check_count(mdoc
, MDOC_ELEM
, CHECK_WARN
, CHECK_GT
, 0);
1175 } else if (np
->child
)
1176 check_count(mdoc
, MDOC_ELEM
, CHECK_WARN
, CHECK_EQ
, 0);
1185 if (MDOC_BLOCK
== mdoc
->last
->type
)
1186 mdoc
->last
->norm
->Es
= mdoc
->last_es
;
1194 mdoc
->last_es
= mdoc
->last
;
1203 struct mdoc_node
*nbl
, *nit
, *nch
;
1206 if (MDOC_BLOCK
!= nit
->type
)
1209 nbl
= nit
->parent
->parent
;
1210 lt
= nbl
->norm
->Bl
.type
;
1222 if (NULL
== nit
->head
->child
)
1223 mandoc_vmsg(MANDOCERR_IT_NOHEAD
,
1224 mdoc
->parse
, nit
->line
, nit
->pos
,
1226 mdoc_argnames
[nbl
->args
->argv
[0].arg
]);
1235 if (NULL
== nit
->body
->child
)
1236 mandoc_vmsg(MANDOCERR_IT_NOBODY
,
1237 mdoc
->parse
, nit
->line
, nit
->pos
,
1239 mdoc_argnames
[nbl
->args
->argv
[0].arg
]);
1242 if (NULL
!= nit
->head
->child
)
1243 mandoc_vmsg(MANDOCERR_ARG_SKIP
,
1244 mdoc
->parse
, nit
->line
, nit
->pos
,
1245 "It %s", nit
->head
->child
->string
);
1248 cols
= (int)nbl
->norm
->Bl
.ncols
;
1250 assert(NULL
== nit
->head
->child
);
1252 for (i
= 0, nch
= nit
->child
; nch
; nch
= nch
->next
)
1253 if (MDOC_BODY
== nch
->type
)
1256 if (i
< cols
|| i
> cols
+ 1)
1257 mandoc_vmsg(MANDOCERR_ARGCOUNT
,
1258 mdoc
->parse
, nit
->line
, nit
->pos
,
1259 "columns == %d (have %d)", cols
, i
);
1269 post_bl_block(POST_ARGS
)
1271 struct mdoc_node
*n
, *ni
, *nc
;
1274 * These are fairly complicated, so we've broken them into two
1275 * functions. post_bl_block_tag() is called when a -tag is
1276 * specified, but no -width (it must be guessed). The second
1277 * when a -width is specified (macro indicators must be
1278 * rewritten into real lengths).
1283 if (LIST_tag
== n
->norm
->Bl
.type
&&
1284 NULL
== n
->norm
->Bl
.width
) {
1285 if ( ! post_bl_block_tag(mdoc
))
1287 assert(n
->norm
->Bl
.width
);
1288 } else if (NULL
!= n
->norm
->Bl
.width
) {
1289 if ( ! post_bl_block_width(mdoc
))
1291 assert(n
->norm
->Bl
.width
);
1294 for (ni
= n
->body
->child
; ni
; ni
= ni
->next
) {
1295 if (NULL
== ni
->body
)
1297 nc
= ni
->body
->last
;
1298 while (NULL
!= nc
) {
1310 if (NULL
== ni
->next
) {
1311 mandoc_msg(MANDOCERR_PAR_MOVE
,
1312 mdoc
->parse
, nc
->line
, nc
->pos
,
1313 mdoc_macronames
[nc
->tok
]);
1314 if ( ! mdoc_node_relink(mdoc
, nc
))
1316 } else if (0 == n
->norm
->Bl
.comp
&&
1317 LIST_column
!= n
->norm
->Bl
.type
) {
1318 mandoc_vmsg(MANDOCERR_PAR_SKIP
,
1319 mdoc
->parse
, nc
->line
, nc
->pos
,
1321 mdoc_macronames
[nc
->tok
]);
1322 mdoc_node_delete(mdoc
, nc
);
1325 nc
= ni
->body
->last
;
1332 post_bl_block_width(POST_ARGS
)
1337 struct mdoc_node
*n
;
1343 * Calculate the real width of a list from the -width string,
1344 * which may contain a macro (with a known default width), a
1345 * literal string, or a scaling width.
1347 * If the value to -width is a macro, then we re-write it to be
1348 * the macro's width as set in share/tmac/mdoc/doc-common.
1351 if (0 == strcmp(n
->norm
->Bl
.width
, "Ds"))
1353 else if (MDOC_MAX
== (tok
= mdoc_hash_find(n
->norm
->Bl
.width
)))
1356 width
= macro2len(tok
);
1358 /* The value already exists: free and reallocate it. */
1362 for (i
= 0; i
< (int)n
->args
->argc
; i
++)
1363 if (MDOC_Width
== n
->args
->argv
[i
].arg
)
1366 assert(i
< (int)n
->args
->argc
);
1368 (void)snprintf(buf
, sizeof(buf
), "%un", (unsigned int)width
);
1369 free(n
->args
->argv
[i
].value
[0]);
1370 n
->args
->argv
[i
].value
[0] = mandoc_strdup(buf
);
1372 /* Set our width! */
1373 n
->norm
->Bl
.width
= n
->args
->argv
[i
].value
[0];
1378 post_bl_block_tag(POST_ARGS
)
1380 struct mdoc_node
*n
, *nn
;
1386 * Calculate the -width for a `Bl -tag' list if it hasn't been
1387 * provided. Uses the first head macro. NOTE AGAIN: this is
1388 * ONLY if the -width argument has NOT been provided. See
1389 * post_bl_block_width() for converting the -width string.
1395 for (nn
= n
->body
->child
; nn
; nn
= nn
->next
) {
1396 if (MDOC_It
!= nn
->tok
)
1399 assert(MDOC_BLOCK
== nn
->type
);
1400 nn
= nn
->head
->child
;
1405 if (MDOC_TEXT
== nn
->type
) {
1406 sz
= strlen(nn
->string
) + 1;
1410 if (0 != (ssz
= macro2len(nn
->tok
)))
1416 /* Defaults to ten ens. */
1418 (void)snprintf(buf
, sizeof(buf
), "%un", (unsigned int)sz
);
1421 * We have to dynamically add this to the macro's argument list.
1422 * We're guaranteed that a MDOC_Width doesn't already exist.
1426 i
= (int)(n
->args
->argc
)++;
1428 n
->args
->argv
= mandoc_reallocarray(n
->args
->argv
,
1429 n
->args
->argc
, sizeof(struct mdoc_argv
));
1431 n
->args
->argv
[i
].arg
= MDOC_Width
;
1432 n
->args
->argv
[i
].line
= n
->line
;
1433 n
->args
->argv
[i
].pos
= n
->pos
;
1434 n
->args
->argv
[i
].sz
= 1;
1435 n
->args
->argv
[i
].value
= mandoc_malloc(sizeof(char *));
1436 n
->args
->argv
[i
].value
[0] = mandoc_strdup(buf
);
1438 /* Set our width! */
1439 n
->norm
->Bl
.width
= n
->args
->argv
[i
].value
[0];
1444 post_bl_head(POST_ARGS
)
1446 struct mdoc_node
*np
, *nn
, *nnp
;
1447 struct mdoc_argv
*argv
;
1450 if (LIST_column
!= mdoc
->last
->norm
->Bl
.type
)
1451 /* FIXME: this should be ERROR class... */
1452 return(hwarn_eq0(mdoc
));
1455 * Append old-style lists, where the column width specifiers
1456 * trail as macro parameters, to the new-style ("normal-form")
1457 * lists where they're argument values following -column.
1460 if (mdoc
->last
->child
== NULL
)
1463 np
= mdoc
->last
->parent
;
1466 for (j
= 0; j
< (int)np
->args
->argc
; j
++)
1467 if (MDOC_Column
== np
->args
->argv
[j
].arg
)
1470 assert(j
< (int)np
->args
->argc
);
1473 * Accommodate for new-style groff column syntax. Shuffle the
1474 * child nodes, all of which must be TEXT, as arguments for the
1475 * column field. Then, delete the head children.
1478 argv
= np
->args
->argv
+ j
;
1480 argv
->sz
+= mdoc
->last
->nchild
;
1481 argv
->value
= mandoc_reallocarray(argv
->value
,
1482 argv
->sz
, sizeof(char *));
1484 mdoc
->last
->norm
->Bl
.ncols
= argv
->sz
;
1485 mdoc
->last
->norm
->Bl
.cols
= (void *)argv
->value
;
1487 for (nn
= mdoc
->last
->child
; nn
; i
++) {
1488 argv
->value
[i
] = nn
->string
;
1492 mdoc_node_delete(NULL
, nnp
);
1495 mdoc
->last
->nchild
= 0;
1496 mdoc
->last
->child
= NULL
;
1504 struct mdoc_node
*nparent
, *nprev
; /* of the Bl block */
1505 struct mdoc_node
*nblock
, *nbody
; /* of the Bl */
1506 struct mdoc_node
*nchild
, *nnext
; /* of the Bl body */
1509 switch (nbody
->type
) {
1511 return(post_bl_block(mdoc
));
1513 return(post_bl_head(mdoc
));
1522 nchild
= nbody
->child
;
1523 while (NULL
!= nchild
) {
1524 if (MDOC_It
== nchild
->tok
|| MDOC_Sm
== nchild
->tok
) {
1525 nchild
= nchild
->next
;
1529 mandoc_msg(MANDOCERR_BL_MOVE
, mdoc
->parse
,
1530 nchild
->line
, nchild
->pos
,
1531 mdoc_macronames
[nchild
->tok
]);
1534 * Move the node out of the Bl block.
1535 * First, collect all required node pointers.
1538 nblock
= nbody
->parent
;
1539 nprev
= nblock
->prev
;
1540 nparent
= nblock
->parent
;
1541 nnext
= nchild
->next
;
1544 * Unlink this child.
1547 assert(NULL
== nchild
->prev
);
1548 if (0 == --nbody
->nchild
) {
1549 nbody
->child
= NULL
;
1551 assert(NULL
== nnext
);
1553 nbody
->child
= nnext
;
1558 * Relink this child.
1561 nchild
->parent
= nparent
;
1562 nchild
->prev
= nprev
;
1563 nchild
->next
= nblock
;
1565 nblock
->prev
= nchild
;
1568 nparent
->child
= nchild
;
1570 nprev
->next
= nchild
;
1588 ebool(struct mdoc
*mdoc
)
1590 struct mdoc_node
*nch
;
1593 tok
= mdoc
->last
->tok
;
1594 nch
= mdoc
->last
->child
;
1598 mdoc
->flags
^= MDOC_SMOFF
;
1602 check_count(mdoc
, MDOC_ELEM
, CHECK_WARN
, CHECK_LT
, 2);
1604 assert(MDOC_TEXT
== nch
->type
);
1606 if (0 == strcmp(nch
->string
, "on")) {
1608 mdoc
->flags
&= ~MDOC_SMOFF
;
1611 if (0 == strcmp(nch
->string
, "off")) {
1613 mdoc
->flags
|= MDOC_SMOFF
;
1617 mandoc_vmsg(MANDOCERR_SM_BAD
,
1618 mdoc
->parse
, nch
->line
, nch
->pos
,
1619 "%s %s", mdoc_macronames
[tok
], nch
->string
);
1620 return(mdoc_node_relink(mdoc
, nch
));
1624 post_root(POST_ARGS
)
1626 struct mdoc_node
*n
;
1628 /* Add missing prologue data. */
1630 if ( ! (MDOC_PBODY
& mdoc
->flags
)) {
1631 mandoc_msg(MANDOCERR_PROLOG_BAD
, mdoc
->parse
, 0, 0, "EOF");
1632 if (mdoc
->meta
.date
== NULL
)
1633 mdoc
->meta
.date
= mdoc
->quick
?
1635 mandoc_normdate(mdoc
->parse
, NULL
, 0, 0);
1636 if (mdoc
->meta
.title
== NULL
)
1637 mdoc
->meta
.title
= mandoc_strdup("UNKNOWN");
1638 if (mdoc
->meta
.vol
== NULL
)
1639 mdoc
->meta
.vol
= mandoc_strdup("LOCAL");
1640 if (mdoc
->meta
.arch
== NULL
)
1641 mdoc
->meta
.msec
= mandoc_strdup("1");
1642 if (mdoc
->meta
.os
== NULL
)
1643 mdoc
->meta
.os
= mandoc_strdup("UNKNOWN");
1649 /* Check that we begin with a proper `Sh'. */
1651 if (NULL
== n
->child
)
1652 mandoc_msg(MANDOCERR_DOC_EMPTY
, mdoc
->parse
,
1653 n
->line
, n
->pos
, NULL
);
1654 else if (MDOC_Sh
!= n
->child
->tok
)
1655 mandoc_msg(MANDOCERR_SEC_BEFORE
, mdoc
->parse
,
1656 n
->child
->line
, n
->child
->pos
,
1657 mdoc_macronames
[n
->child
->tok
]);
1665 struct mdoc_node
*n
, *nch
;
1672 mandoc_msg(MANDOCERR_MACRO_EMPTY
, mdoc
->parse
,
1673 n
->line
, n
->pos
, mdoc_macronames
[n
->tok
]);
1674 mdoc_node_delete(mdoc
, n
);
1678 assert(MDOC_TEXT
== nch
->type
);
1680 if (NULL
== (p
= mdoc_a2st(nch
->string
))) {
1681 mandoc_vmsg(MANDOCERR_ST_BAD
, mdoc
->parse
,
1682 nch
->line
, nch
->pos
, "St %s", nch
->string
);
1683 mdoc_node_delete(mdoc
, n
);
1686 nch
->string
= mandoc_strdup(p
);
1695 struct mdoc_node
*nn
, *next
, *prev
;
1698 switch (mdoc
->last
->type
) {
1700 check_count(mdoc
, MDOC_HEAD
, CHECK_WARN
, CHECK_EQ
, 0);
1703 if (mdoc
->last
->child
)
1705 check_count(mdoc
, MDOC_BODY
, CHECK_WARN
, CHECK_GT
, 0);
1712 * The full `Rs' block needs special handling to order the
1713 * sub-elements according to `rsord'. Pick through each element
1714 * and correctly order it. This is an insertion sort.
1718 for (nn
= mdoc
->last
->child
->next
; nn
; nn
= next
) {
1719 /* Determine order of `nn'. */
1720 for (i
= 0; i
< RSORD_MAX
; i
++)
1721 if (rsord
[i
] == nn
->tok
)
1724 if (i
== RSORD_MAX
) {
1725 mandoc_msg(MANDOCERR_RS_BAD
,
1726 mdoc
->parse
, nn
->line
, nn
->pos
,
1727 mdoc_macronames
[nn
->tok
]);
1729 } else if (MDOC__J
== nn
->tok
|| MDOC__B
== nn
->tok
)
1730 mdoc
->last
->norm
->Rs
.quote_T
++;
1733 * Remove `nn' from the chain. This somewhat
1734 * repeats mdoc_node_unlink(), but since we're
1735 * just re-ordering, there's no need for the
1736 * full unlink process.
1739 if (NULL
!= (next
= nn
->next
))
1740 next
->prev
= nn
->prev
;
1742 if (NULL
!= (prev
= nn
->prev
))
1743 prev
->next
= nn
->next
;
1745 nn
->prev
= nn
->next
= NULL
;
1748 * Scan back until we reach a node that's
1749 * ordered before `nn'.
1752 for ( ; prev
; prev
= prev
->prev
) {
1753 /* Determine order of `prev'. */
1754 for (j
= 0; j
< RSORD_MAX
; j
++)
1755 if (rsord
[j
] == prev
->tok
)
1765 * Set `nn' back into its correct place in front
1766 * of the `prev' node.
1773 prev
->next
->prev
= nn
;
1774 nn
->next
= prev
->next
;
1777 mdoc
->last
->child
->prev
= nn
;
1778 nn
->next
= mdoc
->last
->child
;
1779 mdoc
->last
->child
= nn
;
1787 * For some arguments of some macros,
1788 * convert all breakable hyphens into ASCII_HYPH.
1791 post_hyph(POST_ARGS
)
1793 struct mdoc_node
*n
, *nch
;
1799 if (MDOC_Sh
== n
->tok
|| MDOC_Ss
== n
->tok
)
1803 if (MDOC_D1
== n
->tok
|| MDOC_Nd
== n
->tok
)
1812 for (nch
= n
->child
; nch
; nch
= nch
->next
) {
1813 if (MDOC_TEXT
!= nch
->type
)
1818 while ('\0' != *(++cp
))
1820 isalpha((unsigned char)cp
[-1]) &&
1821 isalpha((unsigned char)cp
[1]))
1828 post_hyphtext(POST_ARGS
)
1832 return(post_hyph(mdoc
));
1839 if (MDOC_LINE
& mdoc
->last
->flags
)
1840 mandoc_msg(MANDOCERR_NS_SKIP
, mdoc
->parse
,
1841 mdoc
->last
->line
, mdoc
->last
->pos
, NULL
);
1851 if (MDOC_HEAD
== mdoc
->last
->type
)
1852 return(post_sh_head(mdoc
));
1853 if (MDOC_BODY
== mdoc
->last
->type
)
1854 return(post_sh_body(mdoc
));
1860 post_sh_body(POST_ARGS
)
1862 struct mdoc_node
*n
;
1864 if (SEC_NAME
!= mdoc
->lastsec
)
1868 * Warn if the NAME section doesn't contain the `Nm' and `Nd'
1869 * macros (can have multiple `Nm' and one `Nd'). Note that the
1870 * children of the BODY declaration can also be "text".
1873 if (NULL
== (n
= mdoc
->last
->child
)) {
1874 mandoc_msg(MANDOCERR_NAMESEC_BAD
, mdoc
->parse
,
1875 mdoc
->last
->line
, mdoc
->last
->pos
, "empty");
1879 for ( ; n
&& n
->next
; n
= n
->next
) {
1880 if (MDOC_ELEM
== n
->type
&& MDOC_Nm
== n
->tok
)
1882 if (MDOC_TEXT
== n
->type
)
1884 mandoc_msg(MANDOCERR_NAMESEC_BAD
, mdoc
->parse
,
1885 n
->line
, n
->pos
, mdoc_macronames
[n
->tok
]);
1889 if (MDOC_BLOCK
== n
->type
&& MDOC_Nd
== n
->tok
)
1892 mandoc_msg(MANDOCERR_NAMESEC_BAD
, mdoc
->parse
,
1893 n
->line
, n
->pos
, mdoc_macronames
[n
->tok
]);
1898 post_sh_head(POST_ARGS
)
1900 struct mdoc_node
*n
;
1901 const char *goodsec
;
1906 * Process a new section. Sections are either "named" or
1907 * "custom". Custom sections are user-defined, while named ones
1908 * follow a conventional order and may only appear in certain
1914 mdoc_deroff(&secname
, mdoc
->last
);
1915 sec
= NULL
== secname
? SEC_CUSTOM
: a2sec(secname
);
1917 /* The NAME should be first. */
1919 if (SEC_NAME
!= sec
&& SEC_NONE
== mdoc
->lastnamed
)
1920 mandoc_vmsg(MANDOCERR_NAMESEC_FIRST
, mdoc
->parse
,
1921 mdoc
->last
->line
, mdoc
->last
->pos
,
1924 /* The SYNOPSIS gets special attention in other areas. */
1926 if (SEC_SYNOPSIS
== sec
) {
1927 roff_setreg(mdoc
->roff
, "nS", 1, '=');
1928 mdoc
->flags
|= MDOC_SYNOPSIS
;
1930 roff_setreg(mdoc
->roff
, "nS", 0, '=');
1931 mdoc
->flags
&= ~MDOC_SYNOPSIS
;
1934 /* Mark our last section. */
1936 mdoc
->lastsec
= sec
;
1939 * Set the section attribute for the current HEAD, for its
1940 * parent BLOCK, and for the HEAD children; the latter can
1941 * only be TEXT nodes, so no recursion is needed.
1942 * For other blocks and elements, including .Sh BODY, this is
1943 * done when allocating the node data structures, but for .Sh
1944 * BLOCK and HEAD, the section is still unknown at that time.
1947 mdoc
->last
->parent
->sec
= sec
;
1948 mdoc
->last
->sec
= sec
;
1949 for (n
= mdoc
->last
->child
; n
; n
= n
->next
)
1952 /* We don't care about custom sections after this. */
1954 if (SEC_CUSTOM
== sec
) {
1960 * Check whether our non-custom section is being repeated or is
1964 if (sec
== mdoc
->lastnamed
)
1965 mandoc_vmsg(MANDOCERR_SEC_REP
, mdoc
->parse
,
1966 mdoc
->last
->line
, mdoc
->last
->pos
,
1969 if (sec
< mdoc
->lastnamed
)
1970 mandoc_vmsg(MANDOCERR_SEC_ORDER
, mdoc
->parse
,
1971 mdoc
->last
->line
, mdoc
->last
->pos
,
1974 /* Mark the last named section. */
1976 mdoc
->lastnamed
= sec
;
1978 /* Check particular section/manual conventions. */
1980 assert(mdoc
->meta
.msec
);
1985 if (*mdoc
->meta
.msec
== '4')
1987 goodsec
= "2, 3, 4, 9";
1989 case SEC_RETURN_VALUES
:
1992 if (*mdoc
->meta
.msec
== '2')
1994 if (*mdoc
->meta
.msec
== '3')
1996 if (NULL
== goodsec
)
1997 goodsec
= "2, 3, 9";
2000 if (*mdoc
->meta
.msec
== '9')
2002 if (NULL
== goodsec
)
2004 mandoc_vmsg(MANDOCERR_SEC_MSEC
, mdoc
->parse
,
2005 mdoc
->last
->line
, mdoc
->last
->pos
,
2006 "Sh %s for %s only", secname
, goodsec
);
2017 post_ignpar(POST_ARGS
)
2019 struct mdoc_node
*np
;
2024 if (MDOC_BODY
!= mdoc
->last
->type
)
2027 if (NULL
!= (np
= mdoc
->last
->child
))
2028 if (MDOC_Pp
== np
->tok
|| MDOC_Lp
== np
->tok
) {
2029 mandoc_vmsg(MANDOCERR_PAR_SKIP
,
2030 mdoc
->parse
, np
->line
, np
->pos
,
2031 "%s after %s", mdoc_macronames
[np
->tok
],
2032 mdoc_macronames
[mdoc
->last
->tok
]);
2033 mdoc_node_delete(mdoc
, np
);
2036 if (NULL
!= (np
= mdoc
->last
->last
))
2037 if (MDOC_Pp
== np
->tok
|| MDOC_Lp
== np
->tok
) {
2038 mandoc_vmsg(MANDOCERR_PAR_SKIP
, mdoc
->parse
,
2039 np
->line
, np
->pos
, "%s at the end of %s",
2040 mdoc_macronames
[np
->tok
],
2041 mdoc_macronames
[mdoc
->last
->tok
]);
2042 mdoc_node_delete(mdoc
, np
);
2052 if (NULL
== mdoc
->last
)
2054 if (MDOC_ELEM
!= n
->type
&& MDOC_BLOCK
!= n
->type
)
2058 * Don't allow prior `Lp' or `Pp' prior to a paragraph-type
2059 * block: `Lp', `Pp', or non-compact `Bd' or `Bl'.
2062 if (MDOC_Pp
!= mdoc
->last
->tok
&&
2063 MDOC_Lp
!= mdoc
->last
->tok
&&
2064 MDOC_br
!= mdoc
->last
->tok
)
2066 if (MDOC_Bl
== n
->tok
&& n
->norm
->Bl
.comp
)
2068 if (MDOC_Bd
== n
->tok
&& n
->norm
->Bd
.comp
)
2070 if (MDOC_It
== n
->tok
&& n
->parent
->norm
->Bl
.comp
)
2073 mandoc_vmsg(MANDOCERR_PAR_SKIP
, mdoc
->parse
,
2074 mdoc
->last
->line
, mdoc
->last
->pos
,
2075 "%s before %s", mdoc_macronames
[mdoc
->last
->tok
],
2076 mdoc_macronames
[n
->tok
]);
2077 mdoc_node_delete(mdoc
, mdoc
->last
);
2084 struct mdoc_node
*np
;
2086 if (mdoc
->last
->tok
== MDOC_sp
)
2091 if (MDOC_ELEM
!= mdoc
->last
->type
&&
2092 MDOC_BLOCK
!= mdoc
->last
->type
)
2095 if (NULL
== (np
= mdoc
->last
->prev
)) {
2096 np
= mdoc
->last
->parent
;
2097 if (MDOC_Sh
!= np
->tok
&& MDOC_Ss
!= np
->tok
)
2100 if (MDOC_Pp
!= np
->tok
&& MDOC_Lp
!= np
->tok
&&
2101 (MDOC_br
!= mdoc
->last
->tok
||
2102 (MDOC_sp
!= np
->tok
&& MDOC_br
!= np
->tok
)))
2106 mandoc_vmsg(MANDOCERR_PAR_SKIP
, mdoc
->parse
,
2107 mdoc
->last
->line
, mdoc
->last
->pos
,
2108 "%s after %s", mdoc_macronames
[mdoc
->last
->tok
],
2109 mdoc_macronames
[np
->tok
]);
2110 mdoc_node_delete(mdoc
, mdoc
->last
);
2115 pre_literal(PRE_ARGS
)
2118 pre_display(mdoc
, n
);
2120 if (MDOC_BODY
!= n
->type
)
2124 * The `Dl' (note "el" not "one") and `Bd -literal' and `Bd
2125 * -unfilled' macros set MDOC_LITERAL on entrance to the body.
2130 mdoc
->flags
|= MDOC_LITERAL
;
2133 if (DISP_literal
== n
->norm
->Bd
.type
)
2134 mdoc
->flags
|= MDOC_LITERAL
;
2135 if (DISP_unfilled
== n
->norm
->Bd
.type
)
2136 mdoc
->flags
|= MDOC_LITERAL
;
2149 struct mdoc_node
*n
;
2152 if (mdoc
->meta
.date
)
2153 free(mdoc
->meta
.date
);
2156 if (NULL
== n
->child
|| '\0' == n
->child
->string
[0]) {
2157 mdoc
->meta
.date
= mdoc
->quick
? mandoc_strdup("") :
2158 mandoc_normdate(mdoc
->parse
, NULL
, n
->line
, n
->pos
);
2159 return(post_prol(mdoc
));
2163 mdoc_deroff(&datestr
, n
);
2165 mdoc
->meta
.date
= datestr
;
2167 mdoc
->meta
.date
= mandoc_normdate(mdoc
->parse
,
2168 datestr
, n
->line
, n
->pos
);
2171 return(post_prol(mdoc
));
2177 struct mdoc_node
*nn
, *n
;
2183 if (mdoc
->meta
.title
)
2184 free(mdoc
->meta
.title
);
2186 free(mdoc
->meta
.vol
);
2187 if (mdoc
->meta
.arch
)
2188 free(mdoc
->meta
.arch
);
2190 mdoc
->meta
.title
= mdoc
->meta
.vol
= mdoc
->meta
.arch
= NULL
;
2192 /* First check that all characters are uppercase. */
2194 if (NULL
!= (nn
= n
->child
))
2195 for (p
= nn
->string
; *p
; p
++) {
2196 if (toupper((unsigned char)*p
) == *p
)
2198 mandoc_vmsg(MANDOCERR_TITLE_CASE
,
2199 mdoc
->parse
, nn
->line
,
2200 nn
->pos
+ (p
- nn
->string
),
2201 "Dt %s", nn
->string
);
2206 * title = unknown, volume = local, msec = 0, arch = NULL
2209 if (NULL
== (nn
= n
->child
)) {
2210 /* XXX: make these macro values. */
2211 /* FIXME: warn about missing values. */
2212 mdoc
->meta
.title
= mandoc_strdup("UNKNOWN");
2213 mdoc
->meta
.vol
= mandoc_strdup("LOCAL");
2214 mdoc
->meta
.msec
= mandoc_strdup("1");
2215 return(post_prol(mdoc
));
2218 /* Handles: `.Dt TITLE'
2219 * title = TITLE, volume = local, msec = 0, arch = NULL
2222 mdoc
->meta
.title
= mandoc_strdup(
2223 '\0' == nn
->string
[0] ? "UNKNOWN" : nn
->string
);
2225 if (NULL
== (nn
= nn
->next
)) {
2226 /* FIXME: warn about missing msec. */
2227 /* XXX: make this a macro value. */
2228 mdoc
->meta
.vol
= mandoc_strdup("LOCAL");
2229 mdoc
->meta
.msec
= mandoc_strdup("1");
2230 return(post_prol(mdoc
));
2233 /* Handles: `.Dt TITLE SEC'
2235 * volume = SEC is msec ? format(msec) : SEC,
2236 * msec = SEC is msec ? atoi(msec) : 0,
2240 cp
= mandoc_a2msec(nn
->string
);
2242 mdoc
->meta
.vol
= mandoc_strdup(cp
);
2243 mdoc
->meta
.msec
= mandoc_strdup(nn
->string
);
2245 mandoc_vmsg(MANDOCERR_MSEC_BAD
, mdoc
->parse
,
2246 nn
->line
, nn
->pos
, "Dt ... %s", nn
->string
);
2247 mdoc
->meta
.vol
= mandoc_strdup(nn
->string
);
2248 mdoc
->meta
.msec
= mandoc_strdup(nn
->string
);
2251 if (NULL
== (nn
= nn
->next
))
2252 return(post_prol(mdoc
));
2254 /* Handles: `.Dt TITLE SEC VOL'
2256 * volume = VOL is vol ? format(VOL) :
2257 * VOL is arch ? format(arch) :
2261 cp
= mdoc_a2vol(nn
->string
);
2263 free(mdoc
->meta
.vol
);
2264 mdoc
->meta
.vol
= mandoc_strdup(cp
);
2266 cp
= mdoc_a2arch(nn
->string
);
2268 mandoc_vmsg(MANDOCERR_ARCH_BAD
, mdoc
->parse
,
2269 nn
->line
, nn
->pos
, "Dt ... %s", nn
->string
);
2270 free(mdoc
->meta
.vol
);
2271 mdoc
->meta
.vol
= mandoc_strdup(nn
->string
);
2273 mdoc
->meta
.arch
= mandoc_strdup(cp
);
2276 /* Ignore any subsequent parameters... */
2277 /* FIXME: warn about subsequent parameters. */
2279 return(post_prol(mdoc
));
2283 post_prol(POST_ARGS
)
2286 * Remove prologue macros from the document after they're
2287 * processed. The final document uses mdoc_meta for these
2288 * values and discards the originals.
2291 mdoc_node_delete(mdoc
, mdoc
->last
);
2292 if (mdoc
->meta
.title
&& mdoc
->meta
.date
&& mdoc
->meta
.os
)
2293 mdoc
->flags
|= MDOC_PBODY
;
2301 struct mdoc_node
*n
;
2304 * Make `Bx's second argument always start with an uppercase
2305 * letter. Groff checks if it's an "accepted" term, but we just
2306 * uppercase blindly.
2309 n
= mdoc
->last
->child
;
2310 if (n
&& NULL
!= (n
= n
->next
))
2311 *n
->string
= (char)toupper((unsigned char)*n
->string
);
2320 struct utsname utsname
;
2321 static char *defbuf
;
2323 struct mdoc_node
*n
;
2328 * Set the operating system by way of the `Os' macro.
2329 * The order of precedence is:
2330 * 1. the argument of the `Os' macro, unless empty
2331 * 2. the -Ios=foo command line argument, if provided
2332 * 3. -DOSNAME="\"foo\"", if provided during compilation
2333 * 4. "sysname release" from uname(3)
2336 free(mdoc
->meta
.os
);
2337 mdoc
->meta
.os
= NULL
;
2338 mdoc_deroff(&mdoc
->meta
.os
, n
);
2340 return(post_prol(mdoc
));
2343 mdoc
->meta
.os
= mandoc_strdup(mdoc
->defos
);
2344 return(post_prol(mdoc
));
2348 mdoc
->meta
.os
= mandoc_strdup(OSNAME
);
2350 if (NULL
== defbuf
) {
2351 if (-1 == uname(&utsname
)) {
2352 mandoc_msg(MANDOCERR_OS_UNAME
, mdoc
->parse
,
2353 n
->line
, n
->pos
, "Os");
2354 defbuf
= mandoc_strdup("UNKNOWN");
2356 mandoc_asprintf(&defbuf
, "%s %s",
2357 utsname
.sysname
, utsname
.release
);
2359 mdoc
->meta
.os
= mandoc_strdup(defbuf
);
2361 return(post_prol(mdoc
));
2365 * If no argument is provided,
2366 * fill in the name of the current manual page.
2371 struct mdoc_node
*n
;
2378 if (mdoc
->meta
.name
== NULL
) {
2379 mandoc_msg(MANDOCERR_EX_NONAME
, mdoc
->parse
,
2380 n
->line
, n
->pos
, "Ex");
2384 mdoc
->next
= MDOC_NEXT_CHILD
;
2386 if ( ! mdoc_word_alloc(mdoc
, n
->line
, n
->pos
, mdoc
->meta
.name
))
2393 static enum mdoc_sec
2394 a2sec(const char *p
)
2398 for (i
= 0; i
< (int)SEC__MAX
; i
++)
2399 if (secnames
[i
] && 0 == strcmp(p
, secnames
[i
]))
2400 return((enum mdoc_sec
)i
);
2406 macro2len(enum mdoct macro
)