1 /* $Id: mdoc_validate.c,v 1.378 2020/02/27 01:43:52 schwarze Exp $ */
3 * Copyright (c) 2008-2012 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2010-2020 Ingo Schwarze <schwarze@openbsd.org>
5 * Copyright (c) 2010 Joerg Sonnenberger <joerg@netbsd.org>
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 #include <sys/types.h>
23 #include <sys/utsname.h>
34 #include "mandoc_aux.h"
36 #include "mandoc_xr.h"
39 #include "libmandoc.h"
43 /* FIXME: .Bl -diag can't have non-text children in HEAD. */
45 #define POST_ARGS struct roff_man *mdoc
53 typedef void (*v_post
)(POST_ARGS
);
55 static int build_list(struct roff_man
*, int);
56 static void check_argv(struct roff_man
*,
57 struct roff_node
*, struct mdoc_argv
*);
58 static void check_args(struct roff_man
*, struct roff_node
*);
59 static void check_text(struct roff_man
*, int, int, char *);
60 static void check_text_em(struct roff_man
*, int, int, char *);
61 static void check_toptext(struct roff_man
*, int, int, const char *);
62 static int child_an(const struct roff_node
*);
63 static size_t macro2len(enum roff_tok
);
64 static void rewrite_macro2len(struct roff_man
*, char **);
65 static int similar(const char *, const char *);
67 static void post_abort(POST_ARGS
) __attribute__((__noreturn__
));
68 static void post_an(POST_ARGS
);
69 static void post_an_norm(POST_ARGS
);
70 static void post_at(POST_ARGS
);
71 static void post_bd(POST_ARGS
);
72 static void post_bf(POST_ARGS
);
73 static void post_bk(POST_ARGS
);
74 static void post_bl(POST_ARGS
);
75 static void post_bl_block(POST_ARGS
);
76 static void post_bl_head(POST_ARGS
);
77 static void post_bl_norm(POST_ARGS
);
78 static void post_bx(POST_ARGS
);
79 static void post_defaults(POST_ARGS
);
80 static void post_display(POST_ARGS
);
81 static void post_dd(POST_ARGS
);
82 static void post_delim(POST_ARGS
);
83 static void post_delim_nb(POST_ARGS
);
84 static void post_dt(POST_ARGS
);
85 static void post_en(POST_ARGS
);
86 static void post_es(POST_ARGS
);
87 static void post_eoln(POST_ARGS
);
88 static void post_ex(POST_ARGS
);
89 static void post_fa(POST_ARGS
);
90 static void post_fn(POST_ARGS
);
91 static void post_fname(POST_ARGS
);
92 static void post_fo(POST_ARGS
);
93 static void post_hyph(POST_ARGS
);
94 static void post_ignpar(POST_ARGS
);
95 static void post_it(POST_ARGS
);
96 static void post_lb(POST_ARGS
);
97 static void post_nd(POST_ARGS
);
98 static void post_nm(POST_ARGS
);
99 static void post_ns(POST_ARGS
);
100 static void post_obsolete(POST_ARGS
);
101 static void post_os(POST_ARGS
);
102 static void post_par(POST_ARGS
);
103 static void post_prevpar(POST_ARGS
);
104 static void post_root(POST_ARGS
);
105 static void post_rs(POST_ARGS
);
106 static void post_rv(POST_ARGS
);
107 static void post_sh(POST_ARGS
);
108 static void post_sh_head(POST_ARGS
);
109 static void post_sh_name(POST_ARGS
);
110 static void post_sh_see_also(POST_ARGS
);
111 static void post_sh_authors(POST_ARGS
);
112 static void post_sm(POST_ARGS
);
113 static void post_st(POST_ARGS
);
114 static void post_std(POST_ARGS
);
115 static void post_sx(POST_ARGS
);
116 static void post_tg(POST_ARGS
);
117 static void post_useless(POST_ARGS
);
118 static void post_xr(POST_ARGS
);
119 static void post_xx(POST_ARGS
);
121 static const v_post mdoc_valids
[MDOC_MAX
- MDOC_Dd
] = {
126 post_ignpar
, /* Ss */
128 post_display
, /* D1 */
129 post_display
, /* Dl */
130 post_display
, /* Bd */
135 post_delim_nb
, /* Ad */
138 post_defaults
, /* Ar */
140 post_delim_nb
, /* Cm */
141 post_delim_nb
, /* Dv */
142 post_delim_nb
, /* Er */
143 post_delim_nb
, /* Ev */
147 post_delim_nb
, /* Fl */
149 post_delim_nb
, /* Ft */
150 post_delim_nb
, /* Ic */
151 post_delim_nb
, /* In */
152 post_defaults
, /* Li */
155 post_delim_nb
, /* Op */
157 post_defaults
, /* Pa */
160 post_delim_nb
, /* Va */
161 post_delim_nb
, /* Vt */
164 post_hyph
, /* %B */ /* FIXME: can be used outside Rs/Re. */
172 post_hyph
, /* %T */ /* FIXME: can be used outside Rs/Re. */
176 post_delim_nb
, /* Aq */
184 post_obsolete
, /* Db */
190 post_delim_nb
, /* Em */
193 post_delim_nb
, /* Ms */
201 post_delim_nb
, /* Pq */
203 post_delim_nb
, /* Ql */
205 post_delim_nb
, /* Qq */
210 post_delim_nb
, /* Sq */
213 post_delim_nb
, /* Sy */
214 post_useless
, /* Tn */
225 post_obsolete
, /* Hf */
226 post_obsolete
, /* Fr */
230 post_delim_nb
, /* Lk */
231 post_defaults
, /* Mt */
232 post_delim_nb
, /* Brq */
245 #define RSORD_MAX 14 /* Number of `Rs' blocks. */
247 static const enum roff_tok rsord
[RSORD_MAX
] = {
264 static const char * const secnames
[SEC__MAX
] = {
271 "IMPLEMENTATION NOTES",
286 "SECURITY CONSIDERATIONS",
291 /* Validate the subtree rooted at mdoc->last. */
293 mdoc_validate(struct roff_man
*mdoc
)
295 struct roff_node
*n
, *np
;
299 * Translate obsolete macros to modern macros first
300 * such that later code does not need to look
301 * for the obsolete versions.
318 * Iterate over all children, recursing into each one
319 * in turn, depth-first.
322 mdoc
->last
= mdoc
->last
->child
;
323 while (mdoc
->last
!= NULL
) {
326 mdoc
->last
= mdoc
->last
->child
;
328 mdoc
->last
= mdoc
->last
->next
;
331 /* Finally validate the macro itself. */
334 mdoc
->next
= ROFF_NEXT_SIBLING
;
338 if (n
->sec
!= SEC_SYNOPSIS
||
339 (np
->tok
!= MDOC_Cd
&& np
->tok
!= MDOC_Fd
))
340 check_text(mdoc
, n
->line
, n
->pos
, n
->string
);
341 if ((n
->flags
& NODE_NOFILL
) == 0 &&
342 (np
->tok
!= MDOC_It
|| np
->type
!= ROFFT_HEAD
||
343 np
->parent
->parent
->norm
->Bl
.type
!= LIST_diag
))
344 check_text_em(mdoc
, n
->line
, n
->pos
, n
->string
);
345 if (np
->tok
== MDOC_It
|| (np
->type
== ROFFT_BODY
&&
346 (np
->tok
== MDOC_Sh
|| np
->tok
== MDOC_Ss
)))
347 check_toptext(mdoc
, n
->line
, n
->pos
, n
->string
);
357 check_args(mdoc
, mdoc
->last
);
360 * Closing delimiters are not special at the
361 * beginning of a block, opening delimiters
362 * are not special at the end.
365 if (n
->child
!= NULL
)
366 n
->child
->flags
&= ~NODE_DELIMC
;
368 n
->last
->flags
&= ~NODE_DELIMO
;
370 /* Call the macro's postprocessor. */
372 if (n
->tok
< ROFF_MAX
) {
377 assert(n
->tok
>= MDOC_Dd
&& n
->tok
< MDOC_MAX
);
378 p
= mdoc_valids
+ (n
->tok
- MDOC_Dd
);
388 check_args(struct roff_man
*mdoc
, struct roff_node
*n
)
395 assert(n
->args
->argc
);
396 for (i
= 0; i
< (int)n
->args
->argc
; i
++)
397 check_argv(mdoc
, n
, &n
->args
->argv
[i
]);
401 check_argv(struct roff_man
*mdoc
, struct roff_node
*n
, struct mdoc_argv
*v
)
405 for (i
= 0; i
< (int)v
->sz
; i
++)
406 check_text(mdoc
, v
->line
, v
->pos
, v
->value
[i
]);
410 check_text(struct roff_man
*mdoc
, int ln
, int pos
, char *p
)
414 if (mdoc
->last
->flags
& NODE_NOFILL
)
417 for (cp
= p
; NULL
!= (p
= strchr(p
, '\t')); p
++)
418 mandoc_msg(MANDOCERR_FI_TAB
, ln
, pos
+ (int)(p
- cp
), NULL
);
422 check_text_em(struct roff_man
*mdoc
, int ln
, int pos
, char *p
)
424 const struct roff_node
*np
, *nn
;
427 np
= mdoc
->last
->prev
;
428 nn
= mdoc
->last
->next
;
430 /* Look for em-dashes wrongly encoded as "--". */
432 for (cp
= p
; *cp
!= '\0'; cp
++) {
433 if (cp
[0] != '-' || cp
[1] != '-')
437 /* Skip input sequences of more than two '-'. */
445 /* Skip "--" directly attached to something else. */
447 if ((cp
- p
> 1 && cp
[-2] != ' ') ||
448 (cp
[1] != '\0' && cp
[1] != ' '))
451 /* Require a letter right before or right afterwards. */
454 isalpha((unsigned char)cp
[-3]) :
456 np
->type
== ROFFT_TEXT
&&
457 *np
->string
!= '\0' &&
458 isalpha((unsigned char)np
->string
[
459 strlen(np
->string
) - 1])) ||
460 (cp
[1] != '\0' && cp
[2] != '\0' ?
461 isalpha((unsigned char)cp
[2]) :
463 nn
->type
== ROFFT_TEXT
&&
464 isalpha((unsigned char)*nn
->string
))) {
465 mandoc_msg(MANDOCERR_DASHDASH
,
466 ln
, pos
+ (int)(cp
- p
) - 1, NULL
);
473 check_toptext(struct roff_man
*mdoc
, int ln
, int pos
, const char *p
)
475 const char *cp
, *cpr
;
480 if ((cp
= strstr(p
, "OpenBSD")) != NULL
)
481 mandoc_msg(MANDOCERR_BX
, ln
, pos
+ (int)(cp
- p
), "Ox");
482 if ((cp
= strstr(p
, "NetBSD")) != NULL
)
483 mandoc_msg(MANDOCERR_BX
, ln
, pos
+ (int)(cp
- p
), "Nx");
484 if ((cp
= strstr(p
, "FreeBSD")) != NULL
)
485 mandoc_msg(MANDOCERR_BX
, ln
, pos
+ (int)(cp
- p
), "Fx");
486 if ((cp
= strstr(p
, "DragonFly")) != NULL
)
487 mandoc_msg(MANDOCERR_BX
, ln
, pos
+ (int)(cp
- p
), "Dx");
490 while ((cp
= strstr(cp
+ 1, "()")) != NULL
) {
491 for (cpr
= cp
- 1; cpr
>= p
; cpr
--)
492 if (*cpr
!= '_' && !isalnum((unsigned char)*cpr
))
494 if ((cpr
< p
|| *cpr
== ' ') && cpr
+ 1 < cp
) {
496 mandoc_msg(MANDOCERR_FUNC
, ln
, pos
+ (int)(cpr
- p
),
497 "%.*s()", (int)(cp
- cpr
), cpr
);
503 post_abort(POST_ARGS
)
509 post_delim(POST_ARGS
)
511 const struct roff_node
*nch
;
516 tok
= mdoc
->last
->tok
;
517 nch
= mdoc
->last
->last
;
518 if (nch
== NULL
|| nch
->type
!= ROFFT_TEXT
)
520 lc
= strchr(nch
->string
, '\0') - 1;
521 if (lc
< nch
->string
)
523 delim
= mdoc_isdelim(lc
);
524 if (delim
== DELIM_NONE
|| delim
== DELIM_OPEN
)
526 if (*lc
== ')' && (tok
== MDOC_Nd
|| tok
== MDOC_Sh
||
527 tok
== MDOC_Ss
|| tok
== MDOC_Fo
))
530 mandoc_msg(MANDOCERR_DELIM
, nch
->line
,
531 nch
->pos
+ (int)(lc
- nch
->string
), "%s%s %s", roff_name
[tok
],
532 nch
== mdoc
->last
->child
? "" : " ...", nch
->string
);
536 post_delim_nb(POST_ARGS
)
538 const struct roff_node
*nch
;
545 * Find candidates: at least two bytes,
546 * the last one a closing or middle delimiter.
549 tok
= mdoc
->last
->tok
;
550 nch
= mdoc
->last
->last
;
551 if (nch
== NULL
|| nch
->type
!= ROFFT_TEXT
)
553 lc
= strchr(nch
->string
, '\0') - 1;
554 if (lc
<= nch
->string
)
556 delim
= mdoc_isdelim(lc
);
557 if (delim
== DELIM_NONE
|| delim
== DELIM_OPEN
)
561 * Reduce false positives by allowing various cases.
564 /* Escaped delimiters. */
565 if (lc
> nch
->string
+ 1 && lc
[-2] == '\\' &&
566 (lc
[-1] == '&' || lc
[-1] == 'e'))
569 /* Specific byte sequences. */
572 for (cp
= lc
; cp
>= nch
->string
; cp
--)
577 if (lc
> nch
->string
+ 1 && lc
[-2] == '.' && lc
[-1] == '.')
591 for (cp
= lc
; cp
>= nch
->string
; cp
--)
596 if (lc
== nch
->string
+ 1 && lc
[-1] == '|')
602 /* Exactly two non-alphanumeric bytes. */
603 if (lc
== nch
->string
+ 1 && !isalnum((unsigned char)lc
[-1]))
606 /* At least three alphabetic words with a sentence ending. */
607 if (strchr("!.:?", *lc
) != NULL
&& (tok
== MDOC_Em
||
608 tok
== MDOC_Li
|| tok
== MDOC_Pq
|| tok
== MDOC_Sy
)) {
610 for (cp
= lc
- 1; cp
>= nch
->string
; cp
--) {
613 if (cp
> nch
->string
&& cp
[-1] == ',')
615 } else if (isalpha((unsigned int)*cp
)) {
623 mandoc_msg(MANDOCERR_DELIM_NB
, nch
->line
,
624 nch
->pos
+ (int)(lc
- nch
->string
), "%s%s %s", roff_name
[tok
],
625 nch
== mdoc
->last
->child
? "" : " ...", nch
->string
);
629 post_bl_norm(POST_ARGS
)
632 struct mdoc_argv
*argv
, *wa
;
634 enum mdocargt mdoclt
;
637 n
= mdoc
->last
->parent
;
638 n
->norm
->Bl
.type
= LIST__NONE
;
641 * First figure out which kind of list to use: bind ourselves to
642 * the first mentioned list type and warn about any remaining
643 * ones. If we find no list type, we default to LIST_item.
646 wa
= (n
->args
== NULL
) ? NULL
: n
->args
->argv
;
647 mdoclt
= MDOC_ARG_MAX
;
648 for (i
= 0; n
->args
&& i
< (int)n
->args
->argc
; i
++) {
649 argv
= n
->args
->argv
+ i
;
652 /* Set list types. */
686 /* Set list arguments. */
688 if (n
->norm
->Bl
.comp
)
689 mandoc_msg(MANDOCERR_ARG_REP
,
690 argv
->line
, argv
->pos
, "Bl -compact");
691 n
->norm
->Bl
.comp
= 1;
696 mandoc_msg(MANDOCERR_ARG_EMPTY
,
697 argv
->line
, argv
->pos
, "Bl -width");
698 n
->norm
->Bl
.width
= "0n";
701 if (NULL
!= n
->norm
->Bl
.width
)
702 mandoc_msg(MANDOCERR_ARG_REP
,
703 argv
->line
, argv
->pos
,
704 "Bl -width %s", argv
->value
[0]);
705 rewrite_macro2len(mdoc
, argv
->value
);
706 n
->norm
->Bl
.width
= argv
->value
[0];
710 mandoc_msg(MANDOCERR_ARG_EMPTY
,
711 argv
->line
, argv
->pos
, "Bl -offset");
714 if (NULL
!= n
->norm
->Bl
.offs
)
715 mandoc_msg(MANDOCERR_ARG_REP
,
716 argv
->line
, argv
->pos
,
717 "Bl -offset %s", argv
->value
[0]);
718 rewrite_macro2len(mdoc
, argv
->value
);
719 n
->norm
->Bl
.offs
= argv
->value
[0];
724 if (LIST__NONE
== lt
)
728 /* Check: multiple list types. */
730 if (LIST__NONE
!= n
->norm
->Bl
.type
) {
731 mandoc_msg(MANDOCERR_BL_REP
, n
->line
, n
->pos
,
732 "Bl -%s", mdoc_argnames
[argv
->arg
]);
736 /* The list type should come first. */
738 if (n
->norm
->Bl
.width
||
741 mandoc_msg(MANDOCERR_BL_LATETYPE
,
742 n
->line
, n
->pos
, "Bl -%s",
743 mdoc_argnames
[n
->args
->argv
[0].arg
]);
745 n
->norm
->Bl
.type
= lt
;
746 if (LIST_column
== lt
) {
747 n
->norm
->Bl
.ncols
= argv
->sz
;
748 n
->norm
->Bl
.cols
= (void *)argv
->value
;
752 /* Allow lists to default to LIST_item. */
754 if (LIST__NONE
== n
->norm
->Bl
.type
) {
755 mandoc_msg(MANDOCERR_BL_NOTYPE
, n
->line
, n
->pos
, "Bl");
756 n
->norm
->Bl
.type
= LIST_item
;
761 * Validate the width field. Some list types don't need width
762 * types and should be warned about them. Others should have it
763 * and must also be warned. Yet others have a default and need
767 switch (n
->norm
->Bl
.type
) {
769 if (n
->norm
->Bl
.width
== NULL
)
770 mandoc_msg(MANDOCERR_BL_NOWIDTH
,
771 n
->line
, n
->pos
, "Bl -tag");
778 if (n
->norm
->Bl
.width
!= NULL
)
779 mandoc_msg(MANDOCERR_BL_SKIPW
, wa
->line
, wa
->pos
,
780 "Bl -%s", mdoc_argnames
[mdoclt
]);
781 n
->norm
->Bl
.width
= NULL
;
786 if (n
->norm
->Bl
.width
== NULL
)
787 n
->norm
->Bl
.width
= "2n";
790 if (n
->norm
->Bl
.width
== NULL
)
791 n
->norm
->Bl
.width
= "3n";
802 struct mdoc_argv
*argv
;
807 for (i
= 0; n
->args
&& i
< (int)n
->args
->argc
; i
++) {
808 argv
= n
->args
->argv
+ i
;
828 mandoc_msg(MANDOCERR_BD_FILE
, n
->line
, n
->pos
, NULL
);
832 mandoc_msg(MANDOCERR_ARG_EMPTY
,
833 argv
->line
, argv
->pos
, "Bd -offset");
836 if (NULL
!= n
->norm
->Bd
.offs
)
837 mandoc_msg(MANDOCERR_ARG_REP
,
838 argv
->line
, argv
->pos
,
839 "Bd -offset %s", argv
->value
[0]);
840 rewrite_macro2len(mdoc
, argv
->value
);
841 n
->norm
->Bd
.offs
= argv
->value
[0];
844 if (n
->norm
->Bd
.comp
)
845 mandoc_msg(MANDOCERR_ARG_REP
,
846 argv
->line
, argv
->pos
, "Bd -compact");
847 n
->norm
->Bd
.comp
= 1;
852 if (DISP__NONE
== dt
)
855 if (DISP__NONE
== n
->norm
->Bd
.type
)
856 n
->norm
->Bd
.type
= dt
;
858 mandoc_msg(MANDOCERR_BD_REP
, n
->line
, n
->pos
,
859 "Bd -%s", mdoc_argnames
[argv
->arg
]);
862 if (DISP__NONE
== n
->norm
->Bd
.type
) {
863 mandoc_msg(MANDOCERR_BD_NOTYPE
, n
->line
, n
->pos
, "Bd");
864 n
->norm
->Bd
.type
= DISP_ragged
;
869 * Stand-alone line macros.
873 post_an_norm(POST_ARGS
)
876 struct mdoc_argv
*argv
;
883 for (i
= 1; i
< n
->args
->argc
; i
++) {
884 argv
= n
->args
->argv
+ i
;
885 mandoc_msg(MANDOCERR_AN_REP
, argv
->line
, argv
->pos
,
886 "An -%s", mdoc_argnames
[argv
->arg
]);
889 argv
= n
->args
->argv
;
890 if (argv
->arg
== MDOC_Split
)
891 n
->norm
->An
.auth
= AUTH_split
;
892 else if (argv
->arg
== MDOC_Nosplit
)
893 n
->norm
->An
.auth
= AUTH_nosplit
;
905 if (n
->child
!= NULL
)
906 mandoc_msg(MANDOCERR_ARG_SKIP
, n
->line
,
907 n
->pos
, "%s %s", roff_name
[n
->tok
], n
->child
->string
);
909 while (n
->child
!= NULL
)
910 roff_node_delete(mdoc
, n
->child
);
912 roff_word_alloc(mdoc
, n
->line
, n
->pos
, n
->tok
== MDOC_Bt
?
913 "is currently in beta test." : "currently under development.");
914 mdoc
->last
->flags
|= NODE_EOS
| NODE_NOSRC
;
919 build_list(struct roff_man
*mdoc
, int tok
)
924 n
= mdoc
->last
->next
;
925 for (ic
= 1;; ic
++) {
926 roff_elem_alloc(mdoc
, n
->line
, n
->pos
, tok
);
927 mdoc
->last
->flags
|= NODE_NOSRC
;
928 roff_node_relink(mdoc
, n
);
929 n
= mdoc
->last
= mdoc
->last
->parent
;
930 mdoc
->next
= ROFF_NEXT_SIBLING
;
933 if (ic
> 1 || n
->next
->next
!= NULL
) {
934 roff_word_alloc(mdoc
, n
->line
, n
->pos
, ",");
935 mdoc
->last
->flags
|= NODE_DELIMC
| NODE_NOSRC
;
937 n
= mdoc
->last
->next
;
938 if (n
->next
== NULL
) {
939 roff_word_alloc(mdoc
, n
->line
, n
->pos
, "and");
940 mdoc
->last
->flags
|= NODE_NOSRC
;
954 mdoc
->next
= ROFF_NEXT_CHILD
;
955 roff_word_alloc(mdoc
, n
->line
, n
->pos
, "The");
956 mdoc
->last
->flags
|= NODE_NOSRC
;
958 if (mdoc
->last
->next
!= NULL
)
959 ic
= build_list(mdoc
, MDOC_Nm
);
960 else if (mdoc
->meta
.name
!= NULL
) {
961 roff_elem_alloc(mdoc
, n
->line
, n
->pos
, MDOC_Nm
);
962 mdoc
->last
->flags
|= NODE_NOSRC
;
963 roff_word_alloc(mdoc
, n
->line
, n
->pos
, mdoc
->meta
.name
);
964 mdoc
->last
->flags
|= NODE_NOSRC
;
965 mdoc
->last
= mdoc
->last
->parent
;
966 mdoc
->next
= ROFF_NEXT_SIBLING
;
969 mandoc_msg(MANDOCERR_EX_NONAME
, n
->line
, n
->pos
, "Ex");
973 roff_word_alloc(mdoc
, n
->line
, n
->pos
,
974 ic
> 1 ? "utilities exit\\~0" : "utility exits\\~0");
975 mdoc
->last
->flags
|= NODE_NOSRC
;
976 roff_word_alloc(mdoc
, n
->line
, n
->pos
,
977 "on success, and\\~>0 if an error occurs.");
978 mdoc
->last
->flags
|= NODE_EOS
| NODE_NOSRC
;
991 assert(n
->child
->type
== ROFFT_TEXT
);
992 mdoc
->next
= ROFF_NEXT_CHILD
;
994 if ((p
= mdoc_a2lib(n
->child
->string
)) != NULL
) {
995 n
->child
->flags
|= NODE_NOPRT
;
996 roff_word_alloc(mdoc
, n
->line
, n
->pos
, p
);
997 mdoc
->last
->flags
= NODE_NOSRC
;
1002 mandoc_msg(MANDOCERR_LB_BAD
, n
->child
->line
,
1003 n
->child
->pos
, "Lb %s", n
->child
->string
);
1005 roff_word_alloc(mdoc
, n
->line
, n
->pos
, "library");
1006 mdoc
->last
->flags
= NODE_NOSRC
;
1007 roff_word_alloc(mdoc
, n
->line
, n
->pos
, "\\(lq");
1008 mdoc
->last
->flags
= NODE_DELIMO
| NODE_NOSRC
;
1009 mdoc
->last
= mdoc
->last
->next
;
1010 roff_word_alloc(mdoc
, n
->line
, n
->pos
, "\\(rq");
1011 mdoc
->last
->flags
= NODE_DELIMC
| NODE_NOSRC
;
1018 struct roff_node
*n
;
1024 mdoc
->next
= ROFF_NEXT_CHILD
;
1025 if (n
->child
!= NULL
) {
1026 roff_word_alloc(mdoc
, n
->line
, n
->pos
, "The");
1027 mdoc
->last
->flags
|= NODE_NOSRC
;
1028 ic
= build_list(mdoc
, MDOC_Fn
);
1029 roff_word_alloc(mdoc
, n
->line
, n
->pos
,
1030 ic
> 1 ? "functions return" : "function returns");
1031 mdoc
->last
->flags
|= NODE_NOSRC
;
1032 roff_word_alloc(mdoc
, n
->line
, n
->pos
,
1033 "the value\\~0 if successful;");
1035 roff_word_alloc(mdoc
, n
->line
, n
->pos
, "Upon successful "
1036 "completion, the value\\~0 is returned;");
1037 mdoc
->last
->flags
|= NODE_NOSRC
;
1039 roff_word_alloc(mdoc
, n
->line
, n
->pos
, "otherwise "
1040 "the value\\~\\-1 is returned and the global variable");
1041 mdoc
->last
->flags
|= NODE_NOSRC
;
1042 roff_elem_alloc(mdoc
, n
->line
, n
->pos
, MDOC_Va
);
1043 mdoc
->last
->flags
|= NODE_NOSRC
;
1044 roff_word_alloc(mdoc
, n
->line
, n
->pos
, "errno");
1045 mdoc
->last
->flags
|= NODE_NOSRC
;
1046 mdoc
->last
= mdoc
->last
->parent
;
1047 mdoc
->next
= ROFF_NEXT_SIBLING
;
1048 roff_word_alloc(mdoc
, n
->line
, n
->pos
,
1049 "is set to indicate the error.");
1050 mdoc
->last
->flags
|= NODE_EOS
| NODE_NOSRC
;
1057 struct roff_node
*n
;
1062 if (n
->args
&& n
->args
->argc
== 1)
1063 if (n
->args
->argv
[0].arg
== MDOC_Std
)
1066 mandoc_msg(MANDOCERR_ARG_STD
, n
->line
, n
->pos
,
1067 "%s", roff_name
[n
->tok
]);
1073 struct roff_node
*n
, *nch
;
1078 assert(nch
->type
== ROFFT_TEXT
);
1080 if ((p
= mdoc_a2st(nch
->string
)) == NULL
) {
1081 mandoc_msg(MANDOCERR_ST_BAD
,
1082 nch
->line
, nch
->pos
, "St %s", nch
->string
);
1083 roff_node_delete(mdoc
, n
);
1087 nch
->flags
|= NODE_NOPRT
;
1088 mdoc
->next
= ROFF_NEXT_CHILD
;
1089 roff_word_alloc(mdoc
, nch
->line
, nch
->pos
, p
);
1090 mdoc
->last
->flags
|= NODE_NOSRC
;
1097 struct roff_node
*n
, *nch
;
1102 if (nch
== NULL
&& n
->next
!= NULL
&&
1103 n
->next
->child
->type
== ROFFT_TEXT
) {
1104 mdoc
->next
= ROFF_NEXT_CHILD
;
1105 roff_word_alloc(mdoc
, n
->line
, n
->pos
, n
->next
->child
->string
);
1107 nch
->flags
|= NODE_NOSRC
;
1110 if (nch
== NULL
|| *nch
->string
== '\0') {
1111 mandoc_msg(MANDOCERR_MACRO_EMPTY
, n
->line
, n
->pos
, "Tg");
1112 roff_node_delete(mdoc
, n
);
1115 len
= strcspn(nch
->string
, " \t");
1116 if (nch
->string
[len
] != '\0')
1117 mandoc_msg(MANDOCERR_TG_SPC
, nch
->line
, nch
->pos
+ len
+ 1,
1118 "Tg %s", nch
->string
);
1119 if (nch
->next
!= NULL
) {
1120 mandoc_msg(MANDOCERR_ARG_EXCESS
, nch
->next
->line
,
1121 nch
->next
->pos
, "Tg ... %s", nch
->next
->string
);
1122 while (nch
->next
!= NULL
)
1123 roff_node_delete(mdoc
, nch
->next
);
1125 if (nch
->string
[len
] != '\0')
1126 roff_node_delete(mdoc
, n
);
1130 post_obsolete(POST_ARGS
)
1132 struct roff_node
*n
;
1135 if (n
->type
== ROFFT_ELEM
|| n
->type
== ROFFT_BLOCK
)
1136 mandoc_msg(MANDOCERR_MACRO_OBS
, n
->line
, n
->pos
,
1137 "%s", roff_name
[n
->tok
]);
1141 post_useless(POST_ARGS
)
1143 struct roff_node
*n
;
1146 mandoc_msg(MANDOCERR_MACRO_USELESS
, n
->line
, n
->pos
,
1147 "%s", roff_name
[n
->tok
]);
1157 struct roff_node
*np
, *nch
;
1160 * Unlike other data pointers, these are "housed" by the HEAD
1161 * element, which contains the goods.
1165 if (np
->type
!= ROFFT_HEAD
)
1168 assert(np
->parent
->type
== ROFFT_BLOCK
);
1169 assert(np
->parent
->tok
== MDOC_Bf
);
1171 /* Check the number of arguments. */
1174 if (np
->parent
->args
== NULL
) {
1176 mandoc_msg(MANDOCERR_BF_NOFONT
,
1177 np
->line
, np
->pos
, "Bf");
1183 mandoc_msg(MANDOCERR_ARG_EXCESS
,
1184 nch
->line
, nch
->pos
, "Bf ... %s", nch
->string
);
1186 /* Extract argument into data. */
1188 if (np
->parent
->args
!= NULL
) {
1189 switch (np
->parent
->args
->argv
[0].arg
) {
1191 np
->norm
->Bf
.font
= FONT_Em
;
1194 np
->norm
->Bf
.font
= FONT_Li
;
1197 np
->norm
->Bf
.font
= FONT_Sy
;
1205 /* Extract parameter into data. */
1207 if ( ! strcmp(np
->child
->string
, "Em"))
1208 np
->norm
->Bf
.font
= FONT_Em
;
1209 else if ( ! strcmp(np
->child
->string
, "Li"))
1210 np
->norm
->Bf
.font
= FONT_Li
;
1211 else if ( ! strcmp(np
->child
->string
, "Sy"))
1212 np
->norm
->Bf
.font
= FONT_Sy
;
1214 mandoc_msg(MANDOCERR_BF_BADFONT
, np
->child
->line
,
1215 np
->child
->pos
, "Bf %s", np
->child
->string
);
1219 post_fname(POST_ARGS
)
1221 const struct roff_node
*n
;
1225 n
= mdoc
->last
->child
;
1228 if (cp
[strlen(cp
+ 1)] == ')')
1232 pos
= strcspn(cp
, "()");
1233 if (cp
[pos
] == '\0')
1236 mandoc_msg(MANDOCERR_FN_PAREN
, n
->line
, n
->pos
+ pos
, "%s", cp
);
1250 const struct roff_node
*n
;
1254 if (n
->type
!= ROFFT_HEAD
)
1257 if (n
->child
== NULL
) {
1258 mandoc_msg(MANDOCERR_FO_NOHEAD
, n
->line
, n
->pos
, "Fo");
1261 if (n
->child
!= n
->last
) {
1262 mandoc_msg(MANDOCERR_ARG_EXCESS
,
1263 n
->child
->next
->line
, n
->child
->next
->pos
,
1264 "Fo ... %s", n
->child
->next
->string
);
1265 while (n
->child
!= n
->last
)
1266 roff_node_delete(mdoc
, n
->last
);
1276 const struct roff_node
*n
;
1279 for (n
= mdoc
->last
->child
; n
!= NULL
; n
= n
->next
) {
1280 for (cp
= n
->string
; *cp
!= '\0'; cp
++) {
1281 /* Ignore callbacks and alterations. */
1282 if (*cp
== '(' || *cp
== '{')
1286 mandoc_msg(MANDOCERR_FA_COMMA
, n
->line
,
1287 n
->pos
+ (int)(cp
- n
->string
), "%s", n
->string
);
1291 post_delim_nb(mdoc
);
1297 struct roff_node
*n
;
1301 if (n
->sec
== SEC_NAME
&& n
->child
!= NULL
&&
1302 n
->child
->type
== ROFFT_TEXT
&& mdoc
->meta
.msec
!= NULL
)
1303 mandoc_xr_add(mdoc
->meta
.msec
, n
->child
->string
, -1, -1);
1305 if (n
->last
!= NULL
&& n
->last
->tok
== MDOC_Pp
)
1306 roff_node_relink(mdoc
, n
->last
);
1308 if (mdoc
->meta
.name
== NULL
)
1309 deroff(&mdoc
->meta
.name
, n
);
1311 if (mdoc
->meta
.name
== NULL
||
1312 (mdoc
->lastsec
== SEC_NAME
&& n
->child
== NULL
))
1313 mandoc_msg(MANDOCERR_NM_NONAME
, n
->line
, n
->pos
, "Nm");
1317 post_delim_nb(mdoc
);
1326 if ((n
->child
!= NULL
&& n
->child
->type
== ROFFT_TEXT
) ||
1327 mdoc
->meta
.name
== NULL
)
1330 mdoc
->next
= ROFF_NEXT_CHILD
;
1331 roff_word_alloc(mdoc
, n
->line
, n
->pos
, mdoc
->meta
.name
);
1332 mdoc
->last
->flags
|= NODE_NOSRC
;
1339 struct roff_node
*n
;
1343 if (n
->type
!= ROFFT_BODY
)
1346 if (n
->sec
!= SEC_NAME
)
1347 mandoc_msg(MANDOCERR_ND_LATE
, n
->line
, n
->pos
, "Nd");
1349 if (n
->child
== NULL
)
1350 mandoc_msg(MANDOCERR_ND_EMPTY
, n
->line
, n
->pos
, "Nd");
1358 post_display(POST_ARGS
)
1360 struct roff_node
*n
, *np
;
1365 if (n
->end
!= ENDBODY_NOT
) {
1366 if (n
->tok
== MDOC_Bd
&&
1367 n
->body
->parent
->args
== NULL
)
1368 roff_node_delete(mdoc
, n
);
1369 } else if (n
->child
== NULL
)
1370 mandoc_msg(MANDOCERR_BLK_EMPTY
, n
->line
, n
->pos
,
1371 "%s", roff_name
[n
->tok
]);
1372 else if (n
->tok
== MDOC_D1
)
1376 if (n
->tok
== MDOC_Bd
) {
1377 if (n
->args
== NULL
) {
1378 mandoc_msg(MANDOCERR_BD_NOARG
,
1379 n
->line
, n
->pos
, "Bd");
1380 mdoc
->next
= ROFF_NEXT_SIBLING
;
1381 while (n
->body
->child
!= NULL
)
1382 roff_node_relink(mdoc
,
1384 roff_node_delete(mdoc
, n
);
1390 for (np
= n
->parent
; np
!= NULL
; np
= np
->parent
) {
1391 if (np
->type
== ROFFT_BLOCK
&& np
->tok
== MDOC_Bd
) {
1392 mandoc_msg(MANDOCERR_BD_NEST
, n
->line
,
1393 n
->pos
, "%s in Bd", roff_name
[n
->tok
]);
1404 post_defaults(POST_ARGS
)
1406 struct roff_node
*nn
;
1408 if (mdoc
->last
->child
!= NULL
) {
1409 post_delim_nb(mdoc
);
1414 * The `Ar' defaults to "file ..." if no value is provided as an
1415 * argument; the `Mt' and `Pa' macros use "~"; the `Li' just
1416 * gets an empty string.
1422 mdoc
->next
= ROFF_NEXT_CHILD
;
1423 roff_word_alloc(mdoc
, nn
->line
, nn
->pos
, "file");
1424 mdoc
->last
->flags
|= NODE_NOSRC
;
1425 roff_word_alloc(mdoc
, nn
->line
, nn
->pos
, "...");
1426 mdoc
->last
->flags
|= NODE_NOSRC
;
1430 mdoc
->next
= ROFF_NEXT_CHILD
;
1431 roff_word_alloc(mdoc
, nn
->line
, nn
->pos
, "~");
1432 mdoc
->last
->flags
|= NODE_NOSRC
;
1443 struct roff_node
*n
, *nch
;
1450 * If we have a child, look it up in the standard keys. If a
1451 * key exist, use that instead of the child; if it doesn't,
1452 * prefix "AT&T UNIX " to the existing data.
1456 if (nch
!= NULL
&& ((att
= mdoc_a2att(nch
->string
)) == NULL
))
1457 mandoc_msg(MANDOCERR_AT_BAD
,
1458 nch
->line
, nch
->pos
, "At %s", nch
->string
);
1460 mdoc
->next
= ROFF_NEXT_CHILD
;
1462 roff_word_alloc(mdoc
, nch
->line
, nch
->pos
, att
);
1463 nch
->flags
|= NODE_NOPRT
;
1465 roff_word_alloc(mdoc
, n
->line
, n
->pos
, "AT&T UNIX");
1466 mdoc
->last
->flags
|= NODE_NOSRC
;
1473 struct roff_node
*np
, *nch
;
1479 if (np
->norm
->An
.auth
== AUTH__NONE
) {
1481 mandoc_msg(MANDOCERR_MACRO_EMPTY
,
1482 np
->line
, np
->pos
, "An");
1484 post_delim_nb(mdoc
);
1485 } else if (nch
!= NULL
)
1486 mandoc_msg(MANDOCERR_ARG_EXCESS
,
1487 nch
->line
, nch
->pos
, "An ... %s", nch
->string
);
1494 post_obsolete(mdoc
);
1495 if (mdoc
->last
->type
== ROFFT_BLOCK
)
1496 mdoc
->last
->norm
->Es
= mdoc
->last_es
;
1503 post_obsolete(mdoc
);
1504 mdoc
->last_es
= mdoc
->last
;
1510 struct roff_node
*n
;
1514 post_delim_nb(mdoc
);
1529 if (n
->child
== NULL
)
1531 v
= n
->child
->string
;
1532 if ((v
[0] != '0' && v
[0] != '1') || v
[1] != '.' ||
1533 v
[2] < '0' || v
[2] > '9' ||
1534 v
[3] < 'a' || v
[3] > 'z' || v
[4] != '\0')
1536 n
->child
->flags
|= NODE_NOPRT
;
1537 mdoc
->next
= ROFF_NEXT_CHILD
;
1538 roff_word_alloc(mdoc
, n
->child
->line
, n
->child
->pos
, v
);
1539 v
= mdoc
->last
->string
;
1540 v
[3] = toupper((unsigned char)v
[3]);
1541 mdoc
->last
->flags
|= NODE_NOSRC
;
1553 mdoc
->next
= ROFF_NEXT_CHILD
;
1554 roff_word_alloc(mdoc
, n
->line
, n
->pos
, os
);
1555 mdoc
->last
->flags
|= NODE_NOSRC
;
1562 struct roff_node
*nbl
, *nit
, *nch
;
1569 if (nit
->type
!= ROFFT_BLOCK
)
1572 nbl
= nit
->parent
->parent
;
1573 lt
= nbl
->norm
->Bl
.type
;
1581 if (nit
->head
->child
== NULL
)
1582 mandoc_msg(MANDOCERR_IT_NOHEAD
,
1583 nit
->line
, nit
->pos
, "Bl -%s It",
1584 mdoc_argnames
[nbl
->args
->argv
[0].arg
]);
1590 if (nit
->body
== NULL
|| nit
->body
->child
== NULL
)
1591 mandoc_msg(MANDOCERR_IT_NOBODY
,
1592 nit
->line
, nit
->pos
, "Bl -%s It",
1593 mdoc_argnames
[nbl
->args
->argv
[0].arg
]);
1596 if ((nch
= nit
->head
->child
) != NULL
)
1597 mandoc_msg(MANDOCERR_ARG_SKIP
,
1598 nit
->line
, nit
->pos
, "It %s",
1599 nch
->string
== NULL
? roff_name
[nch
->tok
] :
1603 cols
= (int)nbl
->norm
->Bl
.ncols
;
1605 assert(nit
->head
->child
== NULL
);
1607 if (nit
->head
->next
->child
== NULL
&&
1608 nit
->head
->next
->next
== NULL
) {
1609 mandoc_msg(MANDOCERR_MACRO_EMPTY
,
1610 nit
->line
, nit
->pos
, "It");
1611 roff_node_delete(mdoc
, nit
);
1616 for (nch
= nit
->child
; nch
!= NULL
; nch
= nch
->next
) {
1617 if (nch
->type
!= ROFFT_BODY
)
1619 if (i
++ && nch
->flags
& NODE_LINE
)
1620 mandoc_msg(MANDOCERR_TA_LINE
,
1621 nch
->line
, nch
->pos
, "Ta");
1623 if (i
< cols
|| i
> cols
+ 1)
1624 mandoc_msg(MANDOCERR_BL_COL
, nit
->line
, nit
->pos
,
1625 "%d columns, %d cells", cols
, i
);
1626 else if (nit
->head
->next
->child
!= NULL
&&
1627 nit
->head
->next
->child
->flags
& NODE_LINE
)
1628 mandoc_msg(MANDOCERR_IT_NOARG
,
1629 nit
->line
, nit
->pos
, "Bl -column It");
1637 post_bl_block(POST_ARGS
)
1639 struct roff_node
*n
, *ni
, *nc
;
1644 for (ni
= n
->body
->child
; ni
!= NULL
; ni
= ni
->next
) {
1645 if (ni
->body
== NULL
)
1647 nc
= ni
->body
->last
;
1648 while (nc
!= NULL
) {
1657 if (ni
->next
== NULL
) {
1658 mandoc_msg(MANDOCERR_PAR_MOVE
, nc
->line
,
1659 nc
->pos
, "%s", roff_name
[nc
->tok
]);
1660 roff_node_relink(mdoc
, nc
);
1661 } else if (n
->norm
->Bl
.comp
== 0 &&
1662 n
->norm
->Bl
.type
!= LIST_column
) {
1663 mandoc_msg(MANDOCERR_PAR_SKIP
,
1665 "%s before It", roff_name
[nc
->tok
]);
1666 roff_node_delete(mdoc
, nc
);
1669 nc
= ni
->body
->last
;
1675 * If the argument of -offset or -width is a macro,
1676 * replace it with the associated default width.
1679 rewrite_macro2len(struct roff_man
*mdoc
, char **arg
)
1686 else if ( ! strcmp(*arg
, "Ds"))
1688 else if ((tok
= roffhash_find(mdoc
->mdocmac
, *arg
, 0)) == TOKEN_NONE
)
1691 width
= macro2len(tok
);
1694 mandoc_asprintf(arg
, "%zun", width
);
1698 post_bl_head(POST_ARGS
)
1700 struct roff_node
*nbl
, *nh
, *nch
, *nnext
;
1701 struct mdoc_argv
*argv
;
1707 if (nh
->norm
->Bl
.type
!= LIST_column
) {
1708 if ((nch
= nh
->child
) == NULL
)
1710 mandoc_msg(MANDOCERR_ARG_EXCESS
,
1711 nch
->line
, nch
->pos
, "Bl ... %s", nch
->string
);
1712 while (nch
!= NULL
) {
1713 roff_node_delete(mdoc
, nch
);
1720 * Append old-style lists, where the column width specifiers
1721 * trail as macro parameters, to the new-style ("normal-form")
1722 * lists where they're argument values following -column.
1725 if (nh
->child
== NULL
)
1729 for (j
= 0; j
< (int)nbl
->args
->argc
; j
++)
1730 if (nbl
->args
->argv
[j
].arg
== MDOC_Column
)
1733 assert(j
< (int)nbl
->args
->argc
);
1736 * Accommodate for new-style groff column syntax. Shuffle the
1737 * child nodes, all of which must be TEXT, as arguments for the
1738 * column field. Then, delete the head children.
1741 argv
= nbl
->args
->argv
+ j
;
1743 for (nch
= nh
->child
; nch
!= NULL
; nch
= nch
->next
)
1745 argv
->value
= mandoc_reallocarray(argv
->value
,
1746 argv
->sz
, sizeof(char *));
1748 nh
->norm
->Bl
.ncols
= argv
->sz
;
1749 nh
->norm
->Bl
.cols
= (void *)argv
->value
;
1751 for (nch
= nh
->child
; nch
!= NULL
; nch
= nnext
) {
1752 argv
->value
[i
++] = nch
->string
;
1755 roff_node_delete(NULL
, nch
);
1763 struct roff_node
*nbody
; /* of the Bl */
1764 struct roff_node
*nchild
, *nnext
; /* of the Bl body */
1765 const char *prev_Er
;
1769 switch (nbody
->type
) {
1771 post_bl_block(mdoc
);
1781 if (nbody
->end
!= ENDBODY_NOT
)
1785 * Up to the first item, move nodes before the list,
1786 * but leave transparent nodes where they are
1787 * if they precede an item.
1788 * The next non-transparent node is kept in nchild.
1789 * It only needs to be updated after a non-transparent
1790 * node was moved out, and at the very beginning
1791 * when no node at all was moved yet.
1794 nchild
= mdoc
->last
;
1796 if (nchild
== mdoc
->last
)
1797 nchild
= roff_node_child(nbody
);
1798 if (nchild
== NULL
) {
1800 mandoc_msg(MANDOCERR_BLK_EMPTY
,
1801 nbody
->line
, nbody
->pos
, "Bl");
1804 if (nchild
->tok
== MDOC_It
) {
1808 mandoc_msg(MANDOCERR_BL_MOVE
, nbody
->child
->line
,
1809 nbody
->child
->pos
, "%s", roff_name
[nbody
->child
->tok
]);
1810 if (nbody
->parent
->prev
== NULL
) {
1811 mdoc
->last
= nbody
->parent
->parent
;
1812 mdoc
->next
= ROFF_NEXT_CHILD
;
1814 mdoc
->last
= nbody
->parent
->prev
;
1815 mdoc
->next
= ROFF_NEXT_SIBLING
;
1817 roff_node_relink(mdoc
, nbody
->child
);
1821 * We have reached the first item,
1822 * so moving nodes out is no longer possible.
1823 * But in .Bl -column, the first rows may be implicit,
1824 * that is, they may not start with .It macros.
1825 * Such rows may be followed by nodes generated on the
1826 * roff level, for example .TS.
1827 * Wrap such roff nodes into an implicit row.
1830 while (nchild
!= NULL
) {
1831 if (nchild
->tok
== MDOC_It
) {
1832 nchild
= roff_node_next(nchild
);
1835 nnext
= nchild
->next
;
1836 mdoc
->last
= nchild
->prev
;
1837 mdoc
->next
= ROFF_NEXT_SIBLING
;
1838 roff_block_alloc(mdoc
, nchild
->line
, nchild
->pos
, MDOC_It
);
1839 roff_head_alloc(mdoc
, nchild
->line
, nchild
->pos
, MDOC_It
);
1840 mdoc
->next
= ROFF_NEXT_SIBLING
;
1841 roff_body_alloc(mdoc
, nchild
->line
, nchild
->pos
, MDOC_It
);
1842 while (nchild
->tok
!= MDOC_It
) {
1843 roff_node_relink(mdoc
, nchild
);
1847 nnext
= nchild
->next
;
1848 mdoc
->next
= ROFF_NEXT_SIBLING
;
1853 if (mdoc
->meta
.os_e
!= MANDOC_OS_NETBSD
)
1857 for (nchild
= nbody
->child
; nchild
!= NULL
; nchild
= nchild
->next
) {
1858 if (nchild
->tok
!= MDOC_It
)
1860 if ((nnext
= nchild
->head
->child
) == NULL
)
1862 if (nnext
->type
== ROFFT_BLOCK
)
1863 nnext
= nnext
->body
->child
;
1864 if (nnext
== NULL
|| nnext
->tok
!= MDOC_Er
)
1866 nnext
= nnext
->child
;
1867 if (prev_Er
!= NULL
) {
1868 order
= strcmp(prev_Er
, nnext
->string
);
1870 mandoc_msg(MANDOCERR_ER_ORDER
,
1871 nnext
->line
, nnext
->pos
,
1872 "Er %s %s (NetBSD)",
1873 prev_Er
, nnext
->string
);
1874 else if (order
== 0)
1875 mandoc_msg(MANDOCERR_ER_REP
,
1876 nnext
->line
, nnext
->pos
,
1877 "Er %s (NetBSD)", prev_Er
);
1879 prev_Er
= nnext
->string
;
1886 struct roff_node
*n
;
1890 if (n
->type
== ROFFT_BLOCK
&& n
->body
->child
== NULL
) {
1891 mandoc_msg(MANDOCERR_BLK_EMPTY
, n
->line
, n
->pos
, "Bk");
1892 roff_node_delete(mdoc
, n
);
1899 struct roff_node
*nch
;
1901 nch
= mdoc
->last
->child
;
1904 mdoc
->flags
^= MDOC_SMOFF
;
1908 assert(nch
->type
== ROFFT_TEXT
);
1910 if ( ! strcmp(nch
->string
, "on")) {
1911 mdoc
->flags
&= ~MDOC_SMOFF
;
1914 if ( ! strcmp(nch
->string
, "off")) {
1915 mdoc
->flags
|= MDOC_SMOFF
;
1919 mandoc_msg(MANDOCERR_SM_BAD
, nch
->line
, nch
->pos
,
1920 "%s %s", roff_name
[mdoc
->last
->tok
], nch
->string
);
1921 roff_node_relink(mdoc
, nch
);
1926 post_root(POST_ARGS
)
1928 struct roff_node
*n
;
1930 /* Add missing prologue data. */
1932 if (mdoc
->meta
.date
== NULL
)
1933 mdoc
->meta
.date
= mandoc_normdate(NULL
, NULL
);
1935 if (mdoc
->meta
.title
== NULL
) {
1936 mandoc_msg(MANDOCERR_DT_NOTITLE
, 0, 0, "EOF");
1937 mdoc
->meta
.title
= mandoc_strdup("UNTITLED");
1940 if (mdoc
->meta
.vol
== NULL
)
1941 mdoc
->meta
.vol
= mandoc_strdup("LOCAL");
1943 if (mdoc
->meta
.os
== NULL
) {
1944 mandoc_msg(MANDOCERR_OS_MISSING
, 0, 0, NULL
);
1945 mdoc
->meta
.os
= mandoc_strdup("");
1946 } else if (mdoc
->meta
.os_e
&&
1947 (mdoc
->meta
.rcsids
& (1 << mdoc
->meta
.os_e
)) == 0)
1948 mandoc_msg(MANDOCERR_RCS_MISSING
, 0, 0,
1949 mdoc
->meta
.os_e
== MANDOC_OS_OPENBSD
?
1950 "(OpenBSD)" : "(NetBSD)");
1952 if (mdoc
->meta
.arch
!= NULL
&&
1953 arch_valid(mdoc
->meta
.arch
, mdoc
->meta
.os_e
) == 0) {
1954 n
= mdoc
->meta
.first
->child
;
1955 while (n
->tok
!= MDOC_Dt
||
1957 n
->child
->next
== NULL
||
1958 n
->child
->next
->next
== NULL
)
1960 n
= n
->child
->next
->next
;
1961 mandoc_msg(MANDOCERR_ARCH_BAD
, n
->line
, n
->pos
,
1962 "Dt ... %s %s", mdoc
->meta
.arch
,
1963 mdoc
->meta
.os_e
== MANDOC_OS_OPENBSD
?
1964 "(OpenBSD)" : "(NetBSD)");
1967 /* Check that we begin with a proper `Sh'. */
1969 n
= mdoc
->meta
.first
->child
;
1971 (n
->type
== ROFFT_COMMENT
||
1972 (n
->tok
>= MDOC_Dd
&&
1973 mdoc_macro(n
->tok
)->flags
& MDOC_PROLOGUE
)))
1977 mandoc_msg(MANDOCERR_DOC_EMPTY
, 0, 0, NULL
);
1978 else if (n
->tok
!= MDOC_Sh
)
1979 mandoc_msg(MANDOCERR_SEC_BEFORE
, n
->line
, n
->pos
,
1980 "%s", roff_name
[n
->tok
]);
1986 struct roff_node
*np
, *nch
, *next
, *prev
;
1991 if (np
->type
!= ROFFT_BODY
)
1994 if (np
->child
== NULL
) {
1995 mandoc_msg(MANDOCERR_RS_EMPTY
, np
->line
, np
->pos
, "Rs");
2000 * The full `Rs' block needs special handling to order the
2001 * sub-elements according to `rsord'. Pick through each element
2002 * and correctly order it. This is an insertion sort.
2006 for (nch
= np
->child
->next
; nch
!= NULL
; nch
= next
) {
2007 /* Determine order number of this child. */
2008 for (i
= 0; i
< RSORD_MAX
; i
++)
2009 if (rsord
[i
] == nch
->tok
)
2012 if (i
== RSORD_MAX
) {
2013 mandoc_msg(MANDOCERR_RS_BAD
, nch
->line
, nch
->pos
,
2014 "%s", roff_name
[nch
->tok
]);
2016 } else if (nch
->tok
== MDOC__J
|| nch
->tok
== MDOC__B
)
2017 np
->norm
->Rs
.quote_T
++;
2020 * Remove this child from the chain. This somewhat
2021 * repeats roff_node_unlink(), but since we're
2022 * just re-ordering, there's no need for the
2023 * full unlink process.
2026 if ((next
= nch
->next
) != NULL
)
2027 next
->prev
= nch
->prev
;
2029 if ((prev
= nch
->prev
) != NULL
)
2030 prev
->next
= nch
->next
;
2032 nch
->prev
= nch
->next
= NULL
;
2035 * Scan back until we reach a node that's
2036 * to be ordered before this child.
2039 for ( ; prev
; prev
= prev
->prev
) {
2040 /* Determine order of `prev'. */
2041 for (j
= 0; j
< RSORD_MAX
; j
++)
2042 if (rsord
[j
] == prev
->tok
)
2052 * Set this child back into its correct place
2053 * in front of the `prev' node.
2059 np
->child
->prev
= nch
;
2060 nch
->next
= np
->child
;
2064 prev
->next
->prev
= nch
;
2065 nch
->next
= prev
->next
;
2072 * For some arguments of some macros,
2073 * convert all breakable hyphens into ASCII_HYPH.
2076 post_hyph(POST_ARGS
)
2078 struct roff_node
*nch
;
2081 for (nch
= mdoc
->last
->child
; nch
!= NULL
; nch
= nch
->next
) {
2082 if (nch
->type
!= ROFFT_TEXT
)
2087 while (*(++cp
) != '\0')
2089 isalpha((unsigned char)cp
[-1]) &&
2090 isalpha((unsigned char)cp
[1]))
2098 struct roff_node
*n
;
2101 if (n
->flags
& NODE_LINE
||
2102 (n
->next
!= NULL
&& n
->next
->flags
& NODE_DELIMC
))
2103 mandoc_msg(MANDOCERR_NS_SKIP
, n
->line
, n
->pos
, NULL
);
2119 switch (mdoc
->last
->type
) {
2124 switch (mdoc
->lastsec
) {
2129 post_sh_see_also(mdoc
);
2132 post_sh_authors(mdoc
);
2144 post_sh_name(POST_ARGS
)
2146 struct roff_node
*n
;
2151 for (n
= mdoc
->last
->child
; n
!= NULL
; n
= n
->next
) {
2154 if (hasnm
&& n
->child
!= NULL
)
2155 mandoc_msg(MANDOCERR_NAMESEC_PUNCT
,
2157 "Nm %s", n
->child
->string
);
2162 if (n
->next
!= NULL
)
2163 mandoc_msg(MANDOCERR_NAMESEC_ND
,
2164 n
->line
, n
->pos
, NULL
);
2167 if (n
->type
== ROFFT_TEXT
&&
2168 n
->string
[0] == ',' && n
->string
[1] == '\0' &&
2169 n
->next
!= NULL
&& n
->next
->tok
== MDOC_Nm
) {
2175 mandoc_msg(MANDOCERR_NAMESEC_BAD
,
2176 n
->line
, n
->pos
, "%s", roff_name
[n
->tok
]);
2183 mandoc_msg(MANDOCERR_NAMESEC_NONM
,
2184 mdoc
->last
->line
, mdoc
->last
->pos
, NULL
);
2186 mandoc_msg(MANDOCERR_NAMESEC_NOND
,
2187 mdoc
->last
->line
, mdoc
->last
->pos
, NULL
);
2191 post_sh_see_also(POST_ARGS
)
2193 const struct roff_node
*n
;
2194 const char *name
, *sec
;
2195 const char *lastname
, *lastsec
, *lastpunct
;
2198 n
= mdoc
->last
->child
;
2199 lastname
= lastsec
= lastpunct
= NULL
;
2201 if (n
->tok
!= MDOC_Xr
||
2203 n
->child
->next
== NULL
)
2206 /* Process one .Xr node. */
2208 name
= n
->child
->string
;
2209 sec
= n
->child
->next
->string
;
2210 if (lastsec
!= NULL
) {
2211 if (lastpunct
[0] != ',' || lastpunct
[1] != '\0')
2212 mandoc_msg(MANDOCERR_XR_PUNCT
, n
->line
,
2213 n
->pos
, "%s before %s(%s)",
2214 lastpunct
, name
, sec
);
2215 cmp
= strcmp(lastsec
, sec
);
2217 mandoc_msg(MANDOCERR_XR_ORDER
, n
->line
,
2218 n
->pos
, "%s(%s) after %s(%s)",
2219 name
, sec
, lastname
, lastsec
);
2220 else if (cmp
== 0 &&
2221 strcasecmp(lastname
, name
) > 0)
2222 mandoc_msg(MANDOCERR_XR_ORDER
, n
->line
,
2223 n
->pos
, "%s after %s", name
, lastname
);
2228 /* Process the following node. */
2233 if (n
->tok
== MDOC_Xr
) {
2237 if (n
->type
!= ROFFT_TEXT
)
2239 for (name
= n
->string
; *name
!= '\0'; name
++)
2240 if (isalpha((const unsigned char)*name
))
2242 lastpunct
= n
->string
;
2243 if (n
->next
== NULL
|| n
->next
->tok
== MDOC_Rs
)
2244 mandoc_msg(MANDOCERR_XR_PUNCT
, n
->line
,
2245 n
->pos
, "%s after %s(%s)",
2246 lastpunct
, lastname
, lastsec
);
2252 child_an(const struct roff_node
*n
)
2255 for (n
= n
->child
; n
!= NULL
; n
= n
->next
)
2256 if ((n
->tok
== MDOC_An
&& n
->child
!= NULL
) || child_an(n
))
2262 post_sh_authors(POST_ARGS
)
2265 if ( ! child_an(mdoc
->last
))
2266 mandoc_msg(MANDOCERR_AN_MISSING
,
2267 mdoc
->last
->line
, mdoc
->last
->pos
, NULL
);
2271 * Return an upper bound for the string distance (allowing
2272 * transpositions). Not a full Levenshtein implementation
2273 * because Levenshtein is quadratic in the string length
2274 * and this function is called for every standard name,
2275 * so the check for each custom name would be cubic.
2276 * The following crude heuristics is linear, resulting
2277 * in quadratic behaviour for checking one custom name,
2278 * which does not cause measurable slowdown.
2281 similar(const char *s1
, const char *s2
)
2283 const int maxdist
= 3;
2286 while (s1
[0] != '\0' && s2
[0] != '\0') {
2287 if (s1
[0] == s2
[0]) {
2292 if (++dist
> maxdist
)
2294 if (s1
[1] == s2
[1]) { /* replacement */
2297 } else if (s1
[0] == s2
[1] && s1
[1] == s2
[0]) {
2298 s1
+= 2; /* transposition */
2300 } else if (s1
[0] == s2
[1]) /* insertion */
2302 else if (s1
[1] == s2
[0]) /* deletion */
2307 dist
+= strlen(s1
) + strlen(s2
);
2308 return dist
> maxdist
? INT_MAX
: dist
;
2312 post_sh_head(POST_ARGS
)
2314 struct roff_node
*nch
;
2315 const char *goodsec
;
2316 const char *const *testsec
;
2321 * Process a new section. Sections are either "named" or
2322 * "custom". Custom sections are user-defined, while named ones
2323 * follow a conventional order and may only appear in certain
2327 sec
= mdoc
->last
->sec
;
2329 /* The NAME should be first. */
2331 if (sec
!= SEC_NAME
&& mdoc
->lastnamed
== SEC_NONE
)
2332 mandoc_msg(MANDOCERR_NAMESEC_FIRST
,
2333 mdoc
->last
->line
, mdoc
->last
->pos
, "Sh %s",
2334 sec
!= SEC_CUSTOM
? secnames
[sec
] :
2335 (nch
= mdoc
->last
->child
) == NULL
? "" :
2336 nch
->type
== ROFFT_TEXT
? nch
->string
:
2337 roff_name
[nch
->tok
]);
2339 /* The SYNOPSIS gets special attention in other areas. */
2341 if (sec
== SEC_SYNOPSIS
) {
2342 roff_setreg(mdoc
->roff
, "nS", 1, '=');
2343 mdoc
->flags
|= MDOC_SYNOPSIS
;
2345 roff_setreg(mdoc
->roff
, "nS", 0, '=');
2346 mdoc
->flags
&= ~MDOC_SYNOPSIS
;
2349 /* Mark our last section. */
2351 mdoc
->lastsec
= sec
;
2353 /* We don't care about custom sections after this. */
2355 if (sec
== SEC_CUSTOM
) {
2356 if ((nch
= mdoc
->last
->child
) == NULL
||
2357 nch
->type
!= ROFFT_TEXT
|| nch
->next
!= NULL
)
2361 for (testsec
= secnames
+ 1; *testsec
!= NULL
; testsec
++) {
2362 dist
= similar(nch
->string
, *testsec
);
2363 if (dist
< mindist
) {
2368 if (goodsec
!= NULL
)
2369 mandoc_msg(MANDOCERR_SEC_TYPO
, nch
->line
, nch
->pos
,
2370 "Sh %s instead of %s", nch
->string
, goodsec
);
2375 * Check whether our non-custom section is being repeated or is
2379 if (sec
== mdoc
->lastnamed
)
2380 mandoc_msg(MANDOCERR_SEC_REP
, mdoc
->last
->line
,
2381 mdoc
->last
->pos
, "Sh %s", secnames
[sec
]);
2383 if (sec
< mdoc
->lastnamed
)
2384 mandoc_msg(MANDOCERR_SEC_ORDER
, mdoc
->last
->line
,
2385 mdoc
->last
->pos
, "Sh %s", secnames
[sec
]);
2387 /* Mark the last named section. */
2389 mdoc
->lastnamed
= sec
;
2391 /* Check particular section/manual conventions. */
2393 if (mdoc
->meta
.msec
== NULL
)
2399 if (*mdoc
->meta
.msec
== '4')
2401 goodsec
= "2, 3, 4, 9";
2403 case SEC_RETURN_VALUES
:
2405 if (*mdoc
->meta
.msec
== '2')
2407 if (*mdoc
->meta
.msec
== '3')
2409 if (NULL
== goodsec
)
2410 goodsec
= "2, 3, 9";
2413 if (*mdoc
->meta
.msec
== '9')
2415 if (NULL
== goodsec
)
2417 mandoc_msg(MANDOCERR_SEC_MSEC
,
2418 mdoc
->last
->line
, mdoc
->last
->pos
,
2419 "Sh %s for %s only", secnames
[sec
], goodsec
);
2429 struct roff_node
*n
, *nch
;
2433 if (nch
->next
== NULL
) {
2434 mandoc_msg(MANDOCERR_XR_NOSEC
,
2435 n
->line
, n
->pos
, "Xr %s", nch
->string
);
2437 assert(nch
->next
== n
->last
);
2438 if(mandoc_xr_add(nch
->next
->string
, nch
->string
,
2439 nch
->line
, nch
->pos
))
2440 mandoc_msg(MANDOCERR_XR_SELF
,
2441 nch
->line
, nch
->pos
, "Xr %s %s",
2442 nch
->string
, nch
->next
->string
);
2444 post_delim_nb(mdoc
);
2448 post_ignpar(POST_ARGS
)
2450 struct roff_node
*np
;
2452 switch (mdoc
->last
->type
) {
2466 if ((np
= mdoc
->last
->child
) != NULL
)
2467 if (np
->tok
== MDOC_Pp
||
2468 np
->tok
== ROFF_br
|| np
->tok
== ROFF_sp
) {
2469 mandoc_msg(MANDOCERR_PAR_SKIP
, np
->line
, np
->pos
,
2470 "%s after %s", roff_name
[np
->tok
],
2471 roff_name
[mdoc
->last
->tok
]);
2472 roff_node_delete(mdoc
, np
);
2475 if ((np
= mdoc
->last
->last
) != NULL
)
2476 if (np
->tok
== MDOC_Pp
|| np
->tok
== ROFF_br
) {
2477 mandoc_msg(MANDOCERR_PAR_SKIP
, np
->line
, np
->pos
,
2478 "%s at the end of %s", roff_name
[np
->tok
],
2479 roff_name
[mdoc
->last
->tok
]);
2480 roff_node_delete(mdoc
, np
);
2485 post_prevpar(POST_ARGS
)
2487 struct roff_node
*n
, *np
;
2490 if (n
->type
!= ROFFT_ELEM
&& n
->type
!= ROFFT_BLOCK
)
2492 if ((np
= roff_node_prev(n
)) == NULL
)
2496 * Don't allow `Pp' prior to a paragraph-type
2497 * block: `Pp' or non-compact `Bd' or `Bl'.
2500 if (np
->tok
!= MDOC_Pp
&& np
->tok
!= ROFF_br
)
2502 if (n
->tok
== MDOC_Bl
&& n
->norm
->Bl
.comp
)
2504 if (n
->tok
== MDOC_Bd
&& n
->norm
->Bd
.comp
)
2506 if (n
->tok
== MDOC_It
&& n
->parent
->norm
->Bl
.comp
)
2509 mandoc_msg(MANDOCERR_PAR_SKIP
, np
->line
, np
->pos
,
2510 "%s before %s", roff_name
[np
->tok
], roff_name
[n
->tok
]);
2511 roff_node_delete(mdoc
, np
);
2517 struct roff_node
*np
;
2522 if (np
->child
!= NULL
)
2523 mandoc_msg(MANDOCERR_ARG_SKIP
, np
->line
, np
->pos
,
2524 "%s %s", roff_name
[np
->tok
], np
->child
->string
);
2530 struct roff_node
*n
;
2533 n
->flags
|= NODE_NOPRT
;
2535 if (mdoc
->meta
.date
!= NULL
) {
2536 mandoc_msg(MANDOCERR_PROLOG_REP
, n
->line
, n
->pos
, "Dd");
2537 free(mdoc
->meta
.date
);
2538 } else if (mdoc
->flags
& MDOC_PBODY
)
2539 mandoc_msg(MANDOCERR_PROLOG_LATE
, n
->line
, n
->pos
, "Dd");
2540 else if (mdoc
->meta
.title
!= NULL
)
2541 mandoc_msg(MANDOCERR_PROLOG_ORDER
,
2542 n
->line
, n
->pos
, "Dd after Dt");
2543 else if (mdoc
->meta
.os
!= NULL
)
2544 mandoc_msg(MANDOCERR_PROLOG_ORDER
,
2545 n
->line
, n
->pos
, "Dd after Os");
2547 if (mdoc
->quick
&& n
!= NULL
)
2548 mdoc
->meta
.date
= mandoc_strdup("");
2550 mdoc
->meta
.date
= mandoc_normdate(n
->child
, n
);
2556 struct roff_node
*nn
, *n
;
2561 n
->flags
|= NODE_NOPRT
;
2563 if (mdoc
->flags
& MDOC_PBODY
) {
2564 mandoc_msg(MANDOCERR_DT_LATE
, n
->line
, n
->pos
, "Dt");
2568 if (mdoc
->meta
.title
!= NULL
)
2569 mandoc_msg(MANDOCERR_PROLOG_REP
, n
->line
, n
->pos
, "Dt");
2570 else if (mdoc
->meta
.os
!= NULL
)
2571 mandoc_msg(MANDOCERR_PROLOG_ORDER
,
2572 n
->line
, n
->pos
, "Dt after Os");
2574 free(mdoc
->meta
.title
);
2575 free(mdoc
->meta
.msec
);
2576 free(mdoc
->meta
.vol
);
2577 free(mdoc
->meta
.arch
);
2579 mdoc
->meta
.title
= NULL
;
2580 mdoc
->meta
.msec
= NULL
;
2581 mdoc
->meta
.vol
= NULL
;
2582 mdoc
->meta
.arch
= NULL
;
2584 /* Mandatory first argument: title. */
2587 if (nn
== NULL
|| *nn
->string
== '\0') {
2588 mandoc_msg(MANDOCERR_DT_NOTITLE
, n
->line
, n
->pos
, "Dt");
2589 mdoc
->meta
.title
= mandoc_strdup("UNTITLED");
2591 mdoc
->meta
.title
= mandoc_strdup(nn
->string
);
2593 /* Check that all characters are uppercase. */
2595 for (p
= nn
->string
; *p
!= '\0'; p
++)
2596 if (islower((unsigned char)*p
)) {
2597 mandoc_msg(MANDOCERR_TITLE_CASE
, nn
->line
,
2598 nn
->pos
+ (int)(p
- nn
->string
),
2599 "Dt %s", nn
->string
);
2604 /* Mandatory second argument: section. */
2610 mandoc_msg(MANDOCERR_MSEC_MISSING
, n
->line
, n
->pos
,
2611 "Dt %s", mdoc
->meta
.title
);
2612 mdoc
->meta
.vol
= mandoc_strdup("LOCAL");
2613 return; /* msec and arch remain NULL. */
2616 mdoc
->meta
.msec
= mandoc_strdup(nn
->string
);
2618 /* Infer volume title from section number. */
2620 cp
= mandoc_a2msec(nn
->string
);
2622 mandoc_msg(MANDOCERR_MSEC_BAD
,
2623 nn
->line
, nn
->pos
, "Dt ... %s", nn
->string
);
2624 mdoc
->meta
.vol
= mandoc_strdup(nn
->string
);
2626 mdoc
->meta
.vol
= mandoc_strdup(cp
);
2628 /* Optional third argument: architecture. */
2630 if ((nn
= nn
->next
) == NULL
)
2633 for (p
= nn
->string
; *p
!= '\0'; p
++)
2634 *p
= tolower((unsigned char)*p
);
2635 mdoc
->meta
.arch
= mandoc_strdup(nn
->string
);
2637 /* Ignore fourth and later arguments. */
2639 if ((nn
= nn
->next
) != NULL
)
2640 mandoc_msg(MANDOCERR_ARG_EXCESS
,
2641 nn
->line
, nn
->pos
, "Dt ... %s", nn
->string
);
2647 struct roff_node
*n
, *nch
;
2650 post_delim_nb(mdoc
);
2656 macro
= !strcmp(nch
->string
, "Open") ? "Ox" :
2657 !strcmp(nch
->string
, "Net") ? "Nx" :
2658 !strcmp(nch
->string
, "Free") ? "Fx" :
2659 !strcmp(nch
->string
, "DragonFly") ? "Dx" : NULL
;
2661 mandoc_msg(MANDOCERR_BX
,
2662 n
->line
, n
->pos
, "%s", macro
);
2665 mdoc
->next
= ROFF_NEXT_SIBLING
;
2666 roff_elem_alloc(mdoc
, n
->line
, n
->pos
, MDOC_Ns
);
2667 mdoc
->last
->flags
|= NODE_NOSRC
;
2668 mdoc
->next
= ROFF_NEXT_SIBLING
;
2670 mdoc
->next
= ROFF_NEXT_CHILD
;
2671 roff_word_alloc(mdoc
, n
->line
, n
->pos
, "BSD");
2672 mdoc
->last
->flags
|= NODE_NOSRC
;
2679 roff_elem_alloc(mdoc
, n
->line
, n
->pos
, MDOC_Ns
);
2680 mdoc
->last
->flags
|= NODE_NOSRC
;
2681 mdoc
->next
= ROFF_NEXT_SIBLING
;
2682 roff_word_alloc(mdoc
, n
->line
, n
->pos
, "-");
2683 mdoc
->last
->flags
|= NODE_NOSRC
;
2684 roff_elem_alloc(mdoc
, n
->line
, n
->pos
, MDOC_Ns
);
2685 mdoc
->last
->flags
|= NODE_NOSRC
;
2689 * Make `Bx's second argument always start with an uppercase
2690 * letter. Groff checks if it's an "accepted" term, but we just
2691 * uppercase blindly.
2694 *nch
->string
= (char)toupper((unsigned char)*nch
->string
);
2701 struct utsname utsname
;
2702 static char *defbuf
;
2704 struct roff_node
*n
;
2707 n
->flags
|= NODE_NOPRT
;
2709 if (mdoc
->meta
.os
!= NULL
)
2710 mandoc_msg(MANDOCERR_PROLOG_REP
, n
->line
, n
->pos
, "Os");
2711 else if (mdoc
->flags
& MDOC_PBODY
)
2712 mandoc_msg(MANDOCERR_PROLOG_LATE
, n
->line
, n
->pos
, "Os");
2717 * Set the operating system by way of the `Os' macro.
2718 * The order of precedence is:
2719 * 1. the argument of the `Os' macro, unless empty
2720 * 2. the -Ios=foo command line argument, if provided
2721 * 3. -DOSNAME="\"foo\"", if provided during compilation
2722 * 4. "sysname release" from uname(3)
2725 free(mdoc
->meta
.os
);
2726 mdoc
->meta
.os
= NULL
;
2727 deroff(&mdoc
->meta
.os
, n
);
2731 if (mdoc
->os_s
!= NULL
) {
2732 mdoc
->meta
.os
= mandoc_strdup(mdoc
->os_s
);
2737 mdoc
->meta
.os
= mandoc_strdup(OSNAME
);
2739 if (defbuf
== NULL
) {
2740 if (uname(&utsname
) == -1) {
2741 mandoc_msg(MANDOCERR_OS_UNAME
, n
->line
, n
->pos
, "Os");
2742 defbuf
= mandoc_strdup("UNKNOWN");
2744 mandoc_asprintf(&defbuf
, "%s %s",
2745 utsname
.sysname
, utsname
.release
);
2747 mdoc
->meta
.os
= mandoc_strdup(defbuf
);
2751 if (mdoc
->meta
.os_e
== MANDOC_OS_OTHER
) {
2752 if (strstr(mdoc
->meta
.os
, "OpenBSD") != NULL
)
2753 mdoc
->meta
.os_e
= MANDOC_OS_OPENBSD
;
2754 else if (strstr(mdoc
->meta
.os
, "NetBSD") != NULL
)
2755 mdoc
->meta
.os_e
= MANDOC_OS_NETBSD
;
2759 * This is the earliest point where we can check
2760 * Mdocdate conventions because we don't know
2761 * the operating system earlier.
2764 if (n
->child
!= NULL
)
2765 mandoc_msg(MANDOCERR_OS_ARG
, n
->child
->line
, n
->child
->pos
,
2766 "Os %s (%s)", n
->child
->string
,
2767 mdoc
->meta
.os_e
== MANDOC_OS_OPENBSD
?
2768 "OpenBSD" : "NetBSD");
2770 while (n
->tok
!= MDOC_Dd
)
2771 if ((n
= n
->prev
) == NULL
)
2773 if ((n
= n
->child
) == NULL
)
2775 if (strncmp(n
->string
, "$" "Mdocdate", 9)) {
2776 if (mdoc
->meta
.os_e
== MANDOC_OS_OPENBSD
)
2777 mandoc_msg(MANDOCERR_MDOCDATE_MISSING
, n
->line
,
2778 n
->pos
, "Dd %s (OpenBSD)", n
->string
);
2780 if (mdoc
->meta
.os_e
== MANDOC_OS_NETBSD
)
2781 mandoc_msg(MANDOCERR_MDOCDATE
, n
->line
,
2782 n
->pos
, "Dd %s (NetBSD)", n
->string
);
2787 mdoc_a2sec(const char *p
)
2791 for (i
= 0; i
< (int)SEC__MAX
; i
++)
2792 if (secnames
[i
] && 0 == strcmp(p
, secnames
[i
]))
2793 return (enum roff_sec
)i
;
2799 macro2len(enum roff_tok macro
)