]>
git.cameronkatri.com Git - mandoc.git/blob - mdoc_validate.c
589fd794847a985f7e55ce4a9be90336947c9465
1 /* $Id: mdoc_validate.c,v 1.331 2017/06/07 23:29:48 schwarze Exp $ */
3 * Copyright (c) 2008-2012 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2010-2017 Ingo Schwarze <schwarze@openbsd.org>
5 * Copyright (c) 2010 Joerg Sonnenberger <joerg@netbsd.org>
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 #include <sys/types.h>
23 #include <sys/utsname.h>
34 #include "mandoc_aux.h"
38 #include "libmandoc.h"
42 /* FIXME: .Bl -diag can't have non-text children in HEAD. */
44 #define POST_ARGS struct roff_man *mdoc
52 typedef void (*v_post
)(POST_ARGS
);
54 static int build_list(struct roff_man
*, int);
55 static void check_text(struct roff_man
*, int, int, char *);
56 static void check_bsd(struct roff_man
*, int, int, char *);
57 static void check_argv(struct roff_man
*,
58 struct roff_node
*, struct mdoc_argv
*);
59 static void check_args(struct roff_man
*, struct roff_node
*);
60 static int child_an(const struct roff_node
*);
61 static size_t macro2len(enum roff_tok
);
62 static void rewrite_macro2len(struct roff_man
*, char **);
64 static void post_an(POST_ARGS
);
65 static void post_an_norm(POST_ARGS
);
66 static void post_at(POST_ARGS
);
67 static void post_bd(POST_ARGS
);
68 static void post_bf(POST_ARGS
);
69 static void post_bk(POST_ARGS
);
70 static void post_bl(POST_ARGS
);
71 static void post_bl_block(POST_ARGS
);
72 static void post_bl_head(POST_ARGS
);
73 static void post_bl_norm(POST_ARGS
);
74 static void post_bx(POST_ARGS
);
75 static void post_defaults(POST_ARGS
);
76 static void post_display(POST_ARGS
);
77 static void post_dd(POST_ARGS
);
78 static void post_dt(POST_ARGS
);
79 static void post_en(POST_ARGS
);
80 static void post_es(POST_ARGS
);
81 static void post_eoln(POST_ARGS
);
82 static void post_ex(POST_ARGS
);
83 static void post_fa(POST_ARGS
);
84 static void post_fn(POST_ARGS
);
85 static void post_fname(POST_ARGS
);
86 static void post_fo(POST_ARGS
);
87 static void post_hyph(POST_ARGS
);
88 static void post_ignpar(POST_ARGS
);
89 static void post_it(POST_ARGS
);
90 static void post_lb(POST_ARGS
);
91 static void post_nd(POST_ARGS
);
92 static void post_nm(POST_ARGS
);
93 static void post_ns(POST_ARGS
);
94 static void post_obsolete(POST_ARGS
);
95 static void post_os(POST_ARGS
);
96 static void post_par(POST_ARGS
);
97 static void post_prevpar(POST_ARGS
);
98 static void post_root(POST_ARGS
);
99 static void post_rs(POST_ARGS
);
100 static void post_rv(POST_ARGS
);
101 static void post_sh(POST_ARGS
);
102 static void post_sh_head(POST_ARGS
);
103 static void post_sh_name(POST_ARGS
);
104 static void post_sh_see_also(POST_ARGS
);
105 static void post_sh_authors(POST_ARGS
);
106 static void post_sm(POST_ARGS
);
107 static void post_st(POST_ARGS
);
108 static void post_std(POST_ARGS
);
109 static void post_useless(POST_ARGS
);
110 static void post_xr(POST_ARGS
);
111 static void post_xx(POST_ARGS
);
113 static const v_post __mdoc_valids
[MDOC_MAX
- MDOC_Dd
] = {
118 post_ignpar
, /* Ss */
120 post_display
, /* D1 */
121 post_display
, /* Dl */
122 post_display
, /* Bd */
130 post_defaults
, /* Ar */
144 post_defaults
, /* Li */
148 post_obsolete
, /* Ot */
149 post_defaults
, /* Pa */
156 post_hyph
, /* %B */ /* FIXME: can be used outside Rs/Re. */
164 post_hyph
, /* %T */ /* FIXME: can be used outside Rs/Re. */
176 post_obsolete
, /* Db */
206 post_useless
, /* Tn */
218 post_obsolete
, /* Fr */
223 post_defaults
, /* Mt */
235 static const v_post
*const mdoc_valids
= __mdoc_valids
- MDOC_Dd
;
237 #define RSORD_MAX 14 /* Number of `Rs' blocks. */
239 static const enum roff_tok rsord
[RSORD_MAX
] = {
256 static const char * const secnames
[SEC__MAX
] = {
263 "IMPLEMENTATION NOTES",
278 "SECURITY CONSIDERATIONS",
284 mdoc_node_validate(struct roff_man
*mdoc
)
290 mdoc
->last
= mdoc
->last
->child
;
291 while (mdoc
->last
!= NULL
) {
292 mdoc_node_validate(mdoc
);
294 mdoc
->last
= mdoc
->last
->child
;
296 mdoc
->last
= mdoc
->last
->next
;
300 mdoc
->next
= ROFF_NEXT_SIBLING
;
303 if (n
->sec
!= SEC_SYNOPSIS
||
304 (n
->parent
->tok
!= MDOC_Cd
&& n
->parent
->tok
!= MDOC_Fd
))
305 check_text(mdoc
, n
->line
, n
->pos
, n
->string
);
306 if (n
->parent
->tok
== MDOC_Sh
||
307 n
->parent
->tok
== MDOC_Ss
||
308 n
->parent
->tok
== MDOC_It
)
309 check_bsd(mdoc
, n
->line
, n
->pos
, n
->string
);
318 check_args(mdoc
, mdoc
->last
);
321 * Closing delimiters are not special at the
322 * beginning of a block, opening delimiters
323 * are not special at the end.
326 if (n
->child
!= NULL
)
327 n
->child
->flags
&= ~NODE_DELIMC
;
329 n
->last
->flags
&= ~NODE_DELIMO
;
331 /* Call the macro's postprocessor. */
333 if (n
->tok
< ROFF_MAX
) {
346 assert(n
->tok
>= MDOC_Dd
&& n
->tok
< MDOC_MAX
);
347 p
= mdoc_valids
+ n
->tok
;
357 check_args(struct roff_man
*mdoc
, struct roff_node
*n
)
364 assert(n
->args
->argc
);
365 for (i
= 0; i
< (int)n
->args
->argc
; i
++)
366 check_argv(mdoc
, n
, &n
->args
->argv
[i
]);
370 check_argv(struct roff_man
*mdoc
, struct roff_node
*n
, struct mdoc_argv
*v
)
374 for (i
= 0; i
< (int)v
->sz
; i
++)
375 check_text(mdoc
, v
->line
, v
->pos
, v
->value
[i
]);
379 check_text(struct roff_man
*mdoc
, int ln
, int pos
, char *p
)
383 if (MDOC_LITERAL
& mdoc
->flags
)
386 for (cp
= p
; NULL
!= (p
= strchr(p
, '\t')); p
++)
387 mandoc_msg(MANDOCERR_FI_TAB
, mdoc
->parse
,
388 ln
, pos
+ (int)(p
- cp
), NULL
);
392 check_bsd(struct roff_man
*mdoc
, int ln
, int pos
, char *p
)
396 if ((cp
= strstr(p
, "OpenBSD")) != NULL
)
397 mandoc_msg(MANDOCERR_BX
, mdoc
->parse
,
398 ln
, pos
+ (cp
- p
), "Ox");
399 if ((cp
= strstr(p
, "NetBSD")) != NULL
)
400 mandoc_msg(MANDOCERR_BX
, mdoc
->parse
,
401 ln
, pos
+ (cp
- p
), "Nx");
402 if ((cp
= strstr(p
, "FreeBSD")) != NULL
)
403 mandoc_msg(MANDOCERR_BX
, mdoc
->parse
,
404 ln
, pos
+ (cp
- p
), "Fx");
405 if ((cp
= strstr(p
, "DragonFly")) != NULL
)
406 mandoc_msg(MANDOCERR_BX
, mdoc
->parse
,
407 ln
, pos
+ (cp
- p
), "Dx");
411 post_bl_norm(POST_ARGS
)
414 struct mdoc_argv
*argv
, *wa
;
416 enum mdocargt mdoclt
;
419 n
= mdoc
->last
->parent
;
420 n
->norm
->Bl
.type
= LIST__NONE
;
423 * First figure out which kind of list to use: bind ourselves to
424 * the first mentioned list type and warn about any remaining
425 * ones. If we find no list type, we default to LIST_item.
428 wa
= (n
->args
== NULL
) ? NULL
: n
->args
->argv
;
429 mdoclt
= MDOC_ARG_MAX
;
430 for (i
= 0; n
->args
&& i
< (int)n
->args
->argc
; i
++) {
431 argv
= n
->args
->argv
+ i
;
434 /* Set list types. */
468 /* Set list arguments. */
470 if (n
->norm
->Bl
.comp
)
471 mandoc_msg(MANDOCERR_ARG_REP
,
472 mdoc
->parse
, argv
->line
,
473 argv
->pos
, "Bl -compact");
474 n
->norm
->Bl
.comp
= 1;
479 mandoc_msg(MANDOCERR_ARG_EMPTY
,
480 mdoc
->parse
, argv
->line
,
481 argv
->pos
, "Bl -width");
482 n
->norm
->Bl
.width
= "0n";
485 if (NULL
!= n
->norm
->Bl
.width
)
486 mandoc_vmsg(MANDOCERR_ARG_REP
,
487 mdoc
->parse
, argv
->line
,
488 argv
->pos
, "Bl -width %s",
490 rewrite_macro2len(mdoc
, argv
->value
);
491 n
->norm
->Bl
.width
= argv
->value
[0];
495 mandoc_msg(MANDOCERR_ARG_EMPTY
,
496 mdoc
->parse
, argv
->line
,
497 argv
->pos
, "Bl -offset");
500 if (NULL
!= n
->norm
->Bl
.offs
)
501 mandoc_vmsg(MANDOCERR_ARG_REP
,
502 mdoc
->parse
, argv
->line
,
503 argv
->pos
, "Bl -offset %s",
505 rewrite_macro2len(mdoc
, argv
->value
);
506 n
->norm
->Bl
.offs
= argv
->value
[0];
511 if (LIST__NONE
== lt
)
515 /* Check: multiple list types. */
517 if (LIST__NONE
!= n
->norm
->Bl
.type
) {
518 mandoc_vmsg(MANDOCERR_BL_REP
,
519 mdoc
->parse
, n
->line
, n
->pos
,
520 "Bl -%s", mdoc_argnames
[argv
->arg
]);
524 /* The list type should come first. */
526 if (n
->norm
->Bl
.width
||
529 mandoc_vmsg(MANDOCERR_BL_LATETYPE
,
530 mdoc
->parse
, n
->line
, n
->pos
, "Bl -%s",
531 mdoc_argnames
[n
->args
->argv
[0].arg
]);
533 n
->norm
->Bl
.type
= lt
;
534 if (LIST_column
== lt
) {
535 n
->norm
->Bl
.ncols
= argv
->sz
;
536 n
->norm
->Bl
.cols
= (void *)argv
->value
;
540 /* Allow lists to default to LIST_item. */
542 if (LIST__NONE
== n
->norm
->Bl
.type
) {
543 mandoc_msg(MANDOCERR_BL_NOTYPE
, mdoc
->parse
,
544 n
->line
, n
->pos
, "Bl");
545 n
->norm
->Bl
.type
= LIST_item
;
550 * Validate the width field. Some list types don't need width
551 * types and should be warned about them. Others should have it
552 * and must also be warned. Yet others have a default and need
556 switch (n
->norm
->Bl
.type
) {
558 if (NULL
== n
->norm
->Bl
.width
)
559 mandoc_msg(MANDOCERR_BL_NOWIDTH
, mdoc
->parse
,
560 n
->line
, n
->pos
, "Bl -tag");
567 if (n
->norm
->Bl
.width
)
568 mandoc_vmsg(MANDOCERR_BL_SKIPW
, mdoc
->parse
,
569 wa
->line
, wa
->pos
, "Bl -%s",
570 mdoc_argnames
[mdoclt
]);
575 if (NULL
== n
->norm
->Bl
.width
)
576 n
->norm
->Bl
.width
= "2n";
579 if (NULL
== n
->norm
->Bl
.width
)
580 n
->norm
->Bl
.width
= "3n";
591 struct mdoc_argv
*argv
;
596 for (i
= 0; n
->args
&& i
< (int)n
->args
->argc
; i
++) {
597 argv
= n
->args
->argv
+ i
;
617 mandoc_msg(MANDOCERR_BD_FILE
, mdoc
->parse
,
618 n
->line
, n
->pos
, NULL
);
622 mandoc_msg(MANDOCERR_ARG_EMPTY
,
623 mdoc
->parse
, argv
->line
,
624 argv
->pos
, "Bd -offset");
627 if (NULL
!= n
->norm
->Bd
.offs
)
628 mandoc_vmsg(MANDOCERR_ARG_REP
,
629 mdoc
->parse
, argv
->line
,
630 argv
->pos
, "Bd -offset %s",
632 rewrite_macro2len(mdoc
, argv
->value
);
633 n
->norm
->Bd
.offs
= argv
->value
[0];
636 if (n
->norm
->Bd
.comp
)
637 mandoc_msg(MANDOCERR_ARG_REP
,
638 mdoc
->parse
, argv
->line
,
639 argv
->pos
, "Bd -compact");
640 n
->norm
->Bd
.comp
= 1;
645 if (DISP__NONE
== dt
)
648 if (DISP__NONE
== n
->norm
->Bd
.type
)
649 n
->norm
->Bd
.type
= dt
;
651 mandoc_vmsg(MANDOCERR_BD_REP
,
652 mdoc
->parse
, n
->line
, n
->pos
,
653 "Bd -%s", mdoc_argnames
[argv
->arg
]);
656 if (DISP__NONE
== n
->norm
->Bd
.type
) {
657 mandoc_msg(MANDOCERR_BD_NOTYPE
, mdoc
->parse
,
658 n
->line
, n
->pos
, "Bd");
659 n
->norm
->Bd
.type
= DISP_ragged
;
664 * Stand-alone line macros.
668 post_an_norm(POST_ARGS
)
671 struct mdoc_argv
*argv
;
678 for (i
= 1; i
< n
->args
->argc
; i
++) {
679 argv
= n
->args
->argv
+ i
;
680 mandoc_vmsg(MANDOCERR_AN_REP
,
681 mdoc
->parse
, argv
->line
, argv
->pos
,
682 "An -%s", mdoc_argnames
[argv
->arg
]);
685 argv
= n
->args
->argv
;
686 if (argv
->arg
== MDOC_Split
)
687 n
->norm
->An
.auth
= AUTH_split
;
688 else if (argv
->arg
== MDOC_Nosplit
)
689 n
->norm
->An
.auth
= AUTH_nosplit
;
701 if (n
->child
!= NULL
)
702 mandoc_vmsg(MANDOCERR_ARG_SKIP
, mdoc
->parse
, n
->line
,
703 n
->pos
, "%s %s", roff_name
[n
->tok
], n
->child
->string
);
705 while (n
->child
!= NULL
)
706 roff_node_delete(mdoc
, n
->child
);
708 roff_word_alloc(mdoc
, n
->line
, n
->pos
, n
->tok
== MDOC_Bt
?
709 "is currently in beta test." : "currently under development.");
710 mdoc
->last
->flags
|= NODE_EOS
| NODE_NOSRC
;
715 build_list(struct roff_man
*mdoc
, int tok
)
720 n
= mdoc
->last
->next
;
721 for (ic
= 1;; ic
++) {
722 roff_elem_alloc(mdoc
, n
->line
, n
->pos
, tok
);
723 mdoc
->last
->flags
|= NODE_NOSRC
;
724 mdoc_node_relink(mdoc
, n
);
725 n
= mdoc
->last
= mdoc
->last
->parent
;
726 mdoc
->next
= ROFF_NEXT_SIBLING
;
729 if (ic
> 1 || n
->next
->next
!= NULL
) {
730 roff_word_alloc(mdoc
, n
->line
, n
->pos
, ",");
731 mdoc
->last
->flags
|= NODE_DELIMC
| NODE_NOSRC
;
733 n
= mdoc
->last
->next
;
734 if (n
->next
== NULL
) {
735 roff_word_alloc(mdoc
, n
->line
, n
->pos
, "and");
736 mdoc
->last
->flags
|= NODE_NOSRC
;
750 mdoc
->next
= ROFF_NEXT_CHILD
;
751 roff_word_alloc(mdoc
, n
->line
, n
->pos
, "The");
752 mdoc
->last
->flags
|= NODE_NOSRC
;
754 if (mdoc
->last
->next
!= NULL
)
755 ic
= build_list(mdoc
, MDOC_Nm
);
756 else if (mdoc
->meta
.name
!= NULL
) {
757 roff_elem_alloc(mdoc
, n
->line
, n
->pos
, MDOC_Nm
);
758 mdoc
->last
->flags
|= NODE_NOSRC
;
759 roff_word_alloc(mdoc
, n
->line
, n
->pos
, mdoc
->meta
.name
);
760 mdoc
->last
->flags
|= NODE_NOSRC
;
761 mdoc
->last
= mdoc
->last
->parent
;
762 mdoc
->next
= ROFF_NEXT_SIBLING
;
765 mandoc_msg(MANDOCERR_EX_NONAME
, mdoc
->parse
,
766 n
->line
, n
->pos
, "Ex");
770 roff_word_alloc(mdoc
, n
->line
, n
->pos
,
771 ic
> 1 ? "utilities exit\\~0" : "utility exits\\~0");
772 mdoc
->last
->flags
|= NODE_NOSRC
;
773 roff_word_alloc(mdoc
, n
->line
, n
->pos
,
774 "on success, and\\~>0 if an error occurs.");
775 mdoc
->last
->flags
|= NODE_EOS
| NODE_NOSRC
;
786 assert(n
->child
->type
== ROFFT_TEXT
);
787 mdoc
->next
= ROFF_NEXT_CHILD
;
789 if ((p
= mdoc_a2lib(n
->child
->string
)) != NULL
) {
790 n
->child
->flags
|= NODE_NOPRT
;
791 roff_word_alloc(mdoc
, n
->line
, n
->pos
, p
);
792 mdoc
->last
->flags
= NODE_NOSRC
;
797 roff_word_alloc(mdoc
, n
->line
, n
->pos
, "library");
798 mdoc
->last
->flags
= NODE_NOSRC
;
799 roff_word_alloc(mdoc
, n
->line
, n
->pos
, "\\(Lq");
800 mdoc
->last
->flags
= NODE_DELIMO
| NODE_NOSRC
;
801 mdoc
->last
= mdoc
->last
->next
;
802 roff_word_alloc(mdoc
, n
->line
, n
->pos
, "\\(Rq");
803 mdoc
->last
->flags
= NODE_DELIMC
| NODE_NOSRC
;
816 mdoc
->next
= ROFF_NEXT_CHILD
;
817 if (n
->child
!= NULL
) {
818 roff_word_alloc(mdoc
, n
->line
, n
->pos
, "The");
819 mdoc
->last
->flags
|= NODE_NOSRC
;
820 ic
= build_list(mdoc
, MDOC_Fn
);
821 roff_word_alloc(mdoc
, n
->line
, n
->pos
,
822 ic
> 1 ? "functions return" : "function returns");
823 mdoc
->last
->flags
|= NODE_NOSRC
;
824 roff_word_alloc(mdoc
, n
->line
, n
->pos
,
825 "the value\\~0 if successful;");
827 roff_word_alloc(mdoc
, n
->line
, n
->pos
, "Upon successful "
828 "completion, the value\\~0 is returned;");
829 mdoc
->last
->flags
|= NODE_NOSRC
;
831 roff_word_alloc(mdoc
, n
->line
, n
->pos
, "otherwise "
832 "the value\\~\\-1 is returned and the global variable");
833 mdoc
->last
->flags
|= NODE_NOSRC
;
834 roff_elem_alloc(mdoc
, n
->line
, n
->pos
, MDOC_Va
);
835 mdoc
->last
->flags
|= NODE_NOSRC
;
836 roff_word_alloc(mdoc
, n
->line
, n
->pos
, "errno");
837 mdoc
->last
->flags
|= NODE_NOSRC
;
838 mdoc
->last
= mdoc
->last
->parent
;
839 mdoc
->next
= ROFF_NEXT_SIBLING
;
840 roff_word_alloc(mdoc
, n
->line
, n
->pos
,
841 "is set to indicate the error.");
842 mdoc
->last
->flags
|= NODE_EOS
| NODE_NOSRC
;
852 if (n
->args
&& n
->args
->argc
== 1)
853 if (n
->args
->argv
[0].arg
== MDOC_Std
)
856 mandoc_msg(MANDOCERR_ARG_STD
, mdoc
->parse
,
857 n
->line
, n
->pos
, roff_name
[n
->tok
]);
863 struct roff_node
*n
, *nch
;
868 assert(nch
->type
== ROFFT_TEXT
);
870 if ((p
= mdoc_a2st(nch
->string
)) == NULL
) {
871 mandoc_vmsg(MANDOCERR_ST_BAD
, mdoc
->parse
,
872 nch
->line
, nch
->pos
, "St %s", nch
->string
);
873 roff_node_delete(mdoc
, n
);
877 nch
->flags
|= NODE_NOPRT
;
878 mdoc
->next
= ROFF_NEXT_CHILD
;
879 roff_word_alloc(mdoc
, nch
->line
, nch
->pos
, p
);
880 mdoc
->last
->flags
|= NODE_NOSRC
;
885 post_obsolete(POST_ARGS
)
890 if (n
->type
== ROFFT_ELEM
|| n
->type
== ROFFT_BLOCK
)
891 mandoc_msg(MANDOCERR_MACRO_OBS
, mdoc
->parse
,
892 n
->line
, n
->pos
, roff_name
[n
->tok
]);
896 post_useless(POST_ARGS
)
901 mandoc_msg(MANDOCERR_MACRO_USELESS
, mdoc
->parse
,
902 n
->line
, n
->pos
, roff_name
[n
->tok
]);
912 struct roff_node
*np
, *nch
;
915 * Unlike other data pointers, these are "housed" by the HEAD
916 * element, which contains the goods.
920 if (np
->type
!= ROFFT_HEAD
)
923 assert(np
->parent
->type
== ROFFT_BLOCK
);
924 assert(np
->parent
->tok
== MDOC_Bf
);
926 /* Check the number of arguments. */
929 if (np
->parent
->args
== NULL
) {
931 mandoc_msg(MANDOCERR_BF_NOFONT
, mdoc
->parse
,
932 np
->line
, np
->pos
, "Bf");
938 mandoc_vmsg(MANDOCERR_ARG_EXCESS
, mdoc
->parse
,
939 nch
->line
, nch
->pos
, "Bf ... %s", nch
->string
);
941 /* Extract argument into data. */
943 if (np
->parent
->args
!= NULL
) {
944 switch (np
->parent
->args
->argv
[0].arg
) {
946 np
->norm
->Bf
.font
= FONT_Em
;
949 np
->norm
->Bf
.font
= FONT_Li
;
952 np
->norm
->Bf
.font
= FONT_Sy
;
960 /* Extract parameter into data. */
962 if ( ! strcmp(np
->child
->string
, "Em"))
963 np
->norm
->Bf
.font
= FONT_Em
;
964 else if ( ! strcmp(np
->child
->string
, "Li"))
965 np
->norm
->Bf
.font
= FONT_Li
;
966 else if ( ! strcmp(np
->child
->string
, "Sy"))
967 np
->norm
->Bf
.font
= FONT_Sy
;
969 mandoc_vmsg(MANDOCERR_BF_BADFONT
, mdoc
->parse
,
970 np
->child
->line
, np
->child
->pos
,
971 "Bf %s", np
->child
->string
);
975 post_fname(POST_ARGS
)
977 const struct roff_node
*n
;
981 n
= mdoc
->last
->child
;
982 pos
= strcspn(n
->string
, "()");
983 cp
= n
->string
+ pos
;
984 if ( ! (cp
[0] == '\0' || (cp
[0] == '(' && cp
[1] == '*')))
985 mandoc_msg(MANDOCERR_FN_PAREN
, mdoc
->parse
,
986 n
->line
, n
->pos
+ pos
, n
->string
);
1000 const struct roff_node
*n
;
1004 if (n
->type
!= ROFFT_HEAD
)
1007 if (n
->child
== NULL
) {
1008 mandoc_msg(MANDOCERR_FO_NOHEAD
, mdoc
->parse
,
1009 n
->line
, n
->pos
, "Fo");
1012 if (n
->child
!= n
->last
) {
1013 mandoc_vmsg(MANDOCERR_ARG_EXCESS
, mdoc
->parse
,
1014 n
->child
->next
->line
, n
->child
->next
->pos
,
1015 "Fo ... %s", n
->child
->next
->string
);
1016 while (n
->child
!= n
->last
)
1017 roff_node_delete(mdoc
, n
->last
);
1026 const struct roff_node
*n
;
1029 for (n
= mdoc
->last
->child
; n
!= NULL
; n
= n
->next
) {
1030 for (cp
= n
->string
; *cp
!= '\0'; cp
++) {
1031 /* Ignore callbacks and alterations. */
1032 if (*cp
== '(' || *cp
== '{')
1036 mandoc_msg(MANDOCERR_FA_COMMA
, mdoc
->parse
,
1037 n
->line
, n
->pos
+ (cp
- n
->string
),
1047 struct roff_node
*n
;
1051 if (n
->last
!= NULL
&&
1052 (n
->last
->tok
== MDOC_Pp
||
1053 n
->last
->tok
== MDOC_Lp
))
1054 mdoc_node_relink(mdoc
, n
->last
);
1056 if (mdoc
->meta
.name
== NULL
)
1057 deroff(&mdoc
->meta
.name
, n
);
1059 if (mdoc
->meta
.name
== NULL
||
1060 (mdoc
->lastsec
== SEC_NAME
&& n
->child
== NULL
))
1061 mandoc_msg(MANDOCERR_NM_NONAME
, mdoc
->parse
,
1062 n
->line
, n
->pos
, "Nm");
1064 if ((n
->type
!= ROFFT_ELEM
&& n
->type
!= ROFFT_HEAD
) ||
1065 (n
->child
!= NULL
&& n
->child
->type
== ROFFT_TEXT
) ||
1066 mdoc
->meta
.name
== NULL
)
1069 mdoc
->next
= ROFF_NEXT_CHILD
;
1070 roff_word_alloc(mdoc
, n
->line
, n
->pos
, mdoc
->meta
.name
);
1071 mdoc
->last
->flags
|= NODE_NOSRC
;
1078 struct roff_node
*n
;
1083 if (n
->type
!= ROFFT_BODY
)
1086 if (n
->sec
!= SEC_NAME
)
1087 mandoc_msg(MANDOCERR_ND_LATE
, mdoc
->parse
,
1088 n
->line
, n
->pos
, "Nd");
1090 if (n
->child
== NULL
)
1091 mandoc_msg(MANDOCERR_ND_EMPTY
, mdoc
->parse
,
1092 n
->line
, n
->pos
, "Nd");
1093 else if (n
->last
->type
== ROFFT_TEXT
&&
1094 (sz
= strlen(n
->last
->string
)) != 0 &&
1095 n
->last
->string
[sz
- 1] == '.')
1096 mandoc_msg(MANDOCERR_ND_DOT
, mdoc
->parse
,
1097 n
->last
->line
, n
->last
->pos
+ sz
- 1, NULL
);
1103 post_display(POST_ARGS
)
1105 struct roff_node
*n
, *np
;
1110 if (n
->end
!= ENDBODY_NOT
) {
1111 if (n
->tok
== MDOC_Bd
&&
1112 n
->body
->parent
->args
== NULL
)
1113 roff_node_delete(mdoc
, n
);
1114 } else if (n
->child
== NULL
)
1115 mandoc_msg(MANDOCERR_BLK_EMPTY
, mdoc
->parse
,
1116 n
->line
, n
->pos
, roff_name
[n
->tok
]);
1117 else if (n
->tok
== MDOC_D1
)
1121 if (n
->tok
== MDOC_Bd
) {
1122 if (n
->args
== NULL
) {
1123 mandoc_msg(MANDOCERR_BD_NOARG
,
1124 mdoc
->parse
, n
->line
, n
->pos
, "Bd");
1125 mdoc
->next
= ROFF_NEXT_SIBLING
;
1126 while (n
->body
->child
!= NULL
)
1127 mdoc_node_relink(mdoc
,
1129 roff_node_delete(mdoc
, n
);
1135 for (np
= n
->parent
; np
!= NULL
; np
= np
->parent
) {
1136 if (np
->type
== ROFFT_BLOCK
&& np
->tok
== MDOC_Bd
) {
1137 mandoc_vmsg(MANDOCERR_BD_NEST
,
1138 mdoc
->parse
, n
->line
, n
->pos
,
1139 "%s in Bd", roff_name
[n
->tok
]);
1150 post_defaults(POST_ARGS
)
1152 struct roff_node
*nn
;
1155 * The `Ar' defaults to "file ..." if no value is provided as an
1156 * argument; the `Mt' and `Pa' macros use "~"; the `Li' just
1157 * gets an empty string.
1160 if (mdoc
->last
->child
!= NULL
)
1167 mdoc
->next
= ROFF_NEXT_CHILD
;
1168 roff_word_alloc(mdoc
, nn
->line
, nn
->pos
, "file");
1169 mdoc
->last
->flags
|= NODE_NOSRC
;
1170 roff_word_alloc(mdoc
, nn
->line
, nn
->pos
, "...");
1171 mdoc
->last
->flags
|= NODE_NOSRC
;
1175 mdoc
->next
= ROFF_NEXT_CHILD
;
1176 roff_word_alloc(mdoc
, nn
->line
, nn
->pos
, "~");
1177 mdoc
->last
->flags
|= NODE_NOSRC
;
1188 struct roff_node
*n
, *nch
;
1195 * If we have a child, look it up in the standard keys. If a
1196 * key exist, use that instead of the child; if it doesn't,
1197 * prefix "AT&T UNIX " to the existing data.
1201 if (nch
!= NULL
&& ((att
= mdoc_a2att(nch
->string
)) == NULL
))
1202 mandoc_vmsg(MANDOCERR_AT_BAD
, mdoc
->parse
,
1203 nch
->line
, nch
->pos
, "At %s", nch
->string
);
1205 mdoc
->next
= ROFF_NEXT_CHILD
;
1207 roff_word_alloc(mdoc
, nch
->line
, nch
->pos
, att
);
1208 nch
->flags
|= NODE_NOPRT
;
1210 roff_word_alloc(mdoc
, n
->line
, n
->pos
, "AT&T UNIX");
1211 mdoc
->last
->flags
|= NODE_NOSRC
;
1218 struct roff_node
*np
, *nch
;
1224 if (np
->norm
->An
.auth
== AUTH__NONE
) {
1226 mandoc_msg(MANDOCERR_MACRO_EMPTY
, mdoc
->parse
,
1227 np
->line
, np
->pos
, "An");
1228 } else if (nch
!= NULL
)
1229 mandoc_vmsg(MANDOCERR_ARG_EXCESS
, mdoc
->parse
,
1230 nch
->line
, nch
->pos
, "An ... %s", nch
->string
);
1237 post_obsolete(mdoc
);
1238 if (mdoc
->last
->type
== ROFFT_BLOCK
)
1239 mdoc
->last
->norm
->Es
= mdoc
->last_es
;
1246 post_obsolete(mdoc
);
1247 mdoc
->last_es
= mdoc
->last
;
1253 struct roff_node
*n
;
1279 mdoc
->next
= ROFF_NEXT_CHILD
;
1280 roff_word_alloc(mdoc
, n
->line
, n
->pos
, os
);
1281 mdoc
->last
->flags
|= NODE_NOSRC
;
1288 struct roff_node
*nbl
, *nit
, *nch
;
1295 if (nit
->type
!= ROFFT_BLOCK
)
1298 nbl
= nit
->parent
->parent
;
1299 lt
= nbl
->norm
->Bl
.type
;
1307 if (nit
->head
->child
== NULL
)
1308 mandoc_vmsg(MANDOCERR_IT_NOHEAD
,
1309 mdoc
->parse
, nit
->line
, nit
->pos
,
1311 mdoc_argnames
[nbl
->args
->argv
[0].arg
]);
1317 if (nit
->body
== NULL
|| nit
->body
->child
== NULL
)
1318 mandoc_vmsg(MANDOCERR_IT_NOBODY
,
1319 mdoc
->parse
, nit
->line
, nit
->pos
,
1321 mdoc_argnames
[nbl
->args
->argv
[0].arg
]);
1324 if ((nch
= nit
->head
->child
) != NULL
)
1325 mandoc_vmsg(MANDOCERR_ARG_SKIP
, mdoc
->parse
,
1326 nit
->line
, nit
->pos
, "It %s",
1327 nch
->string
== NULL
? roff_name
[nch
->tok
] :
1331 cols
= (int)nbl
->norm
->Bl
.ncols
;
1333 assert(nit
->head
->child
== NULL
);
1336 for (nch
= nit
->child
; nch
!= NULL
; nch
= nch
->next
)
1337 if (nch
->type
== ROFFT_BODY
)
1340 if (i
< cols
|| i
> cols
+ 1)
1341 mandoc_vmsg(MANDOCERR_BL_COL
,
1342 mdoc
->parse
, nit
->line
, nit
->pos
,
1343 "%d columns, %d cells", cols
, i
);
1351 post_bl_block(POST_ARGS
)
1353 struct roff_node
*n
, *ni
, *nc
;
1358 for (ni
= n
->body
->child
; ni
!= NULL
; ni
= ni
->next
) {
1359 if (ni
->body
== NULL
)
1361 nc
= ni
->body
->last
;
1362 while (nc
!= NULL
) {
1372 if (ni
->next
== NULL
) {
1373 mandoc_msg(MANDOCERR_PAR_MOVE
,
1374 mdoc
->parse
, nc
->line
, nc
->pos
,
1375 roff_name
[nc
->tok
]);
1376 mdoc_node_relink(mdoc
, nc
);
1377 } else if (n
->norm
->Bl
.comp
== 0 &&
1378 n
->norm
->Bl
.type
!= LIST_column
) {
1379 mandoc_vmsg(MANDOCERR_PAR_SKIP
,
1380 mdoc
->parse
, nc
->line
, nc
->pos
,
1381 "%s before It", roff_name
[nc
->tok
]);
1382 roff_node_delete(mdoc
, nc
);
1385 nc
= ni
->body
->last
;
1391 * If the argument of -offset or -width is a macro,
1392 * replace it with the associated default width.
1395 rewrite_macro2len(struct roff_man
*mdoc
, char **arg
)
1402 else if ( ! strcmp(*arg
, "Ds"))
1404 else if ((tok
= roffhash_find(mdoc
->mdocmac
, *arg
, 0)) == TOKEN_NONE
)
1407 width
= macro2len(tok
);
1410 mandoc_asprintf(arg
, "%zun", width
);
1414 post_bl_head(POST_ARGS
)
1416 struct roff_node
*nbl
, *nh
, *nch
, *nnext
;
1417 struct mdoc_argv
*argv
;
1423 if (nh
->norm
->Bl
.type
!= LIST_column
) {
1424 if ((nch
= nh
->child
) == NULL
)
1426 mandoc_vmsg(MANDOCERR_ARG_EXCESS
, mdoc
->parse
,
1427 nch
->line
, nch
->pos
, "Bl ... %s", nch
->string
);
1428 while (nch
!= NULL
) {
1429 roff_node_delete(mdoc
, nch
);
1436 * Append old-style lists, where the column width specifiers
1437 * trail as macro parameters, to the new-style ("normal-form")
1438 * lists where they're argument values following -column.
1441 if (nh
->child
== NULL
)
1445 for (j
= 0; j
< (int)nbl
->args
->argc
; j
++)
1446 if (nbl
->args
->argv
[j
].arg
== MDOC_Column
)
1449 assert(j
< (int)nbl
->args
->argc
);
1452 * Accommodate for new-style groff column syntax. Shuffle the
1453 * child nodes, all of which must be TEXT, as arguments for the
1454 * column field. Then, delete the head children.
1457 argv
= nbl
->args
->argv
+ j
;
1459 for (nch
= nh
->child
; nch
!= NULL
; nch
= nch
->next
)
1461 argv
->value
= mandoc_reallocarray(argv
->value
,
1462 argv
->sz
, sizeof(char *));
1464 nh
->norm
->Bl
.ncols
= argv
->sz
;
1465 nh
->norm
->Bl
.cols
= (void *)argv
->value
;
1467 for (nch
= nh
->child
; nch
!= NULL
; nch
= nnext
) {
1468 argv
->value
[i
++] = nch
->string
;
1471 roff_node_delete(NULL
, nch
);
1479 struct roff_node
*nparent
, *nprev
; /* of the Bl block */
1480 struct roff_node
*nblock
, *nbody
; /* of the Bl */
1481 struct roff_node
*nchild
, *nnext
; /* of the Bl body */
1482 const char *prev_Er
;
1486 switch (nbody
->type
) {
1488 post_bl_block(mdoc
);
1498 if (nbody
->end
!= ENDBODY_NOT
)
1501 nchild
= nbody
->child
;
1502 if (nchild
== NULL
) {
1503 mandoc_msg(MANDOCERR_BLK_EMPTY
, mdoc
->parse
,
1504 nbody
->line
, nbody
->pos
, "Bl");
1507 while (nchild
!= NULL
) {
1508 nnext
= nchild
->next
;
1509 if (nchild
->tok
== MDOC_It
||
1510 (nchild
->tok
== MDOC_Sm
&&
1511 nnext
!= NULL
&& nnext
->tok
== MDOC_It
)) {
1517 * In .Bl -column, the first rows may be implicit,
1518 * that is, they may not start with .It macros.
1519 * Such rows may be followed by nodes generated on the
1520 * roff level, for example .TS, which cannot be moved
1521 * out of the list. In that case, wrap such roff nodes
1522 * into an implicit row.
1525 if (nchild
->prev
!= NULL
) {
1526 mdoc
->last
= nchild
;
1527 mdoc
->next
= ROFF_NEXT_SIBLING
;
1528 roff_block_alloc(mdoc
, nchild
->line
,
1529 nchild
->pos
, MDOC_It
);
1530 roff_head_alloc(mdoc
, nchild
->line
,
1531 nchild
->pos
, MDOC_It
);
1532 mdoc
->next
= ROFF_NEXT_SIBLING
;
1533 roff_body_alloc(mdoc
, nchild
->line
,
1534 nchild
->pos
, MDOC_It
);
1535 while (nchild
->tok
!= MDOC_It
) {
1536 mdoc_node_relink(mdoc
, nchild
);
1537 if ((nchild
= nnext
) == NULL
)
1539 nnext
= nchild
->next
;
1540 mdoc
->next
= ROFF_NEXT_SIBLING
;
1546 mandoc_msg(MANDOCERR_BL_MOVE
, mdoc
->parse
,
1547 nchild
->line
, nchild
->pos
, roff_name
[nchild
->tok
]);
1550 * Move the node out of the Bl block.
1551 * First, collect all required node pointers.
1554 nblock
= nbody
->parent
;
1555 nprev
= nblock
->prev
;
1556 nparent
= nblock
->parent
;
1559 * Unlink this child.
1562 nbody
->child
= nnext
;
1569 * Relink this child.
1572 nchild
->parent
= nparent
;
1573 nchild
->prev
= nprev
;
1574 nchild
->next
= nblock
;
1576 nblock
->prev
= nchild
;
1578 nparent
->child
= nchild
;
1580 nprev
->next
= nchild
;
1585 if (mdoc
->meta
.os_e
!= MDOC_OS_NETBSD
)
1589 for (nchild
= nbody
->child
; nchild
!= NULL
; nchild
= nchild
->next
) {
1590 if (nchild
->tok
!= MDOC_It
)
1592 if ((nnext
= nchild
->head
->child
) == NULL
)
1594 if (nnext
->type
== ROFFT_BLOCK
)
1595 nnext
= nnext
->body
->child
;
1596 if (nnext
== NULL
|| nnext
->tok
!= MDOC_Er
)
1598 nnext
= nnext
->child
;
1599 if (prev_Er
!= NULL
) {
1600 order
= strcmp(prev_Er
, nnext
->string
);
1602 mandoc_vmsg(MANDOCERR_ER_ORDER
,
1603 mdoc
->parse
, nnext
->line
, nnext
->pos
,
1604 "Er %s %s", prev_Er
, nnext
->string
);
1605 else if (order
== 0)
1606 mandoc_vmsg(MANDOCERR_ER_REP
,
1607 mdoc
->parse
, nnext
->line
, nnext
->pos
,
1610 prev_Er
= nnext
->string
;
1617 struct roff_node
*n
;
1621 if (n
->type
== ROFFT_BLOCK
&& n
->body
->child
== NULL
) {
1622 mandoc_msg(MANDOCERR_BLK_EMPTY
,
1623 mdoc
->parse
, n
->line
, n
->pos
, "Bk");
1624 roff_node_delete(mdoc
, n
);
1631 struct roff_node
*nch
;
1633 nch
= mdoc
->last
->child
;
1636 mdoc
->flags
^= MDOC_SMOFF
;
1640 assert(nch
->type
== ROFFT_TEXT
);
1642 if ( ! strcmp(nch
->string
, "on")) {
1643 mdoc
->flags
&= ~MDOC_SMOFF
;
1646 if ( ! strcmp(nch
->string
, "off")) {
1647 mdoc
->flags
|= MDOC_SMOFF
;
1651 mandoc_vmsg(MANDOCERR_SM_BAD
,
1652 mdoc
->parse
, nch
->line
, nch
->pos
,
1653 "%s %s", roff_name
[mdoc
->last
->tok
], nch
->string
);
1654 mdoc_node_relink(mdoc
, nch
);
1659 post_root(POST_ARGS
)
1661 struct roff_node
*n
;
1663 /* Add missing prologue data. */
1665 if (mdoc
->meta
.date
== NULL
)
1666 mdoc
->meta
.date
= mdoc
->quick
?
1668 mandoc_normdate(mdoc
->parse
, NULL
, 0, 0);
1670 if (mdoc
->meta
.title
== NULL
) {
1671 mandoc_msg(MANDOCERR_DT_NOTITLE
,
1672 mdoc
->parse
, 0, 0, "EOF");
1673 mdoc
->meta
.title
= mandoc_strdup("UNTITLED");
1676 if (mdoc
->meta
.vol
== NULL
)
1677 mdoc
->meta
.vol
= mandoc_strdup("LOCAL");
1679 if (mdoc
->meta
.os
== NULL
) {
1680 mandoc_msg(MANDOCERR_OS_MISSING
,
1681 mdoc
->parse
, 0, 0, NULL
);
1682 mdoc
->meta
.os
= mandoc_strdup("");
1685 /* Check that we begin with a proper `Sh'. */
1687 n
= mdoc
->first
->child
;
1688 while (n
!= NULL
&& n
->tok
!= TOKEN_NONE
&&
1689 mdoc_macros
[n
->tok
].flags
& MDOC_PROLOGUE
)
1693 mandoc_msg(MANDOCERR_DOC_EMPTY
, mdoc
->parse
, 0, 0, NULL
);
1694 else if (n
->tok
!= MDOC_Sh
)
1695 mandoc_msg(MANDOCERR_SEC_BEFORE
, mdoc
->parse
,
1696 n
->line
, n
->pos
, roff_name
[n
->tok
]);
1702 struct roff_node
*np
, *nch
, *next
, *prev
;
1707 if (np
->type
!= ROFFT_BODY
)
1710 if (np
->child
== NULL
) {
1711 mandoc_msg(MANDOCERR_RS_EMPTY
, mdoc
->parse
,
1712 np
->line
, np
->pos
, "Rs");
1717 * The full `Rs' block needs special handling to order the
1718 * sub-elements according to `rsord'. Pick through each element
1719 * and correctly order it. This is an insertion sort.
1723 for (nch
= np
->child
->next
; nch
!= NULL
; nch
= next
) {
1724 /* Determine order number of this child. */
1725 for (i
= 0; i
< RSORD_MAX
; i
++)
1726 if (rsord
[i
] == nch
->tok
)
1729 if (i
== RSORD_MAX
) {
1730 mandoc_msg(MANDOCERR_RS_BAD
, mdoc
->parse
,
1731 nch
->line
, nch
->pos
, roff_name
[nch
->tok
]);
1733 } else if (nch
->tok
== MDOC__J
|| nch
->tok
== MDOC__B
)
1734 np
->norm
->Rs
.quote_T
++;
1737 * Remove this child from the chain. This somewhat
1738 * repeats roff_node_unlink(), but since we're
1739 * just re-ordering, there's no need for the
1740 * full unlink process.
1743 if ((next
= nch
->next
) != NULL
)
1744 next
->prev
= nch
->prev
;
1746 if ((prev
= nch
->prev
) != NULL
)
1747 prev
->next
= nch
->next
;
1749 nch
->prev
= nch
->next
= NULL
;
1752 * Scan back until we reach a node that's
1753 * to be ordered before this child.
1756 for ( ; prev
; prev
= prev
->prev
) {
1757 /* Determine order of `prev'. */
1758 for (j
= 0; j
< RSORD_MAX
; j
++)
1759 if (rsord
[j
] == prev
->tok
)
1769 * Set this child back into its correct place
1770 * in front of the `prev' node.
1776 np
->child
->prev
= nch
;
1777 nch
->next
= np
->child
;
1781 prev
->next
->prev
= nch
;
1782 nch
->next
= prev
->next
;
1789 * For some arguments of some macros,
1790 * convert all breakable hyphens into ASCII_HYPH.
1793 post_hyph(POST_ARGS
)
1795 struct roff_node
*nch
;
1798 for (nch
= mdoc
->last
->child
; nch
!= NULL
; nch
= nch
->next
) {
1799 if (nch
->type
!= ROFFT_TEXT
)
1804 while (*(++cp
) != '\0')
1806 isalpha((unsigned char)cp
[-1]) &&
1807 isalpha((unsigned char)cp
[1]))
1816 if (mdoc
->last
->flags
& NODE_LINE
)
1817 mandoc_msg(MANDOCERR_NS_SKIP
, mdoc
->parse
,
1818 mdoc
->last
->line
, mdoc
->last
->pos
, NULL
);
1827 switch (mdoc
->last
->type
) {
1832 switch (mdoc
->lastsec
) {
1837 post_sh_see_also(mdoc
);
1840 post_sh_authors(mdoc
);
1852 post_sh_name(POST_ARGS
)
1854 struct roff_node
*n
;
1859 for (n
= mdoc
->last
->child
; n
!= NULL
; n
= n
->next
) {
1862 if (hasnm
&& n
->child
!= NULL
)
1863 mandoc_vmsg(MANDOCERR_NAMESEC_PUNCT
,
1864 mdoc
->parse
, n
->line
, n
->pos
,
1865 "Nm %s", n
->child
->string
);
1870 if (n
->next
!= NULL
)
1871 mandoc_msg(MANDOCERR_NAMESEC_ND
,
1872 mdoc
->parse
, n
->line
, n
->pos
, NULL
);
1875 if (n
->type
== ROFFT_TEXT
&&
1876 n
->string
[0] == ',' && n
->string
[1] == '\0' &&
1877 n
->next
!= NULL
&& n
->next
->tok
== MDOC_Nm
) {
1883 mandoc_msg(MANDOCERR_NAMESEC_BAD
, mdoc
->parse
,
1884 n
->line
, n
->pos
, roff_name
[n
->tok
]);
1891 mandoc_msg(MANDOCERR_NAMESEC_NONM
, mdoc
->parse
,
1892 mdoc
->last
->line
, mdoc
->last
->pos
, NULL
);
1894 mandoc_msg(MANDOCERR_NAMESEC_NOND
, mdoc
->parse
,
1895 mdoc
->last
->line
, mdoc
->last
->pos
, NULL
);
1899 post_sh_see_also(POST_ARGS
)
1901 const struct roff_node
*n
;
1902 const char *name
, *sec
;
1903 const char *lastname
, *lastsec
, *lastpunct
;
1906 n
= mdoc
->last
->child
;
1907 lastname
= lastsec
= lastpunct
= NULL
;
1909 if (n
->tok
!= MDOC_Xr
||
1911 n
->child
->next
== NULL
)
1914 /* Process one .Xr node. */
1916 name
= n
->child
->string
;
1917 sec
= n
->child
->next
->string
;
1918 if (lastsec
!= NULL
) {
1919 if (lastpunct
[0] != ',' || lastpunct
[1] != '\0')
1920 mandoc_vmsg(MANDOCERR_XR_PUNCT
,
1921 mdoc
->parse
, n
->line
, n
->pos
,
1922 "%s before %s(%s)", lastpunct
,
1924 cmp
= strcmp(lastsec
, sec
);
1926 mandoc_vmsg(MANDOCERR_XR_ORDER
,
1927 mdoc
->parse
, n
->line
, n
->pos
,
1928 "%s(%s) after %s(%s)", name
,
1929 sec
, lastname
, lastsec
);
1930 else if (cmp
== 0 &&
1931 strcasecmp(lastname
, name
) > 0)
1932 mandoc_vmsg(MANDOCERR_XR_ORDER
,
1933 mdoc
->parse
, n
->line
, n
->pos
,
1934 "%s after %s", name
, lastname
);
1939 /* Process the following node. */
1944 if (n
->tok
== MDOC_Xr
) {
1948 if (n
->type
!= ROFFT_TEXT
)
1950 for (name
= n
->string
; *name
!= '\0'; name
++)
1951 if (isalpha((const unsigned char)*name
))
1953 lastpunct
= n
->string
;
1954 if (n
->next
== NULL
|| n
->next
->tok
== MDOC_Rs
)
1955 mandoc_vmsg(MANDOCERR_XR_PUNCT
, mdoc
->parse
,
1956 n
->line
, n
->pos
, "%s after %s(%s)",
1957 lastpunct
, lastname
, lastsec
);
1963 child_an(const struct roff_node
*n
)
1966 for (n
= n
->child
; n
!= NULL
; n
= n
->next
)
1967 if ((n
->tok
== MDOC_An
&& n
->child
!= NULL
) || child_an(n
))
1973 post_sh_authors(POST_ARGS
)
1976 if ( ! child_an(mdoc
->last
))
1977 mandoc_msg(MANDOCERR_AN_MISSING
, mdoc
->parse
,
1978 mdoc
->last
->line
, mdoc
->last
->pos
, NULL
);
1982 post_sh_head(POST_ARGS
)
1984 struct roff_node
*nch
;
1985 const char *goodsec
;
1989 * Process a new section. Sections are either "named" or
1990 * "custom". Custom sections are user-defined, while named ones
1991 * follow a conventional order and may only appear in certain
1995 sec
= mdoc
->last
->sec
;
1997 /* The NAME should be first. */
1999 if (sec
!= SEC_NAME
&& mdoc
->lastnamed
== SEC_NONE
)
2000 mandoc_vmsg(MANDOCERR_NAMESEC_FIRST
, mdoc
->parse
,
2001 mdoc
->last
->line
, mdoc
->last
->pos
, "Sh %s",
2002 sec
!= SEC_CUSTOM
? secnames
[sec
] :
2003 (nch
= mdoc
->last
->child
) == NULL
? "" :
2004 nch
->type
== ROFFT_TEXT
? nch
->string
:
2005 roff_name
[nch
->tok
]);
2007 /* The SYNOPSIS gets special attention in other areas. */
2009 if (sec
== SEC_SYNOPSIS
) {
2010 roff_setreg(mdoc
->roff
, "nS", 1, '=');
2011 mdoc
->flags
|= MDOC_SYNOPSIS
;
2013 roff_setreg(mdoc
->roff
, "nS", 0, '=');
2014 mdoc
->flags
&= ~MDOC_SYNOPSIS
;
2017 /* Mark our last section. */
2019 mdoc
->lastsec
= sec
;
2021 /* We don't care about custom sections after this. */
2023 if (sec
== SEC_CUSTOM
)
2027 * Check whether our non-custom section is being repeated or is
2031 if (sec
== mdoc
->lastnamed
)
2032 mandoc_vmsg(MANDOCERR_SEC_REP
, mdoc
->parse
,
2033 mdoc
->last
->line
, mdoc
->last
->pos
,
2034 "Sh %s", secnames
[sec
]);
2036 if (sec
< mdoc
->lastnamed
)
2037 mandoc_vmsg(MANDOCERR_SEC_ORDER
, mdoc
->parse
,
2038 mdoc
->last
->line
, mdoc
->last
->pos
,
2039 "Sh %s", secnames
[sec
]);
2041 /* Mark the last named section. */
2043 mdoc
->lastnamed
= sec
;
2045 /* Check particular section/manual conventions. */
2047 if (mdoc
->meta
.msec
== NULL
)
2053 if (*mdoc
->meta
.msec
== '4')
2055 goodsec
= "2, 3, 4, 9";
2057 case SEC_RETURN_VALUES
:
2059 if (*mdoc
->meta
.msec
== '2')
2061 if (*mdoc
->meta
.msec
== '3')
2063 if (NULL
== goodsec
)
2064 goodsec
= "2, 3, 9";
2067 if (*mdoc
->meta
.msec
== '9')
2069 if (NULL
== goodsec
)
2071 mandoc_vmsg(MANDOCERR_SEC_MSEC
, mdoc
->parse
,
2072 mdoc
->last
->line
, mdoc
->last
->pos
,
2073 "Sh %s for %s only", secnames
[sec
], goodsec
);
2083 struct roff_node
*n
, *nch
;
2087 if (nch
->next
== NULL
) {
2088 mandoc_vmsg(MANDOCERR_XR_NOSEC
, mdoc
->parse
,
2089 n
->line
, n
->pos
, "Xr %s", nch
->string
);
2092 assert(nch
->next
== n
->last
);
2096 post_ignpar(POST_ARGS
)
2098 struct roff_node
*np
;
2100 switch (mdoc
->last
->type
) {
2113 if ((np
= mdoc
->last
->child
) != NULL
)
2114 if (np
->tok
== MDOC_Pp
|| np
->tok
== MDOC_Lp
) {
2115 mandoc_vmsg(MANDOCERR_PAR_SKIP
,
2116 mdoc
->parse
, np
->line
, np
->pos
,
2117 "%s after %s", roff_name
[np
->tok
],
2118 roff_name
[mdoc
->last
->tok
]);
2119 roff_node_delete(mdoc
, np
);
2122 if ((np
= mdoc
->last
->last
) != NULL
)
2123 if (np
->tok
== MDOC_Pp
|| np
->tok
== MDOC_Lp
) {
2124 mandoc_vmsg(MANDOCERR_PAR_SKIP
, mdoc
->parse
,
2125 np
->line
, np
->pos
, "%s at the end of %s",
2127 roff_name
[mdoc
->last
->tok
]);
2128 roff_node_delete(mdoc
, np
);
2133 post_prevpar(POST_ARGS
)
2135 struct roff_node
*n
;
2138 if (NULL
== n
->prev
)
2140 if (n
->type
!= ROFFT_ELEM
&& n
->type
!= ROFFT_BLOCK
)
2144 * Don't allow prior `Lp' or `Pp' prior to a paragraph-type
2145 * block: `Lp', `Pp', or non-compact `Bd' or `Bl'.
2148 if (n
->prev
->tok
!= MDOC_Pp
&&
2149 n
->prev
->tok
!= MDOC_Lp
&&
2150 n
->prev
->tok
!= ROFF_br
)
2152 if (n
->tok
== MDOC_Bl
&& n
->norm
->Bl
.comp
)
2154 if (n
->tok
== MDOC_Bd
&& n
->norm
->Bd
.comp
)
2156 if (n
->tok
== MDOC_It
&& n
->parent
->norm
->Bl
.comp
)
2159 mandoc_vmsg(MANDOCERR_PAR_SKIP
, mdoc
->parse
,
2160 n
->prev
->line
, n
->prev
->pos
, "%s before %s",
2161 roff_name
[n
->prev
->tok
], roff_name
[n
->tok
]);
2162 roff_node_delete(mdoc
, n
->prev
);
2168 struct roff_node
*np
;
2171 if (np
->tok
!= ROFF_br
&& np
->tok
!= ROFF_sp
)
2174 if (np
->tok
== ROFF_sp
) {
2175 if (np
->child
!= NULL
&& np
->child
->next
!= NULL
)
2176 mandoc_vmsg(MANDOCERR_ARG_EXCESS
, mdoc
->parse
,
2177 np
->child
->next
->line
, np
->child
->next
->pos
,
2178 "sp ... %s", np
->child
->next
->string
);
2179 } else if (np
->child
!= NULL
)
2180 mandoc_vmsg(MANDOCERR_ARG_SKIP
,
2181 mdoc
->parse
, np
->line
, np
->pos
, "%s %s",
2182 roff_name
[np
->tok
], np
->child
->string
);
2184 if ((np
= mdoc
->last
->prev
) == NULL
) {
2185 np
= mdoc
->last
->parent
;
2186 if (np
->tok
!= MDOC_Sh
&& np
->tok
!= MDOC_Ss
)
2188 } else if (np
->tok
!= MDOC_Pp
&& np
->tok
!= MDOC_Lp
&&
2189 (mdoc
->last
->tok
!= ROFF_br
||
2190 (np
->tok
!= ROFF_sp
&& np
->tok
!= ROFF_br
)))
2193 mandoc_vmsg(MANDOCERR_PAR_SKIP
, mdoc
->parse
,
2194 mdoc
->last
->line
, mdoc
->last
->pos
, "%s after %s",
2195 roff_name
[mdoc
->last
->tok
], roff_name
[np
->tok
]);
2196 roff_node_delete(mdoc
, mdoc
->last
);
2202 struct roff_node
*n
;
2206 n
->flags
|= NODE_NOPRT
;
2208 if (mdoc
->meta
.date
!= NULL
) {
2209 mandoc_msg(MANDOCERR_PROLOG_REP
, mdoc
->parse
,
2210 n
->line
, n
->pos
, "Dd");
2211 free(mdoc
->meta
.date
);
2212 } else if (mdoc
->flags
& MDOC_PBODY
)
2213 mandoc_msg(MANDOCERR_PROLOG_LATE
, mdoc
->parse
,
2214 n
->line
, n
->pos
, "Dd");
2215 else if (mdoc
->meta
.title
!= NULL
)
2216 mandoc_msg(MANDOCERR_PROLOG_ORDER
, mdoc
->parse
,
2217 n
->line
, n
->pos
, "Dd after Dt");
2218 else if (mdoc
->meta
.os
!= NULL
)
2219 mandoc_msg(MANDOCERR_PROLOG_ORDER
, mdoc
->parse
,
2220 n
->line
, n
->pos
, "Dd after Os");
2222 if (n
->child
== NULL
|| n
->child
->string
[0] == '\0') {
2223 mdoc
->meta
.date
= mdoc
->quick
? mandoc_strdup("") :
2224 mandoc_normdate(mdoc
->parse
, NULL
, n
->line
, n
->pos
);
2229 deroff(&datestr
, n
);
2231 mdoc
->meta
.date
= datestr
;
2233 mdoc
->meta
.date
= mandoc_normdate(mdoc
->parse
,
2234 datestr
, n
->line
, n
->pos
);
2242 struct roff_node
*nn
, *n
;
2247 n
->flags
|= NODE_NOPRT
;
2249 if (mdoc
->flags
& MDOC_PBODY
) {
2250 mandoc_msg(MANDOCERR_DT_LATE
, mdoc
->parse
,
2251 n
->line
, n
->pos
, "Dt");
2255 if (mdoc
->meta
.title
!= NULL
)
2256 mandoc_msg(MANDOCERR_PROLOG_REP
, mdoc
->parse
,
2257 n
->line
, n
->pos
, "Dt");
2258 else if (mdoc
->meta
.os
!= NULL
)
2259 mandoc_msg(MANDOCERR_PROLOG_ORDER
, mdoc
->parse
,
2260 n
->line
, n
->pos
, "Dt after Os");
2262 free(mdoc
->meta
.title
);
2263 free(mdoc
->meta
.msec
);
2264 free(mdoc
->meta
.vol
);
2265 free(mdoc
->meta
.arch
);
2267 mdoc
->meta
.title
= NULL
;
2268 mdoc
->meta
.msec
= NULL
;
2269 mdoc
->meta
.vol
= NULL
;
2270 mdoc
->meta
.arch
= NULL
;
2272 /* Mandatory first argument: title. */
2275 if (nn
== NULL
|| *nn
->string
== '\0') {
2276 mandoc_msg(MANDOCERR_DT_NOTITLE
,
2277 mdoc
->parse
, n
->line
, n
->pos
, "Dt");
2278 mdoc
->meta
.title
= mandoc_strdup("UNTITLED");
2280 mdoc
->meta
.title
= mandoc_strdup(nn
->string
);
2282 /* Check that all characters are uppercase. */
2284 for (p
= nn
->string
; *p
!= '\0'; p
++)
2285 if (islower((unsigned char)*p
)) {
2286 mandoc_vmsg(MANDOCERR_TITLE_CASE
,
2287 mdoc
->parse
, nn
->line
,
2288 nn
->pos
+ (p
- nn
->string
),
2289 "Dt %s", nn
->string
);
2294 /* Mandatory second argument: section. */
2300 mandoc_vmsg(MANDOCERR_MSEC_MISSING
,
2301 mdoc
->parse
, n
->line
, n
->pos
,
2302 "Dt %s", mdoc
->meta
.title
);
2303 mdoc
->meta
.vol
= mandoc_strdup("LOCAL");
2304 return; /* msec and arch remain NULL. */
2307 mdoc
->meta
.msec
= mandoc_strdup(nn
->string
);
2309 /* Infer volume title from section number. */
2311 cp
= mandoc_a2msec(nn
->string
);
2313 mandoc_vmsg(MANDOCERR_MSEC_BAD
, mdoc
->parse
,
2314 nn
->line
, nn
->pos
, "Dt ... %s", nn
->string
);
2315 mdoc
->meta
.vol
= mandoc_strdup(nn
->string
);
2317 mdoc
->meta
.vol
= mandoc_strdup(cp
);
2319 /* Optional third argument: architecture. */
2321 if ((nn
= nn
->next
) == NULL
)
2324 for (p
= nn
->string
; *p
!= '\0'; p
++)
2325 *p
= tolower((unsigned char)*p
);
2326 mdoc
->meta
.arch
= mandoc_strdup(nn
->string
);
2328 /* Ignore fourth and later arguments. */
2330 if ((nn
= nn
->next
) != NULL
)
2331 mandoc_vmsg(MANDOCERR_ARG_EXCESS
, mdoc
->parse
,
2332 nn
->line
, nn
->pos
, "Dt ... %s", nn
->string
);
2338 struct roff_node
*n
, *nch
;
2345 macro
= !strcmp(nch
->string
, "Open") ? "Ox" :
2346 !strcmp(nch
->string
, "Net") ? "Nx" :
2347 !strcmp(nch
->string
, "Free") ? "Fx" :
2348 !strcmp(nch
->string
, "DragonFly") ? "Dx" : NULL
;
2350 mandoc_msg(MANDOCERR_BX
, mdoc
->parse
,
2351 n
->line
, n
->pos
, macro
);
2354 mdoc
->next
= ROFF_NEXT_SIBLING
;
2355 roff_elem_alloc(mdoc
, n
->line
, n
->pos
, MDOC_Ns
);
2356 mdoc
->last
->flags
|= NODE_NOSRC
;
2357 mdoc
->next
= ROFF_NEXT_SIBLING
;
2359 mdoc
->next
= ROFF_NEXT_CHILD
;
2360 roff_word_alloc(mdoc
, n
->line
, n
->pos
, "BSD");
2361 mdoc
->last
->flags
|= NODE_NOSRC
;
2368 roff_elem_alloc(mdoc
, n
->line
, n
->pos
, MDOC_Ns
);
2369 mdoc
->last
->flags
|= NODE_NOSRC
;
2370 mdoc
->next
= ROFF_NEXT_SIBLING
;
2371 roff_word_alloc(mdoc
, n
->line
, n
->pos
, "-");
2372 mdoc
->last
->flags
|= NODE_NOSRC
;
2373 roff_elem_alloc(mdoc
, n
->line
, n
->pos
, MDOC_Ns
);
2374 mdoc
->last
->flags
|= NODE_NOSRC
;
2378 * Make `Bx's second argument always start with an uppercase
2379 * letter. Groff checks if it's an "accepted" term, but we just
2380 * uppercase blindly.
2383 *nch
->string
= (char)toupper((unsigned char)*nch
->string
);
2390 struct utsname utsname
;
2391 static char *defbuf
;
2393 struct roff_node
*n
;
2396 n
->flags
|= NODE_NOPRT
;
2398 if (mdoc
->meta
.os
!= NULL
)
2399 mandoc_msg(MANDOCERR_PROLOG_REP
, mdoc
->parse
,
2400 n
->line
, n
->pos
, "Os");
2401 else if (mdoc
->flags
& MDOC_PBODY
)
2402 mandoc_msg(MANDOCERR_PROLOG_LATE
, mdoc
->parse
,
2403 n
->line
, n
->pos
, "Os");
2406 * Set the operating system by way of the `Os' macro.
2407 * The order of precedence is:
2408 * 1. the argument of the `Os' macro, unless empty
2409 * 2. the -Ios=foo command line argument, if provided
2410 * 3. -DOSNAME="\"foo\"", if provided during compilation
2411 * 4. "sysname release" from uname(3)
2414 free(mdoc
->meta
.os
);
2415 mdoc
->meta
.os
= NULL
;
2416 deroff(&mdoc
->meta
.os
, n
);
2421 mdoc
->meta
.os
= mandoc_strdup(mdoc
->defos
);
2426 mdoc
->meta
.os
= mandoc_strdup(OSNAME
);
2428 if (defbuf
== NULL
) {
2429 if (uname(&utsname
) == -1) {
2430 mandoc_msg(MANDOCERR_OS_UNAME
, mdoc
->parse
,
2431 n
->line
, n
->pos
, "Os");
2432 defbuf
= mandoc_strdup("UNKNOWN");
2434 mandoc_asprintf(&defbuf
, "%s %s",
2435 utsname
.sysname
, utsname
.release
);
2437 mdoc
->meta
.os
= mandoc_strdup(defbuf
);
2440 out
: mdoc
->meta
.os_e
= strstr(mdoc
->meta
.os
, "OpenBSD") != NULL
?
2441 MDOC_OS_OPENBSD
: strstr(mdoc
->meta
.os
, "NetBSD") != NULL
?
2442 MDOC_OS_NETBSD
: MDOC_OS_OTHER
;
2446 mdoc_a2sec(const char *p
)
2450 for (i
= 0; i
< (int)SEC__MAX
; i
++)
2451 if (secnames
[i
] && 0 == strcmp(p
, secnames
[i
]))
2452 return (enum roff_sec
)i
;
2458 macro2len(enum roff_tok macro
)