]>
git.cameronkatri.com Git - mandoc.git/blob - mdoc_validate.c
1 /* $Id: mdoc_validate.c,v 1.296 2015/10/12 21:26:02 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
);
314 p
= mdoc_valids
[n
->tok
].pre
;
320 mdoc_valid_post(struct roff_man
*mdoc
)
326 if (n
->flags
& MDOC_VALID
)
328 n
->flags
|= MDOC_VALID
| MDOC_ENDED
;
341 * Closing delimiters are not special at the
342 * beginning of a block, opening delimiters
343 * are not special at the end.
346 if (n
->child
!= NULL
)
347 n
->child
->flags
&= ~MDOC_DELIMC
;
349 n
->last
->flags
&= ~MDOC_DELIMO
;
351 /* Call the macro's postprocessor. */
353 p
= mdoc_valids
[n
->tok
].post
;
361 check_args(struct roff_man
*mdoc
, struct roff_node
*n
)
368 assert(n
->args
->argc
);
369 for (i
= 0; i
< (int)n
->args
->argc
; i
++)
370 check_argv(mdoc
, n
, &n
->args
->argv
[i
]);
374 check_argv(struct roff_man
*mdoc
, struct roff_node
*n
, struct mdoc_argv
*v
)
378 for (i
= 0; i
< (int)v
->sz
; i
++)
379 check_text(mdoc
, v
->line
, v
->pos
, v
->value
[i
]);
383 check_text(struct roff_man
*mdoc
, int ln
, int pos
, char *p
)
387 if (MDOC_LITERAL
& mdoc
->flags
)
390 for (cp
= p
; NULL
!= (p
= strchr(p
, '\t')); p
++)
391 mandoc_msg(MANDOCERR_FI_TAB
, mdoc
->parse
,
392 ln
, pos
+ (int)(p
- cp
), NULL
);
396 pre_display(PRE_ARGS
)
398 struct roff_node
*node
;
400 if (n
->type
!= ROFFT_BLOCK
)
403 for (node
= mdoc
->last
->parent
; node
; node
= node
->parent
)
404 if (node
->type
== ROFFT_BLOCK
)
405 if (MDOC_Bd
== node
->tok
)
409 mandoc_vmsg(MANDOCERR_BD_NEST
,
410 mdoc
->parse
, n
->line
, n
->pos
,
411 "%s in Bd", mdoc_macronames
[n
->tok
]);
417 struct mdoc_argv
*argv
, *wa
;
419 enum mdocargt mdoclt
;
422 if (n
->type
!= ROFFT_BLOCK
)
426 * First figure out which kind of list to use: bind ourselves to
427 * the first mentioned list type and warn about any remaining
428 * ones. If we find no list type, we default to LIST_item.
431 wa
= (n
->args
== NULL
) ? NULL
: n
->args
->argv
;
432 mdoclt
= MDOC_ARG_MAX
;
433 for (i
= 0; n
->args
&& i
< (int)n
->args
->argc
; i
++) {
434 argv
= n
->args
->argv
+ i
;
437 /* Set list types. */
471 /* Set list arguments. */
473 if (n
->norm
->Bl
.comp
)
474 mandoc_msg(MANDOCERR_ARG_REP
,
475 mdoc
->parse
, argv
->line
,
476 argv
->pos
, "Bl -compact");
477 n
->norm
->Bl
.comp
= 1;
482 mandoc_msg(MANDOCERR_ARG_EMPTY
,
483 mdoc
->parse
, argv
->line
,
484 argv
->pos
, "Bl -width");
485 n
->norm
->Bl
.width
= "0n";
488 if (NULL
!= n
->norm
->Bl
.width
)
489 mandoc_vmsg(MANDOCERR_ARG_REP
,
490 mdoc
->parse
, argv
->line
,
491 argv
->pos
, "Bl -width %s",
493 rewrite_macro2len(argv
->value
);
494 n
->norm
->Bl
.width
= argv
->value
[0];
498 mandoc_msg(MANDOCERR_ARG_EMPTY
,
499 mdoc
->parse
, argv
->line
,
500 argv
->pos
, "Bl -offset");
503 if (NULL
!= n
->norm
->Bl
.offs
)
504 mandoc_vmsg(MANDOCERR_ARG_REP
,
505 mdoc
->parse
, argv
->line
,
506 argv
->pos
, "Bl -offset %s",
508 rewrite_macro2len(argv
->value
);
509 n
->norm
->Bl
.offs
= argv
->value
[0];
514 if (LIST__NONE
== lt
)
518 /* Check: multiple list types. */
520 if (LIST__NONE
!= n
->norm
->Bl
.type
) {
521 mandoc_vmsg(MANDOCERR_BL_REP
,
522 mdoc
->parse
, n
->line
, n
->pos
,
523 "Bl -%s", mdoc_argnames
[argv
->arg
]);
527 /* The list type should come first. */
529 if (n
->norm
->Bl
.width
||
532 mandoc_vmsg(MANDOCERR_BL_LATETYPE
,
533 mdoc
->parse
, n
->line
, n
->pos
, "Bl -%s",
534 mdoc_argnames
[n
->args
->argv
[0].arg
]);
536 n
->norm
->Bl
.type
= lt
;
537 if (LIST_column
== lt
) {
538 n
->norm
->Bl
.ncols
= argv
->sz
;
539 n
->norm
->Bl
.cols
= (void *)argv
->value
;
543 /* Allow lists to default to LIST_item. */
545 if (LIST__NONE
== n
->norm
->Bl
.type
) {
546 mandoc_msg(MANDOCERR_BL_NOTYPE
, mdoc
->parse
,
547 n
->line
, n
->pos
, "Bl");
548 n
->norm
->Bl
.type
= LIST_item
;
552 * Validate the width field. Some list types don't need width
553 * types and should be warned about them. Others should have it
554 * and must also be warned. Yet others have a default and need
558 switch (n
->norm
->Bl
.type
) {
560 if (NULL
== n
->norm
->Bl
.width
)
561 mandoc_msg(MANDOCERR_BL_NOWIDTH
, mdoc
->parse
,
562 n
->line
, n
->pos
, "Bl -tag");
569 if (n
->norm
->Bl
.width
)
570 mandoc_vmsg(MANDOCERR_BL_SKIPW
, mdoc
->parse
,
571 wa
->line
, wa
->pos
, "Bl -%s",
572 mdoc_argnames
[mdoclt
]);
577 if (NULL
== n
->norm
->Bl
.width
)
578 n
->norm
->Bl
.width
= "2n";
581 if (NULL
== n
->norm
->Bl
.width
)
582 n
->norm
->Bl
.width
= "3n";
593 struct mdoc_argv
*argv
;
597 pre_literal(mdoc
, n
);
599 if (n
->type
!= ROFFT_BLOCK
)
602 for (i
= 0; n
->args
&& i
< (int)n
->args
->argc
; i
++) {
603 argv
= n
->args
->argv
+ i
;
623 mandoc_msg(MANDOCERR_BD_FILE
, mdoc
->parse
,
624 n
->line
, n
->pos
, NULL
);
628 mandoc_msg(MANDOCERR_ARG_EMPTY
,
629 mdoc
->parse
, argv
->line
,
630 argv
->pos
, "Bd -offset");
633 if (NULL
!= n
->norm
->Bd
.offs
)
634 mandoc_vmsg(MANDOCERR_ARG_REP
,
635 mdoc
->parse
, argv
->line
,
636 argv
->pos
, "Bd -offset %s",
638 rewrite_macro2len(argv
->value
);
639 n
->norm
->Bd
.offs
= argv
->value
[0];
642 if (n
->norm
->Bd
.comp
)
643 mandoc_msg(MANDOCERR_ARG_REP
,
644 mdoc
->parse
, argv
->line
,
645 argv
->pos
, "Bd -compact");
646 n
->norm
->Bd
.comp
= 1;
651 if (DISP__NONE
== dt
)
654 if (DISP__NONE
== n
->norm
->Bd
.type
)
655 n
->norm
->Bd
.type
= dt
;
657 mandoc_vmsg(MANDOCERR_BD_REP
,
658 mdoc
->parse
, n
->line
, n
->pos
,
659 "Bd -%s", mdoc_argnames
[argv
->arg
]);
662 if (DISP__NONE
== n
->norm
->Bd
.type
) {
663 mandoc_msg(MANDOCERR_BD_NOTYPE
, mdoc
->parse
,
664 n
->line
, n
->pos
, "Bd");
665 n
->norm
->Bd
.type
= DISP_ragged
;
673 struct mdoc_argv
*argv
;
679 for (i
= 1; i
< n
->args
->argc
; i
++) {
680 argv
= n
->args
->argv
+ i
;
681 mandoc_vmsg(MANDOCERR_AN_REP
,
682 mdoc
->parse
, argv
->line
, argv
->pos
,
683 "An -%s", mdoc_argnames
[argv
->arg
]);
686 argv
= n
->args
->argv
;
687 if (argv
->arg
== MDOC_Split
)
688 n
->norm
->An
.auth
= AUTH_split
;
689 else if (argv
->arg
== MDOC_Nosplit
)
690 n
->norm
->An
.auth
= AUTH_nosplit
;
699 if (n
->args
&& 1 == n
->args
->argc
)
700 if (MDOC_Std
== n
->args
->argv
[0].arg
)
703 mandoc_msg(MANDOCERR_ARG_STD
, mdoc
->parse
,
704 n
->line
, n
->pos
, mdoc_macronames
[n
->tok
]);
708 pre_obsolete(PRE_ARGS
)
711 if (n
->type
== ROFFT_ELEM
|| n
->type
== ROFFT_BLOCK
)
712 mandoc_msg(MANDOCERR_MACRO_OBS
, mdoc
->parse
,
713 n
->line
, n
->pos
, mdoc_macronames
[n
->tok
]);
720 if (mdoc
->meta
.title
!= NULL
)
721 mandoc_msg(MANDOCERR_PROLOG_REP
, mdoc
->parse
,
722 n
->line
, n
->pos
, "Dt");
723 else if (mdoc
->meta
.os
!= NULL
)
724 mandoc_msg(MANDOCERR_PROLOG_ORDER
, mdoc
->parse
,
725 n
->line
, n
->pos
, "Dt after Os");
732 if (mdoc
->meta
.os
!= NULL
)
733 mandoc_msg(MANDOCERR_PROLOG_REP
, mdoc
->parse
,
734 n
->line
, n
->pos
, "Os");
735 else if (mdoc
->flags
& MDOC_PBODY
)
736 mandoc_msg(MANDOCERR_PROLOG_LATE
, mdoc
->parse
,
737 n
->line
, n
->pos
, "Os");
744 if (mdoc
->meta
.date
!= NULL
)
745 mandoc_msg(MANDOCERR_PROLOG_REP
, mdoc
->parse
,
746 n
->line
, n
->pos
, "Dd");
747 else if (mdoc
->flags
& MDOC_PBODY
)
748 mandoc_msg(MANDOCERR_PROLOG_LATE
, mdoc
->parse
,
749 n
->line
, n
->pos
, "Dd");
750 else if (mdoc
->meta
.title
!= NULL
)
751 mandoc_msg(MANDOCERR_PROLOG_ORDER
, mdoc
->parse
,
752 n
->line
, n
->pos
, "Dd after Dt");
753 else if (mdoc
->meta
.os
!= NULL
)
754 mandoc_msg(MANDOCERR_PROLOG_ORDER
, mdoc
->parse
,
755 n
->line
, n
->pos
, "Dd after Os");
761 struct roff_node
*np
, *nch
;
765 * Unlike other data pointers, these are "housed" by the HEAD
766 * element, which contains the goods.
770 if (np
->type
!= ROFFT_HEAD
)
773 assert(np
->parent
->type
== ROFFT_BLOCK
);
774 assert(MDOC_Bf
== np
->parent
->tok
);
776 /* Check the number of arguments. */
779 if (NULL
== np
->parent
->args
) {
781 mandoc_msg(MANDOCERR_BF_NOFONT
, mdoc
->parse
,
782 np
->line
, np
->pos
, "Bf");
788 mandoc_vmsg(MANDOCERR_ARG_EXCESS
, mdoc
->parse
,
789 nch
->line
, nch
->pos
, "Bf ... %s", nch
->string
);
791 /* Extract argument into data. */
793 if (np
->parent
->args
) {
794 arg
= np
->parent
->args
->argv
[0].arg
;
795 if (MDOC_Emphasis
== arg
)
796 np
->norm
->Bf
.font
= FONT_Em
;
797 else if (MDOC_Literal
== arg
)
798 np
->norm
->Bf
.font
= FONT_Li
;
799 else if (MDOC_Symbolic
== arg
)
800 np
->norm
->Bf
.font
= FONT_Sy
;
806 /* Extract parameter into data. */
808 if (0 == strcmp(np
->child
->string
, "Em"))
809 np
->norm
->Bf
.font
= FONT_Em
;
810 else if (0 == strcmp(np
->child
->string
, "Li"))
811 np
->norm
->Bf
.font
= FONT_Li
;
812 else if (0 == strcmp(np
->child
->string
, "Sy"))
813 np
->norm
->Bf
.font
= FONT_Sy
;
815 mandoc_vmsg(MANDOCERR_BF_BADFONT
, mdoc
->parse
,
816 np
->child
->line
, np
->child
->pos
,
817 "Bf %s", np
->child
->string
);
824 const char *stdlibname
;
827 n
= mdoc
->last
->child
;
828 assert(n
->type
== ROFFT_TEXT
);
830 if (NULL
== (stdlibname
= mdoc_a2lib(n
->string
)))
831 mandoc_asprintf(&libname
,
832 "library \\(Lq%s\\(Rq", n
->string
);
834 libname
= mandoc_strdup(stdlibname
);
843 const struct roff_node
*n
;
847 mandoc_vmsg(MANDOCERR_ARG_SKIP
,
848 mdoc
->parse
, n
->line
, n
->pos
,
849 "%s %s", mdoc_macronames
[n
->tok
],
854 post_fname(POST_ARGS
)
856 const struct roff_node
*n
;
860 n
= mdoc
->last
->child
;
861 pos
= strcspn(n
->string
, "()");
862 cp
= n
->string
+ pos
;
863 if ( ! (cp
[0] == '\0' || (cp
[0] == '(' && cp
[1] == '*')))
864 mandoc_msg(MANDOCERR_FN_PAREN
, mdoc
->parse
,
865 n
->line
, n
->pos
+ pos
, n
->string
);
879 const struct roff_node
*n
;
883 if (n
->type
!= ROFFT_HEAD
)
886 if (n
->child
== NULL
) {
887 mandoc_msg(MANDOCERR_FO_NOHEAD
, mdoc
->parse
,
888 n
->line
, n
->pos
, "Fo");
891 if (n
->child
!= n
->last
) {
892 mandoc_vmsg(MANDOCERR_ARG_EXCESS
, mdoc
->parse
,
893 n
->child
->next
->line
, n
->child
->next
->pos
,
894 "Fo ... %s", n
->child
->next
->string
);
895 while (n
->child
!= n
->last
)
896 roff_node_delete(mdoc
, n
->last
);
905 const struct roff_node
*n
;
908 for (n
= mdoc
->last
->child
; n
!= NULL
; n
= n
->next
) {
909 for (cp
= n
->string
; *cp
!= '\0'; cp
++) {
910 /* Ignore callbacks and alterations. */
911 if (*cp
== '(' || *cp
== '{')
915 mandoc_msg(MANDOCERR_FA_COMMA
, mdoc
->parse
,
916 n
->line
, n
->pos
+ (cp
- n
->string
),
930 if (n
->last
!= NULL
&&
931 (n
->last
->tok
== MDOC_Pp
||
932 n
->last
->tok
== MDOC_Lp
))
933 mdoc_node_relink(mdoc
, n
->last
);
935 if (NULL
!= mdoc
->meta
.name
)
938 deroff(&mdoc
->meta
.name
, n
);
940 if (NULL
== mdoc
->meta
.name
)
941 mandoc_msg(MANDOCERR_NM_NONAME
, mdoc
->parse
,
942 n
->line
, n
->pos
, "Nm");
952 if (n
->type
!= ROFFT_BODY
)
955 if (n
->child
== NULL
)
956 mandoc_msg(MANDOCERR_ND_EMPTY
, mdoc
->parse
,
957 n
->line
, n
->pos
, "Nd");
969 if (n
->type
!= ROFFT_BODY
)
972 if (n
->child
== NULL
)
973 mandoc_msg(MANDOCERR_BLK_EMPTY
, mdoc
->parse
,
974 n
->line
, n
->pos
, "D1");
980 post_literal(POST_ARGS
)
986 if (n
->type
!= ROFFT_BODY
)
989 if (n
->child
== NULL
)
990 mandoc_msg(MANDOCERR_BLK_EMPTY
, mdoc
->parse
,
991 n
->line
, n
->pos
, mdoc_macronames
[n
->tok
]);
993 if (n
->tok
== MDOC_Bd
&&
994 n
->norm
->Bd
.type
!= DISP_literal
&&
995 n
->norm
->Bd
.type
!= DISP_unfilled
)
998 mdoc
->flags
&= ~MDOC_LITERAL
;
1002 post_defaults(POST_ARGS
)
1004 struct roff_node
*nn
;
1007 * The `Ar' defaults to "file ..." if no value is provided as an
1008 * argument; the `Mt' and `Pa' macros use "~"; the `Li' just
1009 * gets an empty string.
1012 if (mdoc
->last
->child
)
1016 mdoc
->next
= ROFF_NEXT_CHILD
;
1020 roff_word_alloc(mdoc
, nn
->line
, nn
->pos
, "file");
1021 roff_word_alloc(mdoc
, nn
->line
, nn
->pos
, "...");
1025 roff_word_alloc(mdoc
, nn
->line
, nn
->pos
, "~");
1036 struct roff_node
*n
;
1037 const char *std_att
;
1041 if (n
->child
== NULL
) {
1042 mdoc
->next
= ROFF_NEXT_CHILD
;
1043 roff_word_alloc(mdoc
, n
->line
, n
->pos
, "AT&T UNIX");
1049 * If we have a child, look it up in the standard keys. If a
1050 * key exist, use that instead of the child; if it doesn't,
1051 * prefix "AT&T UNIX " to the existing data.
1055 assert(n
->type
== ROFFT_TEXT
);
1056 if (NULL
== (std_att
= mdoc_a2att(n
->string
))) {
1057 mandoc_vmsg(MANDOCERR_AT_BAD
, mdoc
->parse
,
1058 n
->line
, n
->pos
, "At %s", n
->string
);
1059 mandoc_asprintf(&att
, "AT&T UNIX %s", n
->string
);
1061 att
= mandoc_strdup(std_att
);
1070 struct roff_node
*np
, *nch
;
1074 if (np
->norm
->An
.auth
== AUTH__NONE
) {
1076 mandoc_msg(MANDOCERR_MACRO_EMPTY
, mdoc
->parse
,
1077 np
->line
, np
->pos
, "An");
1078 } else if (nch
!= NULL
)
1079 mandoc_vmsg(MANDOCERR_ARG_EXCESS
, mdoc
->parse
,
1080 nch
->line
, nch
->pos
, "An ... %s", nch
->string
);
1087 if (mdoc
->last
->type
== ROFFT_BLOCK
)
1088 mdoc
->last
->norm
->Es
= mdoc
->last_es
;
1095 mdoc
->last_es
= mdoc
->last
;
1101 struct roff_node
*nbl
, *nit
, *nch
;
1106 if (nit
->type
!= ROFFT_BLOCK
)
1109 nbl
= nit
->parent
->parent
;
1110 lt
= nbl
->norm
->Bl
.type
;
1118 if (nit
->head
->child
== NULL
)
1119 mandoc_vmsg(MANDOCERR_IT_NOHEAD
,
1120 mdoc
->parse
, nit
->line
, nit
->pos
,
1122 mdoc_argnames
[nbl
->args
->argv
[0].arg
]);
1128 if (nit
->body
== NULL
|| nit
->body
->child
== NULL
)
1129 mandoc_vmsg(MANDOCERR_IT_NOBODY
,
1130 mdoc
->parse
, nit
->line
, nit
->pos
,
1132 mdoc_argnames
[nbl
->args
->argv
[0].arg
]);
1135 if (nit
->head
->child
!= NULL
)
1136 mandoc_vmsg(MANDOCERR_ARG_SKIP
,
1137 mdoc
->parse
, nit
->line
, nit
->pos
,
1138 "It %s", nit
->head
->child
->string
);
1141 cols
= (int)nbl
->norm
->Bl
.ncols
;
1143 assert(nit
->head
->child
== NULL
);
1145 for (i
= 0, nch
= nit
->child
; nch
; nch
= nch
->next
)
1146 if (nch
->type
== ROFFT_BODY
)
1149 if (i
< cols
|| i
> cols
+ 1)
1150 mandoc_vmsg(MANDOCERR_BL_COL
,
1151 mdoc
->parse
, nit
->line
, nit
->pos
,
1152 "%d columns, %d cells", cols
, i
);
1160 post_bl_block(POST_ARGS
)
1162 struct roff_node
*n
, *ni
, *nc
;
1165 * These are fairly complicated, so we've broken them into two
1166 * functions. post_bl_block_tag() is called when a -tag is
1167 * specified, but no -width (it must be guessed). The second
1168 * when a -width is specified (macro indicators must be
1169 * rewritten into real lengths).
1174 if (LIST_tag
== n
->norm
->Bl
.type
&&
1175 NULL
== n
->norm
->Bl
.width
) {
1176 post_bl_block_tag(mdoc
);
1177 assert(n
->norm
->Bl
.width
);
1180 for (ni
= n
->body
->child
; ni
; ni
= ni
->next
) {
1181 if (NULL
== ni
->body
)
1183 nc
= ni
->body
->last
;
1184 while (NULL
!= nc
) {
1194 if (NULL
== ni
->next
) {
1195 mandoc_msg(MANDOCERR_PAR_MOVE
,
1196 mdoc
->parse
, nc
->line
, nc
->pos
,
1197 mdoc_macronames
[nc
->tok
]);
1198 mdoc_node_relink(mdoc
, nc
);
1199 } else if (0 == n
->norm
->Bl
.comp
&&
1200 LIST_column
!= n
->norm
->Bl
.type
) {
1201 mandoc_vmsg(MANDOCERR_PAR_SKIP
,
1202 mdoc
->parse
, nc
->line
, nc
->pos
,
1204 mdoc_macronames
[nc
->tok
]);
1205 roff_node_delete(mdoc
, nc
);
1208 nc
= ni
->body
->last
;
1214 * If the argument of -offset or -width is a macro,
1215 * replace it with the associated default width.
1218 rewrite_macro2len(char **arg
)
1225 else if ( ! strcmp(*arg
, "Ds"))
1227 else if ((tok
= mdoc_hash_find(*arg
)) == TOKEN_NONE
)
1230 width
= macro2len(tok
);
1233 mandoc_asprintf(arg
, "%zun", width
);
1237 post_bl_block_tag(POST_ARGS
)
1239 struct roff_node
*n
, *nn
;
1245 * Calculate the -width for a `Bl -tag' list if it hasn't been
1246 * provided. Uses the first head macro. NOTE AGAIN: this is
1247 * ONLY if the -width argument has NOT been provided. See
1248 * rewrite_macro2len() for converting the -width string.
1254 for (nn
= n
->body
->child
; nn
; nn
= nn
->next
) {
1255 if (MDOC_It
!= nn
->tok
)
1258 assert(nn
->type
== ROFFT_BLOCK
);
1259 nn
= nn
->head
->child
;
1264 if (nn
->type
== ROFFT_TEXT
) {
1265 sz
= strlen(nn
->string
) + 1;
1269 if (0 != (ssz
= macro2len(nn
->tok
)))
1275 /* Defaults to ten ens. */
1277 (void)snprintf(buf
, sizeof(buf
), "%un", (unsigned int)sz
);
1280 * We have to dynamically add this to the macro's argument list.
1281 * We're guaranteed that a MDOC_Width doesn't already exist.
1285 i
= (int)(n
->args
->argc
)++;
1287 n
->args
->argv
= mandoc_reallocarray(n
->args
->argv
,
1288 n
->args
->argc
, sizeof(struct mdoc_argv
));
1290 n
->args
->argv
[i
].arg
= MDOC_Width
;
1291 n
->args
->argv
[i
].line
= n
->line
;
1292 n
->args
->argv
[i
].pos
= n
->pos
;
1293 n
->args
->argv
[i
].sz
= 1;
1294 n
->args
->argv
[i
].value
= mandoc_malloc(sizeof(char *));
1295 n
->args
->argv
[i
].value
[0] = mandoc_strdup(buf
);
1297 /* Set our width! */
1298 n
->norm
->Bl
.width
= n
->args
->argv
[i
].value
[0];
1302 post_bl_head(POST_ARGS
)
1304 struct roff_node
*nbl
, *nh
, *nch
, *nnext
;
1305 struct mdoc_argv
*argv
;
1310 if (nh
->norm
->Bl
.type
!= LIST_column
) {
1311 if ((nch
= nh
->child
) == NULL
)
1313 mandoc_vmsg(MANDOCERR_ARG_EXCESS
, mdoc
->parse
,
1314 nch
->line
, nch
->pos
, "Bl ... %s", nch
->string
);
1315 while (nch
!= NULL
) {
1316 roff_node_delete(mdoc
, nch
);
1323 * Append old-style lists, where the column width specifiers
1324 * trail as macro parameters, to the new-style ("normal-form")
1325 * lists where they're argument values following -column.
1328 if (nh
->child
== NULL
)
1332 for (j
= 0; j
< (int)nbl
->args
->argc
; j
++)
1333 if (nbl
->args
->argv
[j
].arg
== MDOC_Column
)
1336 assert(j
< (int)nbl
->args
->argc
);
1339 * Accommodate for new-style groff column syntax. Shuffle the
1340 * child nodes, all of which must be TEXT, as arguments for the
1341 * column field. Then, delete the head children.
1344 argv
= nbl
->args
->argv
+ j
;
1346 argv
->sz
+= nh
->nchild
;
1347 argv
->value
= mandoc_reallocarray(argv
->value
,
1348 argv
->sz
, sizeof(char *));
1350 nh
->norm
->Bl
.ncols
= argv
->sz
;
1351 nh
->norm
->Bl
.cols
= (void *)argv
->value
;
1353 for (nch
= nh
->child
; nch
!= NULL
; nch
= nnext
) {
1354 argv
->value
[i
++] = nch
->string
;
1357 roff_node_delete(NULL
, nch
);
1366 struct roff_node
*nparent
, *nprev
; /* of the Bl block */
1367 struct roff_node
*nblock
, *nbody
; /* of the Bl */
1368 struct roff_node
*nchild
, *nnext
; /* of the Bl body */
1371 switch (nbody
->type
) {
1373 post_bl_block(mdoc
);
1384 nchild
= nbody
->child
;
1385 if (nchild
== NULL
) {
1386 mandoc_msg(MANDOCERR_BLK_EMPTY
, mdoc
->parse
,
1387 nbody
->line
, nbody
->pos
, "Bl");
1390 while (nchild
!= NULL
) {
1391 if (nchild
->tok
== MDOC_It
||
1392 (nchild
->tok
== MDOC_Sm
&&
1393 nchild
->next
!= NULL
&&
1394 nchild
->next
->tok
== MDOC_It
)) {
1395 nchild
= nchild
->next
;
1399 mandoc_msg(MANDOCERR_BL_MOVE
, mdoc
->parse
,
1400 nchild
->line
, nchild
->pos
,
1401 mdoc_macronames
[nchild
->tok
]);
1404 * Move the node out of the Bl block.
1405 * First, collect all required node pointers.
1408 nblock
= nbody
->parent
;
1409 nprev
= nblock
->prev
;
1410 nparent
= nblock
->parent
;
1411 nnext
= nchild
->next
;
1414 * Unlink this child.
1417 assert(NULL
== nchild
->prev
);
1418 if (0 == --nbody
->nchild
) {
1419 nbody
->child
= NULL
;
1421 assert(NULL
== nnext
);
1423 nbody
->child
= nnext
;
1428 * Relink this child.
1431 nchild
->parent
= nparent
;
1432 nchild
->prev
= nprev
;
1433 nchild
->next
= nblock
;
1435 nblock
->prev
= nchild
;
1438 nparent
->child
= nchild
;
1440 nprev
->next
= nchild
;
1449 struct roff_node
*n
;
1453 if (n
->type
== ROFFT_BLOCK
&& n
->body
->child
== NULL
) {
1454 mandoc_msg(MANDOCERR_BLK_EMPTY
,
1455 mdoc
->parse
, n
->line
, n
->pos
, "Bk");
1456 roff_node_delete(mdoc
, n
);
1461 post_sm(struct roff_man
*mdoc
)
1463 struct roff_node
*nch
;
1465 nch
= mdoc
->last
->child
;
1468 mdoc
->flags
^= MDOC_SMOFF
;
1472 assert(nch
->type
== ROFFT_TEXT
);
1474 if ( ! strcmp(nch
->string
, "on")) {
1475 mdoc
->flags
&= ~MDOC_SMOFF
;
1478 if ( ! strcmp(nch
->string
, "off")) {
1479 mdoc
->flags
|= MDOC_SMOFF
;
1483 mandoc_vmsg(MANDOCERR_SM_BAD
,
1484 mdoc
->parse
, nch
->line
, nch
->pos
,
1485 "%s %s", mdoc_macronames
[mdoc
->last
->tok
], nch
->string
);
1486 mdoc_node_relink(mdoc
, nch
);
1491 post_root(POST_ARGS
)
1493 struct roff_node
*n
;
1495 /* Add missing prologue data. */
1497 if (mdoc
->meta
.date
== NULL
)
1498 mdoc
->meta
.date
= mdoc
->quick
?
1500 mandoc_normdate(mdoc
->parse
, NULL
, 0, 0);
1502 if (mdoc
->meta
.title
== NULL
) {
1503 mandoc_msg(MANDOCERR_DT_NOTITLE
,
1504 mdoc
->parse
, 0, 0, "EOF");
1505 mdoc
->meta
.title
= mandoc_strdup("UNTITLED");
1508 if (mdoc
->meta
.vol
== NULL
)
1509 mdoc
->meta
.vol
= mandoc_strdup("LOCAL");
1511 if (mdoc
->meta
.os
== NULL
) {
1512 mandoc_msg(MANDOCERR_OS_MISSING
,
1513 mdoc
->parse
, 0, 0, NULL
);
1514 mdoc
->meta
.os
= mandoc_strdup("");
1517 /* Check that we begin with a proper `Sh'. */
1519 n
= mdoc
->first
->child
;
1520 while (n
!= NULL
&& n
->tok
!= TOKEN_NONE
&&
1521 mdoc_macros
[n
->tok
].flags
& MDOC_PROLOGUE
)
1525 mandoc_msg(MANDOCERR_DOC_EMPTY
, mdoc
->parse
, 0, 0, NULL
);
1526 else if (n
->tok
!= MDOC_Sh
)
1527 mandoc_msg(MANDOCERR_SEC_BEFORE
, mdoc
->parse
,
1528 n
->line
, n
->pos
, mdoc_macronames
[n
->tok
]);
1534 struct roff_node
*n
, *nch
;
1540 assert(nch
->type
== ROFFT_TEXT
);
1542 if (NULL
== (p
= mdoc_a2st(nch
->string
))) {
1543 mandoc_vmsg(MANDOCERR_ST_BAD
, mdoc
->parse
,
1544 nch
->line
, nch
->pos
, "St %s", nch
->string
);
1545 roff_node_delete(mdoc
, n
);
1548 nch
->string
= mandoc_strdup(p
);
1555 struct roff_node
*np
, *nch
, *next
, *prev
;
1560 if (np
->type
!= ROFFT_BODY
)
1563 if (np
->child
== NULL
) {
1564 mandoc_msg(MANDOCERR_RS_EMPTY
, mdoc
->parse
,
1565 np
->line
, np
->pos
, "Rs");
1570 * The full `Rs' block needs special handling to order the
1571 * sub-elements according to `rsord'. Pick through each element
1572 * and correctly order it. This is an insertion sort.
1576 for (nch
= np
->child
->next
; nch
!= NULL
; nch
= next
) {
1577 /* Determine order number of this child. */
1578 for (i
= 0; i
< RSORD_MAX
; i
++)
1579 if (rsord
[i
] == nch
->tok
)
1582 if (i
== RSORD_MAX
) {
1583 mandoc_msg(MANDOCERR_RS_BAD
,
1584 mdoc
->parse
, nch
->line
, nch
->pos
,
1585 mdoc_macronames
[nch
->tok
]);
1587 } else if (nch
->tok
== MDOC__J
|| nch
->tok
== MDOC__B
)
1588 np
->norm
->Rs
.quote_T
++;
1591 * Remove this child from the chain. This somewhat
1592 * repeats roff_node_unlink(), but since we're
1593 * just re-ordering, there's no need for the
1594 * full unlink process.
1597 if ((next
= nch
->next
) != NULL
)
1598 next
->prev
= nch
->prev
;
1600 if ((prev
= nch
->prev
) != NULL
)
1601 prev
->next
= nch
->next
;
1603 nch
->prev
= nch
->next
= NULL
;
1606 * Scan back until we reach a node that's
1607 * to be ordered before this child.
1610 for ( ; prev
; prev
= prev
->prev
) {
1611 /* Determine order of `prev'. */
1612 for (j
= 0; j
< RSORD_MAX
; j
++)
1613 if (rsord
[j
] == prev
->tok
)
1623 * Set this child back into its correct place
1624 * in front of the `prev' node.
1630 np
->child
->prev
= nch
;
1631 nch
->next
= np
->child
;
1635 prev
->next
->prev
= nch
;
1636 nch
->next
= prev
->next
;
1643 * For some arguments of some macros,
1644 * convert all breakable hyphens into ASCII_HYPH.
1647 post_hyph(POST_ARGS
)
1649 struct roff_node
*nch
;
1652 for (nch
= mdoc
->last
->child
; nch
!= NULL
; nch
= nch
->next
) {
1653 if (nch
->type
!= ROFFT_TEXT
)
1658 while (*(++cp
) != '\0')
1660 isalpha((unsigned char)cp
[-1]) &&
1661 isalpha((unsigned char)cp
[1]))
1670 if (MDOC_LINE
& mdoc
->last
->flags
)
1671 mandoc_msg(MANDOCERR_NS_SKIP
, mdoc
->parse
,
1672 mdoc
->last
->line
, mdoc
->last
->pos
, NULL
);
1681 switch (mdoc
->last
->type
) {
1686 switch (mdoc
->lastsec
) {
1691 post_sh_see_also(mdoc
);
1694 post_sh_authors(mdoc
);
1706 post_sh_name(POST_ARGS
)
1708 struct roff_node
*n
;
1713 for (n
= mdoc
->last
->child
; n
!= NULL
; n
= n
->next
) {
1720 if (n
->next
!= NULL
)
1721 mandoc_msg(MANDOCERR_NAMESEC_ND
,
1722 mdoc
->parse
, n
->line
, n
->pos
, NULL
);
1729 mandoc_msg(MANDOCERR_NAMESEC_BAD
, mdoc
->parse
,
1730 n
->line
, n
->pos
, mdoc_macronames
[n
->tok
]);
1736 mandoc_msg(MANDOCERR_NAMESEC_NONM
, mdoc
->parse
,
1737 mdoc
->last
->line
, mdoc
->last
->pos
, NULL
);
1739 mandoc_msg(MANDOCERR_NAMESEC_NOND
, mdoc
->parse
,
1740 mdoc
->last
->line
, mdoc
->last
->pos
, NULL
);
1744 post_sh_see_also(POST_ARGS
)
1746 const struct roff_node
*n
;
1747 const char *name
, *sec
;
1748 const char *lastname
, *lastsec
, *lastpunct
;
1751 n
= mdoc
->last
->child
;
1752 lastname
= lastsec
= lastpunct
= NULL
;
1754 if (n
->tok
!= MDOC_Xr
|| n
->nchild
< 2)
1757 /* Process one .Xr node. */
1759 name
= n
->child
->string
;
1760 sec
= n
->child
->next
->string
;
1761 if (lastsec
!= NULL
) {
1762 if (lastpunct
[0] != ',' || lastpunct
[1] != '\0')
1763 mandoc_vmsg(MANDOCERR_XR_PUNCT
,
1764 mdoc
->parse
, n
->line
, n
->pos
,
1765 "%s before %s(%s)", lastpunct
,
1767 cmp
= strcmp(lastsec
, sec
);
1769 mandoc_vmsg(MANDOCERR_XR_ORDER
,
1770 mdoc
->parse
, n
->line
, n
->pos
,
1771 "%s(%s) after %s(%s)", name
,
1772 sec
, lastname
, lastsec
);
1773 else if (cmp
== 0 &&
1774 strcasecmp(lastname
, name
) > 0)
1775 mandoc_vmsg(MANDOCERR_XR_ORDER
,
1776 mdoc
->parse
, n
->line
, n
->pos
,
1777 "%s after %s", name
, lastname
);
1782 /* Process the following node. */
1787 if (n
->tok
== MDOC_Xr
) {
1791 if (n
->type
!= ROFFT_TEXT
)
1793 for (name
= n
->string
; *name
!= '\0'; name
++)
1794 if (isalpha((const unsigned char)*name
))
1796 lastpunct
= n
->string
;
1797 if (n
->next
== NULL
)
1798 mandoc_vmsg(MANDOCERR_XR_PUNCT
, mdoc
->parse
,
1799 n
->line
, n
->pos
, "%s after %s(%s)",
1800 lastpunct
, lastname
, lastsec
);
1806 child_an(const struct roff_node
*n
)
1809 for (n
= n
->child
; n
!= NULL
; n
= n
->next
)
1810 if ((n
->tok
== MDOC_An
&& n
->nchild
) || child_an(n
))
1816 post_sh_authors(POST_ARGS
)
1819 if ( ! child_an(mdoc
->last
))
1820 mandoc_msg(MANDOCERR_AN_MISSING
, mdoc
->parse
,
1821 mdoc
->last
->line
, mdoc
->last
->pos
, NULL
);
1825 post_sh_head(POST_ARGS
)
1827 struct roff_node
*n
;
1828 const char *goodsec
;
1833 * Process a new section. Sections are either "named" or
1834 * "custom". Custom sections are user-defined, while named ones
1835 * follow a conventional order and may only appear in certain
1840 deroff(&secname
, mdoc
->last
);
1841 sec
= NULL
== secname
? SEC_CUSTOM
: a2sec(secname
);
1843 /* The NAME should be first. */
1845 if (SEC_NAME
!= sec
&& SEC_NONE
== mdoc
->lastnamed
)
1846 mandoc_vmsg(MANDOCERR_NAMESEC_FIRST
, mdoc
->parse
,
1847 mdoc
->last
->line
, mdoc
->last
->pos
,
1850 /* The SYNOPSIS gets special attention in other areas. */
1852 if (SEC_SYNOPSIS
== sec
) {
1853 roff_setreg(mdoc
->roff
, "nS", 1, '=');
1854 mdoc
->flags
|= MDOC_SYNOPSIS
;
1856 roff_setreg(mdoc
->roff
, "nS", 0, '=');
1857 mdoc
->flags
&= ~MDOC_SYNOPSIS
;
1860 /* Mark our last section. */
1862 mdoc
->lastsec
= sec
;
1865 * Set the section attribute for the current HEAD, for its
1866 * parent BLOCK, and for the HEAD children; the latter can
1867 * only be TEXT nodes, so no recursion is needed.
1868 * For other blocks and elements, including .Sh BODY, this is
1869 * done when allocating the node data structures, but for .Sh
1870 * BLOCK and HEAD, the section is still unknown at that time.
1873 mdoc
->last
->parent
->sec
= sec
;
1874 mdoc
->last
->sec
= sec
;
1875 for (n
= mdoc
->last
->child
; n
; n
= n
->next
)
1878 /* We don't care about custom sections after this. */
1880 if (SEC_CUSTOM
== sec
) {
1886 * Check whether our non-custom section is being repeated or is
1890 if (sec
== mdoc
->lastnamed
)
1891 mandoc_vmsg(MANDOCERR_SEC_REP
, mdoc
->parse
,
1892 mdoc
->last
->line
, mdoc
->last
->pos
,
1895 if (sec
< mdoc
->lastnamed
)
1896 mandoc_vmsg(MANDOCERR_SEC_ORDER
, mdoc
->parse
,
1897 mdoc
->last
->line
, mdoc
->last
->pos
,
1900 /* Mark the last named section. */
1902 mdoc
->lastnamed
= sec
;
1904 /* Check particular section/manual conventions. */
1906 if (mdoc
->meta
.msec
== NULL
) {
1914 if (*mdoc
->meta
.msec
== '4')
1916 goodsec
= "2, 3, 4, 9";
1918 case SEC_RETURN_VALUES
:
1920 if (*mdoc
->meta
.msec
== '2')
1922 if (*mdoc
->meta
.msec
== '3')
1924 if (NULL
== goodsec
)
1925 goodsec
= "2, 3, 9";
1928 if (*mdoc
->meta
.msec
== '9')
1930 if (NULL
== goodsec
)
1932 mandoc_vmsg(MANDOCERR_SEC_MSEC
, mdoc
->parse
,
1933 mdoc
->last
->line
, mdoc
->last
->pos
,
1934 "Sh %s for %s only", secname
, goodsec
);
1943 post_ignpar(POST_ARGS
)
1945 struct roff_node
*np
;
1947 switch (mdoc
->last
->type
) {
1957 if (NULL
!= (np
= mdoc
->last
->child
))
1958 if (MDOC_Pp
== np
->tok
|| MDOC_Lp
== np
->tok
) {
1959 mandoc_vmsg(MANDOCERR_PAR_SKIP
,
1960 mdoc
->parse
, np
->line
, np
->pos
,
1961 "%s after %s", mdoc_macronames
[np
->tok
],
1962 mdoc_macronames
[mdoc
->last
->tok
]);
1963 roff_node_delete(mdoc
, np
);
1966 if (NULL
!= (np
= mdoc
->last
->last
))
1967 if (MDOC_Pp
== np
->tok
|| MDOC_Lp
== np
->tok
) {
1968 mandoc_vmsg(MANDOCERR_PAR_SKIP
, mdoc
->parse
,
1969 np
->line
, np
->pos
, "%s at the end of %s",
1970 mdoc_macronames
[np
->tok
],
1971 mdoc_macronames
[mdoc
->last
->tok
]);
1972 roff_node_delete(mdoc
, np
);
1980 if (NULL
== mdoc
->last
)
1982 if (n
->type
!= ROFFT_ELEM
&& n
->type
!= ROFFT_BLOCK
)
1986 * Don't allow prior `Lp' or `Pp' prior to a paragraph-type
1987 * block: `Lp', `Pp', or non-compact `Bd' or `Bl'.
1990 if (MDOC_Pp
!= mdoc
->last
->tok
&&
1991 MDOC_Lp
!= mdoc
->last
->tok
&&
1992 MDOC_br
!= mdoc
->last
->tok
)
1994 if (MDOC_Bl
== n
->tok
&& n
->norm
->Bl
.comp
)
1996 if (MDOC_Bd
== n
->tok
&& n
->norm
->Bd
.comp
)
1998 if (MDOC_It
== n
->tok
&& n
->parent
->norm
->Bl
.comp
)
2001 mandoc_vmsg(MANDOCERR_PAR_SKIP
, mdoc
->parse
,
2002 mdoc
->last
->line
, mdoc
->last
->pos
,
2003 "%s before %s", mdoc_macronames
[mdoc
->last
->tok
],
2004 mdoc_macronames
[n
->tok
]);
2005 roff_node_delete(mdoc
, mdoc
->last
);
2011 struct roff_node
*np
;
2015 if (np
->tok
== MDOC_sp
) {
2017 mandoc_vmsg(MANDOCERR_ARG_EXCESS
, mdoc
->parse
,
2018 np
->child
->next
->line
, np
->child
->next
->pos
,
2019 "sp ... %s", np
->child
->next
->string
);
2020 } else if (np
->child
!= NULL
)
2021 mandoc_vmsg(MANDOCERR_ARG_SKIP
,
2022 mdoc
->parse
, np
->line
, np
->pos
, "%s %s",
2023 mdoc_macronames
[np
->tok
], np
->child
->string
);
2025 if (NULL
== (np
= mdoc
->last
->prev
)) {
2026 np
= mdoc
->last
->parent
;
2027 if (MDOC_Sh
!= np
->tok
&& MDOC_Ss
!= np
->tok
)
2029 } else if (MDOC_Pp
!= np
->tok
&& MDOC_Lp
!= np
->tok
&&
2030 (MDOC_br
!= mdoc
->last
->tok
||
2031 (MDOC_sp
!= np
->tok
&& MDOC_br
!= np
->tok
)))
2034 mandoc_vmsg(MANDOCERR_PAR_SKIP
, mdoc
->parse
,
2035 mdoc
->last
->line
, mdoc
->last
->pos
,
2036 "%s after %s", mdoc_macronames
[mdoc
->last
->tok
],
2037 mdoc_macronames
[np
->tok
]);
2038 roff_node_delete(mdoc
, mdoc
->last
);
2042 pre_literal(PRE_ARGS
)
2045 pre_display(mdoc
, n
);
2047 if (n
->type
!= ROFFT_BODY
)
2051 * The `Dl' (note "el" not "one") and `Bd -literal' and `Bd
2052 * -unfilled' macros set MDOC_LITERAL on entrance to the body.
2057 mdoc
->flags
|= MDOC_LITERAL
;
2060 if (DISP_literal
== n
->norm
->Bd
.type
)
2061 mdoc
->flags
|= MDOC_LITERAL
;
2062 if (DISP_unfilled
== n
->norm
->Bd
.type
)
2063 mdoc
->flags
|= MDOC_LITERAL
;
2073 struct roff_node
*n
;
2076 if (mdoc
->meta
.date
)
2077 free(mdoc
->meta
.date
);
2080 if (NULL
== n
->child
|| '\0' == n
->child
->string
[0]) {
2081 mdoc
->meta
.date
= mdoc
->quick
? mandoc_strdup("") :
2082 mandoc_normdate(mdoc
->parse
, NULL
, n
->line
, n
->pos
);
2087 deroff(&datestr
, n
);
2089 mdoc
->meta
.date
= datestr
;
2091 mdoc
->meta
.date
= mandoc_normdate(mdoc
->parse
,
2092 datestr
, n
->line
, n
->pos
);
2096 roff_node_delete(mdoc
, n
);
2102 struct roff_node
*nn
, *n
;
2108 free(mdoc
->meta
.title
);
2109 free(mdoc
->meta
.msec
);
2110 free(mdoc
->meta
.vol
);
2111 free(mdoc
->meta
.arch
);
2113 mdoc
->meta
.title
= NULL
;
2114 mdoc
->meta
.msec
= NULL
;
2115 mdoc
->meta
.vol
= NULL
;
2116 mdoc
->meta
.arch
= NULL
;
2118 /* Mandatory first argument: title. */
2121 if (nn
== NULL
|| *nn
->string
== '\0') {
2122 mandoc_msg(MANDOCERR_DT_NOTITLE
,
2123 mdoc
->parse
, n
->line
, n
->pos
, "Dt");
2124 mdoc
->meta
.title
= mandoc_strdup("UNTITLED");
2126 mdoc
->meta
.title
= mandoc_strdup(nn
->string
);
2128 /* Check that all characters are uppercase. */
2130 for (p
= nn
->string
; *p
!= '\0'; p
++)
2131 if (islower((unsigned char)*p
)) {
2132 mandoc_vmsg(MANDOCERR_TITLE_CASE
,
2133 mdoc
->parse
, nn
->line
,
2134 nn
->pos
+ (p
- nn
->string
),
2135 "Dt %s", nn
->string
);
2140 /* Mandatory second argument: section. */
2146 mandoc_vmsg(MANDOCERR_MSEC_MISSING
,
2147 mdoc
->parse
, n
->line
, n
->pos
,
2148 "Dt %s", mdoc
->meta
.title
);
2149 mdoc
->meta
.vol
= mandoc_strdup("LOCAL");
2150 goto out
; /* msec and arch remain NULL. */
2153 mdoc
->meta
.msec
= mandoc_strdup(nn
->string
);
2155 /* Infer volume title from section number. */
2157 cp
= mandoc_a2msec(nn
->string
);
2159 mandoc_vmsg(MANDOCERR_MSEC_BAD
, mdoc
->parse
,
2160 nn
->line
, nn
->pos
, "Dt ... %s", nn
->string
);
2161 mdoc
->meta
.vol
= mandoc_strdup(nn
->string
);
2163 mdoc
->meta
.vol
= mandoc_strdup(cp
);
2165 /* Optional third argument: architecture. */
2167 if ((nn
= nn
->next
) == NULL
)
2170 for (p
= nn
->string
; *p
!= '\0'; p
++)
2171 *p
= tolower((unsigned char)*p
);
2172 mdoc
->meta
.arch
= mandoc_strdup(nn
->string
);
2174 /* Ignore fourth and later arguments. */
2176 if ((nn
= nn
->next
) != NULL
)
2177 mandoc_vmsg(MANDOCERR_ARG_EXCESS
, mdoc
->parse
,
2178 nn
->line
, nn
->pos
, "Dt ... %s", nn
->string
);
2181 roff_node_delete(mdoc
, n
);
2187 struct roff_node
*n
;
2190 * Make `Bx's second argument always start with an uppercase
2191 * letter. Groff checks if it's an "accepted" term, but we just
2192 * uppercase blindly.
2195 n
= mdoc
->last
->child
;
2196 if (n
&& NULL
!= (n
= n
->next
))
2197 *n
->string
= (char)toupper((unsigned char)*n
->string
);
2204 struct utsname utsname
;
2205 static char *defbuf
;
2207 struct roff_node
*n
;
2212 * Set the operating system by way of the `Os' macro.
2213 * The order of precedence is:
2214 * 1. the argument of the `Os' macro, unless empty
2215 * 2. the -Ios=foo command line argument, if provided
2216 * 3. -DOSNAME="\"foo\"", if provided during compilation
2217 * 4. "sysname release" from uname(3)
2220 free(mdoc
->meta
.os
);
2221 mdoc
->meta
.os
= NULL
;
2222 deroff(&mdoc
->meta
.os
, n
);
2227 mdoc
->meta
.os
= mandoc_strdup(mdoc
->defos
);
2232 mdoc
->meta
.os
= mandoc_strdup(OSNAME
);
2234 if (NULL
== defbuf
) {
2235 if (-1 == uname(&utsname
)) {
2236 mandoc_msg(MANDOCERR_OS_UNAME
, mdoc
->parse
,
2237 n
->line
, n
->pos
, "Os");
2238 defbuf
= mandoc_strdup("UNKNOWN");
2240 mandoc_asprintf(&defbuf
, "%s %s",
2241 utsname
.sysname
, utsname
.release
);
2243 mdoc
->meta
.os
= mandoc_strdup(defbuf
);
2247 roff_node_delete(mdoc
, n
);
2251 * If no argument is provided,
2252 * fill in the name of the current manual page.
2257 struct roff_node
*n
;
2264 if (mdoc
->meta
.name
== NULL
) {
2265 mandoc_msg(MANDOCERR_EX_NONAME
, mdoc
->parse
,
2266 n
->line
, n
->pos
, "Ex");
2270 mdoc
->next
= ROFF_NEXT_CHILD
;
2271 roff_word_alloc(mdoc
, n
->line
, n
->pos
, mdoc
->meta
.name
);
2275 static enum roff_sec
2276 a2sec(const char *p
)
2280 for (i
= 0; i
< (int)SEC__MAX
; i
++)
2281 if (secnames
[i
] && 0 == strcmp(p
, secnames
[i
]))
2282 return (enum roff_sec
)i
;
2288 macro2len(int macro
)