]>
git.cameronkatri.com Git - mandoc.git/blob - mdoc_validate.c
75ec2e35b3672700db75c386afa66dd30bbfc8f6
1 /* $Id: mdoc_validate.c,v 1.323 2017/05/04 17:48:29 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_argv(struct roff_man
*,
57 struct roff_node
*, struct mdoc_argv
*);
58 static void check_args(struct roff_man
*, struct roff_node
*);
59 static int child_an(const struct roff_node
*);
60 static size_t macro2len(enum roff_tok
);
61 static void rewrite_macro2len(struct roff_man
*, char **);
63 static void post_an(POST_ARGS
);
64 static void post_an_norm(POST_ARGS
);
65 static void post_at(POST_ARGS
);
66 static void post_bd(POST_ARGS
);
67 static void post_bf(POST_ARGS
);
68 static void post_bk(POST_ARGS
);
69 static void post_bl(POST_ARGS
);
70 static void post_bl_block(POST_ARGS
);
71 static void post_bl_head(POST_ARGS
);
72 static void post_bl_norm(POST_ARGS
);
73 static void post_bx(POST_ARGS
);
74 static void post_defaults(POST_ARGS
);
75 static void post_display(POST_ARGS
);
76 static void post_dd(POST_ARGS
);
77 static void post_dt(POST_ARGS
);
78 static void post_en(POST_ARGS
);
79 static void post_es(POST_ARGS
);
80 static void post_eoln(POST_ARGS
);
81 static void post_ex(POST_ARGS
);
82 static void post_fa(POST_ARGS
);
83 static void post_fn(POST_ARGS
);
84 static void post_fname(POST_ARGS
);
85 static void post_fo(POST_ARGS
);
86 static void post_hyph(POST_ARGS
);
87 static void post_ignpar(POST_ARGS
);
88 static void post_it(POST_ARGS
);
89 static void post_lb(POST_ARGS
);
90 static void post_nd(POST_ARGS
);
91 static void post_nm(POST_ARGS
);
92 static void post_ns(POST_ARGS
);
93 static void post_obsolete(POST_ARGS
);
94 static void post_os(POST_ARGS
);
95 static void post_par(POST_ARGS
);
96 static void post_prevpar(POST_ARGS
);
97 static void post_root(POST_ARGS
);
98 static void post_rs(POST_ARGS
);
99 static void post_rv(POST_ARGS
);
100 static void post_sh(POST_ARGS
);
101 static void post_sh_head(POST_ARGS
);
102 static void post_sh_name(POST_ARGS
);
103 static void post_sh_see_also(POST_ARGS
);
104 static void post_sh_authors(POST_ARGS
);
105 static void post_sm(POST_ARGS
);
106 static void post_st(POST_ARGS
);
107 static void post_std(POST_ARGS
);
108 static void post_xr(POST_ARGS
);
109 static void post_xx(POST_ARGS
);
111 static const v_post __mdoc_valids
[MDOC_MAX
- MDOC_Dd
] = {
116 post_ignpar
, /* Ss */
118 post_display
, /* D1 */
119 post_display
, /* Dl */
120 post_display
, /* Bd */
128 post_defaults
, /* Ar */
142 post_defaults
, /* Li */
146 post_obsolete
, /* Ot */
147 post_defaults
, /* Pa */
154 post_hyph
, /* %B */ /* FIXME: can be used outside Rs/Re. */
162 post_hyph
, /* %T */ /* FIXME: can be used outside Rs/Re. */
174 post_obsolete
, /* Db */
216 post_obsolete
, /* Fr */
221 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
);
314 check_args(mdoc
, mdoc
->last
);
317 * Closing delimiters are not special at the
318 * beginning of a block, opening delimiters
319 * are not special at the end.
322 if (n
->child
!= NULL
)
323 n
->child
->flags
&= ~NODE_DELIMC
;
325 n
->last
->flags
&= ~NODE_DELIMO
;
327 /* Call the macro's postprocessor. */
329 if (n
->tok
< ROFF_MAX
) {
340 assert(n
->tok
>= MDOC_Dd
&& n
->tok
< MDOC_MAX
);
341 p
= mdoc_valids
+ n
->tok
;
351 check_args(struct roff_man
*mdoc
, struct roff_node
*n
)
358 assert(n
->args
->argc
);
359 for (i
= 0; i
< (int)n
->args
->argc
; i
++)
360 check_argv(mdoc
, n
, &n
->args
->argv
[i
]);
364 check_argv(struct roff_man
*mdoc
, struct roff_node
*n
, struct mdoc_argv
*v
)
368 for (i
= 0; i
< (int)v
->sz
; i
++)
369 check_text(mdoc
, v
->line
, v
->pos
, v
->value
[i
]);
373 check_text(struct roff_man
*mdoc
, int ln
, int pos
, char *p
)
377 if (MDOC_LITERAL
& mdoc
->flags
)
380 for (cp
= p
; NULL
!= (p
= strchr(p
, '\t')); p
++)
381 mandoc_msg(MANDOCERR_FI_TAB
, mdoc
->parse
,
382 ln
, pos
+ (int)(p
- cp
), NULL
);
386 post_bl_norm(POST_ARGS
)
389 struct mdoc_argv
*argv
, *wa
;
391 enum mdocargt mdoclt
;
394 n
= mdoc
->last
->parent
;
395 n
->norm
->Bl
.type
= LIST__NONE
;
398 * First figure out which kind of list to use: bind ourselves to
399 * the first mentioned list type and warn about any remaining
400 * ones. If we find no list type, we default to LIST_item.
403 wa
= (n
->args
== NULL
) ? NULL
: n
->args
->argv
;
404 mdoclt
= MDOC_ARG_MAX
;
405 for (i
= 0; n
->args
&& i
< (int)n
->args
->argc
; i
++) {
406 argv
= n
->args
->argv
+ i
;
409 /* Set list types. */
443 /* Set list arguments. */
445 if (n
->norm
->Bl
.comp
)
446 mandoc_msg(MANDOCERR_ARG_REP
,
447 mdoc
->parse
, argv
->line
,
448 argv
->pos
, "Bl -compact");
449 n
->norm
->Bl
.comp
= 1;
454 mandoc_msg(MANDOCERR_ARG_EMPTY
,
455 mdoc
->parse
, argv
->line
,
456 argv
->pos
, "Bl -width");
457 n
->norm
->Bl
.width
= "0n";
460 if (NULL
!= n
->norm
->Bl
.width
)
461 mandoc_vmsg(MANDOCERR_ARG_REP
,
462 mdoc
->parse
, argv
->line
,
463 argv
->pos
, "Bl -width %s",
465 rewrite_macro2len(mdoc
, argv
->value
);
466 n
->norm
->Bl
.width
= argv
->value
[0];
470 mandoc_msg(MANDOCERR_ARG_EMPTY
,
471 mdoc
->parse
, argv
->line
,
472 argv
->pos
, "Bl -offset");
475 if (NULL
!= n
->norm
->Bl
.offs
)
476 mandoc_vmsg(MANDOCERR_ARG_REP
,
477 mdoc
->parse
, argv
->line
,
478 argv
->pos
, "Bl -offset %s",
480 rewrite_macro2len(mdoc
, argv
->value
);
481 n
->norm
->Bl
.offs
= argv
->value
[0];
486 if (LIST__NONE
== lt
)
490 /* Check: multiple list types. */
492 if (LIST__NONE
!= n
->norm
->Bl
.type
) {
493 mandoc_vmsg(MANDOCERR_BL_REP
,
494 mdoc
->parse
, n
->line
, n
->pos
,
495 "Bl -%s", mdoc_argnames
[argv
->arg
]);
499 /* The list type should come first. */
501 if (n
->norm
->Bl
.width
||
504 mandoc_vmsg(MANDOCERR_BL_LATETYPE
,
505 mdoc
->parse
, n
->line
, n
->pos
, "Bl -%s",
506 mdoc_argnames
[n
->args
->argv
[0].arg
]);
508 n
->norm
->Bl
.type
= lt
;
509 if (LIST_column
== lt
) {
510 n
->norm
->Bl
.ncols
= argv
->sz
;
511 n
->norm
->Bl
.cols
= (void *)argv
->value
;
515 /* Allow lists to default to LIST_item. */
517 if (LIST__NONE
== n
->norm
->Bl
.type
) {
518 mandoc_msg(MANDOCERR_BL_NOTYPE
, mdoc
->parse
,
519 n
->line
, n
->pos
, "Bl");
520 n
->norm
->Bl
.type
= LIST_item
;
525 * Validate the width field. Some list types don't need width
526 * types and should be warned about them. Others should have it
527 * and must also be warned. Yet others have a default and need
531 switch (n
->norm
->Bl
.type
) {
533 if (NULL
== n
->norm
->Bl
.width
)
534 mandoc_msg(MANDOCERR_BL_NOWIDTH
, mdoc
->parse
,
535 n
->line
, n
->pos
, "Bl -tag");
542 if (n
->norm
->Bl
.width
)
543 mandoc_vmsg(MANDOCERR_BL_SKIPW
, mdoc
->parse
,
544 wa
->line
, wa
->pos
, "Bl -%s",
545 mdoc_argnames
[mdoclt
]);
550 if (NULL
== n
->norm
->Bl
.width
)
551 n
->norm
->Bl
.width
= "2n";
554 if (NULL
== n
->norm
->Bl
.width
)
555 n
->norm
->Bl
.width
= "3n";
566 struct mdoc_argv
*argv
;
571 for (i
= 0; n
->args
&& i
< (int)n
->args
->argc
; i
++) {
572 argv
= n
->args
->argv
+ i
;
592 mandoc_msg(MANDOCERR_BD_FILE
, mdoc
->parse
,
593 n
->line
, n
->pos
, NULL
);
597 mandoc_msg(MANDOCERR_ARG_EMPTY
,
598 mdoc
->parse
, argv
->line
,
599 argv
->pos
, "Bd -offset");
602 if (NULL
!= n
->norm
->Bd
.offs
)
603 mandoc_vmsg(MANDOCERR_ARG_REP
,
604 mdoc
->parse
, argv
->line
,
605 argv
->pos
, "Bd -offset %s",
607 rewrite_macro2len(mdoc
, argv
->value
);
608 n
->norm
->Bd
.offs
= argv
->value
[0];
611 if (n
->norm
->Bd
.comp
)
612 mandoc_msg(MANDOCERR_ARG_REP
,
613 mdoc
->parse
, argv
->line
,
614 argv
->pos
, "Bd -compact");
615 n
->norm
->Bd
.comp
= 1;
620 if (DISP__NONE
== dt
)
623 if (DISP__NONE
== n
->norm
->Bd
.type
)
624 n
->norm
->Bd
.type
= dt
;
626 mandoc_vmsg(MANDOCERR_BD_REP
,
627 mdoc
->parse
, n
->line
, n
->pos
,
628 "Bd -%s", mdoc_argnames
[argv
->arg
]);
631 if (DISP__NONE
== n
->norm
->Bd
.type
) {
632 mandoc_msg(MANDOCERR_BD_NOTYPE
, mdoc
->parse
,
633 n
->line
, n
->pos
, "Bd");
634 n
->norm
->Bd
.type
= DISP_ragged
;
639 * Stand-alone line macros.
643 post_an_norm(POST_ARGS
)
646 struct mdoc_argv
*argv
;
653 for (i
= 1; i
< n
->args
->argc
; i
++) {
654 argv
= n
->args
->argv
+ i
;
655 mandoc_vmsg(MANDOCERR_AN_REP
,
656 mdoc
->parse
, argv
->line
, argv
->pos
,
657 "An -%s", mdoc_argnames
[argv
->arg
]);
660 argv
= n
->args
->argv
;
661 if (argv
->arg
== MDOC_Split
)
662 n
->norm
->An
.auth
= AUTH_split
;
663 else if (argv
->arg
== MDOC_Nosplit
)
664 n
->norm
->An
.auth
= AUTH_nosplit
;
675 if (n
->child
!= NULL
)
676 mandoc_vmsg(MANDOCERR_ARG_SKIP
, mdoc
->parse
, n
->line
,
677 n
->pos
, "%s %s", roff_name
[n
->tok
], n
->child
->string
);
679 while (n
->child
!= NULL
)
680 roff_node_delete(mdoc
, n
->child
);
682 roff_word_alloc(mdoc
, n
->line
, n
->pos
, n
->tok
== MDOC_Bt
?
683 "is currently in beta test." : "currently under development.");
684 mdoc
->last
->flags
|= NODE_EOS
| NODE_NOSRC
;
689 build_list(struct roff_man
*mdoc
, int tok
)
694 n
= mdoc
->last
->next
;
695 for (ic
= 1;; ic
++) {
696 roff_elem_alloc(mdoc
, n
->line
, n
->pos
, tok
);
697 mdoc
->last
->flags
|= NODE_NOSRC
;
698 mdoc_node_relink(mdoc
, n
);
699 n
= mdoc
->last
= mdoc
->last
->parent
;
700 mdoc
->next
= ROFF_NEXT_SIBLING
;
703 if (ic
> 1 || n
->next
->next
!= NULL
) {
704 roff_word_alloc(mdoc
, n
->line
, n
->pos
, ",");
705 mdoc
->last
->flags
|= NODE_DELIMC
| NODE_NOSRC
;
707 n
= mdoc
->last
->next
;
708 if (n
->next
== NULL
) {
709 roff_word_alloc(mdoc
, n
->line
, n
->pos
, "and");
710 mdoc
->last
->flags
|= NODE_NOSRC
;
724 mdoc
->next
= ROFF_NEXT_CHILD
;
725 roff_word_alloc(mdoc
, n
->line
, n
->pos
, "The");
726 mdoc
->last
->flags
|= NODE_NOSRC
;
728 if (mdoc
->last
->next
!= NULL
)
729 ic
= build_list(mdoc
, MDOC_Nm
);
730 else if (mdoc
->meta
.name
!= NULL
) {
731 roff_elem_alloc(mdoc
, n
->line
, n
->pos
, MDOC_Nm
);
732 mdoc
->last
->flags
|= NODE_NOSRC
;
733 roff_word_alloc(mdoc
, n
->line
, n
->pos
, mdoc
->meta
.name
);
734 mdoc
->last
->flags
|= NODE_NOSRC
;
735 mdoc
->last
= mdoc
->last
->parent
;
736 mdoc
->next
= ROFF_NEXT_SIBLING
;
739 mandoc_msg(MANDOCERR_EX_NONAME
, mdoc
->parse
,
740 n
->line
, n
->pos
, "Ex");
744 roff_word_alloc(mdoc
, n
->line
, n
->pos
,
745 ic
> 1 ? "utilities exit\\~0" : "utility exits\\~0");
746 mdoc
->last
->flags
|= NODE_NOSRC
;
747 roff_word_alloc(mdoc
, n
->line
, n
->pos
,
748 "on success, and\\~>0 if an error occurs.");
749 mdoc
->last
->flags
|= NODE_EOS
| NODE_NOSRC
;
760 assert(n
->child
->type
== ROFFT_TEXT
);
761 mdoc
->next
= ROFF_NEXT_CHILD
;
763 if ((p
= mdoc_a2lib(n
->child
->string
)) != NULL
) {
764 n
->child
->flags
|= NODE_NOPRT
;
765 roff_word_alloc(mdoc
, n
->line
, n
->pos
, p
);
766 mdoc
->last
->flags
= NODE_NOSRC
;
771 roff_word_alloc(mdoc
, n
->line
, n
->pos
, "library");
772 mdoc
->last
->flags
= NODE_NOSRC
;
773 roff_word_alloc(mdoc
, n
->line
, n
->pos
, "\\(Lq");
774 mdoc
->last
->flags
= NODE_DELIMO
| NODE_NOSRC
;
775 mdoc
->last
= mdoc
->last
->next
;
776 roff_word_alloc(mdoc
, n
->line
, n
->pos
, "\\(Rq");
777 mdoc
->last
->flags
= NODE_DELIMC
| NODE_NOSRC
;
790 mdoc
->next
= ROFF_NEXT_CHILD
;
791 if (n
->child
!= NULL
) {
792 roff_word_alloc(mdoc
, n
->line
, n
->pos
, "The");
793 mdoc
->last
->flags
|= NODE_NOSRC
;
794 ic
= build_list(mdoc
, MDOC_Fn
);
795 roff_word_alloc(mdoc
, n
->line
, n
->pos
,
796 ic
> 1 ? "functions return" : "function returns");
797 mdoc
->last
->flags
|= NODE_NOSRC
;
798 roff_word_alloc(mdoc
, n
->line
, n
->pos
,
799 "the value\\~0 if successful;");
801 roff_word_alloc(mdoc
, n
->line
, n
->pos
, "Upon successful "
802 "completion, the value\\~0 is returned;");
803 mdoc
->last
->flags
|= NODE_NOSRC
;
805 roff_word_alloc(mdoc
, n
->line
, n
->pos
, "otherwise "
806 "the value\\~\\-1 is returned and the global variable");
807 mdoc
->last
->flags
|= NODE_NOSRC
;
808 roff_elem_alloc(mdoc
, n
->line
, n
->pos
, MDOC_Va
);
809 mdoc
->last
->flags
|= NODE_NOSRC
;
810 roff_word_alloc(mdoc
, n
->line
, n
->pos
, "errno");
811 mdoc
->last
->flags
|= NODE_NOSRC
;
812 mdoc
->last
= mdoc
->last
->parent
;
813 mdoc
->next
= ROFF_NEXT_SIBLING
;
814 roff_word_alloc(mdoc
, n
->line
, n
->pos
,
815 "is set to indicate the error.");
816 mdoc
->last
->flags
|= NODE_EOS
| NODE_NOSRC
;
826 if (n
->args
&& n
->args
->argc
== 1)
827 if (n
->args
->argv
[0].arg
== MDOC_Std
)
830 mandoc_msg(MANDOCERR_ARG_STD
, mdoc
->parse
,
831 n
->line
, n
->pos
, roff_name
[n
->tok
]);
837 struct roff_node
*n
, *nch
;
842 assert(nch
->type
== ROFFT_TEXT
);
844 if ((p
= mdoc_a2st(nch
->string
)) == NULL
) {
845 mandoc_vmsg(MANDOCERR_ST_BAD
, mdoc
->parse
,
846 nch
->line
, nch
->pos
, "St %s", nch
->string
);
847 roff_node_delete(mdoc
, n
);
851 nch
->flags
|= NODE_NOPRT
;
852 mdoc
->next
= ROFF_NEXT_CHILD
;
853 roff_word_alloc(mdoc
, nch
->line
, nch
->pos
, p
);
854 mdoc
->last
->flags
|= NODE_NOSRC
;
859 post_obsolete(POST_ARGS
)
864 if (n
->type
== ROFFT_ELEM
|| n
->type
== ROFFT_BLOCK
)
865 mandoc_msg(MANDOCERR_MACRO_OBS
, mdoc
->parse
,
866 n
->line
, n
->pos
, roff_name
[n
->tok
]);
876 struct roff_node
*np
, *nch
;
879 * Unlike other data pointers, these are "housed" by the HEAD
880 * element, which contains the goods.
884 if (np
->type
!= ROFFT_HEAD
)
887 assert(np
->parent
->type
== ROFFT_BLOCK
);
888 assert(np
->parent
->tok
== MDOC_Bf
);
890 /* Check the number of arguments. */
893 if (np
->parent
->args
== NULL
) {
895 mandoc_msg(MANDOCERR_BF_NOFONT
, mdoc
->parse
,
896 np
->line
, np
->pos
, "Bf");
902 mandoc_vmsg(MANDOCERR_ARG_EXCESS
, mdoc
->parse
,
903 nch
->line
, nch
->pos
, "Bf ... %s", nch
->string
);
905 /* Extract argument into data. */
907 if (np
->parent
->args
!= NULL
) {
908 switch (np
->parent
->args
->argv
[0].arg
) {
910 np
->norm
->Bf
.font
= FONT_Em
;
913 np
->norm
->Bf
.font
= FONT_Li
;
916 np
->norm
->Bf
.font
= FONT_Sy
;
924 /* Extract parameter into data. */
926 if ( ! strcmp(np
->child
->string
, "Em"))
927 np
->norm
->Bf
.font
= FONT_Em
;
928 else if ( ! strcmp(np
->child
->string
, "Li"))
929 np
->norm
->Bf
.font
= FONT_Li
;
930 else if ( ! strcmp(np
->child
->string
, "Sy"))
931 np
->norm
->Bf
.font
= FONT_Sy
;
933 mandoc_vmsg(MANDOCERR_BF_BADFONT
, mdoc
->parse
,
934 np
->child
->line
, np
->child
->pos
,
935 "Bf %s", np
->child
->string
);
939 post_fname(POST_ARGS
)
941 const struct roff_node
*n
;
945 n
= mdoc
->last
->child
;
946 pos
= strcspn(n
->string
, "()");
947 cp
= n
->string
+ pos
;
948 if ( ! (cp
[0] == '\0' || (cp
[0] == '(' && cp
[1] == '*')))
949 mandoc_msg(MANDOCERR_FN_PAREN
, mdoc
->parse
,
950 n
->line
, n
->pos
+ pos
, n
->string
);
964 const struct roff_node
*n
;
968 if (n
->type
!= ROFFT_HEAD
)
971 if (n
->child
== NULL
) {
972 mandoc_msg(MANDOCERR_FO_NOHEAD
, mdoc
->parse
,
973 n
->line
, n
->pos
, "Fo");
976 if (n
->child
!= n
->last
) {
977 mandoc_vmsg(MANDOCERR_ARG_EXCESS
, mdoc
->parse
,
978 n
->child
->next
->line
, n
->child
->next
->pos
,
979 "Fo ... %s", n
->child
->next
->string
);
980 while (n
->child
!= n
->last
)
981 roff_node_delete(mdoc
, n
->last
);
990 const struct roff_node
*n
;
993 for (n
= mdoc
->last
->child
; n
!= NULL
; n
= n
->next
) {
994 for (cp
= n
->string
; *cp
!= '\0'; cp
++) {
995 /* Ignore callbacks and alterations. */
996 if (*cp
== '(' || *cp
== '{')
1000 mandoc_msg(MANDOCERR_FA_COMMA
, mdoc
->parse
,
1001 n
->line
, n
->pos
+ (cp
- n
->string
),
1011 struct roff_node
*n
;
1015 if (n
->last
!= NULL
&&
1016 (n
->last
->tok
== MDOC_Pp
||
1017 n
->last
->tok
== MDOC_Lp
))
1018 mdoc_node_relink(mdoc
, n
->last
);
1020 if (mdoc
->meta
.name
== NULL
)
1021 deroff(&mdoc
->meta
.name
, n
);
1023 if (mdoc
->meta
.name
== NULL
||
1024 (mdoc
->lastsec
== SEC_NAME
&& n
->child
== NULL
))
1025 mandoc_msg(MANDOCERR_NM_NONAME
, mdoc
->parse
,
1026 n
->line
, n
->pos
, "Nm");
1028 if ((n
->type
!= ROFFT_ELEM
&& n
->type
!= ROFFT_HEAD
) ||
1029 (n
->child
!= NULL
&& n
->child
->type
== ROFFT_TEXT
) ||
1030 mdoc
->meta
.name
== NULL
)
1033 mdoc
->next
= ROFF_NEXT_CHILD
;
1034 roff_word_alloc(mdoc
, n
->line
, n
->pos
, mdoc
->meta
.name
);
1035 mdoc
->last
->flags
|= NODE_NOSRC
;
1042 struct roff_node
*n
;
1046 if (n
->type
!= ROFFT_BODY
)
1049 if (n
->sec
!= SEC_NAME
)
1050 mandoc_msg(MANDOCERR_ND_LATE
, mdoc
->parse
,
1051 n
->line
, n
->pos
, "Nd");
1053 if (n
->child
== NULL
)
1054 mandoc_msg(MANDOCERR_ND_EMPTY
, mdoc
->parse
,
1055 n
->line
, n
->pos
, "Nd");
1061 post_display(POST_ARGS
)
1063 struct roff_node
*n
, *np
;
1068 if (n
->end
!= ENDBODY_NOT
) {
1069 if (n
->tok
== MDOC_Bd
&&
1070 n
->body
->parent
->args
== NULL
)
1071 roff_node_delete(mdoc
, n
);
1072 } else if (n
->child
== NULL
)
1073 mandoc_msg(MANDOCERR_BLK_EMPTY
, mdoc
->parse
,
1074 n
->line
, n
->pos
, roff_name
[n
->tok
]);
1075 else if (n
->tok
== MDOC_D1
)
1079 if (n
->tok
== MDOC_Bd
) {
1080 if (n
->args
== NULL
) {
1081 mandoc_msg(MANDOCERR_BD_NOARG
,
1082 mdoc
->parse
, n
->line
, n
->pos
, "Bd");
1083 mdoc
->next
= ROFF_NEXT_SIBLING
;
1084 while (n
->body
->child
!= NULL
)
1085 mdoc_node_relink(mdoc
,
1087 roff_node_delete(mdoc
, n
);
1093 for (np
= n
->parent
; np
!= NULL
; np
= np
->parent
) {
1094 if (np
->type
== ROFFT_BLOCK
&& np
->tok
== MDOC_Bd
) {
1095 mandoc_vmsg(MANDOCERR_BD_NEST
,
1096 mdoc
->parse
, n
->line
, n
->pos
,
1097 "%s in Bd", roff_name
[n
->tok
]);
1108 post_defaults(POST_ARGS
)
1110 struct roff_node
*nn
;
1113 * The `Ar' defaults to "file ..." if no value is provided as an
1114 * argument; the `Mt' and `Pa' macros use "~"; the `Li' just
1115 * gets an empty string.
1118 if (mdoc
->last
->child
!= NULL
)
1125 mdoc
->next
= ROFF_NEXT_CHILD
;
1126 roff_word_alloc(mdoc
, nn
->line
, nn
->pos
, "file");
1127 mdoc
->last
->flags
|= NODE_NOSRC
;
1128 roff_word_alloc(mdoc
, nn
->line
, nn
->pos
, "...");
1129 mdoc
->last
->flags
|= NODE_NOSRC
;
1133 mdoc
->next
= ROFF_NEXT_CHILD
;
1134 roff_word_alloc(mdoc
, nn
->line
, nn
->pos
, "~");
1135 mdoc
->last
->flags
|= NODE_NOSRC
;
1146 struct roff_node
*n
, *nch
;
1153 * If we have a child, look it up in the standard keys. If a
1154 * key exist, use that instead of the child; if it doesn't,
1155 * prefix "AT&T UNIX " to the existing data.
1159 if (nch
!= NULL
&& ((att
= mdoc_a2att(nch
->string
)) == NULL
))
1160 mandoc_vmsg(MANDOCERR_AT_BAD
, mdoc
->parse
,
1161 nch
->line
, nch
->pos
, "At %s", nch
->string
);
1163 mdoc
->next
= ROFF_NEXT_CHILD
;
1165 roff_word_alloc(mdoc
, nch
->line
, nch
->pos
, att
);
1166 nch
->flags
|= NODE_NOPRT
;
1168 roff_word_alloc(mdoc
, n
->line
, n
->pos
, "AT&T UNIX");
1169 mdoc
->last
->flags
|= NODE_NOSRC
;
1176 struct roff_node
*np
, *nch
;
1182 if (np
->norm
->An
.auth
== AUTH__NONE
) {
1184 mandoc_msg(MANDOCERR_MACRO_EMPTY
, mdoc
->parse
,
1185 np
->line
, np
->pos
, "An");
1186 } else if (nch
!= NULL
)
1187 mandoc_vmsg(MANDOCERR_ARG_EXCESS
, mdoc
->parse
,
1188 nch
->line
, nch
->pos
, "An ... %s", nch
->string
);
1195 post_obsolete(mdoc
);
1196 if (mdoc
->last
->type
== ROFFT_BLOCK
)
1197 mdoc
->last
->norm
->Es
= mdoc
->last_es
;
1204 post_obsolete(mdoc
);
1205 mdoc
->last_es
= mdoc
->last
;
1211 struct roff_node
*n
;
1237 mdoc
->next
= ROFF_NEXT_CHILD
;
1238 roff_word_alloc(mdoc
, n
->line
, n
->pos
, os
);
1239 mdoc
->last
->flags
|= NODE_NOSRC
;
1246 struct roff_node
*nbl
, *nit
, *nch
;
1253 if (nit
->type
!= ROFFT_BLOCK
)
1256 nbl
= nit
->parent
->parent
;
1257 lt
= nbl
->norm
->Bl
.type
;
1265 if (nit
->head
->child
== NULL
)
1266 mandoc_vmsg(MANDOCERR_IT_NOHEAD
,
1267 mdoc
->parse
, nit
->line
, nit
->pos
,
1269 mdoc_argnames
[nbl
->args
->argv
[0].arg
]);
1275 if (nit
->body
== NULL
|| nit
->body
->child
== NULL
)
1276 mandoc_vmsg(MANDOCERR_IT_NOBODY
,
1277 mdoc
->parse
, nit
->line
, nit
->pos
,
1279 mdoc_argnames
[nbl
->args
->argv
[0].arg
]);
1282 if ((nch
= nit
->head
->child
) != NULL
)
1283 mandoc_vmsg(MANDOCERR_ARG_SKIP
, mdoc
->parse
,
1284 nit
->line
, nit
->pos
, "It %s",
1285 nch
->string
== NULL
? roff_name
[nch
->tok
] :
1289 cols
= (int)nbl
->norm
->Bl
.ncols
;
1291 assert(nit
->head
->child
== NULL
);
1294 for (nch
= nit
->child
; nch
!= NULL
; nch
= nch
->next
)
1295 if (nch
->type
== ROFFT_BODY
)
1298 if (i
< cols
|| i
> cols
+ 1)
1299 mandoc_vmsg(MANDOCERR_BL_COL
,
1300 mdoc
->parse
, nit
->line
, nit
->pos
,
1301 "%d columns, %d cells", cols
, i
);
1309 post_bl_block(POST_ARGS
)
1311 struct roff_node
*n
, *ni
, *nc
;
1316 for (ni
= n
->body
->child
; ni
!= NULL
; ni
= ni
->next
) {
1317 if (ni
->body
== NULL
)
1319 nc
= ni
->body
->last
;
1320 while (nc
!= NULL
) {
1330 if (ni
->next
== NULL
) {
1331 mandoc_msg(MANDOCERR_PAR_MOVE
,
1332 mdoc
->parse
, nc
->line
, nc
->pos
,
1333 roff_name
[nc
->tok
]);
1334 mdoc_node_relink(mdoc
, nc
);
1335 } else if (n
->norm
->Bl
.comp
== 0 &&
1336 n
->norm
->Bl
.type
!= LIST_column
) {
1337 mandoc_vmsg(MANDOCERR_PAR_SKIP
,
1338 mdoc
->parse
, nc
->line
, nc
->pos
,
1339 "%s before It", roff_name
[nc
->tok
]);
1340 roff_node_delete(mdoc
, nc
);
1343 nc
= ni
->body
->last
;
1349 * If the argument of -offset or -width is a macro,
1350 * replace it with the associated default width.
1353 rewrite_macro2len(struct roff_man
*mdoc
, char **arg
)
1360 else if ( ! strcmp(*arg
, "Ds"))
1362 else if ((tok
= roffhash_find(mdoc
->mdocmac
, *arg
, 0)) == TOKEN_NONE
)
1365 width
= macro2len(tok
);
1368 mandoc_asprintf(arg
, "%zun", width
);
1372 post_bl_head(POST_ARGS
)
1374 struct roff_node
*nbl
, *nh
, *nch
, *nnext
;
1375 struct mdoc_argv
*argv
;
1381 if (nh
->norm
->Bl
.type
!= LIST_column
) {
1382 if ((nch
= nh
->child
) == NULL
)
1384 mandoc_vmsg(MANDOCERR_ARG_EXCESS
, mdoc
->parse
,
1385 nch
->line
, nch
->pos
, "Bl ... %s", nch
->string
);
1386 while (nch
!= NULL
) {
1387 roff_node_delete(mdoc
, nch
);
1394 * Append old-style lists, where the column width specifiers
1395 * trail as macro parameters, to the new-style ("normal-form")
1396 * lists where they're argument values following -column.
1399 if (nh
->child
== NULL
)
1403 for (j
= 0; j
< (int)nbl
->args
->argc
; j
++)
1404 if (nbl
->args
->argv
[j
].arg
== MDOC_Column
)
1407 assert(j
< (int)nbl
->args
->argc
);
1410 * Accommodate for new-style groff column syntax. Shuffle the
1411 * child nodes, all of which must be TEXT, as arguments for the
1412 * column field. Then, delete the head children.
1415 argv
= nbl
->args
->argv
+ j
;
1417 for (nch
= nh
->child
; nch
!= NULL
; nch
= nch
->next
)
1419 argv
->value
= mandoc_reallocarray(argv
->value
,
1420 argv
->sz
, sizeof(char *));
1422 nh
->norm
->Bl
.ncols
= argv
->sz
;
1423 nh
->norm
->Bl
.cols
= (void *)argv
->value
;
1425 for (nch
= nh
->child
; nch
!= NULL
; nch
= nnext
) {
1426 argv
->value
[i
++] = nch
->string
;
1429 roff_node_delete(NULL
, nch
);
1437 struct roff_node
*nparent
, *nprev
; /* of the Bl block */
1438 struct roff_node
*nblock
, *nbody
; /* of the Bl */
1439 struct roff_node
*nchild
, *nnext
; /* of the Bl body */
1442 switch (nbody
->type
) {
1444 post_bl_block(mdoc
);
1454 if (nbody
->end
!= ENDBODY_NOT
)
1457 nchild
= nbody
->child
;
1458 if (nchild
== NULL
) {
1459 mandoc_msg(MANDOCERR_BLK_EMPTY
, mdoc
->parse
,
1460 nbody
->line
, nbody
->pos
, "Bl");
1463 while (nchild
!= NULL
) {
1464 nnext
= nchild
->next
;
1465 if (nchild
->tok
== MDOC_It
||
1466 (nchild
->tok
== MDOC_Sm
&&
1467 nnext
!= NULL
&& nnext
->tok
== MDOC_It
)) {
1473 * In .Bl -column, the first rows may be implicit,
1474 * that is, they may not start with .It macros.
1475 * Such rows may be followed by nodes generated on the
1476 * roff level, for example .TS, which cannot be moved
1477 * out of the list. In that case, wrap such roff nodes
1478 * into an implicit row.
1481 if (nchild
->prev
!= NULL
) {
1482 mdoc
->last
= nchild
;
1483 mdoc
->next
= ROFF_NEXT_SIBLING
;
1484 roff_block_alloc(mdoc
, nchild
->line
,
1485 nchild
->pos
, MDOC_It
);
1486 roff_head_alloc(mdoc
, nchild
->line
,
1487 nchild
->pos
, MDOC_It
);
1488 mdoc
->next
= ROFF_NEXT_SIBLING
;
1489 roff_body_alloc(mdoc
, nchild
->line
,
1490 nchild
->pos
, MDOC_It
);
1491 while (nchild
->tok
!= MDOC_It
) {
1492 mdoc_node_relink(mdoc
, nchild
);
1493 if ((nchild
= nnext
) == NULL
)
1495 nnext
= nchild
->next
;
1496 mdoc
->next
= ROFF_NEXT_SIBLING
;
1502 mandoc_msg(MANDOCERR_BL_MOVE
, mdoc
->parse
,
1503 nchild
->line
, nchild
->pos
, roff_name
[nchild
->tok
]);
1506 * Move the node out of the Bl block.
1507 * First, collect all required node pointers.
1510 nblock
= nbody
->parent
;
1511 nprev
= nblock
->prev
;
1512 nparent
= nblock
->parent
;
1515 * Unlink this child.
1518 nbody
->child
= nnext
;
1525 * Relink this child.
1528 nchild
->parent
= nparent
;
1529 nchild
->prev
= nprev
;
1530 nchild
->next
= nblock
;
1532 nblock
->prev
= nchild
;
1534 nparent
->child
= nchild
;
1536 nprev
->next
= nchild
;
1545 struct roff_node
*n
;
1549 if (n
->type
== ROFFT_BLOCK
&& n
->body
->child
== NULL
) {
1550 mandoc_msg(MANDOCERR_BLK_EMPTY
,
1551 mdoc
->parse
, n
->line
, n
->pos
, "Bk");
1552 roff_node_delete(mdoc
, n
);
1559 struct roff_node
*nch
;
1561 nch
= mdoc
->last
->child
;
1564 mdoc
->flags
^= MDOC_SMOFF
;
1568 assert(nch
->type
== ROFFT_TEXT
);
1570 if ( ! strcmp(nch
->string
, "on")) {
1571 mdoc
->flags
&= ~MDOC_SMOFF
;
1574 if ( ! strcmp(nch
->string
, "off")) {
1575 mdoc
->flags
|= MDOC_SMOFF
;
1579 mandoc_vmsg(MANDOCERR_SM_BAD
,
1580 mdoc
->parse
, nch
->line
, nch
->pos
,
1581 "%s %s", roff_name
[mdoc
->last
->tok
], nch
->string
);
1582 mdoc_node_relink(mdoc
, nch
);
1587 post_root(POST_ARGS
)
1589 struct roff_node
*n
;
1591 /* Add missing prologue data. */
1593 if (mdoc
->meta
.date
== NULL
)
1594 mdoc
->meta
.date
= mdoc
->quick
?
1596 mandoc_normdate(mdoc
->parse
, NULL
, 0, 0);
1598 if (mdoc
->meta
.title
== NULL
) {
1599 mandoc_msg(MANDOCERR_DT_NOTITLE
,
1600 mdoc
->parse
, 0, 0, "EOF");
1601 mdoc
->meta
.title
= mandoc_strdup("UNTITLED");
1604 if (mdoc
->meta
.vol
== NULL
)
1605 mdoc
->meta
.vol
= mandoc_strdup("LOCAL");
1607 if (mdoc
->meta
.os
== NULL
) {
1608 mandoc_msg(MANDOCERR_OS_MISSING
,
1609 mdoc
->parse
, 0, 0, NULL
);
1610 mdoc
->meta
.os
= mandoc_strdup("");
1613 /* Check that we begin with a proper `Sh'. */
1615 n
= mdoc
->first
->child
;
1616 while (n
!= NULL
&& n
->tok
!= TOKEN_NONE
&&
1617 mdoc_macros
[n
->tok
].flags
& MDOC_PROLOGUE
)
1621 mandoc_msg(MANDOCERR_DOC_EMPTY
, mdoc
->parse
, 0, 0, NULL
);
1622 else if (n
->tok
!= MDOC_Sh
)
1623 mandoc_msg(MANDOCERR_SEC_BEFORE
, mdoc
->parse
,
1624 n
->line
, n
->pos
, roff_name
[n
->tok
]);
1630 struct roff_node
*np
, *nch
, *next
, *prev
;
1635 if (np
->type
!= ROFFT_BODY
)
1638 if (np
->child
== NULL
) {
1639 mandoc_msg(MANDOCERR_RS_EMPTY
, mdoc
->parse
,
1640 np
->line
, np
->pos
, "Rs");
1645 * The full `Rs' block needs special handling to order the
1646 * sub-elements according to `rsord'. Pick through each element
1647 * and correctly order it. This is an insertion sort.
1651 for (nch
= np
->child
->next
; nch
!= NULL
; nch
= next
) {
1652 /* Determine order number of this child. */
1653 for (i
= 0; i
< RSORD_MAX
; i
++)
1654 if (rsord
[i
] == nch
->tok
)
1657 if (i
== RSORD_MAX
) {
1658 mandoc_msg(MANDOCERR_RS_BAD
, mdoc
->parse
,
1659 nch
->line
, nch
->pos
, roff_name
[nch
->tok
]);
1661 } else if (nch
->tok
== MDOC__J
|| nch
->tok
== MDOC__B
)
1662 np
->norm
->Rs
.quote_T
++;
1665 * Remove this child from the chain. This somewhat
1666 * repeats roff_node_unlink(), but since we're
1667 * just re-ordering, there's no need for the
1668 * full unlink process.
1671 if ((next
= nch
->next
) != NULL
)
1672 next
->prev
= nch
->prev
;
1674 if ((prev
= nch
->prev
) != NULL
)
1675 prev
->next
= nch
->next
;
1677 nch
->prev
= nch
->next
= NULL
;
1680 * Scan back until we reach a node that's
1681 * to be ordered before this child.
1684 for ( ; prev
; prev
= prev
->prev
) {
1685 /* Determine order of `prev'. */
1686 for (j
= 0; j
< RSORD_MAX
; j
++)
1687 if (rsord
[j
] == prev
->tok
)
1697 * Set this child back into its correct place
1698 * in front of the `prev' node.
1704 np
->child
->prev
= nch
;
1705 nch
->next
= np
->child
;
1709 prev
->next
->prev
= nch
;
1710 nch
->next
= prev
->next
;
1717 * For some arguments of some macros,
1718 * convert all breakable hyphens into ASCII_HYPH.
1721 post_hyph(POST_ARGS
)
1723 struct roff_node
*nch
;
1726 for (nch
= mdoc
->last
->child
; nch
!= NULL
; nch
= nch
->next
) {
1727 if (nch
->type
!= ROFFT_TEXT
)
1732 while (*(++cp
) != '\0')
1734 isalpha((unsigned char)cp
[-1]) &&
1735 isalpha((unsigned char)cp
[1]))
1744 if (mdoc
->last
->flags
& NODE_LINE
)
1745 mandoc_msg(MANDOCERR_NS_SKIP
, mdoc
->parse
,
1746 mdoc
->last
->line
, mdoc
->last
->pos
, NULL
);
1755 switch (mdoc
->last
->type
) {
1760 switch (mdoc
->lastsec
) {
1765 post_sh_see_also(mdoc
);
1768 post_sh_authors(mdoc
);
1780 post_sh_name(POST_ARGS
)
1782 struct roff_node
*n
;
1787 for (n
= mdoc
->last
->child
; n
!= NULL
; n
= n
->next
) {
1790 if (hasnm
&& n
->child
!= NULL
)
1791 mandoc_vmsg(MANDOCERR_NAMESEC_PUNCT
,
1792 mdoc
->parse
, n
->line
, n
->pos
,
1793 "Nm %s", n
->child
->string
);
1798 if (n
->next
!= NULL
)
1799 mandoc_msg(MANDOCERR_NAMESEC_ND
,
1800 mdoc
->parse
, n
->line
, n
->pos
, NULL
);
1803 if (n
->type
== ROFFT_TEXT
&&
1804 n
->string
[0] == ',' && n
->string
[1] == '\0' &&
1805 n
->next
!= NULL
&& n
->next
->tok
== MDOC_Nm
) {
1811 mandoc_msg(MANDOCERR_NAMESEC_BAD
, mdoc
->parse
,
1812 n
->line
, n
->pos
, roff_name
[n
->tok
]);
1819 mandoc_msg(MANDOCERR_NAMESEC_NONM
, mdoc
->parse
,
1820 mdoc
->last
->line
, mdoc
->last
->pos
, NULL
);
1822 mandoc_msg(MANDOCERR_NAMESEC_NOND
, mdoc
->parse
,
1823 mdoc
->last
->line
, mdoc
->last
->pos
, NULL
);
1827 post_sh_see_also(POST_ARGS
)
1829 const struct roff_node
*n
;
1830 const char *name
, *sec
;
1831 const char *lastname
, *lastsec
, *lastpunct
;
1834 n
= mdoc
->last
->child
;
1835 lastname
= lastsec
= lastpunct
= NULL
;
1837 if (n
->tok
!= MDOC_Xr
||
1839 n
->child
->next
== NULL
)
1842 /* Process one .Xr node. */
1844 name
= n
->child
->string
;
1845 sec
= n
->child
->next
->string
;
1846 if (lastsec
!= NULL
) {
1847 if (lastpunct
[0] != ',' || lastpunct
[1] != '\0')
1848 mandoc_vmsg(MANDOCERR_XR_PUNCT
,
1849 mdoc
->parse
, n
->line
, n
->pos
,
1850 "%s before %s(%s)", lastpunct
,
1852 cmp
= strcmp(lastsec
, sec
);
1854 mandoc_vmsg(MANDOCERR_XR_ORDER
,
1855 mdoc
->parse
, n
->line
, n
->pos
,
1856 "%s(%s) after %s(%s)", name
,
1857 sec
, lastname
, lastsec
);
1858 else if (cmp
== 0 &&
1859 strcasecmp(lastname
, name
) > 0)
1860 mandoc_vmsg(MANDOCERR_XR_ORDER
,
1861 mdoc
->parse
, n
->line
, n
->pos
,
1862 "%s after %s", name
, lastname
);
1867 /* Process the following node. */
1872 if (n
->tok
== MDOC_Xr
) {
1876 if (n
->type
!= ROFFT_TEXT
)
1878 for (name
= n
->string
; *name
!= '\0'; name
++)
1879 if (isalpha((const unsigned char)*name
))
1881 lastpunct
= n
->string
;
1882 if (n
->next
== NULL
)
1883 mandoc_vmsg(MANDOCERR_XR_PUNCT
, mdoc
->parse
,
1884 n
->line
, n
->pos
, "%s after %s(%s)",
1885 lastpunct
, lastname
, lastsec
);
1891 child_an(const struct roff_node
*n
)
1894 for (n
= n
->child
; n
!= NULL
; n
= n
->next
)
1895 if ((n
->tok
== MDOC_An
&& n
->child
!= NULL
) || child_an(n
))
1901 post_sh_authors(POST_ARGS
)
1904 if ( ! child_an(mdoc
->last
))
1905 mandoc_msg(MANDOCERR_AN_MISSING
, mdoc
->parse
,
1906 mdoc
->last
->line
, mdoc
->last
->pos
, NULL
);
1910 post_sh_head(POST_ARGS
)
1912 struct roff_node
*nch
;
1913 const char *goodsec
;
1917 * Process a new section. Sections are either "named" or
1918 * "custom". Custom sections are user-defined, while named ones
1919 * follow a conventional order and may only appear in certain
1923 sec
= mdoc
->last
->sec
;
1925 /* The NAME should be first. */
1927 if (sec
!= SEC_NAME
&& mdoc
->lastnamed
== SEC_NONE
)
1928 mandoc_vmsg(MANDOCERR_NAMESEC_FIRST
, mdoc
->parse
,
1929 mdoc
->last
->line
, mdoc
->last
->pos
, "Sh %s",
1930 sec
!= SEC_CUSTOM
? secnames
[sec
] :
1931 (nch
= mdoc
->last
->child
) == NULL
? "" :
1932 nch
->type
== ROFFT_TEXT
? nch
->string
:
1933 roff_name
[nch
->tok
]);
1935 /* The SYNOPSIS gets special attention in other areas. */
1937 if (sec
== SEC_SYNOPSIS
) {
1938 roff_setreg(mdoc
->roff
, "nS", 1, '=');
1939 mdoc
->flags
|= MDOC_SYNOPSIS
;
1941 roff_setreg(mdoc
->roff
, "nS", 0, '=');
1942 mdoc
->flags
&= ~MDOC_SYNOPSIS
;
1945 /* Mark our last section. */
1947 mdoc
->lastsec
= sec
;
1949 /* We don't care about custom sections after this. */
1951 if (sec
== SEC_CUSTOM
)
1955 * Check whether our non-custom section is being repeated or is
1959 if (sec
== mdoc
->lastnamed
)
1960 mandoc_vmsg(MANDOCERR_SEC_REP
, mdoc
->parse
,
1961 mdoc
->last
->line
, mdoc
->last
->pos
,
1962 "Sh %s", secnames
[sec
]);
1964 if (sec
< mdoc
->lastnamed
)
1965 mandoc_vmsg(MANDOCERR_SEC_ORDER
, mdoc
->parse
,
1966 mdoc
->last
->line
, mdoc
->last
->pos
,
1967 "Sh %s", secnames
[sec
]);
1969 /* Mark the last named section. */
1971 mdoc
->lastnamed
= sec
;
1973 /* Check particular section/manual conventions. */
1975 if (mdoc
->meta
.msec
== NULL
)
1981 if (*mdoc
->meta
.msec
== '4')
1983 goodsec
= "2, 3, 4, 9";
1985 case SEC_RETURN_VALUES
:
1987 if (*mdoc
->meta
.msec
== '2')
1989 if (*mdoc
->meta
.msec
== '3')
1991 if (NULL
== goodsec
)
1992 goodsec
= "2, 3, 9";
1995 if (*mdoc
->meta
.msec
== '9')
1997 if (NULL
== goodsec
)
1999 mandoc_vmsg(MANDOCERR_SEC_MSEC
, mdoc
->parse
,
2000 mdoc
->last
->line
, mdoc
->last
->pos
,
2001 "Sh %s for %s only", secnames
[sec
], goodsec
);
2011 struct roff_node
*n
, *nch
;
2015 if (nch
->next
== NULL
) {
2016 mandoc_vmsg(MANDOCERR_XR_NOSEC
, mdoc
->parse
,
2017 n
->line
, n
->pos
, "Xr %s", nch
->string
);
2020 assert(nch
->next
== n
->last
);
2024 post_ignpar(POST_ARGS
)
2026 struct roff_node
*np
;
2028 switch (mdoc
->last
->type
) {
2041 if ((np
= mdoc
->last
->child
) != NULL
)
2042 if (np
->tok
== MDOC_Pp
|| np
->tok
== MDOC_Lp
) {
2043 mandoc_vmsg(MANDOCERR_PAR_SKIP
,
2044 mdoc
->parse
, np
->line
, np
->pos
,
2045 "%s after %s", roff_name
[np
->tok
],
2046 roff_name
[mdoc
->last
->tok
]);
2047 roff_node_delete(mdoc
, np
);
2050 if ((np
= mdoc
->last
->last
) != NULL
)
2051 if (np
->tok
== MDOC_Pp
|| np
->tok
== MDOC_Lp
) {
2052 mandoc_vmsg(MANDOCERR_PAR_SKIP
, mdoc
->parse
,
2053 np
->line
, np
->pos
, "%s at the end of %s",
2055 roff_name
[mdoc
->last
->tok
]);
2056 roff_node_delete(mdoc
, np
);
2061 post_prevpar(POST_ARGS
)
2063 struct roff_node
*n
;
2066 if (NULL
== n
->prev
)
2068 if (n
->type
!= ROFFT_ELEM
&& n
->type
!= ROFFT_BLOCK
)
2072 * Don't allow prior `Lp' or `Pp' prior to a paragraph-type
2073 * block: `Lp', `Pp', or non-compact `Bd' or `Bl'.
2076 if (n
->prev
->tok
!= MDOC_Pp
&&
2077 n
->prev
->tok
!= MDOC_Lp
&&
2078 n
->prev
->tok
!= ROFF_br
)
2080 if (n
->tok
== MDOC_Bl
&& n
->norm
->Bl
.comp
)
2082 if (n
->tok
== MDOC_Bd
&& n
->norm
->Bd
.comp
)
2084 if (n
->tok
== MDOC_It
&& n
->parent
->norm
->Bl
.comp
)
2087 mandoc_vmsg(MANDOCERR_PAR_SKIP
, mdoc
->parse
,
2088 n
->prev
->line
, n
->prev
->pos
, "%s before %s",
2089 roff_name
[n
->prev
->tok
], roff_name
[n
->tok
]);
2090 roff_node_delete(mdoc
, n
->prev
);
2096 struct roff_node
*np
;
2099 if (np
->tok
!= ROFF_br
&& np
->tok
!= MDOC_sp
)
2102 if (np
->tok
== MDOC_sp
) {
2103 if (np
->child
!= NULL
&& np
->child
->next
!= NULL
)
2104 mandoc_vmsg(MANDOCERR_ARG_EXCESS
, mdoc
->parse
,
2105 np
->child
->next
->line
, np
->child
->next
->pos
,
2106 "sp ... %s", np
->child
->next
->string
);
2107 } else if (np
->child
!= NULL
)
2108 mandoc_vmsg(MANDOCERR_ARG_SKIP
,
2109 mdoc
->parse
, np
->line
, np
->pos
, "%s %s",
2110 roff_name
[np
->tok
], np
->child
->string
);
2112 if ((np
= mdoc
->last
->prev
) == NULL
) {
2113 np
= mdoc
->last
->parent
;
2114 if (np
->tok
!= MDOC_Sh
&& np
->tok
!= MDOC_Ss
)
2116 } else if (np
->tok
!= MDOC_Pp
&& np
->tok
!= MDOC_Lp
&&
2117 (mdoc
->last
->tok
!= ROFF_br
||
2118 (np
->tok
!= MDOC_sp
&& np
->tok
!= ROFF_br
)))
2121 mandoc_vmsg(MANDOCERR_PAR_SKIP
, mdoc
->parse
,
2122 mdoc
->last
->line
, mdoc
->last
->pos
, "%s after %s",
2123 roff_name
[mdoc
->last
->tok
], roff_name
[np
->tok
]);
2124 roff_node_delete(mdoc
, mdoc
->last
);
2130 struct roff_node
*n
;
2134 n
->flags
|= NODE_NOPRT
;
2136 if (mdoc
->meta
.date
!= NULL
) {
2137 mandoc_msg(MANDOCERR_PROLOG_REP
, mdoc
->parse
,
2138 n
->line
, n
->pos
, "Dd");
2139 free(mdoc
->meta
.date
);
2140 } else if (mdoc
->flags
& MDOC_PBODY
)
2141 mandoc_msg(MANDOCERR_PROLOG_LATE
, mdoc
->parse
,
2142 n
->line
, n
->pos
, "Dd");
2143 else if (mdoc
->meta
.title
!= NULL
)
2144 mandoc_msg(MANDOCERR_PROLOG_ORDER
, mdoc
->parse
,
2145 n
->line
, n
->pos
, "Dd after Dt");
2146 else if (mdoc
->meta
.os
!= NULL
)
2147 mandoc_msg(MANDOCERR_PROLOG_ORDER
, mdoc
->parse
,
2148 n
->line
, n
->pos
, "Dd after Os");
2150 if (n
->child
== NULL
|| n
->child
->string
[0] == '\0') {
2151 mdoc
->meta
.date
= mdoc
->quick
? mandoc_strdup("") :
2152 mandoc_normdate(mdoc
->parse
, NULL
, n
->line
, n
->pos
);
2157 deroff(&datestr
, n
);
2159 mdoc
->meta
.date
= datestr
;
2161 mdoc
->meta
.date
= mandoc_normdate(mdoc
->parse
,
2162 datestr
, n
->line
, n
->pos
);
2170 struct roff_node
*nn
, *n
;
2175 n
->flags
|= NODE_NOPRT
;
2177 if (mdoc
->flags
& MDOC_PBODY
) {
2178 mandoc_msg(MANDOCERR_DT_LATE
, mdoc
->parse
,
2179 n
->line
, n
->pos
, "Dt");
2183 if (mdoc
->meta
.title
!= NULL
)
2184 mandoc_msg(MANDOCERR_PROLOG_REP
, mdoc
->parse
,
2185 n
->line
, n
->pos
, "Dt");
2186 else if (mdoc
->meta
.os
!= NULL
)
2187 mandoc_msg(MANDOCERR_PROLOG_ORDER
, mdoc
->parse
,
2188 n
->line
, n
->pos
, "Dt after Os");
2190 free(mdoc
->meta
.title
);
2191 free(mdoc
->meta
.msec
);
2192 free(mdoc
->meta
.vol
);
2193 free(mdoc
->meta
.arch
);
2195 mdoc
->meta
.title
= NULL
;
2196 mdoc
->meta
.msec
= NULL
;
2197 mdoc
->meta
.vol
= NULL
;
2198 mdoc
->meta
.arch
= NULL
;
2200 /* Mandatory first argument: title. */
2203 if (nn
== NULL
|| *nn
->string
== '\0') {
2204 mandoc_msg(MANDOCERR_DT_NOTITLE
,
2205 mdoc
->parse
, n
->line
, n
->pos
, "Dt");
2206 mdoc
->meta
.title
= mandoc_strdup("UNTITLED");
2208 mdoc
->meta
.title
= mandoc_strdup(nn
->string
);
2210 /* Check that all characters are uppercase. */
2212 for (p
= nn
->string
; *p
!= '\0'; p
++)
2213 if (islower((unsigned char)*p
)) {
2214 mandoc_vmsg(MANDOCERR_TITLE_CASE
,
2215 mdoc
->parse
, nn
->line
,
2216 nn
->pos
+ (p
- nn
->string
),
2217 "Dt %s", nn
->string
);
2222 /* Mandatory second argument: section. */
2228 mandoc_vmsg(MANDOCERR_MSEC_MISSING
,
2229 mdoc
->parse
, n
->line
, n
->pos
,
2230 "Dt %s", mdoc
->meta
.title
);
2231 mdoc
->meta
.vol
= mandoc_strdup("LOCAL");
2232 return; /* msec and arch remain NULL. */
2235 mdoc
->meta
.msec
= mandoc_strdup(nn
->string
);
2237 /* Infer volume title from section number. */
2239 cp
= mandoc_a2msec(nn
->string
);
2241 mandoc_vmsg(MANDOCERR_MSEC_BAD
, mdoc
->parse
,
2242 nn
->line
, nn
->pos
, "Dt ... %s", nn
->string
);
2243 mdoc
->meta
.vol
= mandoc_strdup(nn
->string
);
2245 mdoc
->meta
.vol
= mandoc_strdup(cp
);
2247 /* Optional third argument: architecture. */
2249 if ((nn
= nn
->next
) == NULL
)
2252 for (p
= nn
->string
; *p
!= '\0'; p
++)
2253 *p
= tolower((unsigned char)*p
);
2254 mdoc
->meta
.arch
= mandoc_strdup(nn
->string
);
2256 /* Ignore fourth and later arguments. */
2258 if ((nn
= nn
->next
) != NULL
)
2259 mandoc_vmsg(MANDOCERR_ARG_EXCESS
, mdoc
->parse
,
2260 nn
->line
, nn
->pos
, "Dt ... %s", nn
->string
);
2266 struct roff_node
*n
, *nch
;
2274 mdoc
->next
= ROFF_NEXT_SIBLING
;
2275 roff_elem_alloc(mdoc
, n
->line
, n
->pos
, MDOC_Ns
);
2276 mdoc
->last
->flags
|= NODE_NOSRC
;
2277 mdoc
->next
= ROFF_NEXT_SIBLING
;
2279 mdoc
->next
= ROFF_NEXT_CHILD
;
2280 roff_word_alloc(mdoc
, n
->line
, n
->pos
, "BSD");
2281 mdoc
->last
->flags
|= NODE_NOSRC
;
2288 roff_elem_alloc(mdoc
, n
->line
, n
->pos
, MDOC_Ns
);
2289 mdoc
->last
->flags
|= NODE_NOSRC
;
2290 mdoc
->next
= ROFF_NEXT_SIBLING
;
2291 roff_word_alloc(mdoc
, n
->line
, n
->pos
, "-");
2292 mdoc
->last
->flags
|= NODE_NOSRC
;
2293 roff_elem_alloc(mdoc
, n
->line
, n
->pos
, MDOC_Ns
);
2294 mdoc
->last
->flags
|= NODE_NOSRC
;
2298 * Make `Bx's second argument always start with an uppercase
2299 * letter. Groff checks if it's an "accepted" term, but we just
2300 * uppercase blindly.
2303 *nch
->string
= (char)toupper((unsigned char)*nch
->string
);
2310 struct utsname utsname
;
2311 static char *defbuf
;
2313 struct roff_node
*n
;
2316 n
->flags
|= NODE_NOPRT
;
2318 if (mdoc
->meta
.os
!= NULL
)
2319 mandoc_msg(MANDOCERR_PROLOG_REP
, mdoc
->parse
,
2320 n
->line
, n
->pos
, "Os");
2321 else if (mdoc
->flags
& MDOC_PBODY
)
2322 mandoc_msg(MANDOCERR_PROLOG_LATE
, mdoc
->parse
,
2323 n
->line
, n
->pos
, "Os");
2326 * Set the operating system by way of the `Os' macro.
2327 * The order of precedence is:
2328 * 1. the argument of the `Os' macro, unless empty
2329 * 2. the -Ios=foo command line argument, if provided
2330 * 3. -DOSNAME="\"foo\"", if provided during compilation
2331 * 4. "sysname release" from uname(3)
2334 free(mdoc
->meta
.os
);
2335 mdoc
->meta
.os
= NULL
;
2336 deroff(&mdoc
->meta
.os
, n
);
2341 mdoc
->meta
.os
= mandoc_strdup(mdoc
->defos
);
2346 mdoc
->meta
.os
= mandoc_strdup(OSNAME
);
2348 if (defbuf
== NULL
) {
2349 if (uname(&utsname
) == -1) {
2350 mandoc_msg(MANDOCERR_OS_UNAME
, mdoc
->parse
,
2351 n
->line
, n
->pos
, "Os");
2352 defbuf
= mandoc_strdup("UNKNOWN");
2354 mandoc_asprintf(&defbuf
, "%s %s",
2355 utsname
.sysname
, utsname
.release
);
2357 mdoc
->meta
.os
= mandoc_strdup(defbuf
);
2362 mdoc_a2sec(const char *p
)
2366 for (i
= 0; i
< (int)SEC__MAX
; i
++)
2367 if (secnames
[i
] && 0 == strcmp(p
, secnames
[i
]))
2368 return (enum roff_sec
)i
;
2374 macro2len(enum roff_tok macro
)