]>
git.cameronkatri.com Git - mandoc.git/blob - mdoc_validate.c
1 /* $Id: mdoc_validate.c,v 1.329 2017/05/31 15:31:00 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
;
1082 if (n
->type
!= ROFFT_BODY
)
1085 if (n
->sec
!= SEC_NAME
)
1086 mandoc_msg(MANDOCERR_ND_LATE
, mdoc
->parse
,
1087 n
->line
, n
->pos
, "Nd");
1089 if (n
->child
== NULL
)
1090 mandoc_msg(MANDOCERR_ND_EMPTY
, mdoc
->parse
,
1091 n
->line
, n
->pos
, "Nd");
1097 post_display(POST_ARGS
)
1099 struct roff_node
*n
, *np
;
1104 if (n
->end
!= ENDBODY_NOT
) {
1105 if (n
->tok
== MDOC_Bd
&&
1106 n
->body
->parent
->args
== NULL
)
1107 roff_node_delete(mdoc
, n
);
1108 } else if (n
->child
== NULL
)
1109 mandoc_msg(MANDOCERR_BLK_EMPTY
, mdoc
->parse
,
1110 n
->line
, n
->pos
, roff_name
[n
->tok
]);
1111 else if (n
->tok
== MDOC_D1
)
1115 if (n
->tok
== MDOC_Bd
) {
1116 if (n
->args
== NULL
) {
1117 mandoc_msg(MANDOCERR_BD_NOARG
,
1118 mdoc
->parse
, n
->line
, n
->pos
, "Bd");
1119 mdoc
->next
= ROFF_NEXT_SIBLING
;
1120 while (n
->body
->child
!= NULL
)
1121 mdoc_node_relink(mdoc
,
1123 roff_node_delete(mdoc
, n
);
1129 for (np
= n
->parent
; np
!= NULL
; np
= np
->parent
) {
1130 if (np
->type
== ROFFT_BLOCK
&& np
->tok
== MDOC_Bd
) {
1131 mandoc_vmsg(MANDOCERR_BD_NEST
,
1132 mdoc
->parse
, n
->line
, n
->pos
,
1133 "%s in Bd", roff_name
[n
->tok
]);
1144 post_defaults(POST_ARGS
)
1146 struct roff_node
*nn
;
1149 * The `Ar' defaults to "file ..." if no value is provided as an
1150 * argument; the `Mt' and `Pa' macros use "~"; the `Li' just
1151 * gets an empty string.
1154 if (mdoc
->last
->child
!= NULL
)
1161 mdoc
->next
= ROFF_NEXT_CHILD
;
1162 roff_word_alloc(mdoc
, nn
->line
, nn
->pos
, "file");
1163 mdoc
->last
->flags
|= NODE_NOSRC
;
1164 roff_word_alloc(mdoc
, nn
->line
, nn
->pos
, "...");
1165 mdoc
->last
->flags
|= NODE_NOSRC
;
1169 mdoc
->next
= ROFF_NEXT_CHILD
;
1170 roff_word_alloc(mdoc
, nn
->line
, nn
->pos
, "~");
1171 mdoc
->last
->flags
|= NODE_NOSRC
;
1182 struct roff_node
*n
, *nch
;
1189 * If we have a child, look it up in the standard keys. If a
1190 * key exist, use that instead of the child; if it doesn't,
1191 * prefix "AT&T UNIX " to the existing data.
1195 if (nch
!= NULL
&& ((att
= mdoc_a2att(nch
->string
)) == NULL
))
1196 mandoc_vmsg(MANDOCERR_AT_BAD
, mdoc
->parse
,
1197 nch
->line
, nch
->pos
, "At %s", nch
->string
);
1199 mdoc
->next
= ROFF_NEXT_CHILD
;
1201 roff_word_alloc(mdoc
, nch
->line
, nch
->pos
, att
);
1202 nch
->flags
|= NODE_NOPRT
;
1204 roff_word_alloc(mdoc
, n
->line
, n
->pos
, "AT&T UNIX");
1205 mdoc
->last
->flags
|= NODE_NOSRC
;
1212 struct roff_node
*np
, *nch
;
1218 if (np
->norm
->An
.auth
== AUTH__NONE
) {
1220 mandoc_msg(MANDOCERR_MACRO_EMPTY
, mdoc
->parse
,
1221 np
->line
, np
->pos
, "An");
1222 } else if (nch
!= NULL
)
1223 mandoc_vmsg(MANDOCERR_ARG_EXCESS
, mdoc
->parse
,
1224 nch
->line
, nch
->pos
, "An ... %s", nch
->string
);
1231 post_obsolete(mdoc
);
1232 if (mdoc
->last
->type
== ROFFT_BLOCK
)
1233 mdoc
->last
->norm
->Es
= mdoc
->last_es
;
1240 post_obsolete(mdoc
);
1241 mdoc
->last_es
= mdoc
->last
;
1247 struct roff_node
*n
;
1273 mdoc
->next
= ROFF_NEXT_CHILD
;
1274 roff_word_alloc(mdoc
, n
->line
, n
->pos
, os
);
1275 mdoc
->last
->flags
|= NODE_NOSRC
;
1282 struct roff_node
*nbl
, *nit
, *nch
;
1289 if (nit
->type
!= ROFFT_BLOCK
)
1292 nbl
= nit
->parent
->parent
;
1293 lt
= nbl
->norm
->Bl
.type
;
1301 if (nit
->head
->child
== NULL
)
1302 mandoc_vmsg(MANDOCERR_IT_NOHEAD
,
1303 mdoc
->parse
, nit
->line
, nit
->pos
,
1305 mdoc_argnames
[nbl
->args
->argv
[0].arg
]);
1311 if (nit
->body
== NULL
|| nit
->body
->child
== NULL
)
1312 mandoc_vmsg(MANDOCERR_IT_NOBODY
,
1313 mdoc
->parse
, nit
->line
, nit
->pos
,
1315 mdoc_argnames
[nbl
->args
->argv
[0].arg
]);
1318 if ((nch
= nit
->head
->child
) != NULL
)
1319 mandoc_vmsg(MANDOCERR_ARG_SKIP
, mdoc
->parse
,
1320 nit
->line
, nit
->pos
, "It %s",
1321 nch
->string
== NULL
? roff_name
[nch
->tok
] :
1325 cols
= (int)nbl
->norm
->Bl
.ncols
;
1327 assert(nit
->head
->child
== NULL
);
1330 for (nch
= nit
->child
; nch
!= NULL
; nch
= nch
->next
)
1331 if (nch
->type
== ROFFT_BODY
)
1334 if (i
< cols
|| i
> cols
+ 1)
1335 mandoc_vmsg(MANDOCERR_BL_COL
,
1336 mdoc
->parse
, nit
->line
, nit
->pos
,
1337 "%d columns, %d cells", cols
, i
);
1345 post_bl_block(POST_ARGS
)
1347 struct roff_node
*n
, *ni
, *nc
;
1352 for (ni
= n
->body
->child
; ni
!= NULL
; ni
= ni
->next
) {
1353 if (ni
->body
== NULL
)
1355 nc
= ni
->body
->last
;
1356 while (nc
!= NULL
) {
1366 if (ni
->next
== NULL
) {
1367 mandoc_msg(MANDOCERR_PAR_MOVE
,
1368 mdoc
->parse
, nc
->line
, nc
->pos
,
1369 roff_name
[nc
->tok
]);
1370 mdoc_node_relink(mdoc
, nc
);
1371 } else if (n
->norm
->Bl
.comp
== 0 &&
1372 n
->norm
->Bl
.type
!= LIST_column
) {
1373 mandoc_vmsg(MANDOCERR_PAR_SKIP
,
1374 mdoc
->parse
, nc
->line
, nc
->pos
,
1375 "%s before It", roff_name
[nc
->tok
]);
1376 roff_node_delete(mdoc
, nc
);
1379 nc
= ni
->body
->last
;
1385 * If the argument of -offset or -width is a macro,
1386 * replace it with the associated default width.
1389 rewrite_macro2len(struct roff_man
*mdoc
, char **arg
)
1396 else if ( ! strcmp(*arg
, "Ds"))
1398 else if ((tok
= roffhash_find(mdoc
->mdocmac
, *arg
, 0)) == TOKEN_NONE
)
1401 width
= macro2len(tok
);
1404 mandoc_asprintf(arg
, "%zun", width
);
1408 post_bl_head(POST_ARGS
)
1410 struct roff_node
*nbl
, *nh
, *nch
, *nnext
;
1411 struct mdoc_argv
*argv
;
1417 if (nh
->norm
->Bl
.type
!= LIST_column
) {
1418 if ((nch
= nh
->child
) == NULL
)
1420 mandoc_vmsg(MANDOCERR_ARG_EXCESS
, mdoc
->parse
,
1421 nch
->line
, nch
->pos
, "Bl ... %s", nch
->string
);
1422 while (nch
!= NULL
) {
1423 roff_node_delete(mdoc
, nch
);
1430 * Append old-style lists, where the column width specifiers
1431 * trail as macro parameters, to the new-style ("normal-form")
1432 * lists where they're argument values following -column.
1435 if (nh
->child
== NULL
)
1439 for (j
= 0; j
< (int)nbl
->args
->argc
; j
++)
1440 if (nbl
->args
->argv
[j
].arg
== MDOC_Column
)
1443 assert(j
< (int)nbl
->args
->argc
);
1446 * Accommodate for new-style groff column syntax. Shuffle the
1447 * child nodes, all of which must be TEXT, as arguments for the
1448 * column field. Then, delete the head children.
1451 argv
= nbl
->args
->argv
+ j
;
1453 for (nch
= nh
->child
; nch
!= NULL
; nch
= nch
->next
)
1455 argv
->value
= mandoc_reallocarray(argv
->value
,
1456 argv
->sz
, sizeof(char *));
1458 nh
->norm
->Bl
.ncols
= argv
->sz
;
1459 nh
->norm
->Bl
.cols
= (void *)argv
->value
;
1461 for (nch
= nh
->child
; nch
!= NULL
; nch
= nnext
) {
1462 argv
->value
[i
++] = nch
->string
;
1465 roff_node_delete(NULL
, nch
);
1473 struct roff_node
*nparent
, *nprev
; /* of the Bl block */
1474 struct roff_node
*nblock
, *nbody
; /* of the Bl */
1475 struct roff_node
*nchild
, *nnext
; /* of the Bl body */
1478 switch (nbody
->type
) {
1480 post_bl_block(mdoc
);
1490 if (nbody
->end
!= ENDBODY_NOT
)
1493 nchild
= nbody
->child
;
1494 if (nchild
== NULL
) {
1495 mandoc_msg(MANDOCERR_BLK_EMPTY
, mdoc
->parse
,
1496 nbody
->line
, nbody
->pos
, "Bl");
1499 while (nchild
!= NULL
) {
1500 nnext
= nchild
->next
;
1501 if (nchild
->tok
== MDOC_It
||
1502 (nchild
->tok
== MDOC_Sm
&&
1503 nnext
!= NULL
&& nnext
->tok
== MDOC_It
)) {
1509 * In .Bl -column, the first rows may be implicit,
1510 * that is, they may not start with .It macros.
1511 * Such rows may be followed by nodes generated on the
1512 * roff level, for example .TS, which cannot be moved
1513 * out of the list. In that case, wrap such roff nodes
1514 * into an implicit row.
1517 if (nchild
->prev
!= NULL
) {
1518 mdoc
->last
= nchild
;
1519 mdoc
->next
= ROFF_NEXT_SIBLING
;
1520 roff_block_alloc(mdoc
, nchild
->line
,
1521 nchild
->pos
, MDOC_It
);
1522 roff_head_alloc(mdoc
, nchild
->line
,
1523 nchild
->pos
, MDOC_It
);
1524 mdoc
->next
= ROFF_NEXT_SIBLING
;
1525 roff_body_alloc(mdoc
, nchild
->line
,
1526 nchild
->pos
, MDOC_It
);
1527 while (nchild
->tok
!= MDOC_It
) {
1528 mdoc_node_relink(mdoc
, nchild
);
1529 if ((nchild
= nnext
) == NULL
)
1531 nnext
= nchild
->next
;
1532 mdoc
->next
= ROFF_NEXT_SIBLING
;
1538 mandoc_msg(MANDOCERR_BL_MOVE
, mdoc
->parse
,
1539 nchild
->line
, nchild
->pos
, roff_name
[nchild
->tok
]);
1542 * Move the node out of the Bl block.
1543 * First, collect all required node pointers.
1546 nblock
= nbody
->parent
;
1547 nprev
= nblock
->prev
;
1548 nparent
= nblock
->parent
;
1551 * Unlink this child.
1554 nbody
->child
= nnext
;
1561 * Relink this child.
1564 nchild
->parent
= nparent
;
1565 nchild
->prev
= nprev
;
1566 nchild
->next
= nblock
;
1568 nblock
->prev
= nchild
;
1570 nparent
->child
= nchild
;
1572 nprev
->next
= nchild
;
1581 struct roff_node
*n
;
1585 if (n
->type
== ROFFT_BLOCK
&& n
->body
->child
== NULL
) {
1586 mandoc_msg(MANDOCERR_BLK_EMPTY
,
1587 mdoc
->parse
, n
->line
, n
->pos
, "Bk");
1588 roff_node_delete(mdoc
, n
);
1595 struct roff_node
*nch
;
1597 nch
= mdoc
->last
->child
;
1600 mdoc
->flags
^= MDOC_SMOFF
;
1604 assert(nch
->type
== ROFFT_TEXT
);
1606 if ( ! strcmp(nch
->string
, "on")) {
1607 mdoc
->flags
&= ~MDOC_SMOFF
;
1610 if ( ! strcmp(nch
->string
, "off")) {
1611 mdoc
->flags
|= MDOC_SMOFF
;
1615 mandoc_vmsg(MANDOCERR_SM_BAD
,
1616 mdoc
->parse
, nch
->line
, nch
->pos
,
1617 "%s %s", roff_name
[mdoc
->last
->tok
], nch
->string
);
1618 mdoc_node_relink(mdoc
, nch
);
1623 post_root(POST_ARGS
)
1625 struct roff_node
*n
;
1627 /* Add missing prologue data. */
1629 if (mdoc
->meta
.date
== NULL
)
1630 mdoc
->meta
.date
= mdoc
->quick
?
1632 mandoc_normdate(mdoc
->parse
, NULL
, 0, 0);
1634 if (mdoc
->meta
.title
== NULL
) {
1635 mandoc_msg(MANDOCERR_DT_NOTITLE
,
1636 mdoc
->parse
, 0, 0, "EOF");
1637 mdoc
->meta
.title
= mandoc_strdup("UNTITLED");
1640 if (mdoc
->meta
.vol
== NULL
)
1641 mdoc
->meta
.vol
= mandoc_strdup("LOCAL");
1643 if (mdoc
->meta
.os
== NULL
) {
1644 mandoc_msg(MANDOCERR_OS_MISSING
,
1645 mdoc
->parse
, 0, 0, NULL
);
1646 mdoc
->meta
.os
= mandoc_strdup("");
1649 /* Check that we begin with a proper `Sh'. */
1651 n
= mdoc
->first
->child
;
1652 while (n
!= NULL
&& n
->tok
!= TOKEN_NONE
&&
1653 mdoc_macros
[n
->tok
].flags
& MDOC_PROLOGUE
)
1657 mandoc_msg(MANDOCERR_DOC_EMPTY
, mdoc
->parse
, 0, 0, NULL
);
1658 else if (n
->tok
!= MDOC_Sh
)
1659 mandoc_msg(MANDOCERR_SEC_BEFORE
, mdoc
->parse
,
1660 n
->line
, n
->pos
, roff_name
[n
->tok
]);
1666 struct roff_node
*np
, *nch
, *next
, *prev
;
1671 if (np
->type
!= ROFFT_BODY
)
1674 if (np
->child
== NULL
) {
1675 mandoc_msg(MANDOCERR_RS_EMPTY
, mdoc
->parse
,
1676 np
->line
, np
->pos
, "Rs");
1681 * The full `Rs' block needs special handling to order the
1682 * sub-elements according to `rsord'. Pick through each element
1683 * and correctly order it. This is an insertion sort.
1687 for (nch
= np
->child
->next
; nch
!= NULL
; nch
= next
) {
1688 /* Determine order number of this child. */
1689 for (i
= 0; i
< RSORD_MAX
; i
++)
1690 if (rsord
[i
] == nch
->tok
)
1693 if (i
== RSORD_MAX
) {
1694 mandoc_msg(MANDOCERR_RS_BAD
, mdoc
->parse
,
1695 nch
->line
, nch
->pos
, roff_name
[nch
->tok
]);
1697 } else if (nch
->tok
== MDOC__J
|| nch
->tok
== MDOC__B
)
1698 np
->norm
->Rs
.quote_T
++;
1701 * Remove this child from the chain. This somewhat
1702 * repeats roff_node_unlink(), but since we're
1703 * just re-ordering, there's no need for the
1704 * full unlink process.
1707 if ((next
= nch
->next
) != NULL
)
1708 next
->prev
= nch
->prev
;
1710 if ((prev
= nch
->prev
) != NULL
)
1711 prev
->next
= nch
->next
;
1713 nch
->prev
= nch
->next
= NULL
;
1716 * Scan back until we reach a node that's
1717 * to be ordered before this child.
1720 for ( ; prev
; prev
= prev
->prev
) {
1721 /* Determine order of `prev'. */
1722 for (j
= 0; j
< RSORD_MAX
; j
++)
1723 if (rsord
[j
] == prev
->tok
)
1733 * Set this child back into its correct place
1734 * in front of the `prev' node.
1740 np
->child
->prev
= nch
;
1741 nch
->next
= np
->child
;
1745 prev
->next
->prev
= nch
;
1746 nch
->next
= prev
->next
;
1753 * For some arguments of some macros,
1754 * convert all breakable hyphens into ASCII_HYPH.
1757 post_hyph(POST_ARGS
)
1759 struct roff_node
*nch
;
1762 for (nch
= mdoc
->last
->child
; nch
!= NULL
; nch
= nch
->next
) {
1763 if (nch
->type
!= ROFFT_TEXT
)
1768 while (*(++cp
) != '\0')
1770 isalpha((unsigned char)cp
[-1]) &&
1771 isalpha((unsigned char)cp
[1]))
1780 if (mdoc
->last
->flags
& NODE_LINE
)
1781 mandoc_msg(MANDOCERR_NS_SKIP
, mdoc
->parse
,
1782 mdoc
->last
->line
, mdoc
->last
->pos
, NULL
);
1791 switch (mdoc
->last
->type
) {
1796 switch (mdoc
->lastsec
) {
1801 post_sh_see_also(mdoc
);
1804 post_sh_authors(mdoc
);
1816 post_sh_name(POST_ARGS
)
1818 struct roff_node
*n
;
1823 for (n
= mdoc
->last
->child
; n
!= NULL
; n
= n
->next
) {
1826 if (hasnm
&& n
->child
!= NULL
)
1827 mandoc_vmsg(MANDOCERR_NAMESEC_PUNCT
,
1828 mdoc
->parse
, n
->line
, n
->pos
,
1829 "Nm %s", n
->child
->string
);
1834 if (n
->next
!= NULL
)
1835 mandoc_msg(MANDOCERR_NAMESEC_ND
,
1836 mdoc
->parse
, n
->line
, n
->pos
, NULL
);
1839 if (n
->type
== ROFFT_TEXT
&&
1840 n
->string
[0] == ',' && n
->string
[1] == '\0' &&
1841 n
->next
!= NULL
&& n
->next
->tok
== MDOC_Nm
) {
1847 mandoc_msg(MANDOCERR_NAMESEC_BAD
, mdoc
->parse
,
1848 n
->line
, n
->pos
, roff_name
[n
->tok
]);
1855 mandoc_msg(MANDOCERR_NAMESEC_NONM
, mdoc
->parse
,
1856 mdoc
->last
->line
, mdoc
->last
->pos
, NULL
);
1858 mandoc_msg(MANDOCERR_NAMESEC_NOND
, mdoc
->parse
,
1859 mdoc
->last
->line
, mdoc
->last
->pos
, NULL
);
1863 post_sh_see_also(POST_ARGS
)
1865 const struct roff_node
*n
;
1866 const char *name
, *sec
;
1867 const char *lastname
, *lastsec
, *lastpunct
;
1870 n
= mdoc
->last
->child
;
1871 lastname
= lastsec
= lastpunct
= NULL
;
1873 if (n
->tok
!= MDOC_Xr
||
1875 n
->child
->next
== NULL
)
1878 /* Process one .Xr node. */
1880 name
= n
->child
->string
;
1881 sec
= n
->child
->next
->string
;
1882 if (lastsec
!= NULL
) {
1883 if (lastpunct
[0] != ',' || lastpunct
[1] != '\0')
1884 mandoc_vmsg(MANDOCERR_XR_PUNCT
,
1885 mdoc
->parse
, n
->line
, n
->pos
,
1886 "%s before %s(%s)", lastpunct
,
1888 cmp
= strcmp(lastsec
, sec
);
1890 mandoc_vmsg(MANDOCERR_XR_ORDER
,
1891 mdoc
->parse
, n
->line
, n
->pos
,
1892 "%s(%s) after %s(%s)", name
,
1893 sec
, lastname
, lastsec
);
1894 else if (cmp
== 0 &&
1895 strcasecmp(lastname
, name
) > 0)
1896 mandoc_vmsg(MANDOCERR_XR_ORDER
,
1897 mdoc
->parse
, n
->line
, n
->pos
,
1898 "%s after %s", name
, lastname
);
1903 /* Process the following node. */
1908 if (n
->tok
== MDOC_Xr
) {
1912 if (n
->type
!= ROFFT_TEXT
)
1914 for (name
= n
->string
; *name
!= '\0'; name
++)
1915 if (isalpha((const unsigned char)*name
))
1917 lastpunct
= n
->string
;
1918 if (n
->next
== NULL
|| n
->next
->tok
== MDOC_Rs
)
1919 mandoc_vmsg(MANDOCERR_XR_PUNCT
, mdoc
->parse
,
1920 n
->line
, n
->pos
, "%s after %s(%s)",
1921 lastpunct
, lastname
, lastsec
);
1927 child_an(const struct roff_node
*n
)
1930 for (n
= n
->child
; n
!= NULL
; n
= n
->next
)
1931 if ((n
->tok
== MDOC_An
&& n
->child
!= NULL
) || child_an(n
))
1937 post_sh_authors(POST_ARGS
)
1940 if ( ! child_an(mdoc
->last
))
1941 mandoc_msg(MANDOCERR_AN_MISSING
, mdoc
->parse
,
1942 mdoc
->last
->line
, mdoc
->last
->pos
, NULL
);
1946 post_sh_head(POST_ARGS
)
1948 struct roff_node
*nch
;
1949 const char *goodsec
;
1953 * Process a new section. Sections are either "named" or
1954 * "custom". Custom sections are user-defined, while named ones
1955 * follow a conventional order and may only appear in certain
1959 sec
= mdoc
->last
->sec
;
1961 /* The NAME should be first. */
1963 if (sec
!= SEC_NAME
&& mdoc
->lastnamed
== SEC_NONE
)
1964 mandoc_vmsg(MANDOCERR_NAMESEC_FIRST
, mdoc
->parse
,
1965 mdoc
->last
->line
, mdoc
->last
->pos
, "Sh %s",
1966 sec
!= SEC_CUSTOM
? secnames
[sec
] :
1967 (nch
= mdoc
->last
->child
) == NULL
? "" :
1968 nch
->type
== ROFFT_TEXT
? nch
->string
:
1969 roff_name
[nch
->tok
]);
1971 /* The SYNOPSIS gets special attention in other areas. */
1973 if (sec
== SEC_SYNOPSIS
) {
1974 roff_setreg(mdoc
->roff
, "nS", 1, '=');
1975 mdoc
->flags
|= MDOC_SYNOPSIS
;
1977 roff_setreg(mdoc
->roff
, "nS", 0, '=');
1978 mdoc
->flags
&= ~MDOC_SYNOPSIS
;
1981 /* Mark our last section. */
1983 mdoc
->lastsec
= sec
;
1985 /* We don't care about custom sections after this. */
1987 if (sec
== SEC_CUSTOM
)
1991 * Check whether our non-custom section is being repeated or is
1995 if (sec
== mdoc
->lastnamed
)
1996 mandoc_vmsg(MANDOCERR_SEC_REP
, mdoc
->parse
,
1997 mdoc
->last
->line
, mdoc
->last
->pos
,
1998 "Sh %s", secnames
[sec
]);
2000 if (sec
< mdoc
->lastnamed
)
2001 mandoc_vmsg(MANDOCERR_SEC_ORDER
, mdoc
->parse
,
2002 mdoc
->last
->line
, mdoc
->last
->pos
,
2003 "Sh %s", secnames
[sec
]);
2005 /* Mark the last named section. */
2007 mdoc
->lastnamed
= sec
;
2009 /* Check particular section/manual conventions. */
2011 if (mdoc
->meta
.msec
== NULL
)
2017 if (*mdoc
->meta
.msec
== '4')
2019 goodsec
= "2, 3, 4, 9";
2021 case SEC_RETURN_VALUES
:
2023 if (*mdoc
->meta
.msec
== '2')
2025 if (*mdoc
->meta
.msec
== '3')
2027 if (NULL
== goodsec
)
2028 goodsec
= "2, 3, 9";
2031 if (*mdoc
->meta
.msec
== '9')
2033 if (NULL
== goodsec
)
2035 mandoc_vmsg(MANDOCERR_SEC_MSEC
, mdoc
->parse
,
2036 mdoc
->last
->line
, mdoc
->last
->pos
,
2037 "Sh %s for %s only", secnames
[sec
], goodsec
);
2047 struct roff_node
*n
, *nch
;
2051 if (nch
->next
== NULL
) {
2052 mandoc_vmsg(MANDOCERR_XR_NOSEC
, mdoc
->parse
,
2053 n
->line
, n
->pos
, "Xr %s", nch
->string
);
2056 assert(nch
->next
== n
->last
);
2060 post_ignpar(POST_ARGS
)
2062 struct roff_node
*np
;
2064 switch (mdoc
->last
->type
) {
2077 if ((np
= mdoc
->last
->child
) != NULL
)
2078 if (np
->tok
== MDOC_Pp
|| np
->tok
== MDOC_Lp
) {
2079 mandoc_vmsg(MANDOCERR_PAR_SKIP
,
2080 mdoc
->parse
, np
->line
, np
->pos
,
2081 "%s after %s", roff_name
[np
->tok
],
2082 roff_name
[mdoc
->last
->tok
]);
2083 roff_node_delete(mdoc
, np
);
2086 if ((np
= mdoc
->last
->last
) != NULL
)
2087 if (np
->tok
== MDOC_Pp
|| np
->tok
== MDOC_Lp
) {
2088 mandoc_vmsg(MANDOCERR_PAR_SKIP
, mdoc
->parse
,
2089 np
->line
, np
->pos
, "%s at the end of %s",
2091 roff_name
[mdoc
->last
->tok
]);
2092 roff_node_delete(mdoc
, np
);
2097 post_prevpar(POST_ARGS
)
2099 struct roff_node
*n
;
2102 if (NULL
== n
->prev
)
2104 if (n
->type
!= ROFFT_ELEM
&& n
->type
!= ROFFT_BLOCK
)
2108 * Don't allow prior `Lp' or `Pp' prior to a paragraph-type
2109 * block: `Lp', `Pp', or non-compact `Bd' or `Bl'.
2112 if (n
->prev
->tok
!= MDOC_Pp
&&
2113 n
->prev
->tok
!= MDOC_Lp
&&
2114 n
->prev
->tok
!= ROFF_br
)
2116 if (n
->tok
== MDOC_Bl
&& n
->norm
->Bl
.comp
)
2118 if (n
->tok
== MDOC_Bd
&& n
->norm
->Bd
.comp
)
2120 if (n
->tok
== MDOC_It
&& n
->parent
->norm
->Bl
.comp
)
2123 mandoc_vmsg(MANDOCERR_PAR_SKIP
, mdoc
->parse
,
2124 n
->prev
->line
, n
->prev
->pos
, "%s before %s",
2125 roff_name
[n
->prev
->tok
], roff_name
[n
->tok
]);
2126 roff_node_delete(mdoc
, n
->prev
);
2132 struct roff_node
*np
;
2135 if (np
->tok
!= ROFF_br
&& np
->tok
!= ROFF_sp
)
2138 if (np
->tok
== ROFF_sp
) {
2139 if (np
->child
!= NULL
&& np
->child
->next
!= NULL
)
2140 mandoc_vmsg(MANDOCERR_ARG_EXCESS
, mdoc
->parse
,
2141 np
->child
->next
->line
, np
->child
->next
->pos
,
2142 "sp ... %s", np
->child
->next
->string
);
2143 } else if (np
->child
!= NULL
)
2144 mandoc_vmsg(MANDOCERR_ARG_SKIP
,
2145 mdoc
->parse
, np
->line
, np
->pos
, "%s %s",
2146 roff_name
[np
->tok
], np
->child
->string
);
2148 if ((np
= mdoc
->last
->prev
) == NULL
) {
2149 np
= mdoc
->last
->parent
;
2150 if (np
->tok
!= MDOC_Sh
&& np
->tok
!= MDOC_Ss
)
2152 } else if (np
->tok
!= MDOC_Pp
&& np
->tok
!= MDOC_Lp
&&
2153 (mdoc
->last
->tok
!= ROFF_br
||
2154 (np
->tok
!= ROFF_sp
&& np
->tok
!= ROFF_br
)))
2157 mandoc_vmsg(MANDOCERR_PAR_SKIP
, mdoc
->parse
,
2158 mdoc
->last
->line
, mdoc
->last
->pos
, "%s after %s",
2159 roff_name
[mdoc
->last
->tok
], roff_name
[np
->tok
]);
2160 roff_node_delete(mdoc
, mdoc
->last
);
2166 struct roff_node
*n
;
2170 n
->flags
|= NODE_NOPRT
;
2172 if (mdoc
->meta
.date
!= NULL
) {
2173 mandoc_msg(MANDOCERR_PROLOG_REP
, mdoc
->parse
,
2174 n
->line
, n
->pos
, "Dd");
2175 free(mdoc
->meta
.date
);
2176 } else if (mdoc
->flags
& MDOC_PBODY
)
2177 mandoc_msg(MANDOCERR_PROLOG_LATE
, mdoc
->parse
,
2178 n
->line
, n
->pos
, "Dd");
2179 else if (mdoc
->meta
.title
!= NULL
)
2180 mandoc_msg(MANDOCERR_PROLOG_ORDER
, mdoc
->parse
,
2181 n
->line
, n
->pos
, "Dd after Dt");
2182 else if (mdoc
->meta
.os
!= NULL
)
2183 mandoc_msg(MANDOCERR_PROLOG_ORDER
, mdoc
->parse
,
2184 n
->line
, n
->pos
, "Dd after Os");
2186 if (n
->child
== NULL
|| n
->child
->string
[0] == '\0') {
2187 mdoc
->meta
.date
= mdoc
->quick
? mandoc_strdup("") :
2188 mandoc_normdate(mdoc
->parse
, NULL
, n
->line
, n
->pos
);
2193 deroff(&datestr
, n
);
2195 mdoc
->meta
.date
= datestr
;
2197 mdoc
->meta
.date
= mandoc_normdate(mdoc
->parse
,
2198 datestr
, n
->line
, n
->pos
);
2206 struct roff_node
*nn
, *n
;
2211 n
->flags
|= NODE_NOPRT
;
2213 if (mdoc
->flags
& MDOC_PBODY
) {
2214 mandoc_msg(MANDOCERR_DT_LATE
, mdoc
->parse
,
2215 n
->line
, n
->pos
, "Dt");
2219 if (mdoc
->meta
.title
!= NULL
)
2220 mandoc_msg(MANDOCERR_PROLOG_REP
, mdoc
->parse
,
2221 n
->line
, n
->pos
, "Dt");
2222 else if (mdoc
->meta
.os
!= NULL
)
2223 mandoc_msg(MANDOCERR_PROLOG_ORDER
, mdoc
->parse
,
2224 n
->line
, n
->pos
, "Dt after Os");
2226 free(mdoc
->meta
.title
);
2227 free(mdoc
->meta
.msec
);
2228 free(mdoc
->meta
.vol
);
2229 free(mdoc
->meta
.arch
);
2231 mdoc
->meta
.title
= NULL
;
2232 mdoc
->meta
.msec
= NULL
;
2233 mdoc
->meta
.vol
= NULL
;
2234 mdoc
->meta
.arch
= NULL
;
2236 /* Mandatory first argument: title. */
2239 if (nn
== NULL
|| *nn
->string
== '\0') {
2240 mandoc_msg(MANDOCERR_DT_NOTITLE
,
2241 mdoc
->parse
, n
->line
, n
->pos
, "Dt");
2242 mdoc
->meta
.title
= mandoc_strdup("UNTITLED");
2244 mdoc
->meta
.title
= mandoc_strdup(nn
->string
);
2246 /* Check that all characters are uppercase. */
2248 for (p
= nn
->string
; *p
!= '\0'; p
++)
2249 if (islower((unsigned char)*p
)) {
2250 mandoc_vmsg(MANDOCERR_TITLE_CASE
,
2251 mdoc
->parse
, nn
->line
,
2252 nn
->pos
+ (p
- nn
->string
),
2253 "Dt %s", nn
->string
);
2258 /* Mandatory second argument: section. */
2264 mandoc_vmsg(MANDOCERR_MSEC_MISSING
,
2265 mdoc
->parse
, n
->line
, n
->pos
,
2266 "Dt %s", mdoc
->meta
.title
);
2267 mdoc
->meta
.vol
= mandoc_strdup("LOCAL");
2268 return; /* msec and arch remain NULL. */
2271 mdoc
->meta
.msec
= mandoc_strdup(nn
->string
);
2273 /* Infer volume title from section number. */
2275 cp
= mandoc_a2msec(nn
->string
);
2277 mandoc_vmsg(MANDOCERR_MSEC_BAD
, mdoc
->parse
,
2278 nn
->line
, nn
->pos
, "Dt ... %s", nn
->string
);
2279 mdoc
->meta
.vol
= mandoc_strdup(nn
->string
);
2281 mdoc
->meta
.vol
= mandoc_strdup(cp
);
2283 /* Optional third argument: architecture. */
2285 if ((nn
= nn
->next
) == NULL
)
2288 for (p
= nn
->string
; *p
!= '\0'; p
++)
2289 *p
= tolower((unsigned char)*p
);
2290 mdoc
->meta
.arch
= mandoc_strdup(nn
->string
);
2292 /* Ignore fourth and later arguments. */
2294 if ((nn
= nn
->next
) != NULL
)
2295 mandoc_vmsg(MANDOCERR_ARG_EXCESS
, mdoc
->parse
,
2296 nn
->line
, nn
->pos
, "Dt ... %s", nn
->string
);
2302 struct roff_node
*n
, *nch
;
2309 macro
= !strcmp(nch
->string
, "Open") ? "Ox" :
2310 !strcmp(nch
->string
, "Net") ? "Nx" :
2311 !strcmp(nch
->string
, "Free") ? "Fx" :
2312 !strcmp(nch
->string
, "DragonFly") ? "Dx" : NULL
;
2314 mandoc_msg(MANDOCERR_BX
, mdoc
->parse
,
2315 n
->line
, n
->pos
, macro
);
2318 mdoc
->next
= ROFF_NEXT_SIBLING
;
2319 roff_elem_alloc(mdoc
, n
->line
, n
->pos
, MDOC_Ns
);
2320 mdoc
->last
->flags
|= NODE_NOSRC
;
2321 mdoc
->next
= ROFF_NEXT_SIBLING
;
2323 mdoc
->next
= ROFF_NEXT_CHILD
;
2324 roff_word_alloc(mdoc
, n
->line
, n
->pos
, "BSD");
2325 mdoc
->last
->flags
|= NODE_NOSRC
;
2332 roff_elem_alloc(mdoc
, n
->line
, n
->pos
, MDOC_Ns
);
2333 mdoc
->last
->flags
|= NODE_NOSRC
;
2334 mdoc
->next
= ROFF_NEXT_SIBLING
;
2335 roff_word_alloc(mdoc
, n
->line
, n
->pos
, "-");
2336 mdoc
->last
->flags
|= NODE_NOSRC
;
2337 roff_elem_alloc(mdoc
, n
->line
, n
->pos
, MDOC_Ns
);
2338 mdoc
->last
->flags
|= NODE_NOSRC
;
2342 * Make `Bx's second argument always start with an uppercase
2343 * letter. Groff checks if it's an "accepted" term, but we just
2344 * uppercase blindly.
2347 *nch
->string
= (char)toupper((unsigned char)*nch
->string
);
2354 struct utsname utsname
;
2355 static char *defbuf
;
2357 struct roff_node
*n
;
2360 n
->flags
|= NODE_NOPRT
;
2362 if (mdoc
->meta
.os
!= NULL
)
2363 mandoc_msg(MANDOCERR_PROLOG_REP
, mdoc
->parse
,
2364 n
->line
, n
->pos
, "Os");
2365 else if (mdoc
->flags
& MDOC_PBODY
)
2366 mandoc_msg(MANDOCERR_PROLOG_LATE
, mdoc
->parse
,
2367 n
->line
, n
->pos
, "Os");
2370 * Set the operating system by way of the `Os' macro.
2371 * The order of precedence is:
2372 * 1. the argument of the `Os' macro, unless empty
2373 * 2. the -Ios=foo command line argument, if provided
2374 * 3. -DOSNAME="\"foo\"", if provided during compilation
2375 * 4. "sysname release" from uname(3)
2378 free(mdoc
->meta
.os
);
2379 mdoc
->meta
.os
= NULL
;
2380 deroff(&mdoc
->meta
.os
, n
);
2385 mdoc
->meta
.os
= mandoc_strdup(mdoc
->defos
);
2390 mdoc
->meta
.os
= mandoc_strdup(OSNAME
);
2392 if (defbuf
== NULL
) {
2393 if (uname(&utsname
) == -1) {
2394 mandoc_msg(MANDOCERR_OS_UNAME
, mdoc
->parse
,
2395 n
->line
, n
->pos
, "Os");
2396 defbuf
= mandoc_strdup("UNKNOWN");
2398 mandoc_asprintf(&defbuf
, "%s %s",
2399 utsname
.sysname
, utsname
.release
);
2401 mdoc
->meta
.os
= mandoc_strdup(defbuf
);
2406 mdoc_a2sec(const char *p
)
2410 for (i
= 0; i
< (int)SEC__MAX
; i
++)
2411 if (secnames
[i
] && 0 == strcmp(p
, secnames
[i
]))
2412 return (enum roff_sec
)i
;
2418 macro2len(enum roff_tok macro
)