]>
git.cameronkatri.com Git - mandoc.git/blob - mdoc_validate.c
1 /* $Id: mdoc_validate.c,v 1.293 2015/09/26 00:54:04 schwarze Exp $ */
3 * Copyright (c) 2008-2012 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2010-2015 Ingo Schwarze <schwarze@openbsd.org>
5 * Copyright (c) 2010 Joerg Sonnenberger <joerg@netbsd.org>
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 #include <sys/types.h>
23 #include <sys/utsname.h>
34 #include "mandoc_aux.h"
38 #include "libmandoc.h"
42 /* FIXME: .Bl -diag can't have non-text children in HEAD. */
44 #define PRE_ARGS struct roff_man *mdoc, struct roff_node *n
45 #define POST_ARGS struct roff_man *mdoc
53 typedef void (*v_pre
)(PRE_ARGS
);
54 typedef void (*v_post
)(POST_ARGS
);
61 static void check_text(struct roff_man
*, int, int, char *);
62 static void check_argv(struct roff_man
*,
63 struct roff_node
*, struct mdoc_argv
*);
64 static void check_args(struct roff_man
*, struct roff_node
*);
65 static int child_an(const struct roff_node
*);
66 static enum roff_sec
a2sec(const char *);
67 static size_t macro2len(int);
68 static void rewrite_macro2len(char **);
70 static void post_an(POST_ARGS
);
71 static void post_at(POST_ARGS
);
72 static void post_bf(POST_ARGS
);
73 static void post_bk(POST_ARGS
);
74 static void post_bl(POST_ARGS
);
75 static void post_bl_block(POST_ARGS
);
76 static void post_bl_block_tag(POST_ARGS
);
77 static void post_bl_head(POST_ARGS
);
78 static void post_bx(POST_ARGS
);
79 static void post_d1(POST_ARGS
);
80 static void post_defaults(POST_ARGS
);
81 static void post_dd(POST_ARGS
);
82 static void post_dt(POST_ARGS
);
83 static void post_en(POST_ARGS
);
84 static void post_es(POST_ARGS
);
85 static void post_eoln(POST_ARGS
);
86 static void post_ex(POST_ARGS
);
87 static void post_fa(POST_ARGS
);
88 static void post_fn(POST_ARGS
);
89 static void post_fname(POST_ARGS
);
90 static void post_fo(POST_ARGS
);
91 static void post_hyph(POST_ARGS
);
92 static void post_ignpar(POST_ARGS
);
93 static void post_it(POST_ARGS
);
94 static void post_lb(POST_ARGS
);
95 static void post_literal(POST_ARGS
);
96 static void post_nd(POST_ARGS
);
97 static void post_nm(POST_ARGS
);
98 static void post_ns(POST_ARGS
);
99 static void post_os(POST_ARGS
);
100 static void post_par(POST_ARGS
);
101 static void post_root(POST_ARGS
);
102 static void post_rs(POST_ARGS
);
103 static void post_sh(POST_ARGS
);
104 static void post_sh_head(POST_ARGS
);
105 static void post_sh_name(POST_ARGS
);
106 static void post_sh_see_also(POST_ARGS
);
107 static void post_sh_authors(POST_ARGS
);
108 static void post_sm(POST_ARGS
);
109 static void post_st(POST_ARGS
);
111 static void pre_an(PRE_ARGS
);
112 static void pre_bd(PRE_ARGS
);
113 static void pre_bl(PRE_ARGS
);
114 static void pre_dd(PRE_ARGS
);
115 static void pre_display(PRE_ARGS
);
116 static void pre_dt(PRE_ARGS
);
117 static void pre_literal(PRE_ARGS
);
118 static void pre_obsolete(PRE_ARGS
);
119 static void pre_os(PRE_ARGS
);
120 static void pre_par(PRE_ARGS
);
121 static void pre_std(PRE_ARGS
);
123 static const struct valids mdoc_valids
[MDOC_MAX
] = {
124 { NULL
, NULL
}, /* Ap */
125 { pre_dd
, post_dd
}, /* Dd */
126 { pre_dt
, post_dt
}, /* Dt */
127 { pre_os
, post_os
}, /* Os */
128 { NULL
, post_sh
}, /* Sh */
129 { NULL
, post_ignpar
}, /* Ss */
130 { pre_par
, post_par
}, /* Pp */
131 { pre_display
, post_d1
}, /* D1 */
132 { pre_literal
, post_literal
}, /* Dl */
133 { pre_bd
, post_literal
}, /* Bd */
134 { NULL
, NULL
}, /* Ed */
135 { pre_bl
, post_bl
}, /* Bl */
136 { NULL
, NULL
}, /* El */
137 { pre_par
, post_it
}, /* It */
138 { NULL
, NULL
}, /* Ad */
139 { pre_an
, post_an
}, /* An */
140 { NULL
, post_defaults
}, /* Ar */
141 { NULL
, NULL
}, /* Cd */
142 { NULL
, NULL
}, /* Cm */
143 { NULL
, NULL
}, /* Dv */
144 { NULL
, NULL
}, /* Er */
145 { NULL
, NULL
}, /* Ev */
146 { pre_std
, post_ex
}, /* Ex */
147 { NULL
, post_fa
}, /* Fa */
148 { NULL
, NULL
}, /* Fd */
149 { NULL
, NULL
}, /* Fl */
150 { NULL
, post_fn
}, /* Fn */
151 { NULL
, NULL
}, /* Ft */
152 { NULL
, NULL
}, /* Ic */
153 { NULL
, NULL
}, /* In */
154 { NULL
, post_defaults
}, /* Li */
155 { NULL
, post_nd
}, /* Nd */
156 { NULL
, post_nm
}, /* Nm */
157 { NULL
, NULL
}, /* Op */
158 { pre_obsolete
, NULL
}, /* Ot */
159 { NULL
, post_defaults
}, /* Pa */
160 { pre_std
, NULL
}, /* Rv */
161 { NULL
, post_st
}, /* St */
162 { NULL
, NULL
}, /* Va */
163 { NULL
, NULL
}, /* Vt */
164 { NULL
, NULL
}, /* Xr */
165 { NULL
, NULL
}, /* %A */
166 { NULL
, post_hyph
}, /* %B */ /* FIXME: can be used outside Rs/Re. */
167 { NULL
, NULL
}, /* %D */
168 { NULL
, NULL
}, /* %I */
169 { NULL
, NULL
}, /* %J */
170 { NULL
, post_hyph
}, /* %N */
171 { NULL
, post_hyph
}, /* %O */
172 { NULL
, NULL
}, /* %P */
173 { NULL
, post_hyph
}, /* %R */
174 { NULL
, post_hyph
}, /* %T */ /* FIXME: can be used outside Rs/Re. */
175 { NULL
, NULL
}, /* %V */
176 { NULL
, NULL
}, /* Ac */
177 { NULL
, NULL
}, /* Ao */
178 { NULL
, NULL
}, /* Aq */
179 { NULL
, post_at
}, /* At */
180 { NULL
, NULL
}, /* Bc */
181 { NULL
, post_bf
}, /* Bf */
182 { NULL
, NULL
}, /* Bo */
183 { NULL
, NULL
}, /* Bq */
184 { NULL
, NULL
}, /* Bsx */
185 { NULL
, post_bx
}, /* Bx */
186 { pre_obsolete
, NULL
}, /* Db */
187 { NULL
, NULL
}, /* Dc */
188 { NULL
, NULL
}, /* Do */
189 { NULL
, NULL
}, /* Dq */
190 { NULL
, NULL
}, /* Ec */
191 { NULL
, NULL
}, /* Ef */
192 { NULL
, NULL
}, /* Em */
193 { NULL
, NULL
}, /* Eo */
194 { NULL
, NULL
}, /* Fx */
195 { NULL
, NULL
}, /* Ms */
196 { NULL
, NULL
}, /* No */
197 { NULL
, post_ns
}, /* Ns */
198 { NULL
, NULL
}, /* Nx */
199 { NULL
, NULL
}, /* Ox */
200 { NULL
, NULL
}, /* Pc */
201 { NULL
, NULL
}, /* Pf */
202 { NULL
, NULL
}, /* Po */
203 { NULL
, NULL
}, /* Pq */
204 { NULL
, NULL
}, /* Qc */
205 { NULL
, NULL
}, /* Ql */
206 { NULL
, NULL
}, /* Qo */
207 { NULL
, NULL
}, /* Qq */
208 { NULL
, NULL
}, /* Re */
209 { NULL
, post_rs
}, /* Rs */
210 { NULL
, NULL
}, /* Sc */
211 { NULL
, NULL
}, /* So */
212 { NULL
, NULL
}, /* Sq */
213 { NULL
, post_sm
}, /* Sm */
214 { NULL
, post_hyph
}, /* Sx */
215 { NULL
, NULL
}, /* Sy */
216 { NULL
, NULL
}, /* Tn */
217 { NULL
, NULL
}, /* Ux */
218 { NULL
, NULL
}, /* Xc */
219 { NULL
, NULL
}, /* Xo */
220 { NULL
, post_fo
}, /* Fo */
221 { NULL
, NULL
}, /* Fc */
222 { NULL
, NULL
}, /* Oo */
223 { NULL
, NULL
}, /* Oc */
224 { NULL
, post_bk
}, /* Bk */
225 { NULL
, NULL
}, /* Ek */
226 { NULL
, post_eoln
}, /* Bt */
227 { NULL
, NULL
}, /* Hf */
228 { pre_obsolete
, NULL
}, /* Fr */
229 { NULL
, post_eoln
}, /* Ud */
230 { NULL
, post_lb
}, /* Lb */
231 { pre_par
, post_par
}, /* Lp */
232 { NULL
, NULL
}, /* Lk */
233 { NULL
, post_defaults
}, /* Mt */
234 { NULL
, NULL
}, /* Brq */
235 { NULL
, NULL
}, /* Bro */
236 { NULL
, NULL
}, /* Brc */
237 { NULL
, NULL
}, /* %C */
238 { pre_obsolete
, post_es
}, /* Es */
239 { pre_obsolete
, post_en
}, /* En */
240 { NULL
, NULL
}, /* Dx */
241 { NULL
, NULL
}, /* %Q */
242 { NULL
, post_par
}, /* br */
243 { NULL
, post_par
}, /* sp */
244 { NULL
, NULL
}, /* %U */
245 { NULL
, NULL
}, /* Ta */
246 { NULL
, NULL
}, /* ll */
249 #define RSORD_MAX 14 /* Number of `Rs' blocks. */
251 static const int rsord
[RSORD_MAX
] = {
268 static const char * const secnames
[SEC__MAX
] = {
275 "IMPLEMENTATION NOTES",
290 "SECURITY CONSIDERATIONS",
296 mdoc_valid_pre(struct roff_man
*mdoc
, struct roff_node
*n
)
302 if (n
->sec
!= SEC_SYNOPSIS
|| n
->parent
->tok
!= MDOC_Fd
)
303 check_text(mdoc
, n
->line
, n
->pos
, n
->string
);
316 p
= mdoc_valids
[n
->tok
].pre
;
322 mdoc_valid_post(struct roff_man
*mdoc
)
328 if (n
->flags
& MDOC_VALID
)
330 n
->flags
|= MDOC_VALID
| MDOC_ENDED
;
345 * Closing delimiters are not special at the
346 * beginning of a block, opening delimiters
347 * are not special at the end.
350 if (n
->child
!= NULL
)
351 n
->child
->flags
&= ~MDOC_DELIMC
;
353 n
->last
->flags
&= ~MDOC_DELIMO
;
355 /* Call the macro's postprocessor. */
357 p
= mdoc_valids
[n
->tok
].post
;
365 check_args(struct roff_man
*mdoc
, struct roff_node
*n
)
372 assert(n
->args
->argc
);
373 for (i
= 0; i
< (int)n
->args
->argc
; i
++)
374 check_argv(mdoc
, n
, &n
->args
->argv
[i
]);
378 check_argv(struct roff_man
*mdoc
, struct roff_node
*n
, struct mdoc_argv
*v
)
382 for (i
= 0; i
< (int)v
->sz
; i
++)
383 check_text(mdoc
, v
->line
, v
->pos
, v
->value
[i
]);
387 check_text(struct roff_man
*mdoc
, int ln
, int pos
, char *p
)
391 if (MDOC_LITERAL
& mdoc
->flags
)
394 for (cp
= p
; NULL
!= (p
= strchr(p
, '\t')); p
++)
395 mandoc_msg(MANDOCERR_FI_TAB
, mdoc
->parse
,
396 ln
, pos
+ (int)(p
- cp
), NULL
);
400 pre_display(PRE_ARGS
)
402 struct roff_node
*node
;
404 if (n
->type
!= ROFFT_BLOCK
)
407 for (node
= mdoc
->last
->parent
; node
; node
= node
->parent
)
408 if (node
->type
== ROFFT_BLOCK
)
409 if (MDOC_Bd
== node
->tok
)
413 mandoc_vmsg(MANDOCERR_BD_NEST
,
414 mdoc
->parse
, n
->line
, n
->pos
,
415 "%s in Bd", mdoc_macronames
[n
->tok
]);
421 struct mdoc_argv
*argv
, *wa
;
423 enum mdocargt mdoclt
;
426 if (n
->type
!= ROFFT_BLOCK
)
430 * First figure out which kind of list to use: bind ourselves to
431 * the first mentioned list type and warn about any remaining
432 * ones. If we find no list type, we default to LIST_item.
435 wa
= (n
->args
== NULL
) ? NULL
: n
->args
->argv
;
436 mdoclt
= MDOC_ARG_MAX
;
437 for (i
= 0; n
->args
&& i
< (int)n
->args
->argc
; i
++) {
438 argv
= n
->args
->argv
+ i
;
441 /* Set list types. */
475 /* Set list arguments. */
477 if (n
->norm
->Bl
.comp
)
478 mandoc_msg(MANDOCERR_ARG_REP
,
479 mdoc
->parse
, argv
->line
,
480 argv
->pos
, "Bl -compact");
481 n
->norm
->Bl
.comp
= 1;
486 mandoc_msg(MANDOCERR_ARG_EMPTY
,
487 mdoc
->parse
, argv
->line
,
488 argv
->pos
, "Bl -width");
489 n
->norm
->Bl
.width
= "0n";
492 if (NULL
!= n
->norm
->Bl
.width
)
493 mandoc_vmsg(MANDOCERR_ARG_REP
,
494 mdoc
->parse
, argv
->line
,
495 argv
->pos
, "Bl -width %s",
497 rewrite_macro2len(argv
->value
);
498 n
->norm
->Bl
.width
= argv
->value
[0];
502 mandoc_msg(MANDOCERR_ARG_EMPTY
,
503 mdoc
->parse
, argv
->line
,
504 argv
->pos
, "Bl -offset");
507 if (NULL
!= n
->norm
->Bl
.offs
)
508 mandoc_vmsg(MANDOCERR_ARG_REP
,
509 mdoc
->parse
, argv
->line
,
510 argv
->pos
, "Bl -offset %s",
512 rewrite_macro2len(argv
->value
);
513 n
->norm
->Bl
.offs
= argv
->value
[0];
518 if (LIST__NONE
== lt
)
522 /* Check: multiple list types. */
524 if (LIST__NONE
!= n
->norm
->Bl
.type
) {
525 mandoc_vmsg(MANDOCERR_BL_REP
,
526 mdoc
->parse
, n
->line
, n
->pos
,
527 "Bl -%s", mdoc_argnames
[argv
->arg
]);
531 /* The list type should come first. */
533 if (n
->norm
->Bl
.width
||
536 mandoc_vmsg(MANDOCERR_BL_LATETYPE
,
537 mdoc
->parse
, n
->line
, n
->pos
, "Bl -%s",
538 mdoc_argnames
[n
->args
->argv
[0].arg
]);
540 n
->norm
->Bl
.type
= lt
;
541 if (LIST_column
== lt
) {
542 n
->norm
->Bl
.ncols
= argv
->sz
;
543 n
->norm
->Bl
.cols
= (void *)argv
->value
;
547 /* Allow lists to default to LIST_item. */
549 if (LIST__NONE
== n
->norm
->Bl
.type
) {
550 mandoc_msg(MANDOCERR_BL_NOTYPE
, mdoc
->parse
,
551 n
->line
, n
->pos
, "Bl");
552 n
->norm
->Bl
.type
= LIST_item
;
556 * Validate the width field. Some list types don't need width
557 * types and should be warned about them. Others should have it
558 * and must also be warned. Yet others have a default and need
562 switch (n
->norm
->Bl
.type
) {
564 if (NULL
== n
->norm
->Bl
.width
)
565 mandoc_msg(MANDOCERR_BL_NOWIDTH
, mdoc
->parse
,
566 n
->line
, n
->pos
, "Bl -tag");
577 if (n
->norm
->Bl
.width
)
578 mandoc_vmsg(MANDOCERR_BL_SKIPW
, mdoc
->parse
,
579 wa
->line
, wa
->pos
, "Bl -%s",
580 mdoc_argnames
[mdoclt
]);
587 if (NULL
== n
->norm
->Bl
.width
)
588 n
->norm
->Bl
.width
= "2n";
591 if (NULL
== n
->norm
->Bl
.width
)
592 n
->norm
->Bl
.width
= "3n";
603 struct mdoc_argv
*argv
;
607 pre_literal(mdoc
, n
);
609 if (n
->type
!= ROFFT_BLOCK
)
612 for (i
= 0; n
->args
&& i
< (int)n
->args
->argc
; i
++) {
613 argv
= n
->args
->argv
+ i
;
633 mandoc_msg(MANDOCERR_BD_FILE
, mdoc
->parse
,
634 n
->line
, n
->pos
, NULL
);
638 mandoc_msg(MANDOCERR_ARG_EMPTY
,
639 mdoc
->parse
, argv
->line
,
640 argv
->pos
, "Bd -offset");
643 if (NULL
!= n
->norm
->Bd
.offs
)
644 mandoc_vmsg(MANDOCERR_ARG_REP
,
645 mdoc
->parse
, argv
->line
,
646 argv
->pos
, "Bd -offset %s",
648 rewrite_macro2len(argv
->value
);
649 n
->norm
->Bd
.offs
= argv
->value
[0];
652 if (n
->norm
->Bd
.comp
)
653 mandoc_msg(MANDOCERR_ARG_REP
,
654 mdoc
->parse
, argv
->line
,
655 argv
->pos
, "Bd -compact");
656 n
->norm
->Bd
.comp
= 1;
661 if (DISP__NONE
== dt
)
664 if (DISP__NONE
== n
->norm
->Bd
.type
)
665 n
->norm
->Bd
.type
= dt
;
667 mandoc_vmsg(MANDOCERR_BD_REP
,
668 mdoc
->parse
, n
->line
, n
->pos
,
669 "Bd -%s", mdoc_argnames
[argv
->arg
]);
672 if (DISP__NONE
== n
->norm
->Bd
.type
) {
673 mandoc_msg(MANDOCERR_BD_NOTYPE
, mdoc
->parse
,
674 n
->line
, n
->pos
, "Bd");
675 n
->norm
->Bd
.type
= DISP_ragged
;
683 struct mdoc_argv
*argv
;
689 for (i
= 1; i
< n
->args
->argc
; i
++) {
690 argv
= n
->args
->argv
+ i
;
691 mandoc_vmsg(MANDOCERR_AN_REP
,
692 mdoc
->parse
, argv
->line
, argv
->pos
,
693 "An -%s", mdoc_argnames
[argv
->arg
]);
696 argv
= n
->args
->argv
;
697 if (argv
->arg
== MDOC_Split
)
698 n
->norm
->An
.auth
= AUTH_split
;
699 else if (argv
->arg
== MDOC_Nosplit
)
700 n
->norm
->An
.auth
= AUTH_nosplit
;
709 if (n
->args
&& 1 == n
->args
->argc
)
710 if (MDOC_Std
== n
->args
->argv
[0].arg
)
713 mandoc_msg(MANDOCERR_ARG_STD
, mdoc
->parse
,
714 n
->line
, n
->pos
, mdoc_macronames
[n
->tok
]);
718 pre_obsolete(PRE_ARGS
)
721 if (n
->type
== ROFFT_ELEM
|| n
->type
== ROFFT_BLOCK
)
722 mandoc_msg(MANDOCERR_MACRO_OBS
, mdoc
->parse
,
723 n
->line
, n
->pos
, mdoc_macronames
[n
->tok
]);
730 if (mdoc
->meta
.title
!= NULL
)
731 mandoc_msg(MANDOCERR_PROLOG_REP
, mdoc
->parse
,
732 n
->line
, n
->pos
, "Dt");
733 else if (mdoc
->meta
.os
!= NULL
)
734 mandoc_msg(MANDOCERR_PROLOG_ORDER
, mdoc
->parse
,
735 n
->line
, n
->pos
, "Dt after Os");
742 if (mdoc
->meta
.os
!= NULL
)
743 mandoc_msg(MANDOCERR_PROLOG_REP
, mdoc
->parse
,
744 n
->line
, n
->pos
, "Os");
745 else if (mdoc
->flags
& MDOC_PBODY
)
746 mandoc_msg(MANDOCERR_PROLOG_LATE
, mdoc
->parse
,
747 n
->line
, n
->pos
, "Os");
754 if (mdoc
->meta
.date
!= NULL
)
755 mandoc_msg(MANDOCERR_PROLOG_REP
, mdoc
->parse
,
756 n
->line
, n
->pos
, "Dd");
757 else if (mdoc
->flags
& MDOC_PBODY
)
758 mandoc_msg(MANDOCERR_PROLOG_LATE
, mdoc
->parse
,
759 n
->line
, n
->pos
, "Dd");
760 else if (mdoc
->meta
.title
!= NULL
)
761 mandoc_msg(MANDOCERR_PROLOG_ORDER
, mdoc
->parse
,
762 n
->line
, n
->pos
, "Dd after Dt");
763 else if (mdoc
->meta
.os
!= NULL
)
764 mandoc_msg(MANDOCERR_PROLOG_ORDER
, mdoc
->parse
,
765 n
->line
, n
->pos
, "Dd after Os");
771 struct roff_node
*np
, *nch
;
775 * Unlike other data pointers, these are "housed" by the HEAD
776 * element, which contains the goods.
780 if (np
->type
!= ROFFT_HEAD
)
783 assert(np
->parent
->type
== ROFFT_BLOCK
);
784 assert(MDOC_Bf
== np
->parent
->tok
);
786 /* Check the number of arguments. */
789 if (NULL
== np
->parent
->args
) {
791 mandoc_msg(MANDOCERR_BF_NOFONT
, mdoc
->parse
,
792 np
->line
, np
->pos
, "Bf");
798 mandoc_vmsg(MANDOCERR_ARG_EXCESS
, mdoc
->parse
,
799 nch
->line
, nch
->pos
, "Bf ... %s", nch
->string
);
801 /* Extract argument into data. */
803 if (np
->parent
->args
) {
804 arg
= np
->parent
->args
->argv
[0].arg
;
805 if (MDOC_Emphasis
== arg
)
806 np
->norm
->Bf
.font
= FONT_Em
;
807 else if (MDOC_Literal
== arg
)
808 np
->norm
->Bf
.font
= FONT_Li
;
809 else if (MDOC_Symbolic
== arg
)
810 np
->norm
->Bf
.font
= FONT_Sy
;
816 /* Extract parameter into data. */
818 if (0 == strcmp(np
->child
->string
, "Em"))
819 np
->norm
->Bf
.font
= FONT_Em
;
820 else if (0 == strcmp(np
->child
->string
, "Li"))
821 np
->norm
->Bf
.font
= FONT_Li
;
822 else if (0 == strcmp(np
->child
->string
, "Sy"))
823 np
->norm
->Bf
.font
= FONT_Sy
;
825 mandoc_vmsg(MANDOCERR_BF_BADFONT
, mdoc
->parse
,
826 np
->child
->line
, np
->child
->pos
,
827 "Bf %s", np
->child
->string
);
834 const char *stdlibname
;
837 n
= mdoc
->last
->child
;
838 assert(n
->type
== ROFFT_TEXT
);
840 if (NULL
== (stdlibname
= mdoc_a2lib(n
->string
)))
841 mandoc_asprintf(&libname
,
842 "library \\(Lq%s\\(Rq", n
->string
);
844 libname
= mandoc_strdup(stdlibname
);
853 const struct roff_node
*n
;
857 mandoc_vmsg(MANDOCERR_ARG_SKIP
,
858 mdoc
->parse
, n
->line
, n
->pos
,
859 "%s %s", mdoc_macronames
[n
->tok
],
864 post_fname(POST_ARGS
)
866 const struct roff_node
*n
;
870 n
= mdoc
->last
->child
;
871 pos
= strcspn(n
->string
, "()");
872 cp
= n
->string
+ pos
;
873 if ( ! (cp
[0] == '\0' || (cp
[0] == '(' && cp
[1] == '*')))
874 mandoc_msg(MANDOCERR_FN_PAREN
, mdoc
->parse
,
875 n
->line
, n
->pos
+ pos
, n
->string
);
889 const struct roff_node
*n
;
893 if (n
->type
!= ROFFT_HEAD
)
896 if (n
->child
== NULL
) {
897 mandoc_msg(MANDOCERR_FO_NOHEAD
, mdoc
->parse
,
898 n
->line
, n
->pos
, "Fo");
901 if (n
->child
!= n
->last
) {
902 mandoc_vmsg(MANDOCERR_ARG_EXCESS
, mdoc
->parse
,
903 n
->child
->next
->line
, n
->child
->next
->pos
,
904 "Fo ... %s", n
->child
->next
->string
);
905 while (n
->child
!= n
->last
)
906 roff_node_delete(mdoc
, n
->last
);
915 const struct roff_node
*n
;
918 for (n
= mdoc
->last
->child
; n
!= NULL
; n
= n
->next
) {
919 for (cp
= n
->string
; *cp
!= '\0'; cp
++) {
920 /* Ignore callbacks and alterations. */
921 if (*cp
== '(' || *cp
== '{')
925 mandoc_msg(MANDOCERR_FA_COMMA
, mdoc
->parse
,
926 n
->line
, n
->pos
+ (cp
- n
->string
),
940 if (n
->last
!= NULL
&&
941 (n
->last
->tok
== MDOC_Pp
||
942 n
->last
->tok
== MDOC_Lp
))
943 mdoc_node_relink(mdoc
, n
->last
);
945 if (NULL
!= mdoc
->meta
.name
)
948 deroff(&mdoc
->meta
.name
, n
);
950 if (NULL
== mdoc
->meta
.name
)
951 mandoc_msg(MANDOCERR_NM_NONAME
, mdoc
->parse
,
952 n
->line
, n
->pos
, "Nm");
962 if (n
->type
!= ROFFT_BODY
)
965 if (n
->child
== NULL
)
966 mandoc_msg(MANDOCERR_ND_EMPTY
, mdoc
->parse
,
967 n
->line
, n
->pos
, "Nd");
979 if (n
->type
!= ROFFT_BODY
)
982 if (n
->child
== NULL
)
983 mandoc_msg(MANDOCERR_BLK_EMPTY
, mdoc
->parse
,
984 n
->line
, n
->pos
, "D1");
990 post_literal(POST_ARGS
)
996 if (n
->type
!= ROFFT_BODY
)
999 if (n
->child
== NULL
)
1000 mandoc_msg(MANDOCERR_BLK_EMPTY
, mdoc
->parse
,
1001 n
->line
, n
->pos
, mdoc_macronames
[n
->tok
]);
1003 if (n
->tok
== MDOC_Bd
&&
1004 n
->norm
->Bd
.type
!= DISP_literal
&&
1005 n
->norm
->Bd
.type
!= DISP_unfilled
)
1008 mdoc
->flags
&= ~MDOC_LITERAL
;
1012 post_defaults(POST_ARGS
)
1014 struct roff_node
*nn
;
1017 * The `Ar' defaults to "file ..." if no value is provided as an
1018 * argument; the `Mt' and `Pa' macros use "~"; the `Li' just
1019 * gets an empty string.
1022 if (mdoc
->last
->child
)
1026 mdoc
->next
= ROFF_NEXT_CHILD
;
1030 roff_word_alloc(mdoc
, nn
->line
, nn
->pos
, "file");
1031 roff_word_alloc(mdoc
, nn
->line
, nn
->pos
, "...");
1036 roff_word_alloc(mdoc
, nn
->line
, nn
->pos
, "~");
1047 struct roff_node
*n
;
1048 const char *std_att
;
1052 if (n
->child
== NULL
) {
1053 mdoc
->next
= ROFF_NEXT_CHILD
;
1054 roff_word_alloc(mdoc
, n
->line
, n
->pos
, "AT&T UNIX");
1060 * If we have a child, look it up in the standard keys. If a
1061 * key exist, use that instead of the child; if it doesn't,
1062 * prefix "AT&T UNIX " to the existing data.
1066 assert(n
->type
== ROFFT_TEXT
);
1067 if (NULL
== (std_att
= mdoc_a2att(n
->string
))) {
1068 mandoc_vmsg(MANDOCERR_AT_BAD
, mdoc
->parse
,
1069 n
->line
, n
->pos
, "At %s", n
->string
);
1070 mandoc_asprintf(&att
, "AT&T UNIX %s", n
->string
);
1072 att
= mandoc_strdup(std_att
);
1081 struct roff_node
*np
, *nch
;
1085 if (np
->norm
->An
.auth
== AUTH__NONE
) {
1087 mandoc_msg(MANDOCERR_MACRO_EMPTY
, mdoc
->parse
,
1088 np
->line
, np
->pos
, "An");
1089 } else if (nch
!= NULL
)
1090 mandoc_vmsg(MANDOCERR_ARG_EXCESS
, mdoc
->parse
,
1091 nch
->line
, nch
->pos
, "An ... %s", nch
->string
);
1098 if (mdoc
->last
->type
== ROFFT_BLOCK
)
1099 mdoc
->last
->norm
->Es
= mdoc
->last_es
;
1106 mdoc
->last_es
= mdoc
->last
;
1112 struct roff_node
*nbl
, *nit
, *nch
;
1117 if (nit
->type
!= ROFFT_BLOCK
)
1120 nbl
= nit
->parent
->parent
;
1121 lt
= nbl
->norm
->Bl
.type
;
1133 if (nit
->head
->child
== NULL
)
1134 mandoc_vmsg(MANDOCERR_IT_NOHEAD
,
1135 mdoc
->parse
, nit
->line
, nit
->pos
,
1137 mdoc_argnames
[nbl
->args
->argv
[0].arg
]);
1146 if (nit
->body
== NULL
|| nit
->body
->child
== NULL
)
1147 mandoc_vmsg(MANDOCERR_IT_NOBODY
,
1148 mdoc
->parse
, nit
->line
, nit
->pos
,
1150 mdoc_argnames
[nbl
->args
->argv
[0].arg
]);
1153 if (nit
->head
->child
!= NULL
)
1154 mandoc_vmsg(MANDOCERR_ARG_SKIP
,
1155 mdoc
->parse
, nit
->line
, nit
->pos
,
1156 "It %s", nit
->head
->child
->string
);
1159 cols
= (int)nbl
->norm
->Bl
.ncols
;
1161 assert(nit
->head
->child
== NULL
);
1163 for (i
= 0, nch
= nit
->child
; nch
; nch
= nch
->next
)
1164 if (nch
->type
== ROFFT_BODY
)
1167 if (i
< cols
|| i
> cols
+ 1)
1168 mandoc_vmsg(MANDOCERR_BL_COL
,
1169 mdoc
->parse
, nit
->line
, nit
->pos
,
1170 "%d columns, %d cells", cols
, i
);
1178 post_bl_block(POST_ARGS
)
1180 struct roff_node
*n
, *ni
, *nc
;
1183 * These are fairly complicated, so we've broken them into two
1184 * functions. post_bl_block_tag() is called when a -tag is
1185 * specified, but no -width (it must be guessed). The second
1186 * when a -width is specified (macro indicators must be
1187 * rewritten into real lengths).
1192 if (LIST_tag
== n
->norm
->Bl
.type
&&
1193 NULL
== n
->norm
->Bl
.width
) {
1194 post_bl_block_tag(mdoc
);
1195 assert(n
->norm
->Bl
.width
);
1198 for (ni
= n
->body
->child
; ni
; ni
= ni
->next
) {
1199 if (NULL
== ni
->body
)
1201 nc
= ni
->body
->last
;
1202 while (NULL
!= nc
) {
1214 if (NULL
== ni
->next
) {
1215 mandoc_msg(MANDOCERR_PAR_MOVE
,
1216 mdoc
->parse
, nc
->line
, nc
->pos
,
1217 mdoc_macronames
[nc
->tok
]);
1218 mdoc_node_relink(mdoc
, nc
);
1219 } else if (0 == n
->norm
->Bl
.comp
&&
1220 LIST_column
!= n
->norm
->Bl
.type
) {
1221 mandoc_vmsg(MANDOCERR_PAR_SKIP
,
1222 mdoc
->parse
, nc
->line
, nc
->pos
,
1224 mdoc_macronames
[nc
->tok
]);
1225 roff_node_delete(mdoc
, nc
);
1228 nc
= ni
->body
->last
;
1234 * If the argument of -offset or -width is a macro,
1235 * replace it with the associated default width.
1238 rewrite_macro2len(char **arg
)
1245 else if ( ! strcmp(*arg
, "Ds"))
1247 else if ((tok
= mdoc_hash_find(*arg
)) == TOKEN_NONE
)
1250 width
= macro2len(tok
);
1253 mandoc_asprintf(arg
, "%zun", width
);
1257 post_bl_block_tag(POST_ARGS
)
1259 struct roff_node
*n
, *nn
;
1265 * Calculate the -width for a `Bl -tag' list if it hasn't been
1266 * provided. Uses the first head macro. NOTE AGAIN: this is
1267 * ONLY if the -width argument has NOT been provided. See
1268 * rewrite_macro2len() for converting the -width string.
1274 for (nn
= n
->body
->child
; nn
; nn
= nn
->next
) {
1275 if (MDOC_It
!= nn
->tok
)
1278 assert(nn
->type
== ROFFT_BLOCK
);
1279 nn
= nn
->head
->child
;
1284 if (nn
->type
== ROFFT_TEXT
) {
1285 sz
= strlen(nn
->string
) + 1;
1289 if (0 != (ssz
= macro2len(nn
->tok
)))
1295 /* Defaults to ten ens. */
1297 (void)snprintf(buf
, sizeof(buf
), "%un", (unsigned int)sz
);
1300 * We have to dynamically add this to the macro's argument list.
1301 * We're guaranteed that a MDOC_Width doesn't already exist.
1305 i
= (int)(n
->args
->argc
)++;
1307 n
->args
->argv
= mandoc_reallocarray(n
->args
->argv
,
1308 n
->args
->argc
, sizeof(struct mdoc_argv
));
1310 n
->args
->argv
[i
].arg
= MDOC_Width
;
1311 n
->args
->argv
[i
].line
= n
->line
;
1312 n
->args
->argv
[i
].pos
= n
->pos
;
1313 n
->args
->argv
[i
].sz
= 1;
1314 n
->args
->argv
[i
].value
= mandoc_malloc(sizeof(char *));
1315 n
->args
->argv
[i
].value
[0] = mandoc_strdup(buf
);
1317 /* Set our width! */
1318 n
->norm
->Bl
.width
= n
->args
->argv
[i
].value
[0];
1322 post_bl_head(POST_ARGS
)
1324 struct roff_node
*nbl
, *nh
, *nch
, *nnext
;
1325 struct mdoc_argv
*argv
;
1330 if (nh
->norm
->Bl
.type
!= LIST_column
) {
1331 if ((nch
= nh
->child
) == NULL
)
1333 mandoc_vmsg(MANDOCERR_ARG_EXCESS
, mdoc
->parse
,
1334 nch
->line
, nch
->pos
, "Bl ... %s", nch
->string
);
1335 while (nch
!= NULL
) {
1336 roff_node_delete(mdoc
, nch
);
1343 * Append old-style lists, where the column width specifiers
1344 * trail as macro parameters, to the new-style ("normal-form")
1345 * lists where they're argument values following -column.
1348 if (nh
->child
== NULL
)
1352 for (j
= 0; j
< (int)nbl
->args
->argc
; j
++)
1353 if (nbl
->args
->argv
[j
].arg
== MDOC_Column
)
1356 assert(j
< (int)nbl
->args
->argc
);
1359 * Accommodate for new-style groff column syntax. Shuffle the
1360 * child nodes, all of which must be TEXT, as arguments for the
1361 * column field. Then, delete the head children.
1364 argv
= nbl
->args
->argv
+ j
;
1366 argv
->sz
+= nh
->nchild
;
1367 argv
->value
= mandoc_reallocarray(argv
->value
,
1368 argv
->sz
, sizeof(char *));
1370 nh
->norm
->Bl
.ncols
= argv
->sz
;
1371 nh
->norm
->Bl
.cols
= (void *)argv
->value
;
1373 for (nch
= nh
->child
; nch
!= NULL
; nch
= nnext
) {
1374 argv
->value
[i
++] = nch
->string
;
1377 roff_node_delete(NULL
, nch
);
1386 struct roff_node
*nparent
, *nprev
; /* of the Bl block */
1387 struct roff_node
*nblock
, *nbody
; /* of the Bl */
1388 struct roff_node
*nchild
, *nnext
; /* of the Bl body */
1391 switch (nbody
->type
) {
1393 post_bl_block(mdoc
);
1404 nchild
= nbody
->child
;
1405 if (nchild
== NULL
) {
1406 mandoc_msg(MANDOCERR_BLK_EMPTY
, mdoc
->parse
,
1407 nbody
->line
, nbody
->pos
, "Bl");
1410 while (nchild
!= NULL
) {
1411 if (nchild
->tok
== MDOC_It
||
1412 (nchild
->tok
== MDOC_Sm
&&
1413 nchild
->next
!= NULL
&&
1414 nchild
->next
->tok
== MDOC_It
)) {
1415 nchild
= nchild
->next
;
1419 mandoc_msg(MANDOCERR_BL_MOVE
, mdoc
->parse
,
1420 nchild
->line
, nchild
->pos
,
1421 mdoc_macronames
[nchild
->tok
]);
1424 * Move the node out of the Bl block.
1425 * First, collect all required node pointers.
1428 nblock
= nbody
->parent
;
1429 nprev
= nblock
->prev
;
1430 nparent
= nblock
->parent
;
1431 nnext
= nchild
->next
;
1434 * Unlink this child.
1437 assert(NULL
== nchild
->prev
);
1438 if (0 == --nbody
->nchild
) {
1439 nbody
->child
= NULL
;
1441 assert(NULL
== nnext
);
1443 nbody
->child
= nnext
;
1448 * Relink this child.
1451 nchild
->parent
= nparent
;
1452 nchild
->prev
= nprev
;
1453 nchild
->next
= nblock
;
1455 nblock
->prev
= nchild
;
1458 nparent
->child
= nchild
;
1460 nprev
->next
= nchild
;
1469 struct roff_node
*n
;
1473 if (n
->type
== ROFFT_BLOCK
&& n
->body
->child
== NULL
) {
1474 mandoc_msg(MANDOCERR_BLK_EMPTY
,
1475 mdoc
->parse
, n
->line
, n
->pos
, "Bk");
1476 roff_node_delete(mdoc
, n
);
1481 post_sm(struct roff_man
*mdoc
)
1483 struct roff_node
*nch
;
1485 nch
= mdoc
->last
->child
;
1488 mdoc
->flags
^= MDOC_SMOFF
;
1492 assert(nch
->type
== ROFFT_TEXT
);
1494 if ( ! strcmp(nch
->string
, "on")) {
1495 mdoc
->flags
&= ~MDOC_SMOFF
;
1498 if ( ! strcmp(nch
->string
, "off")) {
1499 mdoc
->flags
|= MDOC_SMOFF
;
1503 mandoc_vmsg(MANDOCERR_SM_BAD
,
1504 mdoc
->parse
, nch
->line
, nch
->pos
,
1505 "%s %s", mdoc_macronames
[mdoc
->last
->tok
], nch
->string
);
1506 mdoc_node_relink(mdoc
, nch
);
1511 post_root(POST_ARGS
)
1513 struct roff_node
*n
;
1515 /* Add missing prologue data. */
1517 if (mdoc
->meta
.date
== NULL
)
1518 mdoc
->meta
.date
= mdoc
->quick
?
1520 mandoc_normdate(mdoc
->parse
, NULL
, 0, 0);
1522 if (mdoc
->meta
.title
== NULL
) {
1523 mandoc_msg(MANDOCERR_DT_NOTITLE
,
1524 mdoc
->parse
, 0, 0, "EOF");
1525 mdoc
->meta
.title
= mandoc_strdup("UNTITLED");
1528 if (mdoc
->meta
.vol
== NULL
)
1529 mdoc
->meta
.vol
= mandoc_strdup("LOCAL");
1531 if (mdoc
->meta
.os
== NULL
) {
1532 mandoc_msg(MANDOCERR_OS_MISSING
,
1533 mdoc
->parse
, 0, 0, NULL
);
1534 mdoc
->meta
.os
= mandoc_strdup("");
1537 /* Check that we begin with a proper `Sh'. */
1539 n
= mdoc
->first
->child
;
1540 while (n
!= NULL
&& n
->tok
!= TOKEN_NONE
&&
1541 mdoc_macros
[n
->tok
].flags
& MDOC_PROLOGUE
)
1545 mandoc_msg(MANDOCERR_DOC_EMPTY
, mdoc
->parse
, 0, 0, NULL
);
1546 else if (n
->tok
!= MDOC_Sh
)
1547 mandoc_msg(MANDOCERR_SEC_BEFORE
, mdoc
->parse
,
1548 n
->line
, n
->pos
, mdoc_macronames
[n
->tok
]);
1554 struct roff_node
*n
, *nch
;
1560 assert(nch
->type
== ROFFT_TEXT
);
1562 if (NULL
== (p
= mdoc_a2st(nch
->string
))) {
1563 mandoc_vmsg(MANDOCERR_ST_BAD
, mdoc
->parse
,
1564 nch
->line
, nch
->pos
, "St %s", nch
->string
);
1565 roff_node_delete(mdoc
, n
);
1568 nch
->string
= mandoc_strdup(p
);
1575 struct roff_node
*np
, *nch
, *next
, *prev
;
1580 if (np
->type
!= ROFFT_BODY
)
1583 if (np
->child
== NULL
) {
1584 mandoc_msg(MANDOCERR_RS_EMPTY
, mdoc
->parse
,
1585 np
->line
, np
->pos
, "Rs");
1590 * The full `Rs' block needs special handling to order the
1591 * sub-elements according to `rsord'. Pick through each element
1592 * and correctly order it. This is an insertion sort.
1596 for (nch
= np
->child
->next
; nch
!= NULL
; nch
= next
) {
1597 /* Determine order number of this child. */
1598 for (i
= 0; i
< RSORD_MAX
; i
++)
1599 if (rsord
[i
] == nch
->tok
)
1602 if (i
== RSORD_MAX
) {
1603 mandoc_msg(MANDOCERR_RS_BAD
,
1604 mdoc
->parse
, nch
->line
, nch
->pos
,
1605 mdoc_macronames
[nch
->tok
]);
1607 } else if (nch
->tok
== MDOC__J
|| nch
->tok
== MDOC__B
)
1608 np
->norm
->Rs
.quote_T
++;
1611 * Remove this child from the chain. This somewhat
1612 * repeats roff_node_unlink(), but since we're
1613 * just re-ordering, there's no need for the
1614 * full unlink process.
1617 if ((next
= nch
->next
) != NULL
)
1618 next
->prev
= nch
->prev
;
1620 if ((prev
= nch
->prev
) != NULL
)
1621 prev
->next
= nch
->next
;
1623 nch
->prev
= nch
->next
= NULL
;
1626 * Scan back until we reach a node that's
1627 * to be ordered before this child.
1630 for ( ; prev
; prev
= prev
->prev
) {
1631 /* Determine order of `prev'. */
1632 for (j
= 0; j
< RSORD_MAX
; j
++)
1633 if (rsord
[j
] == prev
->tok
)
1643 * Set this child back into its correct place
1644 * in front of the `prev' node.
1650 np
->child
->prev
= nch
;
1651 nch
->next
= np
->child
;
1655 prev
->next
->prev
= nch
;
1656 nch
->next
= prev
->next
;
1663 * For some arguments of some macros,
1664 * convert all breakable hyphens into ASCII_HYPH.
1667 post_hyph(POST_ARGS
)
1669 struct roff_node
*nch
;
1672 for (nch
= mdoc
->last
->child
; nch
!= NULL
; nch
= nch
->next
) {
1673 if (nch
->type
!= ROFFT_TEXT
)
1678 while (*(++cp
) != '\0')
1680 isalpha((unsigned char)cp
[-1]) &&
1681 isalpha((unsigned char)cp
[1]))
1690 if (MDOC_LINE
& mdoc
->last
->flags
)
1691 mandoc_msg(MANDOCERR_NS_SKIP
, mdoc
->parse
,
1692 mdoc
->last
->line
, mdoc
->last
->pos
, NULL
);
1701 switch (mdoc
->last
->type
) {
1706 switch (mdoc
->lastsec
) {
1711 post_sh_see_also(mdoc
);
1714 post_sh_authors(mdoc
);
1726 post_sh_name(POST_ARGS
)
1728 struct roff_node
*n
;
1733 for (n
= mdoc
->last
->child
; n
!= NULL
; n
= n
->next
) {
1740 if (n
->next
!= NULL
)
1741 mandoc_msg(MANDOCERR_NAMESEC_ND
,
1742 mdoc
->parse
, n
->line
, n
->pos
, NULL
);
1749 mandoc_msg(MANDOCERR_NAMESEC_BAD
, mdoc
->parse
,
1750 n
->line
, n
->pos
, mdoc_macronames
[n
->tok
]);
1756 mandoc_msg(MANDOCERR_NAMESEC_NONM
, mdoc
->parse
,
1757 mdoc
->last
->line
, mdoc
->last
->pos
, NULL
);
1759 mandoc_msg(MANDOCERR_NAMESEC_NOND
, mdoc
->parse
,
1760 mdoc
->last
->line
, mdoc
->last
->pos
, NULL
);
1764 post_sh_see_also(POST_ARGS
)
1766 const struct roff_node
*n
;
1767 const char *name
, *sec
;
1768 const char *lastname
, *lastsec
, *lastpunct
;
1771 n
= mdoc
->last
->child
;
1772 lastname
= lastsec
= lastpunct
= NULL
;
1774 if (n
->tok
!= MDOC_Xr
|| n
->nchild
< 2)
1777 /* Process one .Xr node. */
1779 name
= n
->child
->string
;
1780 sec
= n
->child
->next
->string
;
1781 if (lastsec
!= NULL
) {
1782 if (lastpunct
[0] != ',' || lastpunct
[1] != '\0')
1783 mandoc_vmsg(MANDOCERR_XR_PUNCT
,
1784 mdoc
->parse
, n
->line
, n
->pos
,
1785 "%s before %s(%s)", lastpunct
,
1787 cmp
= strcmp(lastsec
, sec
);
1789 mandoc_vmsg(MANDOCERR_XR_ORDER
,
1790 mdoc
->parse
, n
->line
, n
->pos
,
1791 "%s(%s) after %s(%s)", name
,
1792 sec
, lastname
, lastsec
);
1793 else if (cmp
== 0 &&
1794 strcasecmp(lastname
, name
) > 0)
1795 mandoc_vmsg(MANDOCERR_XR_ORDER
,
1796 mdoc
->parse
, n
->line
, n
->pos
,
1797 "%s after %s", name
, lastname
);
1802 /* Process the following node. */
1807 if (n
->tok
== MDOC_Xr
) {
1811 if (n
->type
!= ROFFT_TEXT
)
1813 for (name
= n
->string
; *name
!= '\0'; name
++)
1814 if (isalpha((const unsigned char)*name
))
1816 lastpunct
= n
->string
;
1817 if (n
->next
== NULL
)
1818 mandoc_vmsg(MANDOCERR_XR_PUNCT
, mdoc
->parse
,
1819 n
->line
, n
->pos
, "%s after %s(%s)",
1820 lastpunct
, lastname
, lastsec
);
1826 child_an(const struct roff_node
*n
)
1829 for (n
= n
->child
; n
!= NULL
; n
= n
->next
)
1830 if ((n
->tok
== MDOC_An
&& n
->nchild
) || child_an(n
))
1836 post_sh_authors(POST_ARGS
)
1839 if ( ! child_an(mdoc
->last
))
1840 mandoc_msg(MANDOCERR_AN_MISSING
, mdoc
->parse
,
1841 mdoc
->last
->line
, mdoc
->last
->pos
, NULL
);
1845 post_sh_head(POST_ARGS
)
1847 struct roff_node
*n
;
1848 const char *goodsec
;
1853 * Process a new section. Sections are either "named" or
1854 * "custom". Custom sections are user-defined, while named ones
1855 * follow a conventional order and may only appear in certain
1861 deroff(&secname
, mdoc
->last
);
1862 sec
= NULL
== secname
? SEC_CUSTOM
: a2sec(secname
);
1864 /* The NAME should be first. */
1866 if (SEC_NAME
!= sec
&& SEC_NONE
== mdoc
->lastnamed
)
1867 mandoc_vmsg(MANDOCERR_NAMESEC_FIRST
, mdoc
->parse
,
1868 mdoc
->last
->line
, mdoc
->last
->pos
,
1871 /* The SYNOPSIS gets special attention in other areas. */
1873 if (SEC_SYNOPSIS
== sec
) {
1874 roff_setreg(mdoc
->roff
, "nS", 1, '=');
1875 mdoc
->flags
|= MDOC_SYNOPSIS
;
1877 roff_setreg(mdoc
->roff
, "nS", 0, '=');
1878 mdoc
->flags
&= ~MDOC_SYNOPSIS
;
1881 /* Mark our last section. */
1883 mdoc
->lastsec
= sec
;
1886 * Set the section attribute for the current HEAD, for its
1887 * parent BLOCK, and for the HEAD children; the latter can
1888 * only be TEXT nodes, so no recursion is needed.
1889 * For other blocks and elements, including .Sh BODY, this is
1890 * done when allocating the node data structures, but for .Sh
1891 * BLOCK and HEAD, the section is still unknown at that time.
1894 mdoc
->last
->parent
->sec
= sec
;
1895 mdoc
->last
->sec
= sec
;
1896 for (n
= mdoc
->last
->child
; n
; n
= n
->next
)
1899 /* We don't care about custom sections after this. */
1901 if (SEC_CUSTOM
== sec
) {
1907 * Check whether our non-custom section is being repeated or is
1911 if (sec
== mdoc
->lastnamed
)
1912 mandoc_vmsg(MANDOCERR_SEC_REP
, mdoc
->parse
,
1913 mdoc
->last
->line
, mdoc
->last
->pos
,
1916 if (sec
< mdoc
->lastnamed
)
1917 mandoc_vmsg(MANDOCERR_SEC_ORDER
, mdoc
->parse
,
1918 mdoc
->last
->line
, mdoc
->last
->pos
,
1921 /* Mark the last named section. */
1923 mdoc
->lastnamed
= sec
;
1925 /* Check particular section/manual conventions. */
1927 if (mdoc
->meta
.msec
== NULL
) {
1935 if (*mdoc
->meta
.msec
== '4')
1937 goodsec
= "2, 3, 4, 9";
1939 case SEC_RETURN_VALUES
:
1942 if (*mdoc
->meta
.msec
== '2')
1944 if (*mdoc
->meta
.msec
== '3')
1946 if (NULL
== goodsec
)
1947 goodsec
= "2, 3, 9";
1950 if (*mdoc
->meta
.msec
== '9')
1952 if (NULL
== goodsec
)
1954 mandoc_vmsg(MANDOCERR_SEC_MSEC
, mdoc
->parse
,
1955 mdoc
->last
->line
, mdoc
->last
->pos
,
1956 "Sh %s for %s only", secname
, goodsec
);
1965 post_ignpar(POST_ARGS
)
1967 struct roff_node
*np
;
1969 switch (mdoc
->last
->type
) {
1979 if (NULL
!= (np
= mdoc
->last
->child
))
1980 if (MDOC_Pp
== np
->tok
|| MDOC_Lp
== np
->tok
) {
1981 mandoc_vmsg(MANDOCERR_PAR_SKIP
,
1982 mdoc
->parse
, np
->line
, np
->pos
,
1983 "%s after %s", mdoc_macronames
[np
->tok
],
1984 mdoc_macronames
[mdoc
->last
->tok
]);
1985 roff_node_delete(mdoc
, np
);
1988 if (NULL
!= (np
= mdoc
->last
->last
))
1989 if (MDOC_Pp
== np
->tok
|| MDOC_Lp
== np
->tok
) {
1990 mandoc_vmsg(MANDOCERR_PAR_SKIP
, mdoc
->parse
,
1991 np
->line
, np
->pos
, "%s at the end of %s",
1992 mdoc_macronames
[np
->tok
],
1993 mdoc_macronames
[mdoc
->last
->tok
]);
1994 roff_node_delete(mdoc
, np
);
2002 if (NULL
== mdoc
->last
)
2004 if (n
->type
!= ROFFT_ELEM
&& n
->type
!= ROFFT_BLOCK
)
2008 * Don't allow prior `Lp' or `Pp' prior to a paragraph-type
2009 * block: `Lp', `Pp', or non-compact `Bd' or `Bl'.
2012 if (MDOC_Pp
!= mdoc
->last
->tok
&&
2013 MDOC_Lp
!= mdoc
->last
->tok
&&
2014 MDOC_br
!= mdoc
->last
->tok
)
2016 if (MDOC_Bl
== n
->tok
&& n
->norm
->Bl
.comp
)
2018 if (MDOC_Bd
== n
->tok
&& n
->norm
->Bd
.comp
)
2020 if (MDOC_It
== n
->tok
&& n
->parent
->norm
->Bl
.comp
)
2023 mandoc_vmsg(MANDOCERR_PAR_SKIP
, mdoc
->parse
,
2024 mdoc
->last
->line
, mdoc
->last
->pos
,
2025 "%s before %s", mdoc_macronames
[mdoc
->last
->tok
],
2026 mdoc_macronames
[n
->tok
]);
2027 roff_node_delete(mdoc
, mdoc
->last
);
2033 struct roff_node
*np
;
2037 if (np
->tok
== MDOC_sp
) {
2039 mandoc_vmsg(MANDOCERR_ARG_EXCESS
, mdoc
->parse
,
2040 np
->child
->next
->line
, np
->child
->next
->pos
,
2041 "sp ... %s", np
->child
->next
->string
);
2042 } else if (np
->child
!= NULL
)
2043 mandoc_vmsg(MANDOCERR_ARG_SKIP
,
2044 mdoc
->parse
, np
->line
, np
->pos
, "%s %s",
2045 mdoc_macronames
[np
->tok
], np
->child
->string
);
2047 if (NULL
== (np
= mdoc
->last
->prev
)) {
2048 np
= mdoc
->last
->parent
;
2049 if (MDOC_Sh
!= np
->tok
&& MDOC_Ss
!= np
->tok
)
2051 } else if (MDOC_Pp
!= np
->tok
&& MDOC_Lp
!= np
->tok
&&
2052 (MDOC_br
!= mdoc
->last
->tok
||
2053 (MDOC_sp
!= np
->tok
&& MDOC_br
!= np
->tok
)))
2056 mandoc_vmsg(MANDOCERR_PAR_SKIP
, mdoc
->parse
,
2057 mdoc
->last
->line
, mdoc
->last
->pos
,
2058 "%s after %s", mdoc_macronames
[mdoc
->last
->tok
],
2059 mdoc_macronames
[np
->tok
]);
2060 roff_node_delete(mdoc
, mdoc
->last
);
2064 pre_literal(PRE_ARGS
)
2067 pre_display(mdoc
, n
);
2069 if (n
->type
!= ROFFT_BODY
)
2073 * The `Dl' (note "el" not "one") and `Bd -literal' and `Bd
2074 * -unfilled' macros set MDOC_LITERAL on entrance to the body.
2079 mdoc
->flags
|= MDOC_LITERAL
;
2082 if (DISP_literal
== n
->norm
->Bd
.type
)
2083 mdoc
->flags
|= MDOC_LITERAL
;
2084 if (DISP_unfilled
== n
->norm
->Bd
.type
)
2085 mdoc
->flags
|= MDOC_LITERAL
;
2095 struct roff_node
*n
;
2098 if (mdoc
->meta
.date
)
2099 free(mdoc
->meta
.date
);
2102 if (NULL
== n
->child
|| '\0' == n
->child
->string
[0]) {
2103 mdoc
->meta
.date
= mdoc
->quick
? mandoc_strdup("") :
2104 mandoc_normdate(mdoc
->parse
, NULL
, n
->line
, n
->pos
);
2109 deroff(&datestr
, n
);
2111 mdoc
->meta
.date
= datestr
;
2113 mdoc
->meta
.date
= mandoc_normdate(mdoc
->parse
,
2114 datestr
, n
->line
, n
->pos
);
2118 roff_node_delete(mdoc
, n
);
2124 struct roff_node
*nn
, *n
;
2130 free(mdoc
->meta
.title
);
2131 free(mdoc
->meta
.msec
);
2132 free(mdoc
->meta
.vol
);
2133 free(mdoc
->meta
.arch
);
2135 mdoc
->meta
.title
= NULL
;
2136 mdoc
->meta
.msec
= NULL
;
2137 mdoc
->meta
.vol
= NULL
;
2138 mdoc
->meta
.arch
= NULL
;
2140 /* Mandatory first argument: title. */
2143 if (nn
== NULL
|| *nn
->string
== '\0') {
2144 mandoc_msg(MANDOCERR_DT_NOTITLE
,
2145 mdoc
->parse
, n
->line
, n
->pos
, "Dt");
2146 mdoc
->meta
.title
= mandoc_strdup("UNTITLED");
2148 mdoc
->meta
.title
= mandoc_strdup(nn
->string
);
2150 /* Check that all characters are uppercase. */
2152 for (p
= nn
->string
; *p
!= '\0'; p
++)
2153 if (islower((unsigned char)*p
)) {
2154 mandoc_vmsg(MANDOCERR_TITLE_CASE
,
2155 mdoc
->parse
, nn
->line
,
2156 nn
->pos
+ (p
- nn
->string
),
2157 "Dt %s", nn
->string
);
2162 /* Mandatory second argument: section. */
2168 mandoc_vmsg(MANDOCERR_MSEC_MISSING
,
2169 mdoc
->parse
, n
->line
, n
->pos
,
2170 "Dt %s", mdoc
->meta
.title
);
2171 mdoc
->meta
.vol
= mandoc_strdup("LOCAL");
2172 goto out
; /* msec and arch remain NULL. */
2175 mdoc
->meta
.msec
= mandoc_strdup(nn
->string
);
2177 /* Infer volume title from section number. */
2179 cp
= mandoc_a2msec(nn
->string
);
2181 mandoc_vmsg(MANDOCERR_MSEC_BAD
, mdoc
->parse
,
2182 nn
->line
, nn
->pos
, "Dt ... %s", nn
->string
);
2183 mdoc
->meta
.vol
= mandoc_strdup(nn
->string
);
2185 mdoc
->meta
.vol
= mandoc_strdup(cp
);
2187 /* Optional third argument: architecture. */
2189 if ((nn
= nn
->next
) == NULL
)
2192 for (p
= nn
->string
; *p
!= '\0'; p
++)
2193 *p
= tolower((unsigned char)*p
);
2194 mdoc
->meta
.arch
= mandoc_strdup(nn
->string
);
2196 /* Ignore fourth and later arguments. */
2198 if ((nn
= nn
->next
) != NULL
)
2199 mandoc_vmsg(MANDOCERR_ARG_EXCESS
, mdoc
->parse
,
2200 nn
->line
, nn
->pos
, "Dt ... %s", nn
->string
);
2203 roff_node_delete(mdoc
, n
);
2209 struct roff_node
*n
;
2212 * Make `Bx's second argument always start with an uppercase
2213 * letter. Groff checks if it's an "accepted" term, but we just
2214 * uppercase blindly.
2217 n
= mdoc
->last
->child
;
2218 if (n
&& NULL
!= (n
= n
->next
))
2219 *n
->string
= (char)toupper((unsigned char)*n
->string
);
2226 struct utsname utsname
;
2227 static char *defbuf
;
2229 struct roff_node
*n
;
2234 * Set the operating system by way of the `Os' macro.
2235 * The order of precedence is:
2236 * 1. the argument of the `Os' macro, unless empty
2237 * 2. the -Ios=foo command line argument, if provided
2238 * 3. -DOSNAME="\"foo\"", if provided during compilation
2239 * 4. "sysname release" from uname(3)
2242 free(mdoc
->meta
.os
);
2243 mdoc
->meta
.os
= NULL
;
2244 deroff(&mdoc
->meta
.os
, n
);
2249 mdoc
->meta
.os
= mandoc_strdup(mdoc
->defos
);
2254 mdoc
->meta
.os
= mandoc_strdup(OSNAME
);
2256 if (NULL
== defbuf
) {
2257 if (-1 == uname(&utsname
)) {
2258 mandoc_msg(MANDOCERR_OS_UNAME
, mdoc
->parse
,
2259 n
->line
, n
->pos
, "Os");
2260 defbuf
= mandoc_strdup("UNKNOWN");
2262 mandoc_asprintf(&defbuf
, "%s %s",
2263 utsname
.sysname
, utsname
.release
);
2265 mdoc
->meta
.os
= mandoc_strdup(defbuf
);
2269 roff_node_delete(mdoc
, n
);
2273 * If no argument is provided,
2274 * fill in the name of the current manual page.
2279 struct roff_node
*n
;
2286 if (mdoc
->meta
.name
== NULL
) {
2287 mandoc_msg(MANDOCERR_EX_NONAME
, mdoc
->parse
,
2288 n
->line
, n
->pos
, "Ex");
2292 mdoc
->next
= ROFF_NEXT_CHILD
;
2293 roff_word_alloc(mdoc
, n
->line
, n
->pos
, mdoc
->meta
.name
);
2297 static enum roff_sec
2298 a2sec(const char *p
)
2302 for (i
= 0; i
< (int)SEC__MAX
; i
++)
2303 if (secnames
[i
] && 0 == strcmp(p
, secnames
[i
]))
2304 return((enum roff_sec
)i
);
2310 macro2len(int macro
)