1 /* $Id: mdoc_validate.c,v 1.391 2022/06/08 16:31:46 schwarze Exp $ */
3 * Copyright (c) 2010-2021 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_fl(POST_ARGS
);
96 static void post_fn(POST_ARGS
);
97 static void post_fname(POST_ARGS
);
98 static void post_fo(POST_ARGS
);
99 static void post_hyph(POST_ARGS
);
100 static void post_it(POST_ARGS
);
101 static void post_lb(POST_ARGS
);
102 static void post_nd(POST_ARGS
);
103 static void post_nm(POST_ARGS
);
104 static void post_ns(POST_ARGS
);
105 static void post_obsolete(POST_ARGS
);
106 static void post_os(POST_ARGS
);
107 static void post_par(POST_ARGS
);
108 static void post_prevpar(POST_ARGS
);
109 static void post_root(POST_ARGS
);
110 static void post_rs(POST_ARGS
);
111 static void post_rv(POST_ARGS
);
112 static void post_section(POST_ARGS
);
113 static void post_sh(POST_ARGS
);
114 static void post_sh_head(POST_ARGS
);
115 static void post_sh_name(POST_ARGS
);
116 static void post_sh_see_also(POST_ARGS
);
117 static void post_sh_authors(POST_ARGS
);
118 static void post_sm(POST_ARGS
);
119 static void post_st(POST_ARGS
);
120 static void post_std(POST_ARGS
);
121 static void post_sx(POST_ARGS
);
122 static void post_tag(POST_ARGS
);
123 static void post_tg(POST_ARGS
);
124 static void post_useless(POST_ARGS
);
125 static void post_xr(POST_ARGS
);
126 static void post_xx(POST_ARGS
);
128 static const v_post mdoc_valids
[MDOC_MAX
- MDOC_Dd
] = {
133 post_section
, /* Ss */
135 post_display
, /* D1 */
136 post_display
, /* Dl */
137 post_display
, /* Bd */
142 post_delim_nb
, /* Ad */
145 post_defaults
, /* Ar */
156 post_delim_nb
, /* Ft */
158 post_delim_nb
, /* In */
162 post_delim_nb
, /* Op */
164 post_defaults
, /* Pa */
168 post_delim_nb
, /* Vt */
171 post_hyph
, /* %B */ /* FIXME: can be used outside Rs/Re. */
179 post_hyph
, /* %T */ /* FIXME: can be used outside Rs/Re. */
183 post_delim_nb
, /* Aq */
191 post_obsolete
, /* Db */
208 post_delim_nb
, /* Pq */
210 post_delim_nb
, /* Ql */
212 post_delim_nb
, /* Qq */
217 post_delim_nb
, /* Sq */
221 post_useless
, /* Tn */
232 post_obsolete
, /* Hf */
233 post_obsolete
, /* Fr */
237 post_delim_nb
, /* Lk */
238 post_defaults
, /* Mt */
239 post_delim_nb
, /* Brq */
252 #define RSORD_MAX 14 /* Number of `Rs' blocks. */
254 static const enum roff_tok rsord
[RSORD_MAX
] = {
271 static const char * const secnames
[SEC__MAX
] = {
278 "IMPLEMENTATION NOTES",
293 "SECURITY CONSIDERATIONS",
297 static int fn_prio
= TAG_STRONG
;
300 /* Validate the subtree rooted at mdoc->last. */
302 mdoc_validate(struct roff_man
*mdoc
)
304 struct roff_node
*n
, *np
;
308 * Translate obsolete macros to modern macros first
309 * such that later code does not need to look
310 * for the obsolete versions.
327 * Iterate over all children, recursing into each one
328 * in turn, depth-first.
331 mdoc
->last
= mdoc
->last
->child
;
332 while (mdoc
->last
!= NULL
) {
335 mdoc
->last
= mdoc
->last
->child
;
337 mdoc
->last
= mdoc
->last
->next
;
340 /* Finally validate the macro itself. */
343 mdoc
->next
= ROFF_NEXT_SIBLING
;
347 if (n
->sec
!= SEC_SYNOPSIS
||
348 (np
->tok
!= MDOC_Cd
&& np
->tok
!= MDOC_Fd
))
349 check_text(mdoc
, n
->line
, n
->pos
, n
->string
);
350 if ((n
->flags
& NODE_NOFILL
) == 0 &&
351 (np
->tok
!= MDOC_It
|| np
->type
!= ROFFT_HEAD
||
352 np
->parent
->parent
->norm
->Bl
.type
!= LIST_diag
))
353 check_text_em(mdoc
, n
->line
, n
->pos
, n
->string
);
354 if (np
->tok
== MDOC_It
|| (np
->type
== ROFFT_BODY
&&
355 (np
->tok
== MDOC_Sh
|| np
->tok
== MDOC_Ss
)))
356 check_toptext(mdoc
, n
->line
, n
->pos
, n
->string
);
366 check_args(mdoc
, mdoc
->last
);
369 * Closing delimiters are not special at the
370 * beginning of a block, opening delimiters
371 * are not special at the end.
374 if (n
->child
!= NULL
)
375 n
->child
->flags
&= ~NODE_DELIMC
;
377 n
->last
->flags
&= ~NODE_DELIMO
;
379 /* Call the macro's postprocessor. */
381 if (n
->tok
< ROFF_MAX
) {
386 assert(n
->tok
>= MDOC_Dd
&& n
->tok
< MDOC_MAX
);
387 p
= mdoc_valids
+ (n
->tok
- MDOC_Dd
);
397 check_args(struct roff_man
*mdoc
, struct roff_node
*n
)
404 assert(n
->args
->argc
);
405 for (i
= 0; i
< (int)n
->args
->argc
; i
++)
406 check_argv(mdoc
, n
, &n
->args
->argv
[i
]);
410 check_argv(struct roff_man
*mdoc
, struct roff_node
*n
, struct mdoc_argv
*v
)
414 for (i
= 0; i
< (int)v
->sz
; i
++)
415 check_text(mdoc
, v
->line
, v
->pos
, v
->value
[i
]);
419 check_text(struct roff_man
*mdoc
, int ln
, int pos
, char *p
)
423 if (mdoc
->last
->flags
& NODE_NOFILL
)
426 for (cp
= p
; NULL
!= (p
= strchr(p
, '\t')); p
++)
427 mandoc_msg(MANDOCERR_FI_TAB
, ln
, pos
+ (int)(p
- cp
), NULL
);
431 check_text_em(struct roff_man
*mdoc
, int ln
, int pos
, char *p
)
433 const struct roff_node
*np
, *nn
;
436 np
= mdoc
->last
->prev
;
437 nn
= mdoc
->last
->next
;
439 /* Look for em-dashes wrongly encoded as "--". */
441 for (cp
= p
; *cp
!= '\0'; cp
++) {
442 if (cp
[0] != '-' || cp
[1] != '-')
446 /* Skip input sequences of more than two '-'. */
454 /* Skip "--" directly attached to something else. */
456 if ((cp
- p
> 1 && cp
[-2] != ' ') ||
457 (cp
[1] != '\0' && cp
[1] != ' '))
460 /* Require a letter right before or right afterwards. */
463 isalpha((unsigned char)cp
[-3]) :
465 np
->type
== ROFFT_TEXT
&&
466 *np
->string
!= '\0' &&
467 isalpha((unsigned char)np
->string
[
468 strlen(np
->string
) - 1])) ||
469 (cp
[1] != '\0' && cp
[2] != '\0' ?
470 isalpha((unsigned char)cp
[2]) :
472 nn
->type
== ROFFT_TEXT
&&
473 isalpha((unsigned char)*nn
->string
))) {
474 mandoc_msg(MANDOCERR_DASHDASH
,
475 ln
, pos
+ (int)(cp
- p
) - 1, NULL
);
482 check_toptext(struct roff_man
*mdoc
, int ln
, int pos
, const char *p
)
484 const char *cp
, *cpr
;
489 if ((cp
= strstr(p
, "OpenBSD")) != NULL
)
490 mandoc_msg(MANDOCERR_BX
, ln
, pos
+ (int)(cp
- p
), "Ox");
491 if ((cp
= strstr(p
, "NetBSD")) != NULL
)
492 mandoc_msg(MANDOCERR_BX
, ln
, pos
+ (int)(cp
- p
), "Nx");
493 if ((cp
= strstr(p
, "FreeBSD")) != NULL
)
494 mandoc_msg(MANDOCERR_BX
, ln
, pos
+ (int)(cp
- p
), "Fx");
495 if ((cp
= strstr(p
, "DragonFly")) != NULL
)
496 mandoc_msg(MANDOCERR_BX
, ln
, pos
+ (int)(cp
- p
), "Dx");
499 while ((cp
= strstr(cp
+ 1, "()")) != NULL
) {
500 for (cpr
= cp
- 1; cpr
>= p
; cpr
--)
501 if (*cpr
!= '_' && !isalnum((unsigned char)*cpr
))
503 if ((cpr
< p
|| *cpr
== ' ') && cpr
+ 1 < cp
) {
505 mandoc_msg(MANDOCERR_FUNC
, ln
, pos
+ (int)(cpr
- p
),
506 "%.*s()", (int)(cp
- cpr
), cpr
);
512 post_abort(POST_ARGS
)
518 post_delim(POST_ARGS
)
520 const struct roff_node
*nch
;
525 tok
= mdoc
->last
->tok
;
526 nch
= mdoc
->last
->last
;
527 if (nch
== NULL
|| nch
->type
!= ROFFT_TEXT
)
529 lc
= strchr(nch
->string
, '\0') - 1;
530 if (lc
< nch
->string
)
532 delim
= mdoc_isdelim(lc
);
533 if (delim
== DELIM_NONE
|| delim
== DELIM_OPEN
)
535 if (*lc
== ')' && (tok
== MDOC_Nd
|| tok
== MDOC_Sh
||
536 tok
== MDOC_Ss
|| tok
== MDOC_Fo
))
539 mandoc_msg(MANDOCERR_DELIM
, nch
->line
,
540 nch
->pos
+ (int)(lc
- nch
->string
), "%s%s %s", roff_name
[tok
],
541 nch
== mdoc
->last
->child
? "" : " ...", nch
->string
);
545 post_delim_nb(POST_ARGS
)
547 const struct roff_node
*nch
;
554 * Find candidates: at least two bytes,
555 * the last one a closing or middle delimiter.
558 tok
= mdoc
->last
->tok
;
559 nch
= mdoc
->last
->last
;
560 if (nch
== NULL
|| nch
->type
!= ROFFT_TEXT
)
562 lc
= strchr(nch
->string
, '\0') - 1;
563 if (lc
<= nch
->string
)
565 delim
= mdoc_isdelim(lc
);
566 if (delim
== DELIM_NONE
|| delim
== DELIM_OPEN
)
570 * Reduce false positives by allowing various cases.
573 /* Escaped delimiters. */
574 if (lc
> nch
->string
+ 1 && lc
[-2] == '\\' &&
575 (lc
[-1] == '&' || lc
[-1] == 'e'))
578 /* Specific byte sequences. */
581 for (cp
= lc
; cp
>= nch
->string
; cp
--)
586 if (lc
> nch
->string
+ 1 && lc
[-2] == '.' && lc
[-1] == '.')
600 for (cp
= lc
; cp
>= nch
->string
; cp
--)
605 if (lc
== nch
->string
+ 1 && lc
[-1] == '|')
611 /* Exactly two non-alphanumeric bytes. */
612 if (lc
== nch
->string
+ 1 && !isalnum((unsigned char)lc
[-1]))
615 /* At least three alphabetic words with a sentence ending. */
616 if (strchr("!.:?", *lc
) != NULL
&& (tok
== MDOC_Em
||
617 tok
== MDOC_Li
|| tok
== MDOC_Pq
|| tok
== MDOC_Sy
)) {
619 for (cp
= lc
- 1; cp
>= nch
->string
; cp
--) {
622 if (cp
> nch
->string
&& cp
[-1] == ',')
624 } else if (isalpha((unsigned int)*cp
)) {
632 mandoc_msg(MANDOCERR_DELIM_NB
, nch
->line
,
633 nch
->pos
+ (int)(lc
- nch
->string
), "%s%s %s", roff_name
[tok
],
634 nch
== mdoc
->last
->child
? "" : " ...", nch
->string
);
638 post_bl_norm(POST_ARGS
)
641 struct mdoc_argv
*argv
, *wa
;
643 enum mdocargt mdoclt
;
646 n
= mdoc
->last
->parent
;
647 n
->norm
->Bl
.type
= LIST__NONE
;
650 * First figure out which kind of list to use: bind ourselves to
651 * the first mentioned list type and warn about any remaining
652 * ones. If we find no list type, we default to LIST_item.
655 wa
= (n
->args
== NULL
) ? NULL
: n
->args
->argv
;
656 mdoclt
= MDOC_ARG_MAX
;
657 for (i
= 0; n
->args
&& i
< (int)n
->args
->argc
; i
++) {
658 argv
= n
->args
->argv
+ i
;
661 /* Set list types. */
695 /* Set list arguments. */
697 if (n
->norm
->Bl
.comp
)
698 mandoc_msg(MANDOCERR_ARG_REP
,
699 argv
->line
, argv
->pos
, "Bl -compact");
700 n
->norm
->Bl
.comp
= 1;
705 mandoc_msg(MANDOCERR_ARG_EMPTY
,
706 argv
->line
, argv
->pos
, "Bl -width");
707 n
->norm
->Bl
.width
= "0n";
710 if (NULL
!= n
->norm
->Bl
.width
)
711 mandoc_msg(MANDOCERR_ARG_REP
,
712 argv
->line
, argv
->pos
,
713 "Bl -width %s", argv
->value
[0]);
714 rewrite_macro2len(mdoc
, argv
->value
);
715 n
->norm
->Bl
.width
= argv
->value
[0];
719 mandoc_msg(MANDOCERR_ARG_EMPTY
,
720 argv
->line
, argv
->pos
, "Bl -offset");
723 if (NULL
!= n
->norm
->Bl
.offs
)
724 mandoc_msg(MANDOCERR_ARG_REP
,
725 argv
->line
, argv
->pos
,
726 "Bl -offset %s", argv
->value
[0]);
727 rewrite_macro2len(mdoc
, argv
->value
);
728 n
->norm
->Bl
.offs
= argv
->value
[0];
733 if (LIST__NONE
== lt
)
737 /* Check: multiple list types. */
739 if (LIST__NONE
!= n
->norm
->Bl
.type
) {
740 mandoc_msg(MANDOCERR_BL_REP
, n
->line
, n
->pos
,
741 "Bl -%s", mdoc_argnames
[argv
->arg
]);
745 /* The list type should come first. */
747 if (n
->norm
->Bl
.width
||
750 mandoc_msg(MANDOCERR_BL_LATETYPE
,
751 n
->line
, n
->pos
, "Bl -%s",
752 mdoc_argnames
[n
->args
->argv
[0].arg
]);
754 n
->norm
->Bl
.type
= lt
;
755 if (LIST_column
== lt
) {
756 n
->norm
->Bl
.ncols
= argv
->sz
;
757 n
->norm
->Bl
.cols
= (void *)argv
->value
;
761 /* Allow lists to default to LIST_item. */
763 if (LIST__NONE
== n
->norm
->Bl
.type
) {
764 mandoc_msg(MANDOCERR_BL_NOTYPE
, n
->line
, n
->pos
, "Bl");
765 n
->norm
->Bl
.type
= LIST_item
;
770 * Validate the width field. Some list types don't need width
771 * types and should be warned about them. Others should have it
772 * and must also be warned. Yet others have a default and need
776 switch (n
->norm
->Bl
.type
) {
778 if (n
->norm
->Bl
.width
== NULL
)
779 mandoc_msg(MANDOCERR_BL_NOWIDTH
,
780 n
->line
, n
->pos
, "Bl -tag");
787 if (n
->norm
->Bl
.width
!= NULL
)
788 mandoc_msg(MANDOCERR_BL_SKIPW
, wa
->line
, wa
->pos
,
789 "Bl -%s", mdoc_argnames
[mdoclt
]);
790 n
->norm
->Bl
.width
= NULL
;
795 if (n
->norm
->Bl
.width
== NULL
)
796 n
->norm
->Bl
.width
= "2n";
799 if (n
->norm
->Bl
.width
== NULL
)
800 n
->norm
->Bl
.width
= "3n";
811 struct mdoc_argv
*argv
;
816 for (i
= 0; n
->args
&& i
< (int)n
->args
->argc
; i
++) {
817 argv
= n
->args
->argv
+ i
;
837 mandoc_msg(MANDOCERR_BD_FILE
, n
->line
, n
->pos
, NULL
);
841 mandoc_msg(MANDOCERR_ARG_EMPTY
,
842 argv
->line
, argv
->pos
, "Bd -offset");
845 if (NULL
!= n
->norm
->Bd
.offs
)
846 mandoc_msg(MANDOCERR_ARG_REP
,
847 argv
->line
, argv
->pos
,
848 "Bd -offset %s", argv
->value
[0]);
849 rewrite_macro2len(mdoc
, argv
->value
);
850 n
->norm
->Bd
.offs
= argv
->value
[0];
853 if (n
->norm
->Bd
.comp
)
854 mandoc_msg(MANDOCERR_ARG_REP
,
855 argv
->line
, argv
->pos
, "Bd -compact");
856 n
->norm
->Bd
.comp
= 1;
861 if (DISP__NONE
== dt
)
864 if (DISP__NONE
== n
->norm
->Bd
.type
)
865 n
->norm
->Bd
.type
= dt
;
867 mandoc_msg(MANDOCERR_BD_REP
, n
->line
, n
->pos
,
868 "Bd -%s", mdoc_argnames
[argv
->arg
]);
871 if (DISP__NONE
== n
->norm
->Bd
.type
) {
872 mandoc_msg(MANDOCERR_BD_NOTYPE
, n
->line
, n
->pos
, "Bd");
873 n
->norm
->Bd
.type
= DISP_ragged
;
878 * Stand-alone line macros.
882 post_an_norm(POST_ARGS
)
885 struct mdoc_argv
*argv
;
892 for (i
= 1; i
< n
->args
->argc
; i
++) {
893 argv
= n
->args
->argv
+ i
;
894 mandoc_msg(MANDOCERR_AN_REP
, argv
->line
, argv
->pos
,
895 "An -%s", mdoc_argnames
[argv
->arg
]);
898 argv
= n
->args
->argv
;
899 if (argv
->arg
== MDOC_Split
)
900 n
->norm
->An
.auth
= AUTH_split
;
901 else if (argv
->arg
== MDOC_Nosplit
)
902 n
->norm
->An
.auth
= AUTH_nosplit
;
914 if (n
->child
!= NULL
)
915 mandoc_msg(MANDOCERR_ARG_SKIP
, n
->line
,
916 n
->pos
, "%s %s", roff_name
[n
->tok
], n
->child
->string
);
918 while (n
->child
!= NULL
)
919 roff_node_delete(mdoc
, n
->child
);
921 roff_word_alloc(mdoc
, n
->line
, n
->pos
, n
->tok
== MDOC_Bt
?
922 "is currently in beta test." : "currently under development.");
923 mdoc
->last
->flags
|= NODE_EOS
| NODE_NOSRC
;
928 build_list(struct roff_man
*mdoc
, int tok
)
933 n
= mdoc
->last
->next
;
934 for (ic
= 1;; ic
++) {
935 roff_elem_alloc(mdoc
, n
->line
, n
->pos
, tok
);
936 mdoc
->last
->flags
|= NODE_NOSRC
;
937 roff_node_relink(mdoc
, n
);
938 n
= mdoc
->last
= mdoc
->last
->parent
;
939 mdoc
->next
= ROFF_NEXT_SIBLING
;
942 if (ic
> 1 || n
->next
->next
!= NULL
) {
943 roff_word_alloc(mdoc
, n
->line
, n
->pos
, ",");
944 mdoc
->last
->flags
|= NODE_DELIMC
| NODE_NOSRC
;
946 n
= mdoc
->last
->next
;
947 if (n
->next
== NULL
) {
948 roff_word_alloc(mdoc
, n
->line
, n
->pos
, "and");
949 mdoc
->last
->flags
|= NODE_NOSRC
;
963 mdoc
->next
= ROFF_NEXT_CHILD
;
964 roff_word_alloc(mdoc
, n
->line
, n
->pos
, "The");
965 mdoc
->last
->flags
|= NODE_NOSRC
;
967 if (mdoc
->last
->next
!= NULL
)
968 ic
= build_list(mdoc
, MDOC_Nm
);
969 else if (mdoc
->meta
.name
!= NULL
) {
970 roff_elem_alloc(mdoc
, n
->line
, n
->pos
, MDOC_Nm
);
971 mdoc
->last
->flags
|= NODE_NOSRC
;
972 roff_word_alloc(mdoc
, n
->line
, n
->pos
, mdoc
->meta
.name
);
973 mdoc
->last
->flags
|= NODE_NOSRC
;
974 mdoc
->last
= mdoc
->last
->parent
;
975 mdoc
->next
= ROFF_NEXT_SIBLING
;
978 mandoc_msg(MANDOCERR_EX_NONAME
, n
->line
, n
->pos
, "Ex");
982 roff_word_alloc(mdoc
, n
->line
, n
->pos
,
983 ic
> 1 ? "utilities exit\\~0" : "utility exits\\~0");
984 mdoc
->last
->flags
|= NODE_NOSRC
;
985 roff_word_alloc(mdoc
, n
->line
, n
->pos
,
986 "on success, and\\~>0 if an error occurs.");
987 mdoc
->last
->flags
|= NODE_EOS
| NODE_NOSRC
;
1000 assert(n
->child
->type
== ROFFT_TEXT
);
1001 mdoc
->next
= ROFF_NEXT_CHILD
;
1003 if ((p
= mdoc_a2lib(n
->child
->string
)) != NULL
) {
1004 n
->child
->flags
|= NODE_NOPRT
;
1005 roff_word_alloc(mdoc
, n
->line
, n
->pos
, p
);
1006 mdoc
->last
->flags
= NODE_NOSRC
;
1011 mandoc_msg(MANDOCERR_LB_BAD
, n
->child
->line
,
1012 n
->child
->pos
, "Lb %s", n
->child
->string
);
1014 roff_word_alloc(mdoc
, n
->line
, n
->pos
, "library");
1015 mdoc
->last
->flags
= NODE_NOSRC
;
1016 roff_word_alloc(mdoc
, n
->line
, n
->pos
, "\\(lq");
1017 mdoc
->last
->flags
= NODE_DELIMO
| NODE_NOSRC
;
1018 mdoc
->last
= mdoc
->last
->next
;
1019 roff_word_alloc(mdoc
, n
->line
, n
->pos
, "\\(rq");
1020 mdoc
->last
->flags
= NODE_DELIMC
| NODE_NOSRC
;
1027 struct roff_node
*n
;
1033 mdoc
->next
= ROFF_NEXT_CHILD
;
1034 if (n
->child
!= NULL
) {
1035 roff_word_alloc(mdoc
, n
->line
, n
->pos
, "The");
1036 mdoc
->last
->flags
|= NODE_NOSRC
;
1037 ic
= build_list(mdoc
, MDOC_Fn
);
1038 roff_word_alloc(mdoc
, n
->line
, n
->pos
,
1039 ic
> 1 ? "functions return" : "function returns");
1040 mdoc
->last
->flags
|= NODE_NOSRC
;
1041 roff_word_alloc(mdoc
, n
->line
, n
->pos
,
1042 "the value\\~0 if successful;");
1044 roff_word_alloc(mdoc
, n
->line
, n
->pos
, "Upon successful "
1045 "completion, the value\\~0 is returned;");
1046 mdoc
->last
->flags
|= NODE_NOSRC
;
1048 roff_word_alloc(mdoc
, n
->line
, n
->pos
, "otherwise "
1049 "the value\\~\\-1 is returned and the global variable");
1050 mdoc
->last
->flags
|= NODE_NOSRC
;
1051 roff_elem_alloc(mdoc
, n
->line
, n
->pos
, MDOC_Va
);
1052 mdoc
->last
->flags
|= NODE_NOSRC
;
1053 roff_word_alloc(mdoc
, n
->line
, n
->pos
, "errno");
1054 mdoc
->last
->flags
|= NODE_NOSRC
;
1055 mdoc
->last
= mdoc
->last
->parent
;
1056 mdoc
->next
= ROFF_NEXT_SIBLING
;
1057 roff_word_alloc(mdoc
, n
->line
, n
->pos
,
1058 "is set to indicate the error.");
1059 mdoc
->last
->flags
|= NODE_EOS
| NODE_NOSRC
;
1066 struct roff_node
*n
;
1071 if (n
->args
&& n
->args
->argc
== 1)
1072 if (n
->args
->argv
[0].arg
== MDOC_Std
)
1075 mandoc_msg(MANDOCERR_ARG_STD
, n
->line
, n
->pos
,
1076 "%s", roff_name
[n
->tok
]);
1082 struct roff_node
*n
, *nch
;
1087 assert(nch
->type
== ROFFT_TEXT
);
1089 if ((p
= mdoc_a2st(nch
->string
)) == NULL
) {
1090 mandoc_msg(MANDOCERR_ST_BAD
,
1091 nch
->line
, nch
->pos
, "St %s", nch
->string
);
1092 roff_node_delete(mdoc
, n
);
1096 nch
->flags
|= NODE_NOPRT
;
1097 mdoc
->next
= ROFF_NEXT_CHILD
;
1098 roff_word_alloc(mdoc
, nch
->line
, nch
->pos
, p
);
1099 mdoc
->last
->flags
|= NODE_NOSRC
;
1106 struct roff_node
*n
; /* The .Tg node. */
1107 struct roff_node
*nch
; /* The first child of the .Tg node. */
1108 struct roff_node
*nn
; /* The next node after the .Tg node. */
1109 struct roff_node
*np
; /* The parent of the next node. */
1110 struct roff_node
*nt
; /* The TEXT node containing the tag. */
1111 size_t len
; /* The number of bytes in the tag. */
1113 /* Find the next node. */
1115 for (nn
= n
; nn
!= NULL
; nn
= nn
->parent
) {
1116 if (nn
->type
!= ROFFT_HEAD
&& nn
->type
!= ROFFT_BODY
&&
1117 nn
->type
!= ROFFT_TAIL
&& nn
->next
!= NULL
) {
1124 nt
= nch
= n
->child
;
1125 if (nch
== NULL
&& nn
!= NULL
&& nn
->child
!= NULL
&&
1126 nn
->child
->type
== ROFFT_TEXT
)
1129 /* Validate the tag. */
1130 if (nt
== NULL
|| *nt
->string
== '\0')
1131 mandoc_msg(MANDOCERR_MACRO_EMPTY
, n
->line
, n
->pos
, "Tg");
1133 roff_node_delete(mdoc
, n
);
1136 len
= strcspn(nt
->string
, " \t\\");
1137 if (nt
->string
[len
] != '\0')
1138 mandoc_msg(MANDOCERR_TG_SPC
, nt
->line
,
1139 nt
->pos
+ len
, "Tg %s", nt
->string
);
1141 /* Keep only the first argument. */
1142 if (nch
!= NULL
&& nch
->next
!= NULL
) {
1143 mandoc_msg(MANDOCERR_ARG_EXCESS
, nch
->next
->line
,
1144 nch
->next
->pos
, "Tg ... %s", nch
->next
->string
);
1145 while (nch
->next
!= NULL
)
1146 roff_node_delete(mdoc
, nch
->next
);
1149 /* Drop the macro if the first argument is invalid. */
1150 if (len
== 0 || nt
->string
[len
] != '\0') {
1151 roff_node_delete(mdoc
, n
);
1155 /* By default, tag the .Tg node itself. */
1156 if (nn
== NULL
|| nn
->flags
& NODE_ID
)
1159 /* Explicit tagging of specific macros. */
1164 nn
= nn
->head
->child
== NULL
? n
: nn
->head
;
1168 while (np
->tok
!= MDOC_Bl
)
1170 switch (np
->norm
->Bl
.type
) {
1185 nn
= nn
->body
->child
== NULL
? n
: nn
->body
;
1195 nn
= nn
->body
->child
== NULL
? n
: nn
->body
;
1211 if (nn
->child
== NULL
)
1218 tag_put(nt
->string
, TAG_MANUAL
, nn
);
1220 n
->flags
|= NODE_NOPRT
;
1224 post_obsolete(POST_ARGS
)
1226 struct roff_node
*n
;
1229 if (n
->type
== ROFFT_ELEM
|| n
->type
== ROFFT_BLOCK
)
1230 mandoc_msg(MANDOCERR_MACRO_OBS
, n
->line
, n
->pos
,
1231 "%s", roff_name
[n
->tok
]);
1235 post_useless(POST_ARGS
)
1237 struct roff_node
*n
;
1240 mandoc_msg(MANDOCERR_MACRO_USELESS
, n
->line
, n
->pos
,
1241 "%s", roff_name
[n
->tok
]);
1251 struct roff_node
*np
, *nch
;
1254 * Unlike other data pointers, these are "housed" by the HEAD
1255 * element, which contains the goods.
1259 if (np
->type
!= ROFFT_HEAD
)
1262 assert(np
->parent
->type
== ROFFT_BLOCK
);
1263 assert(np
->parent
->tok
== MDOC_Bf
);
1265 /* Check the number of arguments. */
1268 if (np
->parent
->args
== NULL
) {
1270 mandoc_msg(MANDOCERR_BF_NOFONT
,
1271 np
->line
, np
->pos
, "Bf");
1277 mandoc_msg(MANDOCERR_ARG_EXCESS
,
1278 nch
->line
, nch
->pos
, "Bf ... %s", nch
->string
);
1280 /* Extract argument into data. */
1282 if (np
->parent
->args
!= NULL
) {
1283 switch (np
->parent
->args
->argv
[0].arg
) {
1285 np
->norm
->Bf
.font
= FONT_Em
;
1288 np
->norm
->Bf
.font
= FONT_Li
;
1291 np
->norm
->Bf
.font
= FONT_Sy
;
1299 /* Extract parameter into data. */
1301 if ( ! strcmp(np
->child
->string
, "Em"))
1302 np
->norm
->Bf
.font
= FONT_Em
;
1303 else if ( ! strcmp(np
->child
->string
, "Li"))
1304 np
->norm
->Bf
.font
= FONT_Li
;
1305 else if ( ! strcmp(np
->child
->string
, "Sy"))
1306 np
->norm
->Bf
.font
= FONT_Sy
;
1308 mandoc_msg(MANDOCERR_BF_BADFONT
, np
->child
->line
,
1309 np
->child
->pos
, "Bf %s", np
->child
->string
);
1313 post_fname(POST_ARGS
)
1315 struct roff_node
*n
, *nch
;
1323 if (cp
[strlen(cp
+ 1)] == ')')
1327 pos
= strcspn(cp
, "()");
1328 if (cp
[pos
] == '\0') {
1329 if (n
->sec
== SEC_DESCRIPTION
||
1330 n
->sec
== SEC_CUSTOM
)
1331 tag_put(NULL
, fn_prio
++, n
);
1335 mandoc_msg(MANDOCERR_FN_PAREN
, nch
->line
, nch
->pos
+ pos
, "%s", cp
);
1348 const struct roff_node
*n
;
1352 if (n
->type
!= ROFFT_HEAD
)
1355 if (n
->child
== NULL
) {
1356 mandoc_msg(MANDOCERR_FO_NOHEAD
, n
->line
, n
->pos
, "Fo");
1359 if (n
->child
!= n
->last
) {
1360 mandoc_msg(MANDOCERR_ARG_EXCESS
,
1361 n
->child
->next
->line
, n
->child
->next
->pos
,
1362 "Fo ... %s", n
->child
->next
->string
);
1363 while (n
->child
!= n
->last
)
1364 roff_node_delete(mdoc
, n
->last
);
1374 const struct roff_node
*n
;
1377 for (n
= mdoc
->last
->child
; n
!= NULL
; n
= n
->next
) {
1378 for (cp
= n
->string
; *cp
!= '\0'; cp
++) {
1379 /* Ignore callbacks and alterations. */
1380 if (*cp
== '(' || *cp
== '{')
1384 mandoc_msg(MANDOCERR_FA_COMMA
, n
->line
,
1385 n
->pos
+ (int)(cp
- n
->string
), "%s", n
->string
);
1389 post_delim_nb(mdoc
);
1395 struct roff_node
*n
;
1399 if (n
->sec
== SEC_NAME
&& n
->child
!= NULL
&&
1400 n
->child
->type
== ROFFT_TEXT
&& mdoc
->meta
.msec
!= NULL
)
1401 mandoc_xr_add(mdoc
->meta
.msec
, n
->child
->string
, -1, -1);
1403 if (n
->last
!= NULL
&& n
->last
->tok
== MDOC_Pp
)
1404 roff_node_relink(mdoc
, n
->last
);
1406 if (mdoc
->meta
.name
== NULL
)
1407 deroff(&mdoc
->meta
.name
, n
);
1409 if (mdoc
->meta
.name
== NULL
||
1410 (mdoc
->lastsec
== SEC_NAME
&& n
->child
== NULL
))
1411 mandoc_msg(MANDOCERR_NM_NONAME
, n
->line
, n
->pos
, "Nm");
1415 post_delim_nb(mdoc
);
1424 if ((n
->child
!= NULL
&& n
->child
->type
== ROFFT_TEXT
) ||
1425 mdoc
->meta
.name
== NULL
)
1428 mdoc
->next
= ROFF_NEXT_CHILD
;
1429 roff_word_alloc(mdoc
, n
->line
, n
->pos
, mdoc
->meta
.name
);
1430 mdoc
->last
->flags
|= NODE_NOSRC
;
1437 struct roff_node
*n
;
1441 if (n
->type
!= ROFFT_BODY
)
1444 if (n
->sec
!= SEC_NAME
)
1445 mandoc_msg(MANDOCERR_ND_LATE
, n
->line
, n
->pos
, "Nd");
1447 if (n
->child
== NULL
)
1448 mandoc_msg(MANDOCERR_ND_EMPTY
, n
->line
, n
->pos
, "Nd");
1456 post_display(POST_ARGS
)
1458 struct roff_node
*n
, *np
;
1463 if (n
->end
!= ENDBODY_NOT
) {
1464 if (n
->tok
== MDOC_Bd
&&
1465 n
->body
->parent
->args
== NULL
)
1466 roff_node_delete(mdoc
, n
);
1467 } else if (n
->child
== NULL
)
1468 mandoc_msg(MANDOCERR_BLK_EMPTY
, n
->line
, n
->pos
,
1469 "%s", roff_name
[n
->tok
]);
1470 else if (n
->tok
== MDOC_D1
)
1474 if (n
->tok
== MDOC_Bd
) {
1475 if (n
->args
== NULL
) {
1476 mandoc_msg(MANDOCERR_BD_NOARG
,
1477 n
->line
, n
->pos
, "Bd");
1478 mdoc
->next
= ROFF_NEXT_SIBLING
;
1479 while (n
->body
->child
!= NULL
)
1480 roff_node_relink(mdoc
,
1482 roff_node_delete(mdoc
, n
);
1488 for (np
= n
->parent
; np
!= NULL
; np
= np
->parent
) {
1489 if (np
->type
== ROFFT_BLOCK
&& np
->tok
== MDOC_Bd
) {
1490 mandoc_msg(MANDOCERR_BD_NEST
, n
->line
,
1491 n
->pos
, "%s in Bd", roff_name
[n
->tok
]);
1502 post_defaults(POST_ARGS
)
1504 struct roff_node
*n
;
1507 if (n
->child
!= NULL
) {
1508 post_delim_nb(mdoc
);
1511 mdoc
->next
= ROFF_NEXT_CHILD
;
1514 roff_word_alloc(mdoc
, n
->line
, n
->pos
, "file");
1515 mdoc
->last
->flags
|= NODE_NOSRC
;
1516 roff_word_alloc(mdoc
, n
->line
, n
->pos
, "...");
1520 roff_word_alloc(mdoc
, n
->line
, n
->pos
, "~");
1525 mdoc
->last
->flags
|= NODE_NOSRC
;
1532 struct roff_node
*n
, *nch
;
1539 * If we have a child, look it up in the standard keys. If a
1540 * key exist, use that instead of the child; if it doesn't,
1541 * prefix "AT&T UNIX " to the existing data.
1545 if (nch
!= NULL
&& ((att
= mdoc_a2att(nch
->string
)) == NULL
))
1546 mandoc_msg(MANDOCERR_AT_BAD
,
1547 nch
->line
, nch
->pos
, "At %s", nch
->string
);
1549 mdoc
->next
= ROFF_NEXT_CHILD
;
1551 roff_word_alloc(mdoc
, nch
->line
, nch
->pos
, att
);
1552 nch
->flags
|= NODE_NOPRT
;
1554 roff_word_alloc(mdoc
, n
->line
, n
->pos
, "AT&T UNIX");
1555 mdoc
->last
->flags
|= NODE_NOSRC
;
1562 struct roff_node
*np
, *nch
;
1568 if (np
->norm
->An
.auth
== AUTH__NONE
) {
1570 mandoc_msg(MANDOCERR_MACRO_EMPTY
,
1571 np
->line
, np
->pos
, "An");
1573 post_delim_nb(mdoc
);
1574 } else if (nch
!= NULL
)
1575 mandoc_msg(MANDOCERR_ARG_EXCESS
,
1576 nch
->line
, nch
->pos
, "An ... %s", nch
->string
);
1583 tag_put(NULL
, TAG_FALLBACK
, mdoc
->last
);
1589 post_obsolete(mdoc
);
1590 if (mdoc
->last
->type
== ROFFT_BLOCK
)
1591 mdoc
->last
->norm
->Es
= mdoc
->last_es
;
1597 struct roff_node
*n
;
1600 if (n
->sec
== SEC_ERRORS
&&
1601 (n
->parent
->tok
== MDOC_It
||
1602 (n
->parent
->tok
== MDOC_Bq
&&
1603 n
->parent
->parent
->parent
->tok
== MDOC_It
)))
1604 tag_put(NULL
, TAG_STRONG
, n
);
1605 post_delim_nb(mdoc
);
1611 struct roff_node
*n
;
1614 if ((n
->prev
== NULL
||
1615 (n
->prev
->type
== ROFFT_TEXT
&&
1616 strcmp(n
->prev
->string
, "|") == 0)) &&
1617 (n
->parent
->tok
== MDOC_It
||
1618 (n
->parent
->tok
== MDOC_Xo
&&
1619 n
->parent
->parent
->prev
== NULL
&&
1620 n
->parent
->parent
->parent
->tok
== MDOC_It
)))
1621 tag_put(NULL
, TAG_STRONG
, n
);
1622 post_delim_nb(mdoc
);
1628 post_obsolete(mdoc
);
1629 mdoc
->last_es
= mdoc
->last
;
1635 struct roff_node
*n
;
1639 * Transform ".Fl Fl long" to ".Fl \-long",
1640 * resulting for example in better HTML output.
1644 if (n
->prev
!= NULL
&& n
->prev
->tok
== MDOC_Fl
&&
1645 n
->prev
->child
== NULL
&& n
->child
!= NULL
&&
1646 (n
->flags
& NODE_LINE
) == 0) {
1647 mandoc_asprintf(&cp
, "\\-%s", n
->child
->string
);
1648 free(n
->child
->string
);
1649 n
->child
->string
= cp
;
1650 roff_node_delete(mdoc
, n
->prev
);
1658 struct roff_node
*n
;
1662 post_delim_nb(mdoc
);
1677 if (n
->child
== NULL
)
1679 v
= n
->child
->string
;
1680 if ((v
[0] != '0' && v
[0] != '1') || v
[1] != '.' ||
1681 v
[2] < '0' || v
[2] > '9' ||
1682 v
[3] < 'a' || v
[3] > 'z' || v
[4] != '\0')
1684 n
->child
->flags
|= NODE_NOPRT
;
1685 mdoc
->next
= ROFF_NEXT_CHILD
;
1686 roff_word_alloc(mdoc
, n
->child
->line
, n
->child
->pos
, v
);
1687 v
= mdoc
->last
->string
;
1688 v
[3] = toupper((unsigned char)v
[3]);
1689 mdoc
->last
->flags
|= NODE_NOSRC
;
1701 mdoc
->next
= ROFF_NEXT_CHILD
;
1702 roff_word_alloc(mdoc
, n
->line
, n
->pos
, os
);
1703 mdoc
->last
->flags
|= NODE_NOSRC
;
1710 struct roff_node
*nbl
, *nit
, *nch
;
1717 if (nit
->type
!= ROFFT_BLOCK
)
1720 nbl
= nit
->parent
->parent
;
1721 lt
= nbl
->norm
->Bl
.type
;
1729 if (nit
->head
->child
== NULL
)
1730 mandoc_msg(MANDOCERR_IT_NOHEAD
,
1731 nit
->line
, nit
->pos
, "Bl -%s It",
1732 mdoc_argnames
[nbl
->args
->argv
[0].arg
]);
1738 if (nit
->body
== NULL
|| nit
->body
->child
== NULL
)
1739 mandoc_msg(MANDOCERR_IT_NOBODY
,
1740 nit
->line
, nit
->pos
, "Bl -%s It",
1741 mdoc_argnames
[nbl
->args
->argv
[0].arg
]);
1744 if ((nch
= nit
->head
->child
) != NULL
)
1745 mandoc_msg(MANDOCERR_ARG_SKIP
,
1746 nit
->line
, nit
->pos
, "It %s",
1747 nch
->type
== ROFFT_TEXT
? nch
->string
:
1748 roff_name
[nch
->tok
]);
1751 cols
= (int)nbl
->norm
->Bl
.ncols
;
1753 assert(nit
->head
->child
== NULL
);
1755 if (nit
->head
->next
->child
== NULL
&&
1756 nit
->head
->next
->next
== NULL
) {
1757 mandoc_msg(MANDOCERR_MACRO_EMPTY
,
1758 nit
->line
, nit
->pos
, "It");
1759 roff_node_delete(mdoc
, nit
);
1764 for (nch
= nit
->child
; nch
!= NULL
; nch
= nch
->next
) {
1765 if (nch
->type
!= ROFFT_BODY
)
1767 if (i
++ && nch
->flags
& NODE_LINE
)
1768 mandoc_msg(MANDOCERR_TA_LINE
,
1769 nch
->line
, nch
->pos
, "Ta");
1771 if (i
< cols
|| i
> cols
+ 1)
1772 mandoc_msg(MANDOCERR_BL_COL
, nit
->line
, nit
->pos
,
1773 "%d columns, %d cells", cols
, i
);
1774 else if (nit
->head
->next
->child
!= NULL
&&
1775 nit
->head
->next
->child
->flags
& NODE_LINE
)
1776 mandoc_msg(MANDOCERR_IT_NOARG
,
1777 nit
->line
, nit
->pos
, "Bl -column It");
1785 post_bl_block(POST_ARGS
)
1787 struct roff_node
*n
, *ni
, *nc
;
1792 for (ni
= n
->body
->child
; ni
!= NULL
; ni
= ni
->next
) {
1793 if (ni
->body
== NULL
)
1795 nc
= ni
->body
->last
;
1796 while (nc
!= NULL
) {
1805 if (ni
->next
== NULL
) {
1806 mandoc_msg(MANDOCERR_PAR_MOVE
, nc
->line
,
1807 nc
->pos
, "%s", roff_name
[nc
->tok
]);
1808 roff_node_relink(mdoc
, nc
);
1809 } else if (n
->norm
->Bl
.comp
== 0 &&
1810 n
->norm
->Bl
.type
!= LIST_column
) {
1811 mandoc_msg(MANDOCERR_PAR_SKIP
,
1813 "%s before It", roff_name
[nc
->tok
]);
1814 roff_node_delete(mdoc
, nc
);
1817 nc
= ni
->body
->last
;
1823 * If the argument of -offset or -width is a macro,
1824 * replace it with the associated default width.
1827 rewrite_macro2len(struct roff_man
*mdoc
, char **arg
)
1834 else if ( ! strcmp(*arg
, "Ds"))
1836 else if ((tok
= roffhash_find(mdoc
->mdocmac
, *arg
, 0)) == TOKEN_NONE
)
1839 width
= macro2len(tok
);
1842 mandoc_asprintf(arg
, "%zun", width
);
1846 post_bl_head(POST_ARGS
)
1848 struct roff_node
*nbl
, *nh
, *nch
, *nnext
;
1849 struct mdoc_argv
*argv
;
1855 if (nh
->norm
->Bl
.type
!= LIST_column
) {
1856 if ((nch
= nh
->child
) == NULL
)
1858 mandoc_msg(MANDOCERR_ARG_EXCESS
,
1859 nch
->line
, nch
->pos
, "Bl ... %s", nch
->string
);
1860 while (nch
!= NULL
) {
1861 roff_node_delete(mdoc
, nch
);
1868 * Append old-style lists, where the column width specifiers
1869 * trail as macro parameters, to the new-style ("normal-form")
1870 * lists where they're argument values following -column.
1873 if (nh
->child
== NULL
)
1877 for (j
= 0; j
< (int)nbl
->args
->argc
; j
++)
1878 if (nbl
->args
->argv
[j
].arg
== MDOC_Column
)
1881 assert(j
< (int)nbl
->args
->argc
);
1884 * Accommodate for new-style groff column syntax. Shuffle the
1885 * child nodes, all of which must be TEXT, as arguments for the
1886 * column field. Then, delete the head children.
1889 argv
= nbl
->args
->argv
+ j
;
1891 for (nch
= nh
->child
; nch
!= NULL
; nch
= nch
->next
)
1893 argv
->value
= mandoc_reallocarray(argv
->value
,
1894 argv
->sz
, sizeof(char *));
1896 nh
->norm
->Bl
.ncols
= argv
->sz
;
1897 nh
->norm
->Bl
.cols
= (void *)argv
->value
;
1899 for (nch
= nh
->child
; nch
!= NULL
; nch
= nnext
) {
1900 argv
->value
[i
++] = nch
->string
;
1903 roff_node_delete(NULL
, nch
);
1911 struct roff_node
*nbody
; /* of the Bl */
1912 struct roff_node
*nchild
, *nnext
; /* of the Bl body */
1913 const char *prev_Er
;
1917 switch (nbody
->type
) {
1919 post_bl_block(mdoc
);
1929 if (nbody
->end
!= ENDBODY_NOT
)
1933 * Up to the first item, move nodes before the list,
1934 * but leave transparent nodes where they are
1935 * if they precede an item.
1936 * The next non-transparent node is kept in nchild.
1937 * It only needs to be updated after a non-transparent
1938 * node was moved out, and at the very beginning
1939 * when no node at all was moved yet.
1942 nchild
= mdoc
->last
;
1944 if (nchild
== mdoc
->last
)
1945 nchild
= roff_node_child(nbody
);
1946 if (nchild
== NULL
) {
1948 mandoc_msg(MANDOCERR_BLK_EMPTY
,
1949 nbody
->line
, nbody
->pos
, "Bl");
1952 if (nchild
->tok
== MDOC_It
) {
1956 mandoc_msg(MANDOCERR_BL_MOVE
, nbody
->child
->line
,
1957 nbody
->child
->pos
, "%s", roff_name
[nbody
->child
->tok
]);
1958 if (nbody
->parent
->prev
== NULL
) {
1959 mdoc
->last
= nbody
->parent
->parent
;
1960 mdoc
->next
= ROFF_NEXT_CHILD
;
1962 mdoc
->last
= nbody
->parent
->prev
;
1963 mdoc
->next
= ROFF_NEXT_SIBLING
;
1965 roff_node_relink(mdoc
, nbody
->child
);
1969 * We have reached the first item,
1970 * so moving nodes out is no longer possible.
1971 * But in .Bl -column, the first rows may be implicit,
1972 * that is, they may not start with .It macros.
1973 * Such rows may be followed by nodes generated on the
1974 * roff level, for example .TS.
1975 * Wrap such roff nodes into an implicit row.
1978 while (nchild
!= NULL
) {
1979 if (nchild
->tok
== MDOC_It
) {
1980 nchild
= roff_node_next(nchild
);
1983 nnext
= nchild
->next
;
1984 mdoc
->last
= nchild
->prev
;
1985 mdoc
->next
= ROFF_NEXT_SIBLING
;
1986 roff_block_alloc(mdoc
, nchild
->line
, nchild
->pos
, MDOC_It
);
1987 roff_head_alloc(mdoc
, nchild
->line
, nchild
->pos
, MDOC_It
);
1988 mdoc
->next
= ROFF_NEXT_SIBLING
;
1989 roff_body_alloc(mdoc
, nchild
->line
, nchild
->pos
, MDOC_It
);
1990 while (nchild
->tok
!= MDOC_It
) {
1991 roff_node_relink(mdoc
, nchild
);
1995 nnext
= nchild
->next
;
1996 mdoc
->next
= ROFF_NEXT_SIBLING
;
2001 if (mdoc
->meta
.os_e
!= MANDOC_OS_NETBSD
)
2005 for (nchild
= nbody
->child
; nchild
!= NULL
; nchild
= nchild
->next
) {
2006 if (nchild
->tok
!= MDOC_It
)
2008 if ((nnext
= nchild
->head
->child
) == NULL
)
2010 if (nnext
->type
== ROFFT_BLOCK
)
2011 nnext
= nnext
->body
->child
;
2012 if (nnext
== NULL
|| nnext
->tok
!= MDOC_Er
)
2014 nnext
= nnext
->child
;
2015 if (prev_Er
!= NULL
) {
2016 order
= strcmp(prev_Er
, nnext
->string
);
2018 mandoc_msg(MANDOCERR_ER_ORDER
,
2019 nnext
->line
, nnext
->pos
,
2020 "Er %s %s (NetBSD)",
2021 prev_Er
, nnext
->string
);
2022 else if (order
== 0)
2023 mandoc_msg(MANDOCERR_ER_REP
,
2024 nnext
->line
, nnext
->pos
,
2025 "Er %s (NetBSD)", prev_Er
);
2027 prev_Er
= nnext
->string
;
2034 struct roff_node
*n
;
2038 if (n
->type
== ROFFT_BLOCK
&& n
->body
->child
== NULL
) {
2039 mandoc_msg(MANDOCERR_BLK_EMPTY
, n
->line
, n
->pos
, "Bk");
2040 roff_node_delete(mdoc
, n
);
2047 struct roff_node
*nch
;
2049 nch
= mdoc
->last
->child
;
2052 mdoc
->flags
^= MDOC_SMOFF
;
2056 assert(nch
->type
== ROFFT_TEXT
);
2058 if ( ! strcmp(nch
->string
, "on")) {
2059 mdoc
->flags
&= ~MDOC_SMOFF
;
2062 if ( ! strcmp(nch
->string
, "off")) {
2063 mdoc
->flags
|= MDOC_SMOFF
;
2067 mandoc_msg(MANDOCERR_SM_BAD
, nch
->line
, nch
->pos
,
2068 "%s %s", roff_name
[mdoc
->last
->tok
], nch
->string
);
2069 roff_node_relink(mdoc
, nch
);
2074 post_root(POST_ARGS
)
2076 struct roff_node
*n
;
2078 /* Add missing prologue data. */
2080 if (mdoc
->meta
.date
== NULL
)
2081 mdoc
->meta
.date
= mandoc_normdate(NULL
, NULL
);
2083 if (mdoc
->meta
.title
== NULL
) {
2084 mandoc_msg(MANDOCERR_DT_NOTITLE
, 0, 0, "EOF");
2085 mdoc
->meta
.title
= mandoc_strdup("UNTITLED");
2088 if (mdoc
->meta
.vol
== NULL
)
2089 mdoc
->meta
.vol
= mandoc_strdup("LOCAL");
2091 if (mdoc
->meta
.os
== NULL
) {
2092 mandoc_msg(MANDOCERR_OS_MISSING
, 0, 0, NULL
);
2093 mdoc
->meta
.os
= mandoc_strdup("");
2094 } else if (mdoc
->meta
.os_e
&&
2095 (mdoc
->meta
.rcsids
& (1 << mdoc
->meta
.os_e
)) == 0)
2096 mandoc_msg(MANDOCERR_RCS_MISSING
, 0, 0,
2097 mdoc
->meta
.os_e
== MANDOC_OS_OPENBSD
?
2098 "(OpenBSD)" : "(NetBSD)");
2100 if (mdoc
->meta
.arch
!= NULL
&&
2101 arch_valid(mdoc
->meta
.arch
, mdoc
->meta
.os_e
) == 0) {
2102 n
= mdoc
->meta
.first
->child
;
2103 while (n
->tok
!= MDOC_Dt
||
2105 n
->child
->next
== NULL
||
2106 n
->child
->next
->next
== NULL
)
2108 n
= n
->child
->next
->next
;
2109 mandoc_msg(MANDOCERR_ARCH_BAD
, n
->line
, n
->pos
,
2110 "Dt ... %s %s", mdoc
->meta
.arch
,
2111 mdoc
->meta
.os_e
== MANDOC_OS_OPENBSD
?
2112 "(OpenBSD)" : "(NetBSD)");
2115 /* Check that we begin with a proper `Sh'. */
2117 n
= mdoc
->meta
.first
->child
;
2119 (n
->type
== ROFFT_COMMENT
||
2120 (n
->tok
>= MDOC_Dd
&&
2121 mdoc_macro(n
->tok
)->flags
& MDOC_PROLOGUE
)))
2125 mandoc_msg(MANDOCERR_DOC_EMPTY
, 0, 0, NULL
);
2126 else if (n
->tok
!= MDOC_Sh
)
2127 mandoc_msg(MANDOCERR_SEC_BEFORE
, n
->line
, n
->pos
,
2128 "%s", roff_name
[n
->tok
]);
2134 struct roff_node
*np
, *nch
, *next
, *prev
;
2139 if (np
->type
!= ROFFT_BODY
)
2142 if (np
->child
== NULL
) {
2143 mandoc_msg(MANDOCERR_RS_EMPTY
, np
->line
, np
->pos
, "Rs");
2148 * The full `Rs' block needs special handling to order the
2149 * sub-elements according to `rsord'. Pick through each element
2150 * and correctly order it. This is an insertion sort.
2154 for (nch
= np
->child
->next
; nch
!= NULL
; nch
= next
) {
2155 /* Determine order number of this child. */
2156 for (i
= 0; i
< RSORD_MAX
; i
++)
2157 if (rsord
[i
] == nch
->tok
)
2160 if (i
== RSORD_MAX
) {
2161 mandoc_msg(MANDOCERR_RS_BAD
, nch
->line
, nch
->pos
,
2162 "%s", roff_name
[nch
->tok
]);
2164 } else if (nch
->tok
== MDOC__J
|| nch
->tok
== MDOC__B
)
2165 np
->norm
->Rs
.quote_T
++;
2168 * Remove this child from the chain. This somewhat
2169 * repeats roff_node_unlink(), but since we're
2170 * just re-ordering, there's no need for the
2171 * full unlink process.
2174 if ((next
= nch
->next
) != NULL
)
2175 next
->prev
= nch
->prev
;
2177 if ((prev
= nch
->prev
) != NULL
)
2178 prev
->next
= nch
->next
;
2180 nch
->prev
= nch
->next
= NULL
;
2183 * Scan back until we reach a node that's
2184 * to be ordered before this child.
2187 for ( ; prev
; prev
= prev
->prev
) {
2188 /* Determine order of `prev'. */
2189 for (j
= 0; j
< RSORD_MAX
; j
++)
2190 if (rsord
[j
] == prev
->tok
)
2200 * Set this child back into its correct place
2201 * in front of the `prev' node.
2207 np
->child
->prev
= nch
;
2208 nch
->next
= np
->child
;
2212 prev
->next
->prev
= nch
;
2213 nch
->next
= prev
->next
;
2220 * For some arguments of some macros,
2221 * convert all breakable hyphens into ASCII_HYPH.
2224 post_hyph(POST_ARGS
)
2226 struct roff_node
*n
, *nch
;
2230 for (nch
= n
->child
; nch
!= NULL
; nch
= nch
->next
) {
2231 if (nch
->type
!= ROFFT_TEXT
)
2236 while (*(++cp
) != '\0')
2238 isalpha((unsigned char)cp
[-1]) &&
2239 isalpha((unsigned char)cp
[1])) {
2240 if (n
->tag
== NULL
&& n
->flags
& NODE_ID
)
2241 n
->tag
= mandoc_strdup(nch
->string
);
2250 struct roff_node
*n
;
2253 if (n
->flags
& NODE_LINE
||
2254 (n
->next
!= NULL
&& n
->next
->flags
& NODE_DELIMC
))
2255 mandoc_msg(MANDOCERR_NS_SKIP
, n
->line
, n
->pos
, NULL
);
2270 switch (mdoc
->last
->type
) {
2275 switch (mdoc
->lastsec
) {
2280 post_sh_see_also(mdoc
);
2283 post_sh_authors(mdoc
);
2295 post_sh_name(POST_ARGS
)
2297 struct roff_node
*n
;
2302 for (n
= mdoc
->last
->child
; n
!= NULL
; n
= n
->next
) {
2305 if (hasnm
&& n
->child
!= NULL
)
2306 mandoc_msg(MANDOCERR_NAMESEC_PUNCT
,
2308 "Nm %s", n
->child
->string
);
2313 if (n
->next
!= NULL
)
2314 mandoc_msg(MANDOCERR_NAMESEC_ND
,
2315 n
->line
, n
->pos
, NULL
);
2318 if (n
->type
== ROFFT_TEXT
&&
2319 n
->string
[0] == ',' && n
->string
[1] == '\0' &&
2320 n
->next
!= NULL
&& n
->next
->tok
== MDOC_Nm
) {
2326 mandoc_msg(MANDOCERR_NAMESEC_BAD
,
2327 n
->line
, n
->pos
, "%s", roff_name
[n
->tok
]);
2334 mandoc_msg(MANDOCERR_NAMESEC_NONM
,
2335 mdoc
->last
->line
, mdoc
->last
->pos
, NULL
);
2337 mandoc_msg(MANDOCERR_NAMESEC_NOND
,
2338 mdoc
->last
->line
, mdoc
->last
->pos
, NULL
);
2342 post_sh_see_also(POST_ARGS
)
2344 const struct roff_node
*n
;
2345 const char *name
, *sec
;
2346 const char *lastname
, *lastsec
, *lastpunct
;
2349 n
= mdoc
->last
->child
;
2350 lastname
= lastsec
= lastpunct
= NULL
;
2352 if (n
->tok
!= MDOC_Xr
||
2354 n
->child
->next
== NULL
)
2357 /* Process one .Xr node. */
2359 name
= n
->child
->string
;
2360 sec
= n
->child
->next
->string
;
2361 if (lastsec
!= NULL
) {
2362 if (lastpunct
[0] != ',' || lastpunct
[1] != '\0')
2363 mandoc_msg(MANDOCERR_XR_PUNCT
, n
->line
,
2364 n
->pos
, "%s before %s(%s)",
2365 lastpunct
, name
, sec
);
2366 cmp
= strcmp(lastsec
, sec
);
2368 mandoc_msg(MANDOCERR_XR_ORDER
, n
->line
,
2369 n
->pos
, "%s(%s) after %s(%s)",
2370 name
, sec
, lastname
, lastsec
);
2371 else if (cmp
== 0 &&
2372 strcasecmp(lastname
, name
) > 0)
2373 mandoc_msg(MANDOCERR_XR_ORDER
, n
->line
,
2374 n
->pos
, "%s after %s", name
, lastname
);
2379 /* Process the following node. */
2384 if (n
->tok
== MDOC_Xr
) {
2388 if (n
->type
!= ROFFT_TEXT
)
2390 for (name
= n
->string
; *name
!= '\0'; name
++)
2391 if (isalpha((const unsigned char)*name
))
2393 lastpunct
= n
->string
;
2394 if (n
->next
== NULL
|| n
->next
->tok
== MDOC_Rs
)
2395 mandoc_msg(MANDOCERR_XR_PUNCT
, n
->line
,
2396 n
->pos
, "%s after %s(%s)",
2397 lastpunct
, lastname
, lastsec
);
2403 child_an(const struct roff_node
*n
)
2406 for (n
= n
->child
; n
!= NULL
; n
= n
->next
)
2407 if ((n
->tok
== MDOC_An
&& n
->child
!= NULL
) || child_an(n
))
2413 post_sh_authors(POST_ARGS
)
2416 if ( ! child_an(mdoc
->last
))
2417 mandoc_msg(MANDOCERR_AN_MISSING
,
2418 mdoc
->last
->line
, mdoc
->last
->pos
, NULL
);
2422 * Return an upper bound for the string distance (allowing
2423 * transpositions). Not a full Levenshtein implementation
2424 * because Levenshtein is quadratic in the string length
2425 * and this function is called for every standard name,
2426 * so the check for each custom name would be cubic.
2427 * The following crude heuristics is linear, resulting
2428 * in quadratic behaviour for checking one custom name,
2429 * which does not cause measurable slowdown.
2432 similar(const char *s1
, const char *s2
)
2434 const int maxdist
= 3;
2437 while (s1
[0] != '\0' && s2
[0] != '\0') {
2438 if (s1
[0] == s2
[0]) {
2443 if (++dist
> maxdist
)
2445 if (s1
[1] == s2
[1]) { /* replacement */
2448 } else if (s1
[0] == s2
[1] && s1
[1] == s2
[0]) {
2449 s1
+= 2; /* transposition */
2451 } else if (s1
[0] == s2
[1]) /* insertion */
2453 else if (s1
[1] == s2
[0]) /* deletion */
2458 dist
+= strlen(s1
) + strlen(s2
);
2459 return dist
> maxdist
? INT_MAX
: dist
;
2463 post_sh_head(POST_ARGS
)
2465 struct roff_node
*nch
;
2466 const char *goodsec
;
2467 const char *const *testsec
;
2472 * Process a new section. Sections are either "named" or
2473 * "custom". Custom sections are user-defined, while named ones
2474 * follow a conventional order and may only appear in certain
2478 sec
= mdoc
->last
->sec
;
2480 /* The NAME should be first. */
2482 if (sec
!= SEC_NAME
&& mdoc
->lastnamed
== SEC_NONE
)
2483 mandoc_msg(MANDOCERR_NAMESEC_FIRST
,
2484 mdoc
->last
->line
, mdoc
->last
->pos
, "Sh %s",
2485 sec
!= SEC_CUSTOM
? secnames
[sec
] :
2486 (nch
= mdoc
->last
->child
) == NULL
? "" :
2487 nch
->type
== ROFFT_TEXT
? nch
->string
:
2488 roff_name
[nch
->tok
]);
2490 /* The SYNOPSIS gets special attention in other areas. */
2492 if (sec
== SEC_SYNOPSIS
) {
2493 roff_setreg(mdoc
->roff
, "nS", 1, '=');
2494 mdoc
->flags
|= MDOC_SYNOPSIS
;
2496 roff_setreg(mdoc
->roff
, "nS", 0, '=');
2497 mdoc
->flags
&= ~MDOC_SYNOPSIS
;
2499 if (sec
== SEC_DESCRIPTION
)
2500 fn_prio
= TAG_STRONG
;
2502 /* Mark our last section. */
2504 mdoc
->lastsec
= sec
;
2506 /* We don't care about custom sections after this. */
2508 if (sec
== SEC_CUSTOM
) {
2509 if ((nch
= mdoc
->last
->child
) == NULL
||
2510 nch
->type
!= ROFFT_TEXT
|| nch
->next
!= NULL
)
2514 for (testsec
= secnames
+ 1; *testsec
!= NULL
; testsec
++) {
2515 dist
= similar(nch
->string
, *testsec
);
2516 if (dist
< mindist
) {
2521 if (goodsec
!= NULL
)
2522 mandoc_msg(MANDOCERR_SEC_TYPO
, nch
->line
, nch
->pos
,
2523 "Sh %s instead of %s", nch
->string
, goodsec
);
2528 * Check whether our non-custom section is being repeated or is
2532 if (sec
== mdoc
->lastnamed
)
2533 mandoc_msg(MANDOCERR_SEC_REP
, mdoc
->last
->line
,
2534 mdoc
->last
->pos
, "Sh %s", secnames
[sec
]);
2536 if (sec
< mdoc
->lastnamed
)
2537 mandoc_msg(MANDOCERR_SEC_ORDER
, mdoc
->last
->line
,
2538 mdoc
->last
->pos
, "Sh %s", secnames
[sec
]);
2540 /* Mark the last named section. */
2542 mdoc
->lastnamed
= sec
;
2544 /* Check particular section/manual conventions. */
2546 if (mdoc
->meta
.msec
== NULL
)
2552 if (*mdoc
->meta
.msec
== '4')
2554 goodsec
= "2, 3, 4, 9";
2556 case SEC_RETURN_VALUES
:
2558 if (*mdoc
->meta
.msec
== '2')
2560 if (*mdoc
->meta
.msec
== '3')
2562 if (NULL
== goodsec
)
2563 goodsec
= "2, 3, 9";
2566 if (*mdoc
->meta
.msec
== '9')
2568 if (NULL
== goodsec
)
2570 mandoc_msg(MANDOCERR_SEC_MSEC
,
2571 mdoc
->last
->line
, mdoc
->last
->pos
,
2572 "Sh %s for %s only", secnames
[sec
], goodsec
);
2582 struct roff_node
*n
, *nch
;
2586 if (nch
->next
== NULL
) {
2587 mandoc_msg(MANDOCERR_XR_NOSEC
,
2588 n
->line
, n
->pos
, "Xr %s", nch
->string
);
2590 assert(nch
->next
== n
->last
);
2591 if(mandoc_xr_add(nch
->next
->string
, nch
->string
,
2592 nch
->line
, nch
->pos
))
2593 mandoc_msg(MANDOCERR_XR_SELF
,
2594 nch
->line
, nch
->pos
, "Xr %s %s",
2595 nch
->string
, nch
->next
->string
);
2597 post_delim_nb(mdoc
);
2601 post_section(POST_ARGS
)
2603 struct roff_node
*n
, *nch
;
2615 for (cp
= tag
; *cp
!= '\0'; cp
++)
2618 if ((nch
= n
->child
) != NULL
&&
2619 nch
->type
== ROFFT_TEXT
&&
2620 strcmp(nch
->string
, tag
) == 0)
2621 tag_put(NULL
, TAG_STRONG
, n
);
2623 tag_put(tag
, TAG_FALLBACK
, n
);
2634 if ((nch
= n
->child
) != NULL
&&
2635 (nch
->tok
== MDOC_Pp
|| nch
->tok
== ROFF_br
||
2636 nch
->tok
== ROFF_sp
)) {
2637 mandoc_msg(MANDOCERR_PAR_SKIP
, nch
->line
, nch
->pos
,
2638 "%s after %s", roff_name
[nch
->tok
],
2640 roff_node_delete(mdoc
, nch
);
2642 if ((nch
= n
->last
) != NULL
&&
2643 (nch
->tok
== MDOC_Pp
|| nch
->tok
== ROFF_br
)) {
2644 mandoc_msg(MANDOCERR_PAR_SKIP
, nch
->line
, nch
->pos
,
2645 "%s at the end of %s", roff_name
[nch
->tok
],
2647 roff_node_delete(mdoc
, nch
);
2652 post_prevpar(POST_ARGS
)
2654 struct roff_node
*n
, *np
;
2657 if (n
->type
!= ROFFT_ELEM
&& n
->type
!= ROFFT_BLOCK
)
2659 if ((np
= roff_node_prev(n
)) == NULL
)
2663 * Don't allow `Pp' prior to a paragraph-type
2664 * block: `Pp' or non-compact `Bd' or `Bl'.
2667 if (np
->tok
!= MDOC_Pp
&& np
->tok
!= ROFF_br
)
2669 if (n
->tok
== MDOC_Bl
&& n
->norm
->Bl
.comp
)
2671 if (n
->tok
== MDOC_Bd
&& n
->norm
->Bd
.comp
)
2673 if (n
->tok
== MDOC_It
&& n
->parent
->norm
->Bl
.comp
)
2676 mandoc_msg(MANDOCERR_PAR_SKIP
, np
->line
, np
->pos
,
2677 "%s before %s", roff_name
[np
->tok
], roff_name
[n
->tok
]);
2678 roff_node_delete(mdoc
, np
);
2684 struct roff_node
*np
;
2686 fn_prio
= TAG_STRONG
;
2690 if (np
->child
!= NULL
)
2691 mandoc_msg(MANDOCERR_ARG_SKIP
, np
->line
, np
->pos
,
2692 "%s %s", roff_name
[np
->tok
], np
->child
->string
);
2698 struct roff_node
*n
;
2701 n
->flags
|= NODE_NOPRT
;
2703 if (mdoc
->meta
.date
!= NULL
) {
2704 mandoc_msg(MANDOCERR_PROLOG_REP
, n
->line
, n
->pos
, "Dd");
2705 free(mdoc
->meta
.date
);
2706 } else if (mdoc
->flags
& MDOC_PBODY
)
2707 mandoc_msg(MANDOCERR_PROLOG_LATE
, n
->line
, n
->pos
, "Dd");
2708 else if (mdoc
->meta
.title
!= NULL
)
2709 mandoc_msg(MANDOCERR_PROLOG_ORDER
,
2710 n
->line
, n
->pos
, "Dd after Dt");
2711 else if (mdoc
->meta
.os
!= NULL
)
2712 mandoc_msg(MANDOCERR_PROLOG_ORDER
,
2713 n
->line
, n
->pos
, "Dd after Os");
2715 if (mdoc
->quick
&& n
!= NULL
)
2716 mdoc
->meta
.date
= mandoc_strdup("");
2718 mdoc
->meta
.date
= mandoc_normdate(n
->child
, n
);
2724 struct roff_node
*nn
, *n
;
2729 n
->flags
|= NODE_NOPRT
;
2731 if (mdoc
->flags
& MDOC_PBODY
) {
2732 mandoc_msg(MANDOCERR_DT_LATE
, n
->line
, n
->pos
, "Dt");
2736 if (mdoc
->meta
.title
!= NULL
)
2737 mandoc_msg(MANDOCERR_PROLOG_REP
, n
->line
, n
->pos
, "Dt");
2738 else if (mdoc
->meta
.os
!= NULL
)
2739 mandoc_msg(MANDOCERR_PROLOG_ORDER
,
2740 n
->line
, n
->pos
, "Dt after Os");
2742 free(mdoc
->meta
.title
);
2743 free(mdoc
->meta
.msec
);
2744 free(mdoc
->meta
.vol
);
2745 free(mdoc
->meta
.arch
);
2747 mdoc
->meta
.title
= NULL
;
2748 mdoc
->meta
.msec
= NULL
;
2749 mdoc
->meta
.vol
= NULL
;
2750 mdoc
->meta
.arch
= NULL
;
2752 /* Mandatory first argument: title. */
2755 if (nn
== NULL
|| *nn
->string
== '\0') {
2756 mandoc_msg(MANDOCERR_DT_NOTITLE
, n
->line
, n
->pos
, "Dt");
2757 mdoc
->meta
.title
= mandoc_strdup("UNTITLED");
2759 mdoc
->meta
.title
= mandoc_strdup(nn
->string
);
2761 /* Check that all characters are uppercase. */
2763 for (p
= nn
->string
; *p
!= '\0'; p
++)
2764 if (islower((unsigned char)*p
)) {
2765 mandoc_msg(MANDOCERR_TITLE_CASE
, nn
->line
,
2766 nn
->pos
+ (int)(p
- nn
->string
),
2767 "Dt %s", nn
->string
);
2772 /* Mandatory second argument: section. */
2778 mandoc_msg(MANDOCERR_MSEC_MISSING
, n
->line
, n
->pos
,
2779 "Dt %s", mdoc
->meta
.title
);
2780 mdoc
->meta
.vol
= mandoc_strdup("LOCAL");
2781 return; /* msec and arch remain NULL. */
2784 mdoc
->meta
.msec
= mandoc_strdup(nn
->string
);
2786 /* Infer volume title from section number. */
2788 cp
= mandoc_a2msec(nn
->string
);
2790 mandoc_msg(MANDOCERR_MSEC_BAD
,
2791 nn
->line
, nn
->pos
, "Dt ... %s", nn
->string
);
2792 mdoc
->meta
.vol
= mandoc_strdup(nn
->string
);
2794 mdoc
->meta
.vol
= mandoc_strdup(cp
);
2795 if (mdoc
->filesec
!= '\0' &&
2796 mdoc
->filesec
!= *nn
->string
&&
2797 *nn
->string
>= '1' && *nn
->string
<= '9')
2798 mandoc_msg(MANDOCERR_MSEC_FILE
, nn
->line
, nn
->pos
,
2799 "*.%c vs Dt ... %c", mdoc
->filesec
, *nn
->string
);
2802 /* Optional third argument: architecture. */
2804 if ((nn
= nn
->next
) == NULL
)
2807 for (p
= nn
->string
; *p
!= '\0'; p
++)
2808 *p
= tolower((unsigned char)*p
);
2809 mdoc
->meta
.arch
= mandoc_strdup(nn
->string
);
2811 /* Ignore fourth and later arguments. */
2813 if ((nn
= nn
->next
) != NULL
)
2814 mandoc_msg(MANDOCERR_ARG_EXCESS
,
2815 nn
->line
, nn
->pos
, "Dt ... %s", nn
->string
);
2821 struct roff_node
*n
, *nch
;
2824 post_delim_nb(mdoc
);
2830 macro
= !strcmp(nch
->string
, "Open") ? "Ox" :
2831 !strcmp(nch
->string
, "Net") ? "Nx" :
2832 !strcmp(nch
->string
, "Free") ? "Fx" :
2833 !strcmp(nch
->string
, "DragonFly") ? "Dx" : NULL
;
2835 mandoc_msg(MANDOCERR_BX
,
2836 n
->line
, n
->pos
, "%s", macro
);
2839 mdoc
->next
= ROFF_NEXT_SIBLING
;
2840 roff_elem_alloc(mdoc
, n
->line
, n
->pos
, MDOC_Ns
);
2841 mdoc
->last
->flags
|= NODE_NOSRC
;
2842 mdoc
->next
= ROFF_NEXT_SIBLING
;
2844 mdoc
->next
= ROFF_NEXT_CHILD
;
2845 roff_word_alloc(mdoc
, n
->line
, n
->pos
, "BSD");
2846 mdoc
->last
->flags
|= NODE_NOSRC
;
2853 roff_elem_alloc(mdoc
, n
->line
, n
->pos
, MDOC_Ns
);
2854 mdoc
->last
->flags
|= NODE_NOSRC
;
2855 mdoc
->next
= ROFF_NEXT_SIBLING
;
2856 roff_word_alloc(mdoc
, n
->line
, n
->pos
, "-");
2857 mdoc
->last
->flags
|= NODE_NOSRC
;
2858 roff_elem_alloc(mdoc
, n
->line
, n
->pos
, MDOC_Ns
);
2859 mdoc
->last
->flags
|= NODE_NOSRC
;
2863 * Make `Bx's second argument always start with an uppercase
2864 * letter. Groff checks if it's an "accepted" term, but we just
2865 * uppercase blindly.
2868 *nch
->string
= (char)toupper((unsigned char)*nch
->string
);
2875 struct utsname utsname
;
2877 struct roff_node
*n
;
2880 n
->flags
|= NODE_NOPRT
;
2882 if (mdoc
->meta
.os
!= NULL
)
2883 mandoc_msg(MANDOCERR_PROLOG_REP
, n
->line
, n
->pos
, "Os");
2884 else if (mdoc
->flags
& MDOC_PBODY
)
2885 mandoc_msg(MANDOCERR_PROLOG_LATE
, n
->line
, n
->pos
, "Os");
2890 * Set the operating system by way of the `Os' macro.
2891 * The order of precedence is:
2892 * 1. the argument of the `Os' macro, unless empty
2893 * 2. the -Ios=foo command line argument, if provided
2894 * 3. -DOSNAME="\"foo\"", if provided during compilation
2895 * 4. "sysname release" from uname(3)
2898 free(mdoc
->meta
.os
);
2899 mdoc
->meta
.os
= NULL
;
2900 deroff(&mdoc
->meta
.os
, n
);
2904 if (mdoc
->os_s
!= NULL
) {
2905 mdoc
->meta
.os
= mandoc_strdup(mdoc
->os_s
);
2910 mdoc
->meta
.os
= mandoc_strdup(OSNAME
);
2912 if (mdoc
->os_r
== NULL
) {
2913 if (uname(&utsname
) == -1) {
2914 mandoc_msg(MANDOCERR_OS_UNAME
, n
->line
, n
->pos
, "Os");
2915 mdoc
->os_r
= mandoc_strdup("UNKNOWN");
2917 mandoc_asprintf(&mdoc
->os_r
, "%s %s",
2918 utsname
.sysname
, utsname
.release
);
2920 mdoc
->meta
.os
= mandoc_strdup(mdoc
->os_r
);
2924 if (mdoc
->meta
.os_e
== MANDOC_OS_OTHER
) {
2925 if (strstr(mdoc
->meta
.os
, "OpenBSD") != NULL
)
2926 mdoc
->meta
.os_e
= MANDOC_OS_OPENBSD
;
2927 else if (strstr(mdoc
->meta
.os
, "NetBSD") != NULL
)
2928 mdoc
->meta
.os_e
= MANDOC_OS_NETBSD
;
2932 * This is the earliest point where we can check
2933 * Mdocdate conventions because we don't know
2934 * the operating system earlier.
2937 if (n
->child
!= NULL
)
2938 mandoc_msg(MANDOCERR_OS_ARG
, n
->child
->line
, n
->child
->pos
,
2939 "Os %s (%s)", n
->child
->string
,
2940 mdoc
->meta
.os_e
== MANDOC_OS_OPENBSD
?
2941 "OpenBSD" : "NetBSD");
2943 while (n
->tok
!= MDOC_Dd
)
2944 if ((n
= n
->prev
) == NULL
)
2946 if ((n
= n
->child
) == NULL
)
2948 if (strncmp(n
->string
, "$" "Mdocdate", 9)) {
2949 if (mdoc
->meta
.os_e
== MANDOC_OS_OPENBSD
)
2950 mandoc_msg(MANDOCERR_MDOCDATE_MISSING
, n
->line
,
2951 n
->pos
, "Dd %s (OpenBSD)", n
->string
);
2953 if (mdoc
->meta
.os_e
== MANDOC_OS_NETBSD
)
2954 mandoc_msg(MANDOCERR_MDOCDATE
, n
->line
,
2955 n
->pos
, "Dd %s (NetBSD)", n
->string
);
2960 mdoc_a2sec(const char *p
)
2964 for (i
= 0; i
< (int)SEC__MAX
; i
++)
2965 if (secnames
[i
] && 0 == strcmp(p
, secnames
[i
]))
2966 return (enum roff_sec
)i
;
2972 macro2len(enum roff_tok macro
)