1 /* $Id: mdoc_validate.c,v 1.381 2020/04/01 20:21:08 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"
42 #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
*nt
; /* The TEXT node containing the tag. */
1109 size_t len
; /* The number of bytes in the tag. */
1111 /* Find the next node. */
1113 for (nn
= n
; nn
!= NULL
; nn
= nn
->parent
) {
1114 if (nn
->next
!= NULL
) {
1121 nt
= nch
= n
->child
;
1122 if (nch
== NULL
&& nn
!= NULL
&& nn
->child
!= NULL
&&
1123 nn
->child
->type
== ROFFT_TEXT
)
1126 /* Validate the tag. */
1127 if (nt
== NULL
|| *nt
->string
== '\0')
1128 mandoc_msg(MANDOCERR_MACRO_EMPTY
, n
->line
, n
->pos
, "Tg");
1130 roff_node_delete(mdoc
, n
);
1133 len
= strcspn(nt
->string
, " \t\\");
1134 if (nt
->string
[len
] != '\0')
1135 mandoc_msg(MANDOCERR_TG_SPC
, nt
->line
,
1136 nt
->pos
+ len
, "Tg %s", nt
->string
);
1138 /* Keep only the first argument. */
1139 if (nch
!= NULL
&& nch
->next
!= NULL
) {
1140 mandoc_msg(MANDOCERR_ARG_EXCESS
, nch
->next
->line
,
1141 nch
->next
->pos
, "Tg ... %s", nch
->next
->string
);
1142 while (nch
->next
!= NULL
)
1143 roff_node_delete(mdoc
, nch
->next
);
1146 /* Drop the macro if the first argument is invalid. */
1147 if (len
== 0 || nt
->string
[len
] != '\0') {
1148 roff_node_delete(mdoc
, n
);
1152 /* By default, tag the .Tg node itself. */
1156 /* Explicit tagging of specific macros. */
1175 if (nn
->child
!= NULL
&& (nn
->flags
& NODE_ID
) == 0)
1182 tag_put(nt
->string
, TAG_MANUAL
, nn
);
1184 n
->flags
|= NODE_NOPRT
;
1188 post_obsolete(POST_ARGS
)
1190 struct roff_node
*n
;
1193 if (n
->type
== ROFFT_ELEM
|| n
->type
== ROFFT_BLOCK
)
1194 mandoc_msg(MANDOCERR_MACRO_OBS
, n
->line
, n
->pos
,
1195 "%s", roff_name
[n
->tok
]);
1199 post_useless(POST_ARGS
)
1201 struct roff_node
*n
;
1204 mandoc_msg(MANDOCERR_MACRO_USELESS
, n
->line
, n
->pos
,
1205 "%s", roff_name
[n
->tok
]);
1215 struct roff_node
*np
, *nch
;
1218 * Unlike other data pointers, these are "housed" by the HEAD
1219 * element, which contains the goods.
1223 if (np
->type
!= ROFFT_HEAD
)
1226 assert(np
->parent
->type
== ROFFT_BLOCK
);
1227 assert(np
->parent
->tok
== MDOC_Bf
);
1229 /* Check the number of arguments. */
1232 if (np
->parent
->args
== NULL
) {
1234 mandoc_msg(MANDOCERR_BF_NOFONT
,
1235 np
->line
, np
->pos
, "Bf");
1241 mandoc_msg(MANDOCERR_ARG_EXCESS
,
1242 nch
->line
, nch
->pos
, "Bf ... %s", nch
->string
);
1244 /* Extract argument into data. */
1246 if (np
->parent
->args
!= NULL
) {
1247 switch (np
->parent
->args
->argv
[0].arg
) {
1249 np
->norm
->Bf
.font
= FONT_Em
;
1252 np
->norm
->Bf
.font
= FONT_Li
;
1255 np
->norm
->Bf
.font
= FONT_Sy
;
1263 /* Extract parameter into data. */
1265 if ( ! strcmp(np
->child
->string
, "Em"))
1266 np
->norm
->Bf
.font
= FONT_Em
;
1267 else if ( ! strcmp(np
->child
->string
, "Li"))
1268 np
->norm
->Bf
.font
= FONT_Li
;
1269 else if ( ! strcmp(np
->child
->string
, "Sy"))
1270 np
->norm
->Bf
.font
= FONT_Sy
;
1272 mandoc_msg(MANDOCERR_BF_BADFONT
, np
->child
->line
,
1273 np
->child
->pos
, "Bf %s", np
->child
->string
);
1277 post_fname(POST_ARGS
)
1279 struct roff_node
*n
, *nch
;
1287 if (cp
[strlen(cp
+ 1)] == ')')
1291 pos
= strcspn(cp
, "()");
1292 if (cp
[pos
] == '\0') {
1293 if (n
->sec
== SEC_DESCRIPTION
||
1294 n
->sec
== SEC_CUSTOM
)
1295 tag_put(NULL
, fn_prio
++, n
);
1299 mandoc_msg(MANDOCERR_FN_PAREN
, nch
->line
, nch
->pos
+ pos
, "%s", cp
);
1312 const struct roff_node
*n
;
1316 if (n
->type
!= ROFFT_HEAD
)
1319 if (n
->child
== NULL
) {
1320 mandoc_msg(MANDOCERR_FO_NOHEAD
, n
->line
, n
->pos
, "Fo");
1323 if (n
->child
!= n
->last
) {
1324 mandoc_msg(MANDOCERR_ARG_EXCESS
,
1325 n
->child
->next
->line
, n
->child
->next
->pos
,
1326 "Fo ... %s", n
->child
->next
->string
);
1327 while (n
->child
!= n
->last
)
1328 roff_node_delete(mdoc
, n
->last
);
1338 const struct roff_node
*n
;
1341 for (n
= mdoc
->last
->child
; n
!= NULL
; n
= n
->next
) {
1342 for (cp
= n
->string
; *cp
!= '\0'; cp
++) {
1343 /* Ignore callbacks and alterations. */
1344 if (*cp
== '(' || *cp
== '{')
1348 mandoc_msg(MANDOCERR_FA_COMMA
, n
->line
,
1349 n
->pos
+ (int)(cp
- n
->string
), "%s", n
->string
);
1353 post_delim_nb(mdoc
);
1359 struct roff_node
*n
;
1363 if (n
->sec
== SEC_NAME
&& n
->child
!= NULL
&&
1364 n
->child
->type
== ROFFT_TEXT
&& mdoc
->meta
.msec
!= NULL
)
1365 mandoc_xr_add(mdoc
->meta
.msec
, n
->child
->string
, -1, -1);
1367 if (n
->last
!= NULL
&& n
->last
->tok
== MDOC_Pp
)
1368 roff_node_relink(mdoc
, n
->last
);
1370 if (mdoc
->meta
.name
== NULL
)
1371 deroff(&mdoc
->meta
.name
, n
);
1373 if (mdoc
->meta
.name
== NULL
||
1374 (mdoc
->lastsec
== SEC_NAME
&& n
->child
== NULL
))
1375 mandoc_msg(MANDOCERR_NM_NONAME
, n
->line
, n
->pos
, "Nm");
1379 post_delim_nb(mdoc
);
1388 if ((n
->child
!= NULL
&& n
->child
->type
== ROFFT_TEXT
) ||
1389 mdoc
->meta
.name
== NULL
)
1392 mdoc
->next
= ROFF_NEXT_CHILD
;
1393 roff_word_alloc(mdoc
, n
->line
, n
->pos
, mdoc
->meta
.name
);
1394 mdoc
->last
->flags
|= NODE_NOSRC
;
1401 struct roff_node
*n
;
1405 if (n
->type
!= ROFFT_BODY
)
1408 if (n
->sec
!= SEC_NAME
)
1409 mandoc_msg(MANDOCERR_ND_LATE
, n
->line
, n
->pos
, "Nd");
1411 if (n
->child
== NULL
)
1412 mandoc_msg(MANDOCERR_ND_EMPTY
, n
->line
, n
->pos
, "Nd");
1420 post_display(POST_ARGS
)
1422 struct roff_node
*n
, *np
;
1427 if (n
->end
!= ENDBODY_NOT
) {
1428 if (n
->tok
== MDOC_Bd
&&
1429 n
->body
->parent
->args
== NULL
)
1430 roff_node_delete(mdoc
, n
);
1431 } else if (n
->child
== NULL
)
1432 mandoc_msg(MANDOCERR_BLK_EMPTY
, n
->line
, n
->pos
,
1433 "%s", roff_name
[n
->tok
]);
1434 else if (n
->tok
== MDOC_D1
)
1438 if (n
->tok
== MDOC_Bd
) {
1439 if (n
->args
== NULL
) {
1440 mandoc_msg(MANDOCERR_BD_NOARG
,
1441 n
->line
, n
->pos
, "Bd");
1442 mdoc
->next
= ROFF_NEXT_SIBLING
;
1443 while (n
->body
->child
!= NULL
)
1444 roff_node_relink(mdoc
,
1446 roff_node_delete(mdoc
, n
);
1452 for (np
= n
->parent
; np
!= NULL
; np
= np
->parent
) {
1453 if (np
->type
== ROFFT_BLOCK
&& np
->tok
== MDOC_Bd
) {
1454 mandoc_msg(MANDOCERR_BD_NEST
, n
->line
,
1455 n
->pos
, "%s in Bd", roff_name
[n
->tok
]);
1466 post_defaults(POST_ARGS
)
1468 struct roff_node
*n
;
1471 if (n
->child
!= NULL
) {
1472 post_delim_nb(mdoc
);
1475 mdoc
->next
= ROFF_NEXT_CHILD
;
1478 roff_word_alloc(mdoc
, n
->line
, n
->pos
, "file");
1479 mdoc
->last
->flags
|= NODE_NOSRC
;
1480 roff_word_alloc(mdoc
, n
->line
, n
->pos
, "...");
1484 roff_word_alloc(mdoc
, n
->line
, n
->pos
, "~");
1489 mdoc
->last
->flags
|= NODE_NOSRC
;
1496 struct roff_node
*n
, *nch
;
1503 * If we have a child, look it up in the standard keys. If a
1504 * key exist, use that instead of the child; if it doesn't,
1505 * prefix "AT&T UNIX " to the existing data.
1509 if (nch
!= NULL
&& ((att
= mdoc_a2att(nch
->string
)) == NULL
))
1510 mandoc_msg(MANDOCERR_AT_BAD
,
1511 nch
->line
, nch
->pos
, "At %s", nch
->string
);
1513 mdoc
->next
= ROFF_NEXT_CHILD
;
1515 roff_word_alloc(mdoc
, nch
->line
, nch
->pos
, att
);
1516 nch
->flags
|= NODE_NOPRT
;
1518 roff_word_alloc(mdoc
, n
->line
, n
->pos
, "AT&T UNIX");
1519 mdoc
->last
->flags
|= NODE_NOSRC
;
1526 struct roff_node
*np
, *nch
;
1532 if (np
->norm
->An
.auth
== AUTH__NONE
) {
1534 mandoc_msg(MANDOCERR_MACRO_EMPTY
,
1535 np
->line
, np
->pos
, "An");
1537 post_delim_nb(mdoc
);
1538 } else if (nch
!= NULL
)
1539 mandoc_msg(MANDOCERR_ARG_EXCESS
,
1540 nch
->line
, nch
->pos
, "An ... %s", nch
->string
);
1547 tag_put(NULL
, TAG_FALLBACK
, mdoc
->last
);
1553 post_obsolete(mdoc
);
1554 if (mdoc
->last
->type
== ROFFT_BLOCK
)
1555 mdoc
->last
->norm
->Es
= mdoc
->last_es
;
1561 struct roff_node
*n
;
1564 if (n
->sec
== SEC_ERRORS
&&
1565 (n
->parent
->tok
== MDOC_It
||
1566 (n
->parent
->tok
== MDOC_Bq
&&
1567 n
->parent
->parent
->parent
->tok
== MDOC_It
)))
1568 tag_put(NULL
, TAG_STRONG
, n
);
1569 post_delim_nb(mdoc
);
1575 struct roff_node
*n
;
1578 if ((n
->prev
== NULL
||
1579 (n
->prev
->type
== ROFFT_TEXT
&&
1580 strcmp(n
->prev
->string
, "|") == 0)) &&
1581 (n
->parent
->tok
== MDOC_It
||
1582 (n
->parent
->tok
== MDOC_Xo
&&
1583 n
->parent
->parent
->prev
== NULL
&&
1584 n
->parent
->parent
->parent
->tok
== MDOC_It
)))
1585 tag_put(NULL
, TAG_STRONG
, n
);
1586 post_delim_nb(mdoc
);
1592 post_obsolete(mdoc
);
1593 mdoc
->last_es
= mdoc
->last
;
1599 struct roff_node
*n
;
1603 post_delim_nb(mdoc
);
1618 if (n
->child
== NULL
)
1620 v
= n
->child
->string
;
1621 if ((v
[0] != '0' && v
[0] != '1') || v
[1] != '.' ||
1622 v
[2] < '0' || v
[2] > '9' ||
1623 v
[3] < 'a' || v
[3] > 'z' || v
[4] != '\0')
1625 n
->child
->flags
|= NODE_NOPRT
;
1626 mdoc
->next
= ROFF_NEXT_CHILD
;
1627 roff_word_alloc(mdoc
, n
->child
->line
, n
->child
->pos
, v
);
1628 v
= mdoc
->last
->string
;
1629 v
[3] = toupper((unsigned char)v
[3]);
1630 mdoc
->last
->flags
|= NODE_NOSRC
;
1642 mdoc
->next
= ROFF_NEXT_CHILD
;
1643 roff_word_alloc(mdoc
, n
->line
, n
->pos
, os
);
1644 mdoc
->last
->flags
|= NODE_NOSRC
;
1651 struct roff_node
*nbl
, *nit
, *nch
;
1658 if (nit
->type
!= ROFFT_BLOCK
)
1661 nbl
= nit
->parent
->parent
;
1662 lt
= nbl
->norm
->Bl
.type
;
1670 if (nit
->head
->child
== NULL
)
1671 mandoc_msg(MANDOCERR_IT_NOHEAD
,
1672 nit
->line
, nit
->pos
, "Bl -%s It",
1673 mdoc_argnames
[nbl
->args
->argv
[0].arg
]);
1679 if (nit
->body
== NULL
|| nit
->body
->child
== NULL
)
1680 mandoc_msg(MANDOCERR_IT_NOBODY
,
1681 nit
->line
, nit
->pos
, "Bl -%s It",
1682 mdoc_argnames
[nbl
->args
->argv
[0].arg
]);
1685 if ((nch
= nit
->head
->child
) != NULL
)
1686 mandoc_msg(MANDOCERR_ARG_SKIP
,
1687 nit
->line
, nit
->pos
, "It %s",
1688 nch
->type
== ROFFT_TEXT
? nch
->string
:
1689 roff_name
[nch
->tok
]);
1692 cols
= (int)nbl
->norm
->Bl
.ncols
;
1694 assert(nit
->head
->child
== NULL
);
1696 if (nit
->head
->next
->child
== NULL
&&
1697 nit
->head
->next
->next
== NULL
) {
1698 mandoc_msg(MANDOCERR_MACRO_EMPTY
,
1699 nit
->line
, nit
->pos
, "It");
1700 roff_node_delete(mdoc
, nit
);
1705 for (nch
= nit
->child
; nch
!= NULL
; nch
= nch
->next
) {
1706 if (nch
->type
!= ROFFT_BODY
)
1708 if (i
++ && nch
->flags
& NODE_LINE
)
1709 mandoc_msg(MANDOCERR_TA_LINE
,
1710 nch
->line
, nch
->pos
, "Ta");
1712 if (i
< cols
|| i
> cols
+ 1)
1713 mandoc_msg(MANDOCERR_BL_COL
, nit
->line
, nit
->pos
,
1714 "%d columns, %d cells", cols
, i
);
1715 else if (nit
->head
->next
->child
!= NULL
&&
1716 nit
->head
->next
->child
->flags
& NODE_LINE
)
1717 mandoc_msg(MANDOCERR_IT_NOARG
,
1718 nit
->line
, nit
->pos
, "Bl -column It");
1726 post_bl_block(POST_ARGS
)
1728 struct roff_node
*n
, *ni
, *nc
;
1733 for (ni
= n
->body
->child
; ni
!= NULL
; ni
= ni
->next
) {
1734 if (ni
->body
== NULL
)
1736 nc
= ni
->body
->last
;
1737 while (nc
!= NULL
) {
1746 if (ni
->next
== NULL
) {
1747 mandoc_msg(MANDOCERR_PAR_MOVE
, nc
->line
,
1748 nc
->pos
, "%s", roff_name
[nc
->tok
]);
1749 roff_node_relink(mdoc
, nc
);
1750 } else if (n
->norm
->Bl
.comp
== 0 &&
1751 n
->norm
->Bl
.type
!= LIST_column
) {
1752 mandoc_msg(MANDOCERR_PAR_SKIP
,
1754 "%s before It", roff_name
[nc
->tok
]);
1755 roff_node_delete(mdoc
, nc
);
1758 nc
= ni
->body
->last
;
1764 * If the argument of -offset or -width is a macro,
1765 * replace it with the associated default width.
1768 rewrite_macro2len(struct roff_man
*mdoc
, char **arg
)
1775 else if ( ! strcmp(*arg
, "Ds"))
1777 else if ((tok
= roffhash_find(mdoc
->mdocmac
, *arg
, 0)) == TOKEN_NONE
)
1780 width
= macro2len(tok
);
1783 mandoc_asprintf(arg
, "%zun", width
);
1787 post_bl_head(POST_ARGS
)
1789 struct roff_node
*nbl
, *nh
, *nch
, *nnext
;
1790 struct mdoc_argv
*argv
;
1796 if (nh
->norm
->Bl
.type
!= LIST_column
) {
1797 if ((nch
= nh
->child
) == NULL
)
1799 mandoc_msg(MANDOCERR_ARG_EXCESS
,
1800 nch
->line
, nch
->pos
, "Bl ... %s", nch
->string
);
1801 while (nch
!= NULL
) {
1802 roff_node_delete(mdoc
, nch
);
1809 * Append old-style lists, where the column width specifiers
1810 * trail as macro parameters, to the new-style ("normal-form")
1811 * lists where they're argument values following -column.
1814 if (nh
->child
== NULL
)
1818 for (j
= 0; j
< (int)nbl
->args
->argc
; j
++)
1819 if (nbl
->args
->argv
[j
].arg
== MDOC_Column
)
1822 assert(j
< (int)nbl
->args
->argc
);
1825 * Accommodate for new-style groff column syntax. Shuffle the
1826 * child nodes, all of which must be TEXT, as arguments for the
1827 * column field. Then, delete the head children.
1830 argv
= nbl
->args
->argv
+ j
;
1832 for (nch
= nh
->child
; nch
!= NULL
; nch
= nch
->next
)
1834 argv
->value
= mandoc_reallocarray(argv
->value
,
1835 argv
->sz
, sizeof(char *));
1837 nh
->norm
->Bl
.ncols
= argv
->sz
;
1838 nh
->norm
->Bl
.cols
= (void *)argv
->value
;
1840 for (nch
= nh
->child
; nch
!= NULL
; nch
= nnext
) {
1841 argv
->value
[i
++] = nch
->string
;
1844 roff_node_delete(NULL
, nch
);
1852 struct roff_node
*nbody
; /* of the Bl */
1853 struct roff_node
*nchild
, *nnext
; /* of the Bl body */
1854 const char *prev_Er
;
1858 switch (nbody
->type
) {
1860 post_bl_block(mdoc
);
1870 if (nbody
->end
!= ENDBODY_NOT
)
1874 * Up to the first item, move nodes before the list,
1875 * but leave transparent nodes where they are
1876 * if they precede an item.
1877 * The next non-transparent node is kept in nchild.
1878 * It only needs to be updated after a non-transparent
1879 * node was moved out, and at the very beginning
1880 * when no node at all was moved yet.
1883 nchild
= mdoc
->last
;
1885 if (nchild
== mdoc
->last
)
1886 nchild
= roff_node_child(nbody
);
1887 if (nchild
== NULL
) {
1889 mandoc_msg(MANDOCERR_BLK_EMPTY
,
1890 nbody
->line
, nbody
->pos
, "Bl");
1893 if (nchild
->tok
== MDOC_It
) {
1897 mandoc_msg(MANDOCERR_BL_MOVE
, nbody
->child
->line
,
1898 nbody
->child
->pos
, "%s", roff_name
[nbody
->child
->tok
]);
1899 if (nbody
->parent
->prev
== NULL
) {
1900 mdoc
->last
= nbody
->parent
->parent
;
1901 mdoc
->next
= ROFF_NEXT_CHILD
;
1903 mdoc
->last
= nbody
->parent
->prev
;
1904 mdoc
->next
= ROFF_NEXT_SIBLING
;
1906 roff_node_relink(mdoc
, nbody
->child
);
1910 * We have reached the first item,
1911 * so moving nodes out is no longer possible.
1912 * But in .Bl -column, the first rows may be implicit,
1913 * that is, they may not start with .It macros.
1914 * Such rows may be followed by nodes generated on the
1915 * roff level, for example .TS.
1916 * Wrap such roff nodes into an implicit row.
1919 while (nchild
!= NULL
) {
1920 if (nchild
->tok
== MDOC_It
) {
1921 nchild
= roff_node_next(nchild
);
1924 nnext
= nchild
->next
;
1925 mdoc
->last
= nchild
->prev
;
1926 mdoc
->next
= ROFF_NEXT_SIBLING
;
1927 roff_block_alloc(mdoc
, nchild
->line
, nchild
->pos
, MDOC_It
);
1928 roff_head_alloc(mdoc
, nchild
->line
, nchild
->pos
, MDOC_It
);
1929 mdoc
->next
= ROFF_NEXT_SIBLING
;
1930 roff_body_alloc(mdoc
, nchild
->line
, nchild
->pos
, MDOC_It
);
1931 while (nchild
->tok
!= MDOC_It
) {
1932 roff_node_relink(mdoc
, nchild
);
1936 nnext
= nchild
->next
;
1937 mdoc
->next
= ROFF_NEXT_SIBLING
;
1942 if (mdoc
->meta
.os_e
!= MANDOC_OS_NETBSD
)
1946 for (nchild
= nbody
->child
; nchild
!= NULL
; nchild
= nchild
->next
) {
1947 if (nchild
->tok
!= MDOC_It
)
1949 if ((nnext
= nchild
->head
->child
) == NULL
)
1951 if (nnext
->type
== ROFFT_BLOCK
)
1952 nnext
= nnext
->body
->child
;
1953 if (nnext
== NULL
|| nnext
->tok
!= MDOC_Er
)
1955 nnext
= nnext
->child
;
1956 if (prev_Er
!= NULL
) {
1957 order
= strcmp(prev_Er
, nnext
->string
);
1959 mandoc_msg(MANDOCERR_ER_ORDER
,
1960 nnext
->line
, nnext
->pos
,
1961 "Er %s %s (NetBSD)",
1962 prev_Er
, nnext
->string
);
1963 else if (order
== 0)
1964 mandoc_msg(MANDOCERR_ER_REP
,
1965 nnext
->line
, nnext
->pos
,
1966 "Er %s (NetBSD)", prev_Er
);
1968 prev_Er
= nnext
->string
;
1975 struct roff_node
*n
;
1979 if (n
->type
== ROFFT_BLOCK
&& n
->body
->child
== NULL
) {
1980 mandoc_msg(MANDOCERR_BLK_EMPTY
, n
->line
, n
->pos
, "Bk");
1981 roff_node_delete(mdoc
, n
);
1988 struct roff_node
*nch
;
1990 nch
= mdoc
->last
->child
;
1993 mdoc
->flags
^= MDOC_SMOFF
;
1997 assert(nch
->type
== ROFFT_TEXT
);
1999 if ( ! strcmp(nch
->string
, "on")) {
2000 mdoc
->flags
&= ~MDOC_SMOFF
;
2003 if ( ! strcmp(nch
->string
, "off")) {
2004 mdoc
->flags
|= MDOC_SMOFF
;
2008 mandoc_msg(MANDOCERR_SM_BAD
, nch
->line
, nch
->pos
,
2009 "%s %s", roff_name
[mdoc
->last
->tok
], nch
->string
);
2010 roff_node_relink(mdoc
, nch
);
2015 post_root(POST_ARGS
)
2017 struct roff_node
*n
;
2019 /* Add missing prologue data. */
2021 if (mdoc
->meta
.date
== NULL
)
2022 mdoc
->meta
.date
= mandoc_normdate(NULL
, NULL
);
2024 if (mdoc
->meta
.title
== NULL
) {
2025 mandoc_msg(MANDOCERR_DT_NOTITLE
, 0, 0, "EOF");
2026 mdoc
->meta
.title
= mandoc_strdup("UNTITLED");
2029 if (mdoc
->meta
.vol
== NULL
)
2030 mdoc
->meta
.vol
= mandoc_strdup("LOCAL");
2032 if (mdoc
->meta
.os
== NULL
) {
2033 mandoc_msg(MANDOCERR_OS_MISSING
, 0, 0, NULL
);
2034 mdoc
->meta
.os
= mandoc_strdup("");
2035 } else if (mdoc
->meta
.os_e
&&
2036 (mdoc
->meta
.rcsids
& (1 << mdoc
->meta
.os_e
)) == 0)
2037 mandoc_msg(MANDOCERR_RCS_MISSING
, 0, 0,
2038 mdoc
->meta
.os_e
== MANDOC_OS_OPENBSD
?
2039 "(OpenBSD)" : "(NetBSD)");
2041 if (mdoc
->meta
.arch
!= NULL
&&
2042 arch_valid(mdoc
->meta
.arch
, mdoc
->meta
.os_e
) == 0) {
2043 n
= mdoc
->meta
.first
->child
;
2044 while (n
->tok
!= MDOC_Dt
||
2046 n
->child
->next
== NULL
||
2047 n
->child
->next
->next
== NULL
)
2049 n
= n
->child
->next
->next
;
2050 mandoc_msg(MANDOCERR_ARCH_BAD
, n
->line
, n
->pos
,
2051 "Dt ... %s %s", mdoc
->meta
.arch
,
2052 mdoc
->meta
.os_e
== MANDOC_OS_OPENBSD
?
2053 "(OpenBSD)" : "(NetBSD)");
2056 /* Check that we begin with a proper `Sh'. */
2058 n
= mdoc
->meta
.first
->child
;
2060 (n
->type
== ROFFT_COMMENT
||
2061 (n
->tok
>= MDOC_Dd
&&
2062 mdoc_macro(n
->tok
)->flags
& MDOC_PROLOGUE
)))
2066 mandoc_msg(MANDOCERR_DOC_EMPTY
, 0, 0, NULL
);
2067 else if (n
->tok
!= MDOC_Sh
)
2068 mandoc_msg(MANDOCERR_SEC_BEFORE
, n
->line
, n
->pos
,
2069 "%s", roff_name
[n
->tok
]);
2075 struct roff_node
*np
, *nch
, *next
, *prev
;
2080 if (np
->type
!= ROFFT_BODY
)
2083 if (np
->child
== NULL
) {
2084 mandoc_msg(MANDOCERR_RS_EMPTY
, np
->line
, np
->pos
, "Rs");
2089 * The full `Rs' block needs special handling to order the
2090 * sub-elements according to `rsord'. Pick through each element
2091 * and correctly order it. This is an insertion sort.
2095 for (nch
= np
->child
->next
; nch
!= NULL
; nch
= next
) {
2096 /* Determine order number of this child. */
2097 for (i
= 0; i
< RSORD_MAX
; i
++)
2098 if (rsord
[i
] == nch
->tok
)
2101 if (i
== RSORD_MAX
) {
2102 mandoc_msg(MANDOCERR_RS_BAD
, nch
->line
, nch
->pos
,
2103 "%s", roff_name
[nch
->tok
]);
2105 } else if (nch
->tok
== MDOC__J
|| nch
->tok
== MDOC__B
)
2106 np
->norm
->Rs
.quote_T
++;
2109 * Remove this child from the chain. This somewhat
2110 * repeats roff_node_unlink(), but since we're
2111 * just re-ordering, there's no need for the
2112 * full unlink process.
2115 if ((next
= nch
->next
) != NULL
)
2116 next
->prev
= nch
->prev
;
2118 if ((prev
= nch
->prev
) != NULL
)
2119 prev
->next
= nch
->next
;
2121 nch
->prev
= nch
->next
= NULL
;
2124 * Scan back until we reach a node that's
2125 * to be ordered before this child.
2128 for ( ; prev
; prev
= prev
->prev
) {
2129 /* Determine order of `prev'. */
2130 for (j
= 0; j
< RSORD_MAX
; j
++)
2131 if (rsord
[j
] == prev
->tok
)
2141 * Set this child back into its correct place
2142 * in front of the `prev' node.
2148 np
->child
->prev
= nch
;
2149 nch
->next
= np
->child
;
2153 prev
->next
->prev
= nch
;
2154 nch
->next
= prev
->next
;
2161 * For some arguments of some macros,
2162 * convert all breakable hyphens into ASCII_HYPH.
2165 post_hyph(POST_ARGS
)
2167 struct roff_node
*nch
;
2170 for (nch
= mdoc
->last
->child
; nch
!= NULL
; nch
= nch
->next
) {
2171 if (nch
->type
!= ROFFT_TEXT
)
2176 while (*(++cp
) != '\0')
2178 isalpha((unsigned char)cp
[-1]) &&
2179 isalpha((unsigned char)cp
[1]))
2187 struct roff_node
*n
;
2190 if (n
->flags
& NODE_LINE
||
2191 (n
->next
!= NULL
&& n
->next
->flags
& NODE_DELIMC
))
2192 mandoc_msg(MANDOCERR_NS_SKIP
, n
->line
, n
->pos
, NULL
);
2207 switch (mdoc
->last
->type
) {
2212 switch (mdoc
->lastsec
) {
2217 post_sh_see_also(mdoc
);
2220 post_sh_authors(mdoc
);
2232 post_sh_name(POST_ARGS
)
2234 struct roff_node
*n
;
2239 for (n
= mdoc
->last
->child
; n
!= NULL
; n
= n
->next
) {
2242 if (hasnm
&& n
->child
!= NULL
)
2243 mandoc_msg(MANDOCERR_NAMESEC_PUNCT
,
2245 "Nm %s", n
->child
->string
);
2250 if (n
->next
!= NULL
)
2251 mandoc_msg(MANDOCERR_NAMESEC_ND
,
2252 n
->line
, n
->pos
, NULL
);
2255 if (n
->type
== ROFFT_TEXT
&&
2256 n
->string
[0] == ',' && n
->string
[1] == '\0' &&
2257 n
->next
!= NULL
&& n
->next
->tok
== MDOC_Nm
) {
2263 mandoc_msg(MANDOCERR_NAMESEC_BAD
,
2264 n
->line
, n
->pos
, "%s", roff_name
[n
->tok
]);
2271 mandoc_msg(MANDOCERR_NAMESEC_NONM
,
2272 mdoc
->last
->line
, mdoc
->last
->pos
, NULL
);
2274 mandoc_msg(MANDOCERR_NAMESEC_NOND
,
2275 mdoc
->last
->line
, mdoc
->last
->pos
, NULL
);
2279 post_sh_see_also(POST_ARGS
)
2281 const struct roff_node
*n
;
2282 const char *name
, *sec
;
2283 const char *lastname
, *lastsec
, *lastpunct
;
2286 n
= mdoc
->last
->child
;
2287 lastname
= lastsec
= lastpunct
= NULL
;
2289 if (n
->tok
!= MDOC_Xr
||
2291 n
->child
->next
== NULL
)
2294 /* Process one .Xr node. */
2296 name
= n
->child
->string
;
2297 sec
= n
->child
->next
->string
;
2298 if (lastsec
!= NULL
) {
2299 if (lastpunct
[0] != ',' || lastpunct
[1] != '\0')
2300 mandoc_msg(MANDOCERR_XR_PUNCT
, n
->line
,
2301 n
->pos
, "%s before %s(%s)",
2302 lastpunct
, name
, sec
);
2303 cmp
= strcmp(lastsec
, sec
);
2305 mandoc_msg(MANDOCERR_XR_ORDER
, n
->line
,
2306 n
->pos
, "%s(%s) after %s(%s)",
2307 name
, sec
, lastname
, lastsec
);
2308 else if (cmp
== 0 &&
2309 strcasecmp(lastname
, name
) > 0)
2310 mandoc_msg(MANDOCERR_XR_ORDER
, n
->line
,
2311 n
->pos
, "%s after %s", name
, lastname
);
2316 /* Process the following node. */
2321 if (n
->tok
== MDOC_Xr
) {
2325 if (n
->type
!= ROFFT_TEXT
)
2327 for (name
= n
->string
; *name
!= '\0'; name
++)
2328 if (isalpha((const unsigned char)*name
))
2330 lastpunct
= n
->string
;
2331 if (n
->next
== NULL
|| n
->next
->tok
== MDOC_Rs
)
2332 mandoc_msg(MANDOCERR_XR_PUNCT
, n
->line
,
2333 n
->pos
, "%s after %s(%s)",
2334 lastpunct
, lastname
, lastsec
);
2340 child_an(const struct roff_node
*n
)
2343 for (n
= n
->child
; n
!= NULL
; n
= n
->next
)
2344 if ((n
->tok
== MDOC_An
&& n
->child
!= NULL
) || child_an(n
))
2350 post_sh_authors(POST_ARGS
)
2353 if ( ! child_an(mdoc
->last
))
2354 mandoc_msg(MANDOCERR_AN_MISSING
,
2355 mdoc
->last
->line
, mdoc
->last
->pos
, NULL
);
2359 * Return an upper bound for the string distance (allowing
2360 * transpositions). Not a full Levenshtein implementation
2361 * because Levenshtein is quadratic in the string length
2362 * and this function is called for every standard name,
2363 * so the check for each custom name would be cubic.
2364 * The following crude heuristics is linear, resulting
2365 * in quadratic behaviour for checking one custom name,
2366 * which does not cause measurable slowdown.
2369 similar(const char *s1
, const char *s2
)
2371 const int maxdist
= 3;
2374 while (s1
[0] != '\0' && s2
[0] != '\0') {
2375 if (s1
[0] == s2
[0]) {
2380 if (++dist
> maxdist
)
2382 if (s1
[1] == s2
[1]) { /* replacement */
2385 } else if (s1
[0] == s2
[1] && s1
[1] == s2
[0]) {
2386 s1
+= 2; /* transposition */
2388 } else if (s1
[0] == s2
[1]) /* insertion */
2390 else if (s1
[1] == s2
[0]) /* deletion */
2395 dist
+= strlen(s1
) + strlen(s2
);
2396 return dist
> maxdist
? INT_MAX
: dist
;
2400 post_sh_head(POST_ARGS
)
2402 struct roff_node
*nch
;
2403 const char *goodsec
;
2404 const char *const *testsec
;
2409 * Process a new section. Sections are either "named" or
2410 * "custom". Custom sections are user-defined, while named ones
2411 * follow a conventional order and may only appear in certain
2415 sec
= mdoc
->last
->sec
;
2417 /* The NAME should be first. */
2419 if (sec
!= SEC_NAME
&& mdoc
->lastnamed
== SEC_NONE
)
2420 mandoc_msg(MANDOCERR_NAMESEC_FIRST
,
2421 mdoc
->last
->line
, mdoc
->last
->pos
, "Sh %s",
2422 sec
!= SEC_CUSTOM
? secnames
[sec
] :
2423 (nch
= mdoc
->last
->child
) == NULL
? "" :
2424 nch
->type
== ROFFT_TEXT
? nch
->string
:
2425 roff_name
[nch
->tok
]);
2427 /* The SYNOPSIS gets special attention in other areas. */
2429 if (sec
== SEC_SYNOPSIS
) {
2430 roff_setreg(mdoc
->roff
, "nS", 1, '=');
2431 mdoc
->flags
|= MDOC_SYNOPSIS
;
2433 roff_setreg(mdoc
->roff
, "nS", 0, '=');
2434 mdoc
->flags
&= ~MDOC_SYNOPSIS
;
2436 if (sec
== SEC_DESCRIPTION
)
2437 fn_prio
= TAG_STRONG
;
2439 /* Mark our last section. */
2441 mdoc
->lastsec
= sec
;
2443 /* We don't care about custom sections after this. */
2445 if (sec
== SEC_CUSTOM
) {
2446 if ((nch
= mdoc
->last
->child
) == NULL
||
2447 nch
->type
!= ROFFT_TEXT
|| nch
->next
!= NULL
)
2451 for (testsec
= secnames
+ 1; *testsec
!= NULL
; testsec
++) {
2452 dist
= similar(nch
->string
, *testsec
);
2453 if (dist
< mindist
) {
2458 if (goodsec
!= NULL
)
2459 mandoc_msg(MANDOCERR_SEC_TYPO
, nch
->line
, nch
->pos
,
2460 "Sh %s instead of %s", nch
->string
, goodsec
);
2465 * Check whether our non-custom section is being repeated or is
2469 if (sec
== mdoc
->lastnamed
)
2470 mandoc_msg(MANDOCERR_SEC_REP
, mdoc
->last
->line
,
2471 mdoc
->last
->pos
, "Sh %s", secnames
[sec
]);
2473 if (sec
< mdoc
->lastnamed
)
2474 mandoc_msg(MANDOCERR_SEC_ORDER
, mdoc
->last
->line
,
2475 mdoc
->last
->pos
, "Sh %s", secnames
[sec
]);
2477 /* Mark the last named section. */
2479 mdoc
->lastnamed
= sec
;
2481 /* Check particular section/manual conventions. */
2483 if (mdoc
->meta
.msec
== NULL
)
2489 if (*mdoc
->meta
.msec
== '4')
2491 goodsec
= "2, 3, 4, 9";
2493 case SEC_RETURN_VALUES
:
2495 if (*mdoc
->meta
.msec
== '2')
2497 if (*mdoc
->meta
.msec
== '3')
2499 if (NULL
== goodsec
)
2500 goodsec
= "2, 3, 9";
2503 if (*mdoc
->meta
.msec
== '9')
2505 if (NULL
== goodsec
)
2507 mandoc_msg(MANDOCERR_SEC_MSEC
,
2508 mdoc
->last
->line
, mdoc
->last
->pos
,
2509 "Sh %s for %s only", secnames
[sec
], goodsec
);
2519 struct roff_node
*n
, *nch
;
2523 if (nch
->next
== NULL
) {
2524 mandoc_msg(MANDOCERR_XR_NOSEC
,
2525 n
->line
, n
->pos
, "Xr %s", nch
->string
);
2527 assert(nch
->next
== n
->last
);
2528 if(mandoc_xr_add(nch
->next
->string
, nch
->string
,
2529 nch
->line
, nch
->pos
))
2530 mandoc_msg(MANDOCERR_XR_SELF
,
2531 nch
->line
, nch
->pos
, "Xr %s %s",
2532 nch
->string
, nch
->next
->string
);
2534 post_delim_nb(mdoc
);
2538 post_section(POST_ARGS
)
2540 struct roff_node
*n
, *nch
;
2552 for (cp
= tag
; *cp
!= '\0'; cp
++)
2555 if ((nch
= n
->child
) != NULL
&&
2556 nch
->type
== ROFFT_TEXT
&&
2557 strcmp(nch
->string
, tag
) == 0)
2558 tag_put(NULL
, TAG_WEAK
, n
);
2560 tag_put(tag
, TAG_FALLBACK
, n
);
2571 if ((nch
= n
->child
) != NULL
&&
2572 (nch
->tok
== MDOC_Pp
|| nch
->tok
== ROFF_br
||
2573 nch
->tok
== ROFF_sp
)) {
2574 mandoc_msg(MANDOCERR_PAR_SKIP
, nch
->line
, nch
->pos
,
2575 "%s after %s", roff_name
[nch
->tok
],
2577 roff_node_delete(mdoc
, nch
);
2579 if ((nch
= n
->last
) != NULL
&&
2580 (nch
->tok
== MDOC_Pp
|| nch
->tok
== ROFF_br
)) {
2581 mandoc_msg(MANDOCERR_PAR_SKIP
, nch
->line
, nch
->pos
,
2582 "%s at the end of %s", roff_name
[nch
->tok
],
2584 roff_node_delete(mdoc
, nch
);
2589 post_prevpar(POST_ARGS
)
2591 struct roff_node
*n
, *np
;
2594 if (n
->type
!= ROFFT_ELEM
&& n
->type
!= ROFFT_BLOCK
)
2596 if ((np
= roff_node_prev(n
)) == NULL
)
2600 * Don't allow `Pp' prior to a paragraph-type
2601 * block: `Pp' or non-compact `Bd' or `Bl'.
2604 if (np
->tok
!= MDOC_Pp
&& np
->tok
!= ROFF_br
)
2606 if (n
->tok
== MDOC_Bl
&& n
->norm
->Bl
.comp
)
2608 if (n
->tok
== MDOC_Bd
&& n
->norm
->Bd
.comp
)
2610 if (n
->tok
== MDOC_It
&& n
->parent
->norm
->Bl
.comp
)
2613 mandoc_msg(MANDOCERR_PAR_SKIP
, np
->line
, np
->pos
,
2614 "%s before %s", roff_name
[np
->tok
], roff_name
[n
->tok
]);
2615 roff_node_delete(mdoc
, np
);
2621 struct roff_node
*np
;
2623 fn_prio
= TAG_STRONG
;
2627 if (np
->child
!= NULL
)
2628 mandoc_msg(MANDOCERR_ARG_SKIP
, np
->line
, np
->pos
,
2629 "%s %s", roff_name
[np
->tok
], np
->child
->string
);
2635 struct roff_node
*n
;
2638 n
->flags
|= NODE_NOPRT
;
2640 if (mdoc
->meta
.date
!= NULL
) {
2641 mandoc_msg(MANDOCERR_PROLOG_REP
, n
->line
, n
->pos
, "Dd");
2642 free(mdoc
->meta
.date
);
2643 } else if (mdoc
->flags
& MDOC_PBODY
)
2644 mandoc_msg(MANDOCERR_PROLOG_LATE
, n
->line
, n
->pos
, "Dd");
2645 else if (mdoc
->meta
.title
!= NULL
)
2646 mandoc_msg(MANDOCERR_PROLOG_ORDER
,
2647 n
->line
, n
->pos
, "Dd after Dt");
2648 else if (mdoc
->meta
.os
!= NULL
)
2649 mandoc_msg(MANDOCERR_PROLOG_ORDER
,
2650 n
->line
, n
->pos
, "Dd after Os");
2652 if (mdoc
->quick
&& n
!= NULL
)
2653 mdoc
->meta
.date
= mandoc_strdup("");
2655 mdoc
->meta
.date
= mandoc_normdate(n
->child
, n
);
2661 struct roff_node
*nn
, *n
;
2666 n
->flags
|= NODE_NOPRT
;
2668 if (mdoc
->flags
& MDOC_PBODY
) {
2669 mandoc_msg(MANDOCERR_DT_LATE
, n
->line
, n
->pos
, "Dt");
2673 if (mdoc
->meta
.title
!= NULL
)
2674 mandoc_msg(MANDOCERR_PROLOG_REP
, n
->line
, n
->pos
, "Dt");
2675 else if (mdoc
->meta
.os
!= NULL
)
2676 mandoc_msg(MANDOCERR_PROLOG_ORDER
,
2677 n
->line
, n
->pos
, "Dt after Os");
2679 free(mdoc
->meta
.title
);
2680 free(mdoc
->meta
.msec
);
2681 free(mdoc
->meta
.vol
);
2682 free(mdoc
->meta
.arch
);
2684 mdoc
->meta
.title
= NULL
;
2685 mdoc
->meta
.msec
= NULL
;
2686 mdoc
->meta
.vol
= NULL
;
2687 mdoc
->meta
.arch
= NULL
;
2689 /* Mandatory first argument: title. */
2692 if (nn
== NULL
|| *nn
->string
== '\0') {
2693 mandoc_msg(MANDOCERR_DT_NOTITLE
, n
->line
, n
->pos
, "Dt");
2694 mdoc
->meta
.title
= mandoc_strdup("UNTITLED");
2696 mdoc
->meta
.title
= mandoc_strdup(nn
->string
);
2698 /* Check that all characters are uppercase. */
2700 for (p
= nn
->string
; *p
!= '\0'; p
++)
2701 if (islower((unsigned char)*p
)) {
2702 mandoc_msg(MANDOCERR_TITLE_CASE
, nn
->line
,
2703 nn
->pos
+ (int)(p
- nn
->string
),
2704 "Dt %s", nn
->string
);
2709 /* Mandatory second argument: section. */
2715 mandoc_msg(MANDOCERR_MSEC_MISSING
, n
->line
, n
->pos
,
2716 "Dt %s", mdoc
->meta
.title
);
2717 mdoc
->meta
.vol
= mandoc_strdup("LOCAL");
2718 return; /* msec and arch remain NULL. */
2721 mdoc
->meta
.msec
= mandoc_strdup(nn
->string
);
2723 /* Infer volume title from section number. */
2725 cp
= mandoc_a2msec(nn
->string
);
2727 mandoc_msg(MANDOCERR_MSEC_BAD
,
2728 nn
->line
, nn
->pos
, "Dt ... %s", nn
->string
);
2729 mdoc
->meta
.vol
= mandoc_strdup(nn
->string
);
2731 mdoc
->meta
.vol
= mandoc_strdup(cp
);
2733 /* Optional third argument: architecture. */
2735 if ((nn
= nn
->next
) == NULL
)
2738 for (p
= nn
->string
; *p
!= '\0'; p
++)
2739 *p
= tolower((unsigned char)*p
);
2740 mdoc
->meta
.arch
= mandoc_strdup(nn
->string
);
2742 /* Ignore fourth and later arguments. */
2744 if ((nn
= nn
->next
) != NULL
)
2745 mandoc_msg(MANDOCERR_ARG_EXCESS
,
2746 nn
->line
, nn
->pos
, "Dt ... %s", nn
->string
);
2752 struct roff_node
*n
, *nch
;
2755 post_delim_nb(mdoc
);
2761 macro
= !strcmp(nch
->string
, "Open") ? "Ox" :
2762 !strcmp(nch
->string
, "Net") ? "Nx" :
2763 !strcmp(nch
->string
, "Free") ? "Fx" :
2764 !strcmp(nch
->string
, "DragonFly") ? "Dx" : NULL
;
2766 mandoc_msg(MANDOCERR_BX
,
2767 n
->line
, n
->pos
, "%s", macro
);
2770 mdoc
->next
= ROFF_NEXT_SIBLING
;
2771 roff_elem_alloc(mdoc
, n
->line
, n
->pos
, MDOC_Ns
);
2772 mdoc
->last
->flags
|= NODE_NOSRC
;
2773 mdoc
->next
= ROFF_NEXT_SIBLING
;
2775 mdoc
->next
= ROFF_NEXT_CHILD
;
2776 roff_word_alloc(mdoc
, n
->line
, n
->pos
, "BSD");
2777 mdoc
->last
->flags
|= NODE_NOSRC
;
2784 roff_elem_alloc(mdoc
, n
->line
, n
->pos
, MDOC_Ns
);
2785 mdoc
->last
->flags
|= NODE_NOSRC
;
2786 mdoc
->next
= ROFF_NEXT_SIBLING
;
2787 roff_word_alloc(mdoc
, n
->line
, n
->pos
, "-");
2788 mdoc
->last
->flags
|= NODE_NOSRC
;
2789 roff_elem_alloc(mdoc
, n
->line
, n
->pos
, MDOC_Ns
);
2790 mdoc
->last
->flags
|= NODE_NOSRC
;
2794 * Make `Bx's second argument always start with an uppercase
2795 * letter. Groff checks if it's an "accepted" term, but we just
2796 * uppercase blindly.
2799 *nch
->string
= (char)toupper((unsigned char)*nch
->string
);
2806 struct utsname utsname
;
2807 static char *defbuf
;
2809 struct roff_node
*n
;
2812 n
->flags
|= NODE_NOPRT
;
2814 if (mdoc
->meta
.os
!= NULL
)
2815 mandoc_msg(MANDOCERR_PROLOG_REP
, n
->line
, n
->pos
, "Os");
2816 else if (mdoc
->flags
& MDOC_PBODY
)
2817 mandoc_msg(MANDOCERR_PROLOG_LATE
, n
->line
, n
->pos
, "Os");
2822 * Set the operating system by way of the `Os' macro.
2823 * The order of precedence is:
2824 * 1. the argument of the `Os' macro, unless empty
2825 * 2. the -Ios=foo command line argument, if provided
2826 * 3. -DOSNAME="\"foo\"", if provided during compilation
2827 * 4. "sysname release" from uname(3)
2830 free(mdoc
->meta
.os
);
2831 mdoc
->meta
.os
= NULL
;
2832 deroff(&mdoc
->meta
.os
, n
);
2836 if (mdoc
->os_s
!= NULL
) {
2837 mdoc
->meta
.os
= mandoc_strdup(mdoc
->os_s
);
2842 mdoc
->meta
.os
= mandoc_strdup(OSNAME
);
2844 if (defbuf
== NULL
) {
2845 if (uname(&utsname
) == -1) {
2846 mandoc_msg(MANDOCERR_OS_UNAME
, n
->line
, n
->pos
, "Os");
2847 defbuf
= mandoc_strdup("UNKNOWN");
2849 mandoc_asprintf(&defbuf
, "%s %s",
2850 utsname
.sysname
, utsname
.release
);
2852 mdoc
->meta
.os
= mandoc_strdup(defbuf
);
2856 if (mdoc
->meta
.os_e
== MANDOC_OS_OTHER
) {
2857 if (strstr(mdoc
->meta
.os
, "OpenBSD") != NULL
)
2858 mdoc
->meta
.os_e
= MANDOC_OS_OPENBSD
;
2859 else if (strstr(mdoc
->meta
.os
, "NetBSD") != NULL
)
2860 mdoc
->meta
.os_e
= MANDOC_OS_NETBSD
;
2864 * This is the earliest point where we can check
2865 * Mdocdate conventions because we don't know
2866 * the operating system earlier.
2869 if (n
->child
!= NULL
)
2870 mandoc_msg(MANDOCERR_OS_ARG
, n
->child
->line
, n
->child
->pos
,
2871 "Os %s (%s)", n
->child
->string
,
2872 mdoc
->meta
.os_e
== MANDOC_OS_OPENBSD
?
2873 "OpenBSD" : "NetBSD");
2875 while (n
->tok
!= MDOC_Dd
)
2876 if ((n
= n
->prev
) == NULL
)
2878 if ((n
= n
->child
) == NULL
)
2880 if (strncmp(n
->string
, "$" "Mdocdate", 9)) {
2881 if (mdoc
->meta
.os_e
== MANDOC_OS_OPENBSD
)
2882 mandoc_msg(MANDOCERR_MDOCDATE_MISSING
, n
->line
,
2883 n
->pos
, "Dd %s (OpenBSD)", n
->string
);
2885 if (mdoc
->meta
.os_e
== MANDOC_OS_NETBSD
)
2886 mandoc_msg(MANDOCERR_MDOCDATE
, n
->line
,
2887 n
->pos
, "Dd %s (NetBSD)", n
->string
);
2892 mdoc_a2sec(const char *p
)
2896 for (i
= 0; i
< (int)SEC__MAX
; i
++)
2897 if (secnames
[i
] && 0 == strcmp(p
, secnames
[i
]))
2898 return (enum roff_sec
)i
;
2904 macro2len(enum roff_tok macro
)