1 /* $Id: mdoc_validate.c,v 1.385 2020/04/18 20:40:10 schwarze Exp $ */
3 * Copyright (c) 2010-2020 Ingo Schwarze <schwarze@openbsd.org>
4 * Copyright (c) 2008-2012 Kristaps Dzonsons <kristaps@bsd.lv>
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.
19 * Validation module for mdoc(7) syntax trees used by mandoc(1).
23 #include <sys/types.h>
25 #include <sys/utsname.h>
36 #include "mandoc_aux.h"
38 #include "mandoc_xr.h"
41 #include "libmandoc.h"
46 /* FIXME: .Bl -diag can't have non-text children in HEAD. */
48 #define POST_ARGS struct roff_man *mdoc
56 typedef void (*v_post
)(POST_ARGS
);
58 static int build_list(struct roff_man
*, int);
59 static void check_argv(struct roff_man
*,
60 struct roff_node
*, struct mdoc_argv
*);
61 static void check_args(struct roff_man
*, struct roff_node
*);
62 static void check_text(struct roff_man
*, int, int, char *);
63 static void check_text_em(struct roff_man
*, int, int, char *);
64 static void check_toptext(struct roff_man
*, int, int, const char *);
65 static int child_an(const struct roff_node
*);
66 static size_t macro2len(enum roff_tok
);
67 static void rewrite_macro2len(struct roff_man
*, char **);
68 static int similar(const char *, const char *);
70 static void post_abort(POST_ARGS
) __attribute__((__noreturn__
));
71 static void post_an(POST_ARGS
);
72 static void post_an_norm(POST_ARGS
);
73 static void post_at(POST_ARGS
);
74 static void post_bd(POST_ARGS
);
75 static void post_bf(POST_ARGS
);
76 static void post_bk(POST_ARGS
);
77 static void post_bl(POST_ARGS
);
78 static void post_bl_block(POST_ARGS
);
79 static void post_bl_head(POST_ARGS
);
80 static void post_bl_norm(POST_ARGS
);
81 static void post_bx(POST_ARGS
);
82 static void post_defaults(POST_ARGS
);
83 static void post_display(POST_ARGS
);
84 static void post_dd(POST_ARGS
);
85 static void post_delim(POST_ARGS
);
86 static void post_delim_nb(POST_ARGS
);
87 static void post_dt(POST_ARGS
);
88 static void post_em(POST_ARGS
);
89 static void post_en(POST_ARGS
);
90 static void post_er(POST_ARGS
);
91 static void post_es(POST_ARGS
);
92 static void post_eoln(POST_ARGS
);
93 static void post_ex(POST_ARGS
);
94 static void post_fa(POST_ARGS
);
95 static void post_fn(POST_ARGS
);
96 static void post_fname(POST_ARGS
);
97 static void post_fo(POST_ARGS
);
98 static void post_hyph(POST_ARGS
);
99 static void post_it(POST_ARGS
);
100 static void post_lb(POST_ARGS
);
101 static void post_nd(POST_ARGS
);
102 static void post_nm(POST_ARGS
);
103 static void post_ns(POST_ARGS
);
104 static void post_obsolete(POST_ARGS
);
105 static void post_os(POST_ARGS
);
106 static void post_par(POST_ARGS
);
107 static void post_prevpar(POST_ARGS
);
108 static void post_root(POST_ARGS
);
109 static void post_rs(POST_ARGS
);
110 static void post_rv(POST_ARGS
);
111 static void post_section(POST_ARGS
);
112 static void post_sh(POST_ARGS
);
113 static void post_sh_head(POST_ARGS
);
114 static void post_sh_name(POST_ARGS
);
115 static void post_sh_see_also(POST_ARGS
);
116 static void post_sh_authors(POST_ARGS
);
117 static void post_sm(POST_ARGS
);
118 static void post_st(POST_ARGS
);
119 static void post_std(POST_ARGS
);
120 static void post_sx(POST_ARGS
);
121 static void post_tag(POST_ARGS
);
122 static void post_tg(POST_ARGS
);
123 static void post_useless(POST_ARGS
);
124 static void post_xr(POST_ARGS
);
125 static void post_xx(POST_ARGS
);
127 static const v_post mdoc_valids
[MDOC_MAX
- MDOC_Dd
] = {
132 post_section
, /* Ss */
134 post_display
, /* D1 */
135 post_display
, /* Dl */
136 post_display
, /* Bd */
141 post_delim_nb
, /* Ad */
144 post_defaults
, /* Ar */
155 post_delim_nb
, /* Ft */
157 post_delim_nb
, /* In */
161 post_delim_nb
, /* Op */
163 post_defaults
, /* Pa */
166 post_delim_nb
, /* Va */
167 post_delim_nb
, /* Vt */
170 post_hyph
, /* %B */ /* FIXME: can be used outside Rs/Re. */
178 post_hyph
, /* %T */ /* FIXME: can be used outside Rs/Re. */
182 post_delim_nb
, /* Aq */
190 post_obsolete
, /* Db */
207 post_delim_nb
, /* Pq */
209 post_delim_nb
, /* Ql */
211 post_delim_nb
, /* Qq */
216 post_delim_nb
, /* Sq */
220 post_useless
, /* Tn */
231 post_obsolete
, /* Hf */
232 post_obsolete
, /* Fr */
236 post_delim_nb
, /* Lk */
237 post_defaults
, /* Mt */
238 post_delim_nb
, /* Brq */
251 #define RSORD_MAX 14 /* Number of `Rs' blocks. */
253 static const enum roff_tok rsord
[RSORD_MAX
] = {
270 static const char * const secnames
[SEC__MAX
] = {
277 "IMPLEMENTATION NOTES",
292 "SECURITY CONSIDERATIONS",
296 static int fn_prio
= TAG_STRONG
;
299 /* Validate the subtree rooted at mdoc->last. */
301 mdoc_validate(struct roff_man
*mdoc
)
303 struct roff_node
*n
, *np
;
307 * Translate obsolete macros to modern macros first
308 * such that later code does not need to look
309 * for the obsolete versions.
326 * Iterate over all children, recursing into each one
327 * in turn, depth-first.
330 mdoc
->last
= mdoc
->last
->child
;
331 while (mdoc
->last
!= NULL
) {
334 mdoc
->last
= mdoc
->last
->child
;
336 mdoc
->last
= mdoc
->last
->next
;
339 /* Finally validate the macro itself. */
342 mdoc
->next
= ROFF_NEXT_SIBLING
;
346 if (n
->sec
!= SEC_SYNOPSIS
||
347 (np
->tok
!= MDOC_Cd
&& np
->tok
!= MDOC_Fd
))
348 check_text(mdoc
, n
->line
, n
->pos
, n
->string
);
349 if ((n
->flags
& NODE_NOFILL
) == 0 &&
350 (np
->tok
!= MDOC_It
|| np
->type
!= ROFFT_HEAD
||
351 np
->parent
->parent
->norm
->Bl
.type
!= LIST_diag
))
352 check_text_em(mdoc
, n
->line
, n
->pos
, n
->string
);
353 if (np
->tok
== MDOC_It
|| (np
->type
== ROFFT_BODY
&&
354 (np
->tok
== MDOC_Sh
|| np
->tok
== MDOC_Ss
)))
355 check_toptext(mdoc
, n
->line
, n
->pos
, n
->string
);
365 check_args(mdoc
, mdoc
->last
);
368 * Closing delimiters are not special at the
369 * beginning of a block, opening delimiters
370 * are not special at the end.
373 if (n
->child
!= NULL
)
374 n
->child
->flags
&= ~NODE_DELIMC
;
376 n
->last
->flags
&= ~NODE_DELIMO
;
378 /* Call the macro's postprocessor. */
380 if (n
->tok
< ROFF_MAX
) {
385 assert(n
->tok
>= MDOC_Dd
&& n
->tok
< MDOC_MAX
);
386 p
= mdoc_valids
+ (n
->tok
- MDOC_Dd
);
396 check_args(struct roff_man
*mdoc
, struct roff_node
*n
)
403 assert(n
->args
->argc
);
404 for (i
= 0; i
< (int)n
->args
->argc
; i
++)
405 check_argv(mdoc
, n
, &n
->args
->argv
[i
]);
409 check_argv(struct roff_man
*mdoc
, struct roff_node
*n
, struct mdoc_argv
*v
)
413 for (i
= 0; i
< (int)v
->sz
; i
++)
414 check_text(mdoc
, v
->line
, v
->pos
, v
->value
[i
]);
418 check_text(struct roff_man
*mdoc
, int ln
, int pos
, char *p
)
422 if (mdoc
->last
->flags
& NODE_NOFILL
)
425 for (cp
= p
; NULL
!= (p
= strchr(p
, '\t')); p
++)
426 mandoc_msg(MANDOCERR_FI_TAB
, ln
, pos
+ (int)(p
- cp
), NULL
);
430 check_text_em(struct roff_man
*mdoc
, int ln
, int pos
, char *p
)
432 const struct roff_node
*np
, *nn
;
435 np
= mdoc
->last
->prev
;
436 nn
= mdoc
->last
->next
;
438 /* Look for em-dashes wrongly encoded as "--". */
440 for (cp
= p
; *cp
!= '\0'; cp
++) {
441 if (cp
[0] != '-' || cp
[1] != '-')
445 /* Skip input sequences of more than two '-'. */
453 /* Skip "--" directly attached to something else. */
455 if ((cp
- p
> 1 && cp
[-2] != ' ') ||
456 (cp
[1] != '\0' && cp
[1] != ' '))
459 /* Require a letter right before or right afterwards. */
462 isalpha((unsigned char)cp
[-3]) :
464 np
->type
== ROFFT_TEXT
&&
465 *np
->string
!= '\0' &&
466 isalpha((unsigned char)np
->string
[
467 strlen(np
->string
) - 1])) ||
468 (cp
[1] != '\0' && cp
[2] != '\0' ?
469 isalpha((unsigned char)cp
[2]) :
471 nn
->type
== ROFFT_TEXT
&&
472 isalpha((unsigned char)*nn
->string
))) {
473 mandoc_msg(MANDOCERR_DASHDASH
,
474 ln
, pos
+ (int)(cp
- p
) - 1, NULL
);
481 check_toptext(struct roff_man
*mdoc
, int ln
, int pos
, const char *p
)
483 const char *cp
, *cpr
;
488 if ((cp
= strstr(p
, "OpenBSD")) != NULL
)
489 mandoc_msg(MANDOCERR_BX
, ln
, pos
+ (int)(cp
- p
), "Ox");
490 if ((cp
= strstr(p
, "NetBSD")) != NULL
)
491 mandoc_msg(MANDOCERR_BX
, ln
, pos
+ (int)(cp
- p
), "Nx");
492 if ((cp
= strstr(p
, "FreeBSD")) != NULL
)
493 mandoc_msg(MANDOCERR_BX
, ln
, pos
+ (int)(cp
- p
), "Fx");
494 if ((cp
= strstr(p
, "DragonFly")) != NULL
)
495 mandoc_msg(MANDOCERR_BX
, ln
, pos
+ (int)(cp
- p
), "Dx");
498 while ((cp
= strstr(cp
+ 1, "()")) != NULL
) {
499 for (cpr
= cp
- 1; cpr
>= p
; cpr
--)
500 if (*cpr
!= '_' && !isalnum((unsigned char)*cpr
))
502 if ((cpr
< p
|| *cpr
== ' ') && cpr
+ 1 < cp
) {
504 mandoc_msg(MANDOCERR_FUNC
, ln
, pos
+ (int)(cpr
- p
),
505 "%.*s()", (int)(cp
- cpr
), cpr
);
511 post_abort(POST_ARGS
)
517 post_delim(POST_ARGS
)
519 const struct roff_node
*nch
;
524 tok
= mdoc
->last
->tok
;
525 nch
= mdoc
->last
->last
;
526 if (nch
== NULL
|| nch
->type
!= ROFFT_TEXT
)
528 lc
= strchr(nch
->string
, '\0') - 1;
529 if (lc
< nch
->string
)
531 delim
= mdoc_isdelim(lc
);
532 if (delim
== DELIM_NONE
|| delim
== DELIM_OPEN
)
534 if (*lc
== ')' && (tok
== MDOC_Nd
|| tok
== MDOC_Sh
||
535 tok
== MDOC_Ss
|| tok
== MDOC_Fo
))
538 mandoc_msg(MANDOCERR_DELIM
, nch
->line
,
539 nch
->pos
+ (int)(lc
- nch
->string
), "%s%s %s", roff_name
[tok
],
540 nch
== mdoc
->last
->child
? "" : " ...", nch
->string
);
544 post_delim_nb(POST_ARGS
)
546 const struct roff_node
*nch
;
553 * Find candidates: at least two bytes,
554 * the last one a closing or middle delimiter.
557 tok
= mdoc
->last
->tok
;
558 nch
= mdoc
->last
->last
;
559 if (nch
== NULL
|| nch
->type
!= ROFFT_TEXT
)
561 lc
= strchr(nch
->string
, '\0') - 1;
562 if (lc
<= nch
->string
)
564 delim
= mdoc_isdelim(lc
);
565 if (delim
== DELIM_NONE
|| delim
== DELIM_OPEN
)
569 * Reduce false positives by allowing various cases.
572 /* Escaped delimiters. */
573 if (lc
> nch
->string
+ 1 && lc
[-2] == '\\' &&
574 (lc
[-1] == '&' || lc
[-1] == 'e'))
577 /* Specific byte sequences. */
580 for (cp
= lc
; cp
>= nch
->string
; cp
--)
585 if (lc
> nch
->string
+ 1 && lc
[-2] == '.' && lc
[-1] == '.')
599 for (cp
= lc
; cp
>= nch
->string
; cp
--)
604 if (lc
== nch
->string
+ 1 && lc
[-1] == '|')
610 /* Exactly two non-alphanumeric bytes. */
611 if (lc
== nch
->string
+ 1 && !isalnum((unsigned char)lc
[-1]))
614 /* At least three alphabetic words with a sentence ending. */
615 if (strchr("!.:?", *lc
) != NULL
&& (tok
== MDOC_Em
||
616 tok
== MDOC_Li
|| tok
== MDOC_Pq
|| tok
== MDOC_Sy
)) {
618 for (cp
= lc
- 1; cp
>= nch
->string
; cp
--) {
621 if (cp
> nch
->string
&& cp
[-1] == ',')
623 } else if (isalpha((unsigned int)*cp
)) {
631 mandoc_msg(MANDOCERR_DELIM_NB
, nch
->line
,
632 nch
->pos
+ (int)(lc
- nch
->string
), "%s%s %s", roff_name
[tok
],
633 nch
== mdoc
->last
->child
? "" : " ...", nch
->string
);
637 post_bl_norm(POST_ARGS
)
640 struct mdoc_argv
*argv
, *wa
;
642 enum mdocargt mdoclt
;
645 n
= mdoc
->last
->parent
;
646 n
->norm
->Bl
.type
= LIST__NONE
;
649 * First figure out which kind of list to use: bind ourselves to
650 * the first mentioned list type and warn about any remaining
651 * ones. If we find no list type, we default to LIST_item.
654 wa
= (n
->args
== NULL
) ? NULL
: n
->args
->argv
;
655 mdoclt
= MDOC_ARG_MAX
;
656 for (i
= 0; n
->args
&& i
< (int)n
->args
->argc
; i
++) {
657 argv
= n
->args
->argv
+ i
;
660 /* Set list types. */
694 /* Set list arguments. */
696 if (n
->norm
->Bl
.comp
)
697 mandoc_msg(MANDOCERR_ARG_REP
,
698 argv
->line
, argv
->pos
, "Bl -compact");
699 n
->norm
->Bl
.comp
= 1;
704 mandoc_msg(MANDOCERR_ARG_EMPTY
,
705 argv
->line
, argv
->pos
, "Bl -width");
706 n
->norm
->Bl
.width
= "0n";
709 if (NULL
!= n
->norm
->Bl
.width
)
710 mandoc_msg(MANDOCERR_ARG_REP
,
711 argv
->line
, argv
->pos
,
712 "Bl -width %s", argv
->value
[0]);
713 rewrite_macro2len(mdoc
, argv
->value
);
714 n
->norm
->Bl
.width
= argv
->value
[0];
718 mandoc_msg(MANDOCERR_ARG_EMPTY
,
719 argv
->line
, argv
->pos
, "Bl -offset");
722 if (NULL
!= n
->norm
->Bl
.offs
)
723 mandoc_msg(MANDOCERR_ARG_REP
,
724 argv
->line
, argv
->pos
,
725 "Bl -offset %s", argv
->value
[0]);
726 rewrite_macro2len(mdoc
, argv
->value
);
727 n
->norm
->Bl
.offs
= argv
->value
[0];
732 if (LIST__NONE
== lt
)
736 /* Check: multiple list types. */
738 if (LIST__NONE
!= n
->norm
->Bl
.type
) {
739 mandoc_msg(MANDOCERR_BL_REP
, n
->line
, n
->pos
,
740 "Bl -%s", mdoc_argnames
[argv
->arg
]);
744 /* The list type should come first. */
746 if (n
->norm
->Bl
.width
||
749 mandoc_msg(MANDOCERR_BL_LATETYPE
,
750 n
->line
, n
->pos
, "Bl -%s",
751 mdoc_argnames
[n
->args
->argv
[0].arg
]);
753 n
->norm
->Bl
.type
= lt
;
754 if (LIST_column
== lt
) {
755 n
->norm
->Bl
.ncols
= argv
->sz
;
756 n
->norm
->Bl
.cols
= (void *)argv
->value
;
760 /* Allow lists to default to LIST_item. */
762 if (LIST__NONE
== n
->norm
->Bl
.type
) {
763 mandoc_msg(MANDOCERR_BL_NOTYPE
, n
->line
, n
->pos
, "Bl");
764 n
->norm
->Bl
.type
= LIST_item
;
769 * Validate the width field. Some list types don't need width
770 * types and should be warned about them. Others should have it
771 * and must also be warned. Yet others have a default and need
775 switch (n
->norm
->Bl
.type
) {
777 if (n
->norm
->Bl
.width
== NULL
)
778 mandoc_msg(MANDOCERR_BL_NOWIDTH
,
779 n
->line
, n
->pos
, "Bl -tag");
786 if (n
->norm
->Bl
.width
!= NULL
)
787 mandoc_msg(MANDOCERR_BL_SKIPW
, wa
->line
, wa
->pos
,
788 "Bl -%s", mdoc_argnames
[mdoclt
]);
789 n
->norm
->Bl
.width
= NULL
;
794 if (n
->norm
->Bl
.width
== NULL
)
795 n
->norm
->Bl
.width
= "2n";
798 if (n
->norm
->Bl
.width
== NULL
)
799 n
->norm
->Bl
.width
= "3n";
810 struct mdoc_argv
*argv
;
815 for (i
= 0; n
->args
&& i
< (int)n
->args
->argc
; i
++) {
816 argv
= n
->args
->argv
+ i
;
836 mandoc_msg(MANDOCERR_BD_FILE
, n
->line
, n
->pos
, NULL
);
840 mandoc_msg(MANDOCERR_ARG_EMPTY
,
841 argv
->line
, argv
->pos
, "Bd -offset");
844 if (NULL
!= n
->norm
->Bd
.offs
)
845 mandoc_msg(MANDOCERR_ARG_REP
,
846 argv
->line
, argv
->pos
,
847 "Bd -offset %s", argv
->value
[0]);
848 rewrite_macro2len(mdoc
, argv
->value
);
849 n
->norm
->Bd
.offs
= argv
->value
[0];
852 if (n
->norm
->Bd
.comp
)
853 mandoc_msg(MANDOCERR_ARG_REP
,
854 argv
->line
, argv
->pos
, "Bd -compact");
855 n
->norm
->Bd
.comp
= 1;
860 if (DISP__NONE
== dt
)
863 if (DISP__NONE
== n
->norm
->Bd
.type
)
864 n
->norm
->Bd
.type
= dt
;
866 mandoc_msg(MANDOCERR_BD_REP
, n
->line
, n
->pos
,
867 "Bd -%s", mdoc_argnames
[argv
->arg
]);
870 if (DISP__NONE
== n
->norm
->Bd
.type
) {
871 mandoc_msg(MANDOCERR_BD_NOTYPE
, n
->line
, n
->pos
, "Bd");
872 n
->norm
->Bd
.type
= DISP_ragged
;
877 * Stand-alone line macros.
881 post_an_norm(POST_ARGS
)
884 struct mdoc_argv
*argv
;
891 for (i
= 1; i
< n
->args
->argc
; i
++) {
892 argv
= n
->args
->argv
+ i
;
893 mandoc_msg(MANDOCERR_AN_REP
, argv
->line
, argv
->pos
,
894 "An -%s", mdoc_argnames
[argv
->arg
]);
897 argv
= n
->args
->argv
;
898 if (argv
->arg
== MDOC_Split
)
899 n
->norm
->An
.auth
= AUTH_split
;
900 else if (argv
->arg
== MDOC_Nosplit
)
901 n
->norm
->An
.auth
= AUTH_nosplit
;
913 if (n
->child
!= NULL
)
914 mandoc_msg(MANDOCERR_ARG_SKIP
, n
->line
,
915 n
->pos
, "%s %s", roff_name
[n
->tok
], n
->child
->string
);
917 while (n
->child
!= NULL
)
918 roff_node_delete(mdoc
, n
->child
);
920 roff_word_alloc(mdoc
, n
->line
, n
->pos
, n
->tok
== MDOC_Bt
?
921 "is currently in beta test." : "currently under development.");
922 mdoc
->last
->flags
|= NODE_EOS
| NODE_NOSRC
;
927 build_list(struct roff_man
*mdoc
, int tok
)
932 n
= mdoc
->last
->next
;
933 for (ic
= 1;; ic
++) {
934 roff_elem_alloc(mdoc
, n
->line
, n
->pos
, tok
);
935 mdoc
->last
->flags
|= NODE_NOSRC
;
936 roff_node_relink(mdoc
, n
);
937 n
= mdoc
->last
= mdoc
->last
->parent
;
938 mdoc
->next
= ROFF_NEXT_SIBLING
;
941 if (ic
> 1 || n
->next
->next
!= NULL
) {
942 roff_word_alloc(mdoc
, n
->line
, n
->pos
, ",");
943 mdoc
->last
->flags
|= NODE_DELIMC
| NODE_NOSRC
;
945 n
= mdoc
->last
->next
;
946 if (n
->next
== NULL
) {
947 roff_word_alloc(mdoc
, n
->line
, n
->pos
, "and");
948 mdoc
->last
->flags
|= NODE_NOSRC
;
962 mdoc
->next
= ROFF_NEXT_CHILD
;
963 roff_word_alloc(mdoc
, n
->line
, n
->pos
, "The");
964 mdoc
->last
->flags
|= NODE_NOSRC
;
966 if (mdoc
->last
->next
!= NULL
)
967 ic
= build_list(mdoc
, MDOC_Nm
);
968 else if (mdoc
->meta
.name
!= NULL
) {
969 roff_elem_alloc(mdoc
, n
->line
, n
->pos
, MDOC_Nm
);
970 mdoc
->last
->flags
|= NODE_NOSRC
;
971 roff_word_alloc(mdoc
, n
->line
, n
->pos
, mdoc
->meta
.name
);
972 mdoc
->last
->flags
|= NODE_NOSRC
;
973 mdoc
->last
= mdoc
->last
->parent
;
974 mdoc
->next
= ROFF_NEXT_SIBLING
;
977 mandoc_msg(MANDOCERR_EX_NONAME
, n
->line
, n
->pos
, "Ex");
981 roff_word_alloc(mdoc
, n
->line
, n
->pos
,
982 ic
> 1 ? "utilities exit\\~0" : "utility exits\\~0");
983 mdoc
->last
->flags
|= NODE_NOSRC
;
984 roff_word_alloc(mdoc
, n
->line
, n
->pos
,
985 "on success, and\\~>0 if an error occurs.");
986 mdoc
->last
->flags
|= NODE_EOS
| NODE_NOSRC
;
999 assert(n
->child
->type
== ROFFT_TEXT
);
1000 mdoc
->next
= ROFF_NEXT_CHILD
;
1002 if ((p
= mdoc_a2lib(n
->child
->string
)) != NULL
) {
1003 n
->child
->flags
|= NODE_NOPRT
;
1004 roff_word_alloc(mdoc
, n
->line
, n
->pos
, p
);
1005 mdoc
->last
->flags
= NODE_NOSRC
;
1010 mandoc_msg(MANDOCERR_LB_BAD
, n
->child
->line
,
1011 n
->child
->pos
, "Lb %s", n
->child
->string
);
1013 roff_word_alloc(mdoc
, n
->line
, n
->pos
, "library");
1014 mdoc
->last
->flags
= NODE_NOSRC
;
1015 roff_word_alloc(mdoc
, n
->line
, n
->pos
, "\\(lq");
1016 mdoc
->last
->flags
= NODE_DELIMO
| NODE_NOSRC
;
1017 mdoc
->last
= mdoc
->last
->next
;
1018 roff_word_alloc(mdoc
, n
->line
, n
->pos
, "\\(rq");
1019 mdoc
->last
->flags
= NODE_DELIMC
| NODE_NOSRC
;
1026 struct roff_node
*n
;
1032 mdoc
->next
= ROFF_NEXT_CHILD
;
1033 if (n
->child
!= NULL
) {
1034 roff_word_alloc(mdoc
, n
->line
, n
->pos
, "The");
1035 mdoc
->last
->flags
|= NODE_NOSRC
;
1036 ic
= build_list(mdoc
, MDOC_Fn
);
1037 roff_word_alloc(mdoc
, n
->line
, n
->pos
,
1038 ic
> 1 ? "functions return" : "function returns");
1039 mdoc
->last
->flags
|= NODE_NOSRC
;
1040 roff_word_alloc(mdoc
, n
->line
, n
->pos
,
1041 "the value\\~0 if successful;");
1043 roff_word_alloc(mdoc
, n
->line
, n
->pos
, "Upon successful "
1044 "completion, the value\\~0 is returned;");
1045 mdoc
->last
->flags
|= NODE_NOSRC
;
1047 roff_word_alloc(mdoc
, n
->line
, n
->pos
, "otherwise "
1048 "the value\\~\\-1 is returned and the global variable");
1049 mdoc
->last
->flags
|= NODE_NOSRC
;
1050 roff_elem_alloc(mdoc
, n
->line
, n
->pos
, MDOC_Va
);
1051 mdoc
->last
->flags
|= NODE_NOSRC
;
1052 roff_word_alloc(mdoc
, n
->line
, n
->pos
, "errno");
1053 mdoc
->last
->flags
|= NODE_NOSRC
;
1054 mdoc
->last
= mdoc
->last
->parent
;
1055 mdoc
->next
= ROFF_NEXT_SIBLING
;
1056 roff_word_alloc(mdoc
, n
->line
, n
->pos
,
1057 "is set to indicate the error.");
1058 mdoc
->last
->flags
|= NODE_EOS
| NODE_NOSRC
;
1065 struct roff_node
*n
;
1070 if (n
->args
&& n
->args
->argc
== 1)
1071 if (n
->args
->argv
[0].arg
== MDOC_Std
)
1074 mandoc_msg(MANDOCERR_ARG_STD
, n
->line
, n
->pos
,
1075 "%s", roff_name
[n
->tok
]);
1081 struct roff_node
*n
, *nch
;
1086 assert(nch
->type
== ROFFT_TEXT
);
1088 if ((p
= mdoc_a2st(nch
->string
)) == NULL
) {
1089 mandoc_msg(MANDOCERR_ST_BAD
,
1090 nch
->line
, nch
->pos
, "St %s", nch
->string
);
1091 roff_node_delete(mdoc
, n
);
1095 nch
->flags
|= NODE_NOPRT
;
1096 mdoc
->next
= ROFF_NEXT_CHILD
;
1097 roff_word_alloc(mdoc
, nch
->line
, nch
->pos
, p
);
1098 mdoc
->last
->flags
|= NODE_NOSRC
;
1105 struct roff_node
*n
; /* The .Tg node. */
1106 struct roff_node
*nch
; /* The first child of the .Tg node. */
1107 struct roff_node
*nn
; /* The next node after the .Tg node. */
1108 struct roff_node
*np
; /* The parent of the next node. */
1109 struct roff_node
*nt
; /* The TEXT node containing the tag. */
1110 size_t len
; /* The number of bytes in the tag. */
1112 /* Find the next node. */
1114 for (nn
= n
; nn
!= NULL
; nn
= nn
->parent
) {
1115 if (nn
->next
!= NULL
) {
1122 nt
= nch
= n
->child
;
1123 if (nch
== NULL
&& nn
!= NULL
&& nn
->child
!= NULL
&&
1124 nn
->child
->type
== ROFFT_TEXT
)
1127 /* Validate the tag. */
1128 if (nt
== NULL
|| *nt
->string
== '\0')
1129 mandoc_msg(MANDOCERR_MACRO_EMPTY
, n
->line
, n
->pos
, "Tg");
1131 roff_node_delete(mdoc
, n
);
1134 len
= strcspn(nt
->string
, " \t\\");
1135 if (nt
->string
[len
] != '\0')
1136 mandoc_msg(MANDOCERR_TG_SPC
, nt
->line
,
1137 nt
->pos
+ len
, "Tg %s", nt
->string
);
1139 /* Keep only the first argument. */
1140 if (nch
!= NULL
&& nch
->next
!= NULL
) {
1141 mandoc_msg(MANDOCERR_ARG_EXCESS
, nch
->next
->line
,
1142 nch
->next
->pos
, "Tg ... %s", nch
->next
->string
);
1143 while (nch
->next
!= NULL
)
1144 roff_node_delete(mdoc
, nch
->next
);
1147 /* Drop the macro if the first argument is invalid. */
1148 if (len
== 0 || nt
->string
[len
] != '\0') {
1149 roff_node_delete(mdoc
, n
);
1153 /* By default, tag the .Tg node itself. */
1154 if (nn
== NULL
|| nn
->flags
& NODE_ID
)
1157 /* Explicit tagging of specific macros. */
1162 nn
= nn
->head
->child
== NULL
? n
: nn
->head
;
1166 while (np
->tok
!= MDOC_Bl
)
1168 switch (np
->norm
->Bl
.type
) {
1183 nn
= nn
->body
->child
== NULL
? n
: nn
->body
;
1193 nn
= nn
->body
->child
== NULL
? n
: nn
->body
;
1209 if (nn
->child
== NULL
)
1216 tag_put(nt
->string
, TAG_MANUAL
, nn
);
1218 n
->flags
|= NODE_NOPRT
;
1222 post_obsolete(POST_ARGS
)
1224 struct roff_node
*n
;
1227 if (n
->type
== ROFFT_ELEM
|| n
->type
== ROFFT_BLOCK
)
1228 mandoc_msg(MANDOCERR_MACRO_OBS
, n
->line
, n
->pos
,
1229 "%s", roff_name
[n
->tok
]);
1233 post_useless(POST_ARGS
)
1235 struct roff_node
*n
;
1238 mandoc_msg(MANDOCERR_MACRO_USELESS
, n
->line
, n
->pos
,
1239 "%s", roff_name
[n
->tok
]);
1249 struct roff_node
*np
, *nch
;
1252 * Unlike other data pointers, these are "housed" by the HEAD
1253 * element, which contains the goods.
1257 if (np
->type
!= ROFFT_HEAD
)
1260 assert(np
->parent
->type
== ROFFT_BLOCK
);
1261 assert(np
->parent
->tok
== MDOC_Bf
);
1263 /* Check the number of arguments. */
1266 if (np
->parent
->args
== NULL
) {
1268 mandoc_msg(MANDOCERR_BF_NOFONT
,
1269 np
->line
, np
->pos
, "Bf");
1275 mandoc_msg(MANDOCERR_ARG_EXCESS
,
1276 nch
->line
, nch
->pos
, "Bf ... %s", nch
->string
);
1278 /* Extract argument into data. */
1280 if (np
->parent
->args
!= NULL
) {
1281 switch (np
->parent
->args
->argv
[0].arg
) {
1283 np
->norm
->Bf
.font
= FONT_Em
;
1286 np
->norm
->Bf
.font
= FONT_Li
;
1289 np
->norm
->Bf
.font
= FONT_Sy
;
1297 /* Extract parameter into data. */
1299 if ( ! strcmp(np
->child
->string
, "Em"))
1300 np
->norm
->Bf
.font
= FONT_Em
;
1301 else if ( ! strcmp(np
->child
->string
, "Li"))
1302 np
->norm
->Bf
.font
= FONT_Li
;
1303 else if ( ! strcmp(np
->child
->string
, "Sy"))
1304 np
->norm
->Bf
.font
= FONT_Sy
;
1306 mandoc_msg(MANDOCERR_BF_BADFONT
, np
->child
->line
,
1307 np
->child
->pos
, "Bf %s", np
->child
->string
);
1311 post_fname(POST_ARGS
)
1313 struct roff_node
*n
, *nch
;
1321 if (cp
[strlen(cp
+ 1)] == ')')
1325 pos
= strcspn(cp
, "()");
1326 if (cp
[pos
] == '\0') {
1327 if (n
->sec
== SEC_DESCRIPTION
||
1328 n
->sec
== SEC_CUSTOM
)
1329 tag_put(NULL
, fn_prio
++, n
);
1333 mandoc_msg(MANDOCERR_FN_PAREN
, nch
->line
, nch
->pos
+ pos
, "%s", cp
);
1346 const struct roff_node
*n
;
1350 if (n
->type
!= ROFFT_HEAD
)
1353 if (n
->child
== NULL
) {
1354 mandoc_msg(MANDOCERR_FO_NOHEAD
, n
->line
, n
->pos
, "Fo");
1357 if (n
->child
!= n
->last
) {
1358 mandoc_msg(MANDOCERR_ARG_EXCESS
,
1359 n
->child
->next
->line
, n
->child
->next
->pos
,
1360 "Fo ... %s", n
->child
->next
->string
);
1361 while (n
->child
!= n
->last
)
1362 roff_node_delete(mdoc
, n
->last
);
1372 const struct roff_node
*n
;
1375 for (n
= mdoc
->last
->child
; n
!= NULL
; n
= n
->next
) {
1376 for (cp
= n
->string
; *cp
!= '\0'; cp
++) {
1377 /* Ignore callbacks and alterations. */
1378 if (*cp
== '(' || *cp
== '{')
1382 mandoc_msg(MANDOCERR_FA_COMMA
, n
->line
,
1383 n
->pos
+ (int)(cp
- n
->string
), "%s", n
->string
);
1387 post_delim_nb(mdoc
);
1393 struct roff_node
*n
;
1397 if (n
->sec
== SEC_NAME
&& n
->child
!= NULL
&&
1398 n
->child
->type
== ROFFT_TEXT
&& mdoc
->meta
.msec
!= NULL
)
1399 mandoc_xr_add(mdoc
->meta
.msec
, n
->child
->string
, -1, -1);
1401 if (n
->last
!= NULL
&& n
->last
->tok
== MDOC_Pp
)
1402 roff_node_relink(mdoc
, n
->last
);
1404 if (mdoc
->meta
.name
== NULL
)
1405 deroff(&mdoc
->meta
.name
, n
);
1407 if (mdoc
->meta
.name
== NULL
||
1408 (mdoc
->lastsec
== SEC_NAME
&& n
->child
== NULL
))
1409 mandoc_msg(MANDOCERR_NM_NONAME
, n
->line
, n
->pos
, "Nm");
1413 post_delim_nb(mdoc
);
1422 if ((n
->child
!= NULL
&& n
->child
->type
== ROFFT_TEXT
) ||
1423 mdoc
->meta
.name
== NULL
)
1426 mdoc
->next
= ROFF_NEXT_CHILD
;
1427 roff_word_alloc(mdoc
, n
->line
, n
->pos
, mdoc
->meta
.name
);
1428 mdoc
->last
->flags
|= NODE_NOSRC
;
1435 struct roff_node
*n
;
1439 if (n
->type
!= ROFFT_BODY
)
1442 if (n
->sec
!= SEC_NAME
)
1443 mandoc_msg(MANDOCERR_ND_LATE
, n
->line
, n
->pos
, "Nd");
1445 if (n
->child
== NULL
)
1446 mandoc_msg(MANDOCERR_ND_EMPTY
, n
->line
, n
->pos
, "Nd");
1454 post_display(POST_ARGS
)
1456 struct roff_node
*n
, *np
;
1461 if (n
->end
!= ENDBODY_NOT
) {
1462 if (n
->tok
== MDOC_Bd
&&
1463 n
->body
->parent
->args
== NULL
)
1464 roff_node_delete(mdoc
, n
);
1465 } else if (n
->child
== NULL
)
1466 mandoc_msg(MANDOCERR_BLK_EMPTY
, n
->line
, n
->pos
,
1467 "%s", roff_name
[n
->tok
]);
1468 else if (n
->tok
== MDOC_D1
)
1472 if (n
->tok
== MDOC_Bd
) {
1473 if (n
->args
== NULL
) {
1474 mandoc_msg(MANDOCERR_BD_NOARG
,
1475 n
->line
, n
->pos
, "Bd");
1476 mdoc
->next
= ROFF_NEXT_SIBLING
;
1477 while (n
->body
->child
!= NULL
)
1478 roff_node_relink(mdoc
,
1480 roff_node_delete(mdoc
, n
);
1486 for (np
= n
->parent
; np
!= NULL
; np
= np
->parent
) {
1487 if (np
->type
== ROFFT_BLOCK
&& np
->tok
== MDOC_Bd
) {
1488 mandoc_msg(MANDOCERR_BD_NEST
, n
->line
,
1489 n
->pos
, "%s in Bd", roff_name
[n
->tok
]);
1500 post_defaults(POST_ARGS
)
1502 struct roff_node
*n
;
1505 if (n
->child
!= NULL
) {
1506 post_delim_nb(mdoc
);
1509 mdoc
->next
= ROFF_NEXT_CHILD
;
1512 roff_word_alloc(mdoc
, n
->line
, n
->pos
, "file");
1513 mdoc
->last
->flags
|= NODE_NOSRC
;
1514 roff_word_alloc(mdoc
, n
->line
, n
->pos
, "...");
1518 roff_word_alloc(mdoc
, n
->line
, n
->pos
, "~");
1523 mdoc
->last
->flags
|= NODE_NOSRC
;
1530 struct roff_node
*n
, *nch
;
1537 * If we have a child, look it up in the standard keys. If a
1538 * key exist, use that instead of the child; if it doesn't,
1539 * prefix "AT&T UNIX " to the existing data.
1543 if (nch
!= NULL
&& ((att
= mdoc_a2att(nch
->string
)) == NULL
))
1544 mandoc_msg(MANDOCERR_AT_BAD
,
1545 nch
->line
, nch
->pos
, "At %s", nch
->string
);
1547 mdoc
->next
= ROFF_NEXT_CHILD
;
1549 roff_word_alloc(mdoc
, nch
->line
, nch
->pos
, att
);
1550 nch
->flags
|= NODE_NOPRT
;
1552 roff_word_alloc(mdoc
, n
->line
, n
->pos
, "AT&T UNIX");
1553 mdoc
->last
->flags
|= NODE_NOSRC
;
1560 struct roff_node
*np
, *nch
;
1566 if (np
->norm
->An
.auth
== AUTH__NONE
) {
1568 mandoc_msg(MANDOCERR_MACRO_EMPTY
,
1569 np
->line
, np
->pos
, "An");
1571 post_delim_nb(mdoc
);
1572 } else if (nch
!= NULL
)
1573 mandoc_msg(MANDOCERR_ARG_EXCESS
,
1574 nch
->line
, nch
->pos
, "An ... %s", nch
->string
);
1581 tag_put(NULL
, TAG_FALLBACK
, mdoc
->last
);
1587 post_obsolete(mdoc
);
1588 if (mdoc
->last
->type
== ROFFT_BLOCK
)
1589 mdoc
->last
->norm
->Es
= mdoc
->last_es
;
1595 struct roff_node
*n
;
1598 if (n
->sec
== SEC_ERRORS
&&
1599 (n
->parent
->tok
== MDOC_It
||
1600 (n
->parent
->tok
== MDOC_Bq
&&
1601 n
->parent
->parent
->parent
->tok
== MDOC_It
)))
1602 tag_put(NULL
, TAG_STRONG
, n
);
1603 post_delim_nb(mdoc
);
1609 struct roff_node
*n
;
1612 if ((n
->prev
== NULL
||
1613 (n
->prev
->type
== ROFFT_TEXT
&&
1614 strcmp(n
->prev
->string
, "|") == 0)) &&
1615 (n
->parent
->tok
== MDOC_It
||
1616 (n
->parent
->tok
== MDOC_Xo
&&
1617 n
->parent
->parent
->prev
== NULL
&&
1618 n
->parent
->parent
->parent
->tok
== MDOC_It
)))
1619 tag_put(NULL
, TAG_STRONG
, n
);
1620 post_delim_nb(mdoc
);
1626 post_obsolete(mdoc
);
1627 mdoc
->last_es
= mdoc
->last
;
1633 struct roff_node
*n
;
1637 post_delim_nb(mdoc
);
1652 if (n
->child
== NULL
)
1654 v
= n
->child
->string
;
1655 if ((v
[0] != '0' && v
[0] != '1') || v
[1] != '.' ||
1656 v
[2] < '0' || v
[2] > '9' ||
1657 v
[3] < 'a' || v
[3] > 'z' || v
[4] != '\0')
1659 n
->child
->flags
|= NODE_NOPRT
;
1660 mdoc
->next
= ROFF_NEXT_CHILD
;
1661 roff_word_alloc(mdoc
, n
->child
->line
, n
->child
->pos
, v
);
1662 v
= mdoc
->last
->string
;
1663 v
[3] = toupper((unsigned char)v
[3]);
1664 mdoc
->last
->flags
|= NODE_NOSRC
;
1676 mdoc
->next
= ROFF_NEXT_CHILD
;
1677 roff_word_alloc(mdoc
, n
->line
, n
->pos
, os
);
1678 mdoc
->last
->flags
|= NODE_NOSRC
;
1685 struct roff_node
*nbl
, *nit
, *nch
;
1692 if (nit
->type
!= ROFFT_BLOCK
)
1695 nbl
= nit
->parent
->parent
;
1696 lt
= nbl
->norm
->Bl
.type
;
1704 if (nit
->head
->child
== NULL
)
1705 mandoc_msg(MANDOCERR_IT_NOHEAD
,
1706 nit
->line
, nit
->pos
, "Bl -%s It",
1707 mdoc_argnames
[nbl
->args
->argv
[0].arg
]);
1713 if (nit
->body
== NULL
|| nit
->body
->child
== NULL
)
1714 mandoc_msg(MANDOCERR_IT_NOBODY
,
1715 nit
->line
, nit
->pos
, "Bl -%s It",
1716 mdoc_argnames
[nbl
->args
->argv
[0].arg
]);
1719 if ((nch
= nit
->head
->child
) != NULL
)
1720 mandoc_msg(MANDOCERR_ARG_SKIP
,
1721 nit
->line
, nit
->pos
, "It %s",
1722 nch
->type
== ROFFT_TEXT
? nch
->string
:
1723 roff_name
[nch
->tok
]);
1726 cols
= (int)nbl
->norm
->Bl
.ncols
;
1728 assert(nit
->head
->child
== NULL
);
1730 if (nit
->head
->next
->child
== NULL
&&
1731 nit
->head
->next
->next
== NULL
) {
1732 mandoc_msg(MANDOCERR_MACRO_EMPTY
,
1733 nit
->line
, nit
->pos
, "It");
1734 roff_node_delete(mdoc
, nit
);
1739 for (nch
= nit
->child
; nch
!= NULL
; nch
= nch
->next
) {
1740 if (nch
->type
!= ROFFT_BODY
)
1742 if (i
++ && nch
->flags
& NODE_LINE
)
1743 mandoc_msg(MANDOCERR_TA_LINE
,
1744 nch
->line
, nch
->pos
, "Ta");
1746 if (i
< cols
|| i
> cols
+ 1)
1747 mandoc_msg(MANDOCERR_BL_COL
, nit
->line
, nit
->pos
,
1748 "%d columns, %d cells", cols
, i
);
1749 else if (nit
->head
->next
->child
!= NULL
&&
1750 nit
->head
->next
->child
->flags
& NODE_LINE
)
1751 mandoc_msg(MANDOCERR_IT_NOARG
,
1752 nit
->line
, nit
->pos
, "Bl -column It");
1760 post_bl_block(POST_ARGS
)
1762 struct roff_node
*n
, *ni
, *nc
;
1767 for (ni
= n
->body
->child
; ni
!= NULL
; ni
= ni
->next
) {
1768 if (ni
->body
== NULL
)
1770 nc
= ni
->body
->last
;
1771 while (nc
!= NULL
) {
1780 if (ni
->next
== NULL
) {
1781 mandoc_msg(MANDOCERR_PAR_MOVE
, nc
->line
,
1782 nc
->pos
, "%s", roff_name
[nc
->tok
]);
1783 roff_node_relink(mdoc
, nc
);
1784 } else if (n
->norm
->Bl
.comp
== 0 &&
1785 n
->norm
->Bl
.type
!= LIST_column
) {
1786 mandoc_msg(MANDOCERR_PAR_SKIP
,
1788 "%s before It", roff_name
[nc
->tok
]);
1789 roff_node_delete(mdoc
, nc
);
1792 nc
= ni
->body
->last
;
1798 * If the argument of -offset or -width is a macro,
1799 * replace it with the associated default width.
1802 rewrite_macro2len(struct roff_man
*mdoc
, char **arg
)
1809 else if ( ! strcmp(*arg
, "Ds"))
1811 else if ((tok
= roffhash_find(mdoc
->mdocmac
, *arg
, 0)) == TOKEN_NONE
)
1814 width
= macro2len(tok
);
1817 mandoc_asprintf(arg
, "%zun", width
);
1821 post_bl_head(POST_ARGS
)
1823 struct roff_node
*nbl
, *nh
, *nch
, *nnext
;
1824 struct mdoc_argv
*argv
;
1830 if (nh
->norm
->Bl
.type
!= LIST_column
) {
1831 if ((nch
= nh
->child
) == NULL
)
1833 mandoc_msg(MANDOCERR_ARG_EXCESS
,
1834 nch
->line
, nch
->pos
, "Bl ... %s", nch
->string
);
1835 while (nch
!= NULL
) {
1836 roff_node_delete(mdoc
, nch
);
1843 * Append old-style lists, where the column width specifiers
1844 * trail as macro parameters, to the new-style ("normal-form")
1845 * lists where they're argument values following -column.
1848 if (nh
->child
== NULL
)
1852 for (j
= 0; j
< (int)nbl
->args
->argc
; j
++)
1853 if (nbl
->args
->argv
[j
].arg
== MDOC_Column
)
1856 assert(j
< (int)nbl
->args
->argc
);
1859 * Accommodate for new-style groff column syntax. Shuffle the
1860 * child nodes, all of which must be TEXT, as arguments for the
1861 * column field. Then, delete the head children.
1864 argv
= nbl
->args
->argv
+ j
;
1866 for (nch
= nh
->child
; nch
!= NULL
; nch
= nch
->next
)
1868 argv
->value
= mandoc_reallocarray(argv
->value
,
1869 argv
->sz
, sizeof(char *));
1871 nh
->norm
->Bl
.ncols
= argv
->sz
;
1872 nh
->norm
->Bl
.cols
= (void *)argv
->value
;
1874 for (nch
= nh
->child
; nch
!= NULL
; nch
= nnext
) {
1875 argv
->value
[i
++] = nch
->string
;
1878 roff_node_delete(NULL
, nch
);
1886 struct roff_node
*nbody
; /* of the Bl */
1887 struct roff_node
*nchild
, *nnext
; /* of the Bl body */
1888 const char *prev_Er
;
1892 switch (nbody
->type
) {
1894 post_bl_block(mdoc
);
1904 if (nbody
->end
!= ENDBODY_NOT
)
1908 * Up to the first item, move nodes before the list,
1909 * but leave transparent nodes where they are
1910 * if they precede an item.
1911 * The next non-transparent node is kept in nchild.
1912 * It only needs to be updated after a non-transparent
1913 * node was moved out, and at the very beginning
1914 * when no node at all was moved yet.
1917 nchild
= mdoc
->last
;
1919 if (nchild
== mdoc
->last
)
1920 nchild
= roff_node_child(nbody
);
1921 if (nchild
== NULL
) {
1923 mandoc_msg(MANDOCERR_BLK_EMPTY
,
1924 nbody
->line
, nbody
->pos
, "Bl");
1927 if (nchild
->tok
== MDOC_It
) {
1931 mandoc_msg(MANDOCERR_BL_MOVE
, nbody
->child
->line
,
1932 nbody
->child
->pos
, "%s", roff_name
[nbody
->child
->tok
]);
1933 if (nbody
->parent
->prev
== NULL
) {
1934 mdoc
->last
= nbody
->parent
->parent
;
1935 mdoc
->next
= ROFF_NEXT_CHILD
;
1937 mdoc
->last
= nbody
->parent
->prev
;
1938 mdoc
->next
= ROFF_NEXT_SIBLING
;
1940 roff_node_relink(mdoc
, nbody
->child
);
1944 * We have reached the first item,
1945 * so moving nodes out is no longer possible.
1946 * But in .Bl -column, the first rows may be implicit,
1947 * that is, they may not start with .It macros.
1948 * Such rows may be followed by nodes generated on the
1949 * roff level, for example .TS.
1950 * Wrap such roff nodes into an implicit row.
1953 while (nchild
!= NULL
) {
1954 if (nchild
->tok
== MDOC_It
) {
1955 nchild
= roff_node_next(nchild
);
1958 nnext
= nchild
->next
;
1959 mdoc
->last
= nchild
->prev
;
1960 mdoc
->next
= ROFF_NEXT_SIBLING
;
1961 roff_block_alloc(mdoc
, nchild
->line
, nchild
->pos
, MDOC_It
);
1962 roff_head_alloc(mdoc
, nchild
->line
, nchild
->pos
, MDOC_It
);
1963 mdoc
->next
= ROFF_NEXT_SIBLING
;
1964 roff_body_alloc(mdoc
, nchild
->line
, nchild
->pos
, MDOC_It
);
1965 while (nchild
->tok
!= MDOC_It
) {
1966 roff_node_relink(mdoc
, nchild
);
1970 nnext
= nchild
->next
;
1971 mdoc
->next
= ROFF_NEXT_SIBLING
;
1976 if (mdoc
->meta
.os_e
!= MANDOC_OS_NETBSD
)
1980 for (nchild
= nbody
->child
; nchild
!= NULL
; nchild
= nchild
->next
) {
1981 if (nchild
->tok
!= MDOC_It
)
1983 if ((nnext
= nchild
->head
->child
) == NULL
)
1985 if (nnext
->type
== ROFFT_BLOCK
)
1986 nnext
= nnext
->body
->child
;
1987 if (nnext
== NULL
|| nnext
->tok
!= MDOC_Er
)
1989 nnext
= nnext
->child
;
1990 if (prev_Er
!= NULL
) {
1991 order
= strcmp(prev_Er
, nnext
->string
);
1993 mandoc_msg(MANDOCERR_ER_ORDER
,
1994 nnext
->line
, nnext
->pos
,
1995 "Er %s %s (NetBSD)",
1996 prev_Er
, nnext
->string
);
1997 else if (order
== 0)
1998 mandoc_msg(MANDOCERR_ER_REP
,
1999 nnext
->line
, nnext
->pos
,
2000 "Er %s (NetBSD)", prev_Er
);
2002 prev_Er
= nnext
->string
;
2009 struct roff_node
*n
;
2013 if (n
->type
== ROFFT_BLOCK
&& n
->body
->child
== NULL
) {
2014 mandoc_msg(MANDOCERR_BLK_EMPTY
, n
->line
, n
->pos
, "Bk");
2015 roff_node_delete(mdoc
, n
);
2022 struct roff_node
*nch
;
2024 nch
= mdoc
->last
->child
;
2027 mdoc
->flags
^= MDOC_SMOFF
;
2031 assert(nch
->type
== ROFFT_TEXT
);
2033 if ( ! strcmp(nch
->string
, "on")) {
2034 mdoc
->flags
&= ~MDOC_SMOFF
;
2037 if ( ! strcmp(nch
->string
, "off")) {
2038 mdoc
->flags
|= MDOC_SMOFF
;
2042 mandoc_msg(MANDOCERR_SM_BAD
, nch
->line
, nch
->pos
,
2043 "%s %s", roff_name
[mdoc
->last
->tok
], nch
->string
);
2044 roff_node_relink(mdoc
, nch
);
2049 post_root(POST_ARGS
)
2051 struct roff_node
*n
;
2053 /* Add missing prologue data. */
2055 if (mdoc
->meta
.date
== NULL
)
2056 mdoc
->meta
.date
= mandoc_normdate(NULL
, NULL
);
2058 if (mdoc
->meta
.title
== NULL
) {
2059 mandoc_msg(MANDOCERR_DT_NOTITLE
, 0, 0, "EOF");
2060 mdoc
->meta
.title
= mandoc_strdup("UNTITLED");
2063 if (mdoc
->meta
.vol
== NULL
)
2064 mdoc
->meta
.vol
= mandoc_strdup("LOCAL");
2066 if (mdoc
->meta
.os
== NULL
) {
2067 mandoc_msg(MANDOCERR_OS_MISSING
, 0, 0, NULL
);
2068 mdoc
->meta
.os
= mandoc_strdup("");
2069 } else if (mdoc
->meta
.os_e
&&
2070 (mdoc
->meta
.rcsids
& (1 << mdoc
->meta
.os_e
)) == 0)
2071 mandoc_msg(MANDOCERR_RCS_MISSING
, 0, 0,
2072 mdoc
->meta
.os_e
== MANDOC_OS_OPENBSD
?
2073 "(OpenBSD)" : "(NetBSD)");
2075 if (mdoc
->meta
.arch
!= NULL
&&
2076 arch_valid(mdoc
->meta
.arch
, mdoc
->meta
.os_e
) == 0) {
2077 n
= mdoc
->meta
.first
->child
;
2078 while (n
->tok
!= MDOC_Dt
||
2080 n
->child
->next
== NULL
||
2081 n
->child
->next
->next
== NULL
)
2083 n
= n
->child
->next
->next
;
2084 mandoc_msg(MANDOCERR_ARCH_BAD
, n
->line
, n
->pos
,
2085 "Dt ... %s %s", mdoc
->meta
.arch
,
2086 mdoc
->meta
.os_e
== MANDOC_OS_OPENBSD
?
2087 "(OpenBSD)" : "(NetBSD)");
2090 /* Check that we begin with a proper `Sh'. */
2092 n
= mdoc
->meta
.first
->child
;
2094 (n
->type
== ROFFT_COMMENT
||
2095 (n
->tok
>= MDOC_Dd
&&
2096 mdoc_macro(n
->tok
)->flags
& MDOC_PROLOGUE
)))
2100 mandoc_msg(MANDOCERR_DOC_EMPTY
, 0, 0, NULL
);
2101 else if (n
->tok
!= MDOC_Sh
)
2102 mandoc_msg(MANDOCERR_SEC_BEFORE
, n
->line
, n
->pos
,
2103 "%s", roff_name
[n
->tok
]);
2109 struct roff_node
*np
, *nch
, *next
, *prev
;
2114 if (np
->type
!= ROFFT_BODY
)
2117 if (np
->child
== NULL
) {
2118 mandoc_msg(MANDOCERR_RS_EMPTY
, np
->line
, np
->pos
, "Rs");
2123 * The full `Rs' block needs special handling to order the
2124 * sub-elements according to `rsord'. Pick through each element
2125 * and correctly order it. This is an insertion sort.
2129 for (nch
= np
->child
->next
; nch
!= NULL
; nch
= next
) {
2130 /* Determine order number of this child. */
2131 for (i
= 0; i
< RSORD_MAX
; i
++)
2132 if (rsord
[i
] == nch
->tok
)
2135 if (i
== RSORD_MAX
) {
2136 mandoc_msg(MANDOCERR_RS_BAD
, nch
->line
, nch
->pos
,
2137 "%s", roff_name
[nch
->tok
]);
2139 } else if (nch
->tok
== MDOC__J
|| nch
->tok
== MDOC__B
)
2140 np
->norm
->Rs
.quote_T
++;
2143 * Remove this child from the chain. This somewhat
2144 * repeats roff_node_unlink(), but since we're
2145 * just re-ordering, there's no need for the
2146 * full unlink process.
2149 if ((next
= nch
->next
) != NULL
)
2150 next
->prev
= nch
->prev
;
2152 if ((prev
= nch
->prev
) != NULL
)
2153 prev
->next
= nch
->next
;
2155 nch
->prev
= nch
->next
= NULL
;
2158 * Scan back until we reach a node that's
2159 * to be ordered before this child.
2162 for ( ; prev
; prev
= prev
->prev
) {
2163 /* Determine order of `prev'. */
2164 for (j
= 0; j
< RSORD_MAX
; j
++)
2165 if (rsord
[j
] == prev
->tok
)
2175 * Set this child back into its correct place
2176 * in front of the `prev' node.
2182 np
->child
->prev
= nch
;
2183 nch
->next
= np
->child
;
2187 prev
->next
->prev
= nch
;
2188 nch
->next
= prev
->next
;
2195 * For some arguments of some macros,
2196 * convert all breakable hyphens into ASCII_HYPH.
2199 post_hyph(POST_ARGS
)
2201 struct roff_node
*n
, *nch
;
2205 for (nch
= n
->child
; nch
!= NULL
; nch
= nch
->next
) {
2206 if (nch
->type
!= ROFFT_TEXT
)
2211 while (*(++cp
) != '\0')
2213 isalpha((unsigned char)cp
[-1]) &&
2214 isalpha((unsigned char)cp
[1])) {
2215 if (n
->tag
== NULL
&& n
->flags
& NODE_ID
)
2216 n
->tag
= mandoc_strdup(nch
->string
);
2225 struct roff_node
*n
;
2228 if (n
->flags
& NODE_LINE
||
2229 (n
->next
!= NULL
&& n
->next
->flags
& NODE_DELIMC
))
2230 mandoc_msg(MANDOCERR_NS_SKIP
, n
->line
, n
->pos
, NULL
);
2245 switch (mdoc
->last
->type
) {
2250 switch (mdoc
->lastsec
) {
2255 post_sh_see_also(mdoc
);
2258 post_sh_authors(mdoc
);
2270 post_sh_name(POST_ARGS
)
2272 struct roff_node
*n
;
2277 for (n
= mdoc
->last
->child
; n
!= NULL
; n
= n
->next
) {
2280 if (hasnm
&& n
->child
!= NULL
)
2281 mandoc_msg(MANDOCERR_NAMESEC_PUNCT
,
2283 "Nm %s", n
->child
->string
);
2288 if (n
->next
!= NULL
)
2289 mandoc_msg(MANDOCERR_NAMESEC_ND
,
2290 n
->line
, n
->pos
, NULL
);
2293 if (n
->type
== ROFFT_TEXT
&&
2294 n
->string
[0] == ',' && n
->string
[1] == '\0' &&
2295 n
->next
!= NULL
&& n
->next
->tok
== MDOC_Nm
) {
2301 mandoc_msg(MANDOCERR_NAMESEC_BAD
,
2302 n
->line
, n
->pos
, "%s", roff_name
[n
->tok
]);
2309 mandoc_msg(MANDOCERR_NAMESEC_NONM
,
2310 mdoc
->last
->line
, mdoc
->last
->pos
, NULL
);
2312 mandoc_msg(MANDOCERR_NAMESEC_NOND
,
2313 mdoc
->last
->line
, mdoc
->last
->pos
, NULL
);
2317 post_sh_see_also(POST_ARGS
)
2319 const struct roff_node
*n
;
2320 const char *name
, *sec
;
2321 const char *lastname
, *lastsec
, *lastpunct
;
2324 n
= mdoc
->last
->child
;
2325 lastname
= lastsec
= lastpunct
= NULL
;
2327 if (n
->tok
!= MDOC_Xr
||
2329 n
->child
->next
== NULL
)
2332 /* Process one .Xr node. */
2334 name
= n
->child
->string
;
2335 sec
= n
->child
->next
->string
;
2336 if (lastsec
!= NULL
) {
2337 if (lastpunct
[0] != ',' || lastpunct
[1] != '\0')
2338 mandoc_msg(MANDOCERR_XR_PUNCT
, n
->line
,
2339 n
->pos
, "%s before %s(%s)",
2340 lastpunct
, name
, sec
);
2341 cmp
= strcmp(lastsec
, sec
);
2343 mandoc_msg(MANDOCERR_XR_ORDER
, n
->line
,
2344 n
->pos
, "%s(%s) after %s(%s)",
2345 name
, sec
, lastname
, lastsec
);
2346 else if (cmp
== 0 &&
2347 strcasecmp(lastname
, name
) > 0)
2348 mandoc_msg(MANDOCERR_XR_ORDER
, n
->line
,
2349 n
->pos
, "%s after %s", name
, lastname
);
2354 /* Process the following node. */
2359 if (n
->tok
== MDOC_Xr
) {
2363 if (n
->type
!= ROFFT_TEXT
)
2365 for (name
= n
->string
; *name
!= '\0'; name
++)
2366 if (isalpha((const unsigned char)*name
))
2368 lastpunct
= n
->string
;
2369 if (n
->next
== NULL
|| n
->next
->tok
== MDOC_Rs
)
2370 mandoc_msg(MANDOCERR_XR_PUNCT
, n
->line
,
2371 n
->pos
, "%s after %s(%s)",
2372 lastpunct
, lastname
, lastsec
);
2378 child_an(const struct roff_node
*n
)
2381 for (n
= n
->child
; n
!= NULL
; n
= n
->next
)
2382 if ((n
->tok
== MDOC_An
&& n
->child
!= NULL
) || child_an(n
))
2388 post_sh_authors(POST_ARGS
)
2391 if ( ! child_an(mdoc
->last
))
2392 mandoc_msg(MANDOCERR_AN_MISSING
,
2393 mdoc
->last
->line
, mdoc
->last
->pos
, NULL
);
2397 * Return an upper bound for the string distance (allowing
2398 * transpositions). Not a full Levenshtein implementation
2399 * because Levenshtein is quadratic in the string length
2400 * and this function is called for every standard name,
2401 * so the check for each custom name would be cubic.
2402 * The following crude heuristics is linear, resulting
2403 * in quadratic behaviour for checking one custom name,
2404 * which does not cause measurable slowdown.
2407 similar(const char *s1
, const char *s2
)
2409 const int maxdist
= 3;
2412 while (s1
[0] != '\0' && s2
[0] != '\0') {
2413 if (s1
[0] == s2
[0]) {
2418 if (++dist
> maxdist
)
2420 if (s1
[1] == s2
[1]) { /* replacement */
2423 } else if (s1
[0] == s2
[1] && s1
[1] == s2
[0]) {
2424 s1
+= 2; /* transposition */
2426 } else if (s1
[0] == s2
[1]) /* insertion */
2428 else if (s1
[1] == s2
[0]) /* deletion */
2433 dist
+= strlen(s1
) + strlen(s2
);
2434 return dist
> maxdist
? INT_MAX
: dist
;
2438 post_sh_head(POST_ARGS
)
2440 struct roff_node
*nch
;
2441 const char *goodsec
;
2442 const char *const *testsec
;
2447 * Process a new section. Sections are either "named" or
2448 * "custom". Custom sections are user-defined, while named ones
2449 * follow a conventional order and may only appear in certain
2453 sec
= mdoc
->last
->sec
;
2455 /* The NAME should be first. */
2457 if (sec
!= SEC_NAME
&& mdoc
->lastnamed
== SEC_NONE
)
2458 mandoc_msg(MANDOCERR_NAMESEC_FIRST
,
2459 mdoc
->last
->line
, mdoc
->last
->pos
, "Sh %s",
2460 sec
!= SEC_CUSTOM
? secnames
[sec
] :
2461 (nch
= mdoc
->last
->child
) == NULL
? "" :
2462 nch
->type
== ROFFT_TEXT
? nch
->string
:
2463 roff_name
[nch
->tok
]);
2465 /* The SYNOPSIS gets special attention in other areas. */
2467 if (sec
== SEC_SYNOPSIS
) {
2468 roff_setreg(mdoc
->roff
, "nS", 1, '=');
2469 mdoc
->flags
|= MDOC_SYNOPSIS
;
2471 roff_setreg(mdoc
->roff
, "nS", 0, '=');
2472 mdoc
->flags
&= ~MDOC_SYNOPSIS
;
2474 if (sec
== SEC_DESCRIPTION
)
2475 fn_prio
= TAG_STRONG
;
2477 /* Mark our last section. */
2479 mdoc
->lastsec
= sec
;
2481 /* We don't care about custom sections after this. */
2483 if (sec
== SEC_CUSTOM
) {
2484 if ((nch
= mdoc
->last
->child
) == NULL
||
2485 nch
->type
!= ROFFT_TEXT
|| nch
->next
!= NULL
)
2489 for (testsec
= secnames
+ 1; *testsec
!= NULL
; testsec
++) {
2490 dist
= similar(nch
->string
, *testsec
);
2491 if (dist
< mindist
) {
2496 if (goodsec
!= NULL
)
2497 mandoc_msg(MANDOCERR_SEC_TYPO
, nch
->line
, nch
->pos
,
2498 "Sh %s instead of %s", nch
->string
, goodsec
);
2503 * Check whether our non-custom section is being repeated or is
2507 if (sec
== mdoc
->lastnamed
)
2508 mandoc_msg(MANDOCERR_SEC_REP
, mdoc
->last
->line
,
2509 mdoc
->last
->pos
, "Sh %s", secnames
[sec
]);
2511 if (sec
< mdoc
->lastnamed
)
2512 mandoc_msg(MANDOCERR_SEC_ORDER
, mdoc
->last
->line
,
2513 mdoc
->last
->pos
, "Sh %s", secnames
[sec
]);
2515 /* Mark the last named section. */
2517 mdoc
->lastnamed
= sec
;
2519 /* Check particular section/manual conventions. */
2521 if (mdoc
->meta
.msec
== NULL
)
2527 if (*mdoc
->meta
.msec
== '4')
2529 goodsec
= "2, 3, 4, 9";
2531 case SEC_RETURN_VALUES
:
2533 if (*mdoc
->meta
.msec
== '2')
2535 if (*mdoc
->meta
.msec
== '3')
2537 if (NULL
== goodsec
)
2538 goodsec
= "2, 3, 9";
2541 if (*mdoc
->meta
.msec
== '9')
2543 if (NULL
== goodsec
)
2545 mandoc_msg(MANDOCERR_SEC_MSEC
,
2546 mdoc
->last
->line
, mdoc
->last
->pos
,
2547 "Sh %s for %s only", secnames
[sec
], goodsec
);
2557 struct roff_node
*n
, *nch
;
2561 if (nch
->next
== NULL
) {
2562 mandoc_msg(MANDOCERR_XR_NOSEC
,
2563 n
->line
, n
->pos
, "Xr %s", nch
->string
);
2565 assert(nch
->next
== n
->last
);
2566 if(mandoc_xr_add(nch
->next
->string
, nch
->string
,
2567 nch
->line
, nch
->pos
))
2568 mandoc_msg(MANDOCERR_XR_SELF
,
2569 nch
->line
, nch
->pos
, "Xr %s %s",
2570 nch
->string
, nch
->next
->string
);
2572 post_delim_nb(mdoc
);
2576 post_section(POST_ARGS
)
2578 struct roff_node
*n
, *nch
;
2590 for (cp
= tag
; *cp
!= '\0'; cp
++)
2593 if ((nch
= n
->child
) != NULL
&&
2594 nch
->type
== ROFFT_TEXT
&&
2595 strcmp(nch
->string
, tag
) == 0)
2596 tag_put(NULL
, TAG_WEAK
, n
);
2598 tag_put(tag
, TAG_FALLBACK
, n
);
2609 if ((nch
= n
->child
) != NULL
&&
2610 (nch
->tok
== MDOC_Pp
|| nch
->tok
== ROFF_br
||
2611 nch
->tok
== ROFF_sp
)) {
2612 mandoc_msg(MANDOCERR_PAR_SKIP
, nch
->line
, nch
->pos
,
2613 "%s after %s", roff_name
[nch
->tok
],
2615 roff_node_delete(mdoc
, nch
);
2617 if ((nch
= n
->last
) != NULL
&&
2618 (nch
->tok
== MDOC_Pp
|| nch
->tok
== ROFF_br
)) {
2619 mandoc_msg(MANDOCERR_PAR_SKIP
, nch
->line
, nch
->pos
,
2620 "%s at the end of %s", roff_name
[nch
->tok
],
2622 roff_node_delete(mdoc
, nch
);
2627 post_prevpar(POST_ARGS
)
2629 struct roff_node
*n
, *np
;
2632 if (n
->type
!= ROFFT_ELEM
&& n
->type
!= ROFFT_BLOCK
)
2634 if ((np
= roff_node_prev(n
)) == NULL
)
2638 * Don't allow `Pp' prior to a paragraph-type
2639 * block: `Pp' or non-compact `Bd' or `Bl'.
2642 if (np
->tok
!= MDOC_Pp
&& np
->tok
!= ROFF_br
)
2644 if (n
->tok
== MDOC_Bl
&& n
->norm
->Bl
.comp
)
2646 if (n
->tok
== MDOC_Bd
&& n
->norm
->Bd
.comp
)
2648 if (n
->tok
== MDOC_It
&& n
->parent
->norm
->Bl
.comp
)
2651 mandoc_msg(MANDOCERR_PAR_SKIP
, np
->line
, np
->pos
,
2652 "%s before %s", roff_name
[np
->tok
], roff_name
[n
->tok
]);
2653 roff_node_delete(mdoc
, np
);
2659 struct roff_node
*np
;
2661 fn_prio
= TAG_STRONG
;
2665 if (np
->child
!= NULL
)
2666 mandoc_msg(MANDOCERR_ARG_SKIP
, np
->line
, np
->pos
,
2667 "%s %s", roff_name
[np
->tok
], np
->child
->string
);
2673 struct roff_node
*n
;
2676 n
->flags
|= NODE_NOPRT
;
2678 if (mdoc
->meta
.date
!= NULL
) {
2679 mandoc_msg(MANDOCERR_PROLOG_REP
, n
->line
, n
->pos
, "Dd");
2680 free(mdoc
->meta
.date
);
2681 } else if (mdoc
->flags
& MDOC_PBODY
)
2682 mandoc_msg(MANDOCERR_PROLOG_LATE
, n
->line
, n
->pos
, "Dd");
2683 else if (mdoc
->meta
.title
!= NULL
)
2684 mandoc_msg(MANDOCERR_PROLOG_ORDER
,
2685 n
->line
, n
->pos
, "Dd after Dt");
2686 else if (mdoc
->meta
.os
!= NULL
)
2687 mandoc_msg(MANDOCERR_PROLOG_ORDER
,
2688 n
->line
, n
->pos
, "Dd after Os");
2690 if (mdoc
->quick
&& n
!= NULL
)
2691 mdoc
->meta
.date
= mandoc_strdup("");
2693 mdoc
->meta
.date
= mandoc_normdate(n
->child
, n
);
2699 struct roff_node
*nn
, *n
;
2704 n
->flags
|= NODE_NOPRT
;
2706 if (mdoc
->flags
& MDOC_PBODY
) {
2707 mandoc_msg(MANDOCERR_DT_LATE
, n
->line
, n
->pos
, "Dt");
2711 if (mdoc
->meta
.title
!= NULL
)
2712 mandoc_msg(MANDOCERR_PROLOG_REP
, n
->line
, n
->pos
, "Dt");
2713 else if (mdoc
->meta
.os
!= NULL
)
2714 mandoc_msg(MANDOCERR_PROLOG_ORDER
,
2715 n
->line
, n
->pos
, "Dt after Os");
2717 free(mdoc
->meta
.title
);
2718 free(mdoc
->meta
.msec
);
2719 free(mdoc
->meta
.vol
);
2720 free(mdoc
->meta
.arch
);
2722 mdoc
->meta
.title
= NULL
;
2723 mdoc
->meta
.msec
= NULL
;
2724 mdoc
->meta
.vol
= NULL
;
2725 mdoc
->meta
.arch
= NULL
;
2727 /* Mandatory first argument: title. */
2730 if (nn
== NULL
|| *nn
->string
== '\0') {
2731 mandoc_msg(MANDOCERR_DT_NOTITLE
, n
->line
, n
->pos
, "Dt");
2732 mdoc
->meta
.title
= mandoc_strdup("UNTITLED");
2734 mdoc
->meta
.title
= mandoc_strdup(nn
->string
);
2736 /* Check that all characters are uppercase. */
2738 for (p
= nn
->string
; *p
!= '\0'; p
++)
2739 if (islower((unsigned char)*p
)) {
2740 mandoc_msg(MANDOCERR_TITLE_CASE
, nn
->line
,
2741 nn
->pos
+ (int)(p
- nn
->string
),
2742 "Dt %s", nn
->string
);
2747 /* Mandatory second argument: section. */
2753 mandoc_msg(MANDOCERR_MSEC_MISSING
, n
->line
, n
->pos
,
2754 "Dt %s", mdoc
->meta
.title
);
2755 mdoc
->meta
.vol
= mandoc_strdup("LOCAL");
2756 return; /* msec and arch remain NULL. */
2759 mdoc
->meta
.msec
= mandoc_strdup(nn
->string
);
2761 /* Infer volume title from section number. */
2763 cp
= mandoc_a2msec(nn
->string
);
2765 mandoc_msg(MANDOCERR_MSEC_BAD
,
2766 nn
->line
, nn
->pos
, "Dt ... %s", nn
->string
);
2767 mdoc
->meta
.vol
= mandoc_strdup(nn
->string
);
2769 mdoc
->meta
.vol
= mandoc_strdup(cp
);
2771 /* Optional third argument: architecture. */
2773 if ((nn
= nn
->next
) == NULL
)
2776 for (p
= nn
->string
; *p
!= '\0'; p
++)
2777 *p
= tolower((unsigned char)*p
);
2778 mdoc
->meta
.arch
= mandoc_strdup(nn
->string
);
2780 /* Ignore fourth and later arguments. */
2782 if ((nn
= nn
->next
) != NULL
)
2783 mandoc_msg(MANDOCERR_ARG_EXCESS
,
2784 nn
->line
, nn
->pos
, "Dt ... %s", nn
->string
);
2790 struct roff_node
*n
, *nch
;
2793 post_delim_nb(mdoc
);
2799 macro
= !strcmp(nch
->string
, "Open") ? "Ox" :
2800 !strcmp(nch
->string
, "Net") ? "Nx" :
2801 !strcmp(nch
->string
, "Free") ? "Fx" :
2802 !strcmp(nch
->string
, "DragonFly") ? "Dx" : NULL
;
2804 mandoc_msg(MANDOCERR_BX
,
2805 n
->line
, n
->pos
, "%s", macro
);
2808 mdoc
->next
= ROFF_NEXT_SIBLING
;
2809 roff_elem_alloc(mdoc
, n
->line
, n
->pos
, MDOC_Ns
);
2810 mdoc
->last
->flags
|= NODE_NOSRC
;
2811 mdoc
->next
= ROFF_NEXT_SIBLING
;
2813 mdoc
->next
= ROFF_NEXT_CHILD
;
2814 roff_word_alloc(mdoc
, n
->line
, n
->pos
, "BSD");
2815 mdoc
->last
->flags
|= NODE_NOSRC
;
2822 roff_elem_alloc(mdoc
, n
->line
, n
->pos
, MDOC_Ns
);
2823 mdoc
->last
->flags
|= NODE_NOSRC
;
2824 mdoc
->next
= ROFF_NEXT_SIBLING
;
2825 roff_word_alloc(mdoc
, n
->line
, n
->pos
, "-");
2826 mdoc
->last
->flags
|= NODE_NOSRC
;
2827 roff_elem_alloc(mdoc
, n
->line
, n
->pos
, MDOC_Ns
);
2828 mdoc
->last
->flags
|= NODE_NOSRC
;
2832 * Make `Bx's second argument always start with an uppercase
2833 * letter. Groff checks if it's an "accepted" term, but we just
2834 * uppercase blindly.
2837 *nch
->string
= (char)toupper((unsigned char)*nch
->string
);
2844 struct utsname utsname
;
2845 static char *defbuf
;
2847 struct roff_node
*n
;
2850 n
->flags
|= NODE_NOPRT
;
2852 if (mdoc
->meta
.os
!= NULL
)
2853 mandoc_msg(MANDOCERR_PROLOG_REP
, n
->line
, n
->pos
, "Os");
2854 else if (mdoc
->flags
& MDOC_PBODY
)
2855 mandoc_msg(MANDOCERR_PROLOG_LATE
, n
->line
, n
->pos
, "Os");
2860 * Set the operating system by way of the `Os' macro.
2861 * The order of precedence is:
2862 * 1. the argument of the `Os' macro, unless empty
2863 * 2. the -Ios=foo command line argument, if provided
2864 * 3. -DOSNAME="\"foo\"", if provided during compilation
2865 * 4. "sysname release" from uname(3)
2868 free(mdoc
->meta
.os
);
2869 mdoc
->meta
.os
= NULL
;
2870 deroff(&mdoc
->meta
.os
, n
);
2874 if (mdoc
->os_s
!= NULL
) {
2875 mdoc
->meta
.os
= mandoc_strdup(mdoc
->os_s
);
2880 mdoc
->meta
.os
= mandoc_strdup(OSNAME
);
2882 if (defbuf
== NULL
) {
2883 if (uname(&utsname
) == -1) {
2884 mandoc_msg(MANDOCERR_OS_UNAME
, n
->line
, n
->pos
, "Os");
2885 defbuf
= mandoc_strdup("UNKNOWN");
2887 mandoc_asprintf(&defbuf
, "%s %s",
2888 utsname
.sysname
, utsname
.release
);
2890 mdoc
->meta
.os
= mandoc_strdup(defbuf
);
2894 if (mdoc
->meta
.os_e
== MANDOC_OS_OTHER
) {
2895 if (strstr(mdoc
->meta
.os
, "OpenBSD") != NULL
)
2896 mdoc
->meta
.os_e
= MANDOC_OS_OPENBSD
;
2897 else if (strstr(mdoc
->meta
.os
, "NetBSD") != NULL
)
2898 mdoc
->meta
.os_e
= MANDOC_OS_NETBSD
;
2902 * This is the earliest point where we can check
2903 * Mdocdate conventions because we don't know
2904 * the operating system earlier.
2907 if (n
->child
!= NULL
)
2908 mandoc_msg(MANDOCERR_OS_ARG
, n
->child
->line
, n
->child
->pos
,
2909 "Os %s (%s)", n
->child
->string
,
2910 mdoc
->meta
.os_e
== MANDOC_OS_OPENBSD
?
2911 "OpenBSD" : "NetBSD");
2913 while (n
->tok
!= MDOC_Dd
)
2914 if ((n
= n
->prev
) == NULL
)
2916 if ((n
= n
->child
) == NULL
)
2918 if (strncmp(n
->string
, "$" "Mdocdate", 9)) {
2919 if (mdoc
->meta
.os_e
== MANDOC_OS_OPENBSD
)
2920 mandoc_msg(MANDOCERR_MDOCDATE_MISSING
, n
->line
,
2921 n
->pos
, "Dd %s (OpenBSD)", n
->string
);
2923 if (mdoc
->meta
.os_e
== MANDOC_OS_NETBSD
)
2924 mandoc_msg(MANDOCERR_MDOCDATE
, n
->line
,
2925 n
->pos
, "Dd %s (NetBSD)", n
->string
);
2930 mdoc_a2sec(const char *p
)
2934 for (i
= 0; i
< (int)SEC__MAX
; i
++)
2935 if (secnames
[i
] && 0 == strcmp(p
, secnames
[i
]))
2936 return (enum roff_sec
)i
;
2942 macro2len(enum roff_tok macro
)