]>
git.cameronkatri.com Git - mandoc.git/blob - mdoc_validate.c
1 /* $Id: mdoc_validate.c,v 1.336 2017/06/11 17:16:51 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 void check_toptext(struct roff_man
*, int, int, const char *);
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_delim(POST_ARGS
);
79 static void post_dt(POST_ARGS
);
80 static void post_en(POST_ARGS
);
81 static void post_es(POST_ARGS
);
82 static void post_eoln(POST_ARGS
);
83 static void post_ex(POST_ARGS
);
84 static void post_fa(POST_ARGS
);
85 static void post_fn(POST_ARGS
);
86 static void post_fname(POST_ARGS
);
87 static void post_fo(POST_ARGS
);
88 static void post_hyph(POST_ARGS
);
89 static void post_ignpar(POST_ARGS
);
90 static void post_it(POST_ARGS
);
91 static void post_lb(POST_ARGS
);
92 static void post_nd(POST_ARGS
);
93 static void post_nm(POST_ARGS
);
94 static void post_ns(POST_ARGS
);
95 static void post_obsolete(POST_ARGS
);
96 static void post_os(POST_ARGS
);
97 static void post_par(POST_ARGS
);
98 static void post_prevpar(POST_ARGS
);
99 static void post_root(POST_ARGS
);
100 static void post_rs(POST_ARGS
);
101 static void post_rv(POST_ARGS
);
102 static void post_sh(POST_ARGS
);
103 static void post_sh_head(POST_ARGS
);
104 static void post_sh_name(POST_ARGS
);
105 static void post_sh_see_also(POST_ARGS
);
106 static void post_sh_authors(POST_ARGS
);
107 static void post_sm(POST_ARGS
);
108 static void post_st(POST_ARGS
);
109 static void post_std(POST_ARGS
);
110 static void post_useless(POST_ARGS
);
111 static void post_xr(POST_ARGS
);
112 static void post_xx(POST_ARGS
);
114 static const v_post __mdoc_valids
[MDOC_MAX
- MDOC_Dd
] = {
119 post_ignpar
, /* Ss */
121 post_display
, /* D1 */
122 post_display
, /* Dl */
123 post_display
, /* Bd */
131 post_defaults
, /* Ar */
145 post_defaults
, /* Li */
149 post_obsolete
, /* Ot */
150 post_defaults
, /* Pa */
157 post_hyph
, /* %B */ /* FIXME: can be used outside Rs/Re. */
165 post_hyph
, /* %T */ /* FIXME: can be used outside Rs/Re. */
177 post_obsolete
, /* Db */
207 post_useless
, /* Tn */
218 post_obsolete
, /* Hf */
219 post_obsolete
, /* Fr */
224 post_defaults
, /* Mt */
225 post_delim
, /* Brq */
226 post_delim
, /* Bro */
236 static const v_post
*const mdoc_valids
= __mdoc_valids
- MDOC_Dd
;
238 #define RSORD_MAX 14 /* Number of `Rs' blocks. */
240 static const enum roff_tok rsord
[RSORD_MAX
] = {
257 static const char * const secnames
[SEC__MAX
] = {
264 "IMPLEMENTATION NOTES",
279 "SECURITY CONSIDERATIONS",
285 mdoc_node_validate(struct roff_man
*mdoc
)
291 mdoc
->last
= mdoc
->last
->child
;
292 while (mdoc
->last
!= NULL
) {
293 mdoc_node_validate(mdoc
);
295 mdoc
->last
= mdoc
->last
->child
;
297 mdoc
->last
= mdoc
->last
->next
;
301 mdoc
->next
= ROFF_NEXT_SIBLING
;
304 if (n
->sec
!= SEC_SYNOPSIS
||
305 (n
->parent
->tok
!= MDOC_Cd
&& n
->parent
->tok
!= MDOC_Fd
))
306 check_text(mdoc
, n
->line
, n
->pos
, n
->string
);
307 if (n
->parent
->tok
== MDOC_It
||
308 (n
->parent
->type
== ROFFT_BODY
&&
309 (n
->parent
->tok
== MDOC_Sh
||
310 n
->parent
->tok
== MDOC_Ss
)))
311 check_toptext(mdoc
, n
->line
, n
->pos
, n
->string
);
320 check_args(mdoc
, mdoc
->last
);
323 * Closing delimiters are not special at the
324 * beginning of a block, opening delimiters
325 * are not special at the end.
328 if (n
->child
!= NULL
)
329 n
->child
->flags
&= ~NODE_DELIMC
;
331 n
->last
->flags
&= ~NODE_DELIMO
;
333 /* Call the macro's postprocessor. */
335 if (n
->tok
< ROFF_MAX
) {
348 assert(n
->tok
>= MDOC_Dd
&& n
->tok
< MDOC_MAX
);
349 p
= mdoc_valids
+ n
->tok
;
359 check_args(struct roff_man
*mdoc
, struct roff_node
*n
)
366 assert(n
->args
->argc
);
367 for (i
= 0; i
< (int)n
->args
->argc
; i
++)
368 check_argv(mdoc
, n
, &n
->args
->argv
[i
]);
372 check_argv(struct roff_man
*mdoc
, struct roff_node
*n
, struct mdoc_argv
*v
)
376 for (i
= 0; i
< (int)v
->sz
; i
++)
377 check_text(mdoc
, v
->line
, v
->pos
, v
->value
[i
]);
381 check_text(struct roff_man
*mdoc
, int ln
, int pos
, char *p
)
385 if (MDOC_LITERAL
& mdoc
->flags
)
388 for (cp
= p
; NULL
!= (p
= strchr(p
, '\t')); p
++)
389 mandoc_msg(MANDOCERR_FI_TAB
, mdoc
->parse
,
390 ln
, pos
+ (int)(p
- cp
), NULL
);
394 check_toptext(struct roff_man
*mdoc
, int ln
, int pos
, const char *p
)
396 const char *cp
, *cpr
;
401 if ((cp
= strstr(p
, "OpenBSD")) != NULL
)
402 mandoc_msg(MANDOCERR_BX
, mdoc
->parse
,
403 ln
, pos
+ (cp
- p
), "Ox");
404 if ((cp
= strstr(p
, "NetBSD")) != NULL
)
405 mandoc_msg(MANDOCERR_BX
, mdoc
->parse
,
406 ln
, pos
+ (cp
- p
), "Nx");
407 if ((cp
= strstr(p
, "FreeBSD")) != NULL
)
408 mandoc_msg(MANDOCERR_BX
, mdoc
->parse
,
409 ln
, pos
+ (cp
- p
), "Fx");
410 if ((cp
= strstr(p
, "DragonFly")) != NULL
)
411 mandoc_msg(MANDOCERR_BX
, mdoc
->parse
,
412 ln
, pos
+ (cp
- p
), "Dx");
415 while ((cp
= strstr(cp
+ 1, "()")) != NULL
) {
416 for (cpr
= cp
- 1; cpr
>= p
; cpr
--)
417 if (*cpr
!= '_' && !isalnum((unsigned char)*cpr
))
419 if ((cpr
< p
|| *cpr
== ' ') && cpr
+ 1 < cp
) {
421 mandoc_vmsg(MANDOCERR_FUNC
, mdoc
->parse
,
423 "%.*s()", (int)(cp
- cpr
), cpr
);
429 post_delim(POST_ARGS
)
431 const struct roff_node
*nch
;
438 * Find candidates: at least two bytes,
439 * the last one a closing or middle delimiter.
442 tok
= mdoc
->last
->tok
;
443 nch
= mdoc
->last
->last
;
444 if (nch
== NULL
|| nch
->type
!= ROFFT_TEXT
)
446 lc
= strchr(nch
->string
, '\0') - 1;
447 if (lc
<= nch
->string
)
449 delim
= mdoc_isdelim(lc
);
450 if (delim
== DELIM_NONE
|| delim
== DELIM_OPEN
)
454 * Reduce false positives by allowing various cases.
457 /* Escaped delimiters. */
458 if (lc
> nch
->string
+ 1 && lc
[-2] == '\\' &&
459 (lc
[-1] == '&' || lc
[-1] == 'e'))
462 /* Specific byte sequences. */
465 for (cp
= lc
; cp
>= nch
->string
; cp
--)
470 if (lc
> nch
->string
+ 1 && lc
[-2] == '.' && lc
[-1] == '.')
484 for (cp
= lc
; cp
>= nch
->string
; cp
--)
489 if (lc
== nch
->string
+ 1 && lc
[-1] == '|')
495 /* Exactly two non-alphanumeric bytes. */
496 if (lc
== nch
->string
+ 1 && !isalnum((unsigned char)lc
[-1]))
499 /* At least three alphabetic words with a sentence ending. */
500 if (strchr("!.:?", *lc
) != NULL
&& (tok
== MDOC_Em
||
501 tok
== MDOC_Li
|| tok
== MDOC_Po
|| tok
== MDOC_Pq
||
504 for (cp
= lc
- 1; cp
>= nch
->string
; cp
--) {
507 if (cp
> nch
->string
&& cp
[-1] == ',')
509 } else if (isalpha((unsigned int)*cp
)) {
517 mandoc_vmsg(MANDOCERR_DELIM
, mdoc
->parse
,
518 nch
->line
, nch
->pos
+ (lc
- nch
->string
),
519 "%s%s %s", roff_name
[tok
],
520 nch
== mdoc
->last
->child
? "" : " ...", nch
->string
);
524 post_bl_norm(POST_ARGS
)
527 struct mdoc_argv
*argv
, *wa
;
529 enum mdocargt mdoclt
;
532 n
= mdoc
->last
->parent
;
533 n
->norm
->Bl
.type
= LIST__NONE
;
536 * First figure out which kind of list to use: bind ourselves to
537 * the first mentioned list type and warn about any remaining
538 * ones. If we find no list type, we default to LIST_item.
541 wa
= (n
->args
== NULL
) ? NULL
: n
->args
->argv
;
542 mdoclt
= MDOC_ARG_MAX
;
543 for (i
= 0; n
->args
&& i
< (int)n
->args
->argc
; i
++) {
544 argv
= n
->args
->argv
+ i
;
547 /* Set list types. */
581 /* Set list arguments. */
583 if (n
->norm
->Bl
.comp
)
584 mandoc_msg(MANDOCERR_ARG_REP
,
585 mdoc
->parse
, argv
->line
,
586 argv
->pos
, "Bl -compact");
587 n
->norm
->Bl
.comp
= 1;
592 mandoc_msg(MANDOCERR_ARG_EMPTY
,
593 mdoc
->parse
, argv
->line
,
594 argv
->pos
, "Bl -width");
595 n
->norm
->Bl
.width
= "0n";
598 if (NULL
!= n
->norm
->Bl
.width
)
599 mandoc_vmsg(MANDOCERR_ARG_REP
,
600 mdoc
->parse
, argv
->line
,
601 argv
->pos
, "Bl -width %s",
603 rewrite_macro2len(mdoc
, argv
->value
);
604 n
->norm
->Bl
.width
= argv
->value
[0];
608 mandoc_msg(MANDOCERR_ARG_EMPTY
,
609 mdoc
->parse
, argv
->line
,
610 argv
->pos
, "Bl -offset");
613 if (NULL
!= n
->norm
->Bl
.offs
)
614 mandoc_vmsg(MANDOCERR_ARG_REP
,
615 mdoc
->parse
, argv
->line
,
616 argv
->pos
, "Bl -offset %s",
618 rewrite_macro2len(mdoc
, argv
->value
);
619 n
->norm
->Bl
.offs
= argv
->value
[0];
624 if (LIST__NONE
== lt
)
628 /* Check: multiple list types. */
630 if (LIST__NONE
!= n
->norm
->Bl
.type
) {
631 mandoc_vmsg(MANDOCERR_BL_REP
,
632 mdoc
->parse
, n
->line
, n
->pos
,
633 "Bl -%s", mdoc_argnames
[argv
->arg
]);
637 /* The list type should come first. */
639 if (n
->norm
->Bl
.width
||
642 mandoc_vmsg(MANDOCERR_BL_LATETYPE
,
643 mdoc
->parse
, n
->line
, n
->pos
, "Bl -%s",
644 mdoc_argnames
[n
->args
->argv
[0].arg
]);
646 n
->norm
->Bl
.type
= lt
;
647 if (LIST_column
== lt
) {
648 n
->norm
->Bl
.ncols
= argv
->sz
;
649 n
->norm
->Bl
.cols
= (void *)argv
->value
;
653 /* Allow lists to default to LIST_item. */
655 if (LIST__NONE
== n
->norm
->Bl
.type
) {
656 mandoc_msg(MANDOCERR_BL_NOTYPE
, mdoc
->parse
,
657 n
->line
, n
->pos
, "Bl");
658 n
->norm
->Bl
.type
= LIST_item
;
663 * Validate the width field. Some list types don't need width
664 * types and should be warned about them. Others should have it
665 * and must also be warned. Yet others have a default and need
669 switch (n
->norm
->Bl
.type
) {
671 if (NULL
== n
->norm
->Bl
.width
)
672 mandoc_msg(MANDOCERR_BL_NOWIDTH
, mdoc
->parse
,
673 n
->line
, n
->pos
, "Bl -tag");
680 if (n
->norm
->Bl
.width
)
681 mandoc_vmsg(MANDOCERR_BL_SKIPW
, mdoc
->parse
,
682 wa
->line
, wa
->pos
, "Bl -%s",
683 mdoc_argnames
[mdoclt
]);
688 if (NULL
== n
->norm
->Bl
.width
)
689 n
->norm
->Bl
.width
= "2n";
692 if (NULL
== n
->norm
->Bl
.width
)
693 n
->norm
->Bl
.width
= "3n";
704 struct mdoc_argv
*argv
;
709 for (i
= 0; n
->args
&& i
< (int)n
->args
->argc
; i
++) {
710 argv
= n
->args
->argv
+ i
;
730 mandoc_msg(MANDOCERR_BD_FILE
, mdoc
->parse
,
731 n
->line
, n
->pos
, NULL
);
735 mandoc_msg(MANDOCERR_ARG_EMPTY
,
736 mdoc
->parse
, argv
->line
,
737 argv
->pos
, "Bd -offset");
740 if (NULL
!= n
->norm
->Bd
.offs
)
741 mandoc_vmsg(MANDOCERR_ARG_REP
,
742 mdoc
->parse
, argv
->line
,
743 argv
->pos
, "Bd -offset %s",
745 rewrite_macro2len(mdoc
, argv
->value
);
746 n
->norm
->Bd
.offs
= argv
->value
[0];
749 if (n
->norm
->Bd
.comp
)
750 mandoc_msg(MANDOCERR_ARG_REP
,
751 mdoc
->parse
, argv
->line
,
752 argv
->pos
, "Bd -compact");
753 n
->norm
->Bd
.comp
= 1;
758 if (DISP__NONE
== dt
)
761 if (DISP__NONE
== n
->norm
->Bd
.type
)
762 n
->norm
->Bd
.type
= dt
;
764 mandoc_vmsg(MANDOCERR_BD_REP
,
765 mdoc
->parse
, n
->line
, n
->pos
,
766 "Bd -%s", mdoc_argnames
[argv
->arg
]);
769 if (DISP__NONE
== n
->norm
->Bd
.type
) {
770 mandoc_msg(MANDOCERR_BD_NOTYPE
, mdoc
->parse
,
771 n
->line
, n
->pos
, "Bd");
772 n
->norm
->Bd
.type
= DISP_ragged
;
777 * Stand-alone line macros.
781 post_an_norm(POST_ARGS
)
784 struct mdoc_argv
*argv
;
791 for (i
= 1; i
< n
->args
->argc
; i
++) {
792 argv
= n
->args
->argv
+ i
;
793 mandoc_vmsg(MANDOCERR_AN_REP
,
794 mdoc
->parse
, argv
->line
, argv
->pos
,
795 "An -%s", mdoc_argnames
[argv
->arg
]);
798 argv
= n
->args
->argv
;
799 if (argv
->arg
== MDOC_Split
)
800 n
->norm
->An
.auth
= AUTH_split
;
801 else if (argv
->arg
== MDOC_Nosplit
)
802 n
->norm
->An
.auth
= AUTH_nosplit
;
814 if (n
->child
!= NULL
)
815 mandoc_vmsg(MANDOCERR_ARG_SKIP
, mdoc
->parse
, n
->line
,
816 n
->pos
, "%s %s", roff_name
[n
->tok
], n
->child
->string
);
818 while (n
->child
!= NULL
)
819 roff_node_delete(mdoc
, n
->child
);
821 roff_word_alloc(mdoc
, n
->line
, n
->pos
, n
->tok
== MDOC_Bt
?
822 "is currently in beta test." : "currently under development.");
823 mdoc
->last
->flags
|= NODE_EOS
| NODE_NOSRC
;
828 build_list(struct roff_man
*mdoc
, int tok
)
833 n
= mdoc
->last
->next
;
834 for (ic
= 1;; ic
++) {
835 roff_elem_alloc(mdoc
, n
->line
, n
->pos
, tok
);
836 mdoc
->last
->flags
|= NODE_NOSRC
;
837 mdoc_node_relink(mdoc
, n
);
838 n
= mdoc
->last
= mdoc
->last
->parent
;
839 mdoc
->next
= ROFF_NEXT_SIBLING
;
842 if (ic
> 1 || n
->next
->next
!= NULL
) {
843 roff_word_alloc(mdoc
, n
->line
, n
->pos
, ",");
844 mdoc
->last
->flags
|= NODE_DELIMC
| NODE_NOSRC
;
846 n
= mdoc
->last
->next
;
847 if (n
->next
== NULL
) {
848 roff_word_alloc(mdoc
, n
->line
, n
->pos
, "and");
849 mdoc
->last
->flags
|= NODE_NOSRC
;
863 mdoc
->next
= ROFF_NEXT_CHILD
;
864 roff_word_alloc(mdoc
, n
->line
, n
->pos
, "The");
865 mdoc
->last
->flags
|= NODE_NOSRC
;
867 if (mdoc
->last
->next
!= NULL
)
868 ic
= build_list(mdoc
, MDOC_Nm
);
869 else if (mdoc
->meta
.name
!= NULL
) {
870 roff_elem_alloc(mdoc
, n
->line
, n
->pos
, MDOC_Nm
);
871 mdoc
->last
->flags
|= NODE_NOSRC
;
872 roff_word_alloc(mdoc
, n
->line
, n
->pos
, mdoc
->meta
.name
);
873 mdoc
->last
->flags
|= NODE_NOSRC
;
874 mdoc
->last
= mdoc
->last
->parent
;
875 mdoc
->next
= ROFF_NEXT_SIBLING
;
878 mandoc_msg(MANDOCERR_EX_NONAME
, mdoc
->parse
,
879 n
->line
, n
->pos
, "Ex");
883 roff_word_alloc(mdoc
, n
->line
, n
->pos
,
884 ic
> 1 ? "utilities exit\\~0" : "utility exits\\~0");
885 mdoc
->last
->flags
|= NODE_NOSRC
;
886 roff_word_alloc(mdoc
, n
->line
, n
->pos
,
887 "on success, and\\~>0 if an error occurs.");
888 mdoc
->last
->flags
|= NODE_EOS
| NODE_NOSRC
;
901 assert(n
->child
->type
== ROFFT_TEXT
);
902 mdoc
->next
= ROFF_NEXT_CHILD
;
904 if ((p
= mdoc_a2lib(n
->child
->string
)) != NULL
) {
905 n
->child
->flags
|= NODE_NOPRT
;
906 roff_word_alloc(mdoc
, n
->line
, n
->pos
, p
);
907 mdoc
->last
->flags
= NODE_NOSRC
;
912 mandoc_vmsg(MANDOCERR_LB_BAD
, mdoc
->parse
, n
->child
->line
,
913 n
->child
->pos
, "Lb %s", n
->child
->string
);
915 roff_word_alloc(mdoc
, n
->line
, n
->pos
, "library");
916 mdoc
->last
->flags
= NODE_NOSRC
;
917 roff_word_alloc(mdoc
, n
->line
, n
->pos
, "\\(Lq");
918 mdoc
->last
->flags
= NODE_DELIMO
| NODE_NOSRC
;
919 mdoc
->last
= mdoc
->last
->next
;
920 roff_word_alloc(mdoc
, n
->line
, n
->pos
, "\\(Rq");
921 mdoc
->last
->flags
= NODE_DELIMC
| NODE_NOSRC
;
934 mdoc
->next
= ROFF_NEXT_CHILD
;
935 if (n
->child
!= NULL
) {
936 roff_word_alloc(mdoc
, n
->line
, n
->pos
, "The");
937 mdoc
->last
->flags
|= NODE_NOSRC
;
938 ic
= build_list(mdoc
, MDOC_Fn
);
939 roff_word_alloc(mdoc
, n
->line
, n
->pos
,
940 ic
> 1 ? "functions return" : "function returns");
941 mdoc
->last
->flags
|= NODE_NOSRC
;
942 roff_word_alloc(mdoc
, n
->line
, n
->pos
,
943 "the value\\~0 if successful;");
945 roff_word_alloc(mdoc
, n
->line
, n
->pos
, "Upon successful "
946 "completion, the value\\~0 is returned;");
947 mdoc
->last
->flags
|= NODE_NOSRC
;
949 roff_word_alloc(mdoc
, n
->line
, n
->pos
, "otherwise "
950 "the value\\~\\-1 is returned and the global variable");
951 mdoc
->last
->flags
|= NODE_NOSRC
;
952 roff_elem_alloc(mdoc
, n
->line
, n
->pos
, MDOC_Va
);
953 mdoc
->last
->flags
|= NODE_NOSRC
;
954 roff_word_alloc(mdoc
, n
->line
, n
->pos
, "errno");
955 mdoc
->last
->flags
|= NODE_NOSRC
;
956 mdoc
->last
= mdoc
->last
->parent
;
957 mdoc
->next
= ROFF_NEXT_SIBLING
;
958 roff_word_alloc(mdoc
, n
->line
, n
->pos
,
959 "is set to indicate the error.");
960 mdoc
->last
->flags
|= NODE_EOS
| NODE_NOSRC
;
970 if (n
->args
&& n
->args
->argc
== 1)
971 if (n
->args
->argv
[0].arg
== MDOC_Std
)
974 mandoc_msg(MANDOCERR_ARG_STD
, mdoc
->parse
,
975 n
->line
, n
->pos
, roff_name
[n
->tok
]);
981 struct roff_node
*n
, *nch
;
986 assert(nch
->type
== ROFFT_TEXT
);
988 if ((p
= mdoc_a2st(nch
->string
)) == NULL
) {
989 mandoc_vmsg(MANDOCERR_ST_BAD
, mdoc
->parse
,
990 nch
->line
, nch
->pos
, "St %s", nch
->string
);
991 roff_node_delete(mdoc
, n
);
995 nch
->flags
|= NODE_NOPRT
;
996 mdoc
->next
= ROFF_NEXT_CHILD
;
997 roff_word_alloc(mdoc
, nch
->line
, nch
->pos
, p
);
998 mdoc
->last
->flags
|= NODE_NOSRC
;
1003 post_obsolete(POST_ARGS
)
1005 struct roff_node
*n
;
1008 if (n
->type
== ROFFT_ELEM
|| n
->type
== ROFFT_BLOCK
)
1009 mandoc_msg(MANDOCERR_MACRO_OBS
, mdoc
->parse
,
1010 n
->line
, n
->pos
, roff_name
[n
->tok
]);
1014 post_useless(POST_ARGS
)
1016 struct roff_node
*n
;
1019 mandoc_msg(MANDOCERR_MACRO_USELESS
, mdoc
->parse
,
1020 n
->line
, n
->pos
, roff_name
[n
->tok
]);
1030 struct roff_node
*np
, *nch
;
1033 * Unlike other data pointers, these are "housed" by the HEAD
1034 * element, which contains the goods.
1038 if (np
->type
!= ROFFT_HEAD
)
1041 assert(np
->parent
->type
== ROFFT_BLOCK
);
1042 assert(np
->parent
->tok
== MDOC_Bf
);
1044 /* Check the number of arguments. */
1047 if (np
->parent
->args
== NULL
) {
1049 mandoc_msg(MANDOCERR_BF_NOFONT
, mdoc
->parse
,
1050 np
->line
, np
->pos
, "Bf");
1056 mandoc_vmsg(MANDOCERR_ARG_EXCESS
, mdoc
->parse
,
1057 nch
->line
, nch
->pos
, "Bf ... %s", nch
->string
);
1059 /* Extract argument into data. */
1061 if (np
->parent
->args
!= NULL
) {
1062 switch (np
->parent
->args
->argv
[0].arg
) {
1064 np
->norm
->Bf
.font
= FONT_Em
;
1067 np
->norm
->Bf
.font
= FONT_Li
;
1070 np
->norm
->Bf
.font
= FONT_Sy
;
1078 /* Extract parameter into data. */
1080 if ( ! strcmp(np
->child
->string
, "Em"))
1081 np
->norm
->Bf
.font
= FONT_Em
;
1082 else if ( ! strcmp(np
->child
->string
, "Li"))
1083 np
->norm
->Bf
.font
= FONT_Li
;
1084 else if ( ! strcmp(np
->child
->string
, "Sy"))
1085 np
->norm
->Bf
.font
= FONT_Sy
;
1087 mandoc_vmsg(MANDOCERR_BF_BADFONT
, mdoc
->parse
,
1088 np
->child
->line
, np
->child
->pos
,
1089 "Bf %s", np
->child
->string
);
1093 post_fname(POST_ARGS
)
1095 const struct roff_node
*n
;
1099 n
= mdoc
->last
->child
;
1100 pos
= strcspn(n
->string
, "()");
1101 cp
= n
->string
+ pos
;
1102 if ( ! (cp
[0] == '\0' || (cp
[0] == '(' && cp
[1] == '*')))
1103 mandoc_msg(MANDOCERR_FN_PAREN
, mdoc
->parse
,
1104 n
->line
, n
->pos
+ pos
, n
->string
);
1118 const struct roff_node
*n
;
1122 if (n
->type
!= ROFFT_HEAD
)
1125 if (n
->child
== NULL
) {
1126 mandoc_msg(MANDOCERR_FO_NOHEAD
, mdoc
->parse
,
1127 n
->line
, n
->pos
, "Fo");
1130 if (n
->child
!= n
->last
) {
1131 mandoc_vmsg(MANDOCERR_ARG_EXCESS
, mdoc
->parse
,
1132 n
->child
->next
->line
, n
->child
->next
->pos
,
1133 "Fo ... %s", n
->child
->next
->string
);
1134 while (n
->child
!= n
->last
)
1135 roff_node_delete(mdoc
, n
->last
);
1144 const struct roff_node
*n
;
1147 for (n
= mdoc
->last
->child
; n
!= NULL
; n
= n
->next
) {
1148 for (cp
= n
->string
; *cp
!= '\0'; cp
++) {
1149 /* Ignore callbacks and alterations. */
1150 if (*cp
== '(' || *cp
== '{')
1154 mandoc_msg(MANDOCERR_FA_COMMA
, mdoc
->parse
,
1155 n
->line
, n
->pos
+ (cp
- n
->string
),
1166 struct roff_node
*n
;
1170 if (n
->last
!= NULL
&&
1171 (n
->last
->tok
== MDOC_Pp
||
1172 n
->last
->tok
== MDOC_Lp
))
1173 mdoc_node_relink(mdoc
, n
->last
);
1175 if (mdoc
->meta
.name
== NULL
)
1176 deroff(&mdoc
->meta
.name
, n
);
1178 if (mdoc
->meta
.name
== NULL
||
1179 (mdoc
->lastsec
== SEC_NAME
&& n
->child
== NULL
))
1180 mandoc_msg(MANDOCERR_NM_NONAME
, mdoc
->parse
,
1181 n
->line
, n
->pos
, "Nm");
1183 if (n
->type
== ROFFT_ELEM
)
1186 if ((n
->type
!= ROFFT_ELEM
&& n
->type
!= ROFFT_HEAD
) ||
1187 (n
->child
!= NULL
&& n
->child
->type
== ROFFT_TEXT
) ||
1188 mdoc
->meta
.name
== NULL
)
1191 mdoc
->next
= ROFF_NEXT_CHILD
;
1192 roff_word_alloc(mdoc
, n
->line
, n
->pos
, mdoc
->meta
.name
);
1193 mdoc
->last
->flags
|= NODE_NOSRC
;
1200 struct roff_node
*n
;
1205 if (n
->type
!= ROFFT_BODY
)
1208 if (n
->sec
!= SEC_NAME
)
1209 mandoc_msg(MANDOCERR_ND_LATE
, mdoc
->parse
,
1210 n
->line
, n
->pos
, "Nd");
1212 if (n
->child
== NULL
)
1213 mandoc_msg(MANDOCERR_ND_EMPTY
, mdoc
->parse
,
1214 n
->line
, n
->pos
, "Nd");
1215 else if (n
->last
->type
== ROFFT_TEXT
&&
1216 (sz
= strlen(n
->last
->string
)) != 0 &&
1217 n
->last
->string
[sz
- 1] == '.')
1218 mandoc_msg(MANDOCERR_ND_DOT
, mdoc
->parse
,
1219 n
->last
->line
, n
->last
->pos
+ sz
- 1, NULL
);
1225 post_display(POST_ARGS
)
1227 struct roff_node
*n
, *np
;
1232 if (n
->end
!= ENDBODY_NOT
) {
1233 if (n
->tok
== MDOC_Bd
&&
1234 n
->body
->parent
->args
== NULL
)
1235 roff_node_delete(mdoc
, n
);
1236 } else if (n
->child
== NULL
)
1237 mandoc_msg(MANDOCERR_BLK_EMPTY
, mdoc
->parse
,
1238 n
->line
, n
->pos
, roff_name
[n
->tok
]);
1239 else if (n
->tok
== MDOC_D1
)
1243 if (n
->tok
== MDOC_Bd
) {
1244 if (n
->args
== NULL
) {
1245 mandoc_msg(MANDOCERR_BD_NOARG
,
1246 mdoc
->parse
, n
->line
, n
->pos
, "Bd");
1247 mdoc
->next
= ROFF_NEXT_SIBLING
;
1248 while (n
->body
->child
!= NULL
)
1249 mdoc_node_relink(mdoc
,
1251 roff_node_delete(mdoc
, n
);
1257 for (np
= n
->parent
; np
!= NULL
; np
= np
->parent
) {
1258 if (np
->type
== ROFFT_BLOCK
&& np
->tok
== MDOC_Bd
) {
1259 mandoc_vmsg(MANDOCERR_BD_NEST
,
1260 mdoc
->parse
, n
->line
, n
->pos
,
1261 "%s in Bd", roff_name
[n
->tok
]);
1272 post_defaults(POST_ARGS
)
1274 struct roff_node
*nn
;
1276 if (mdoc
->last
->child
!= NULL
) {
1282 * The `Ar' defaults to "file ..." if no value is provided as an
1283 * argument; the `Mt' and `Pa' macros use "~"; the `Li' just
1284 * gets an empty string.
1290 mdoc
->next
= ROFF_NEXT_CHILD
;
1291 roff_word_alloc(mdoc
, nn
->line
, nn
->pos
, "file");
1292 mdoc
->last
->flags
|= NODE_NOSRC
;
1293 roff_word_alloc(mdoc
, nn
->line
, nn
->pos
, "...");
1294 mdoc
->last
->flags
|= NODE_NOSRC
;
1298 mdoc
->next
= ROFF_NEXT_CHILD
;
1299 roff_word_alloc(mdoc
, nn
->line
, nn
->pos
, "~");
1300 mdoc
->last
->flags
|= NODE_NOSRC
;
1311 struct roff_node
*n
, *nch
;
1318 * If we have a child, look it up in the standard keys. If a
1319 * key exist, use that instead of the child; if it doesn't,
1320 * prefix "AT&T UNIX " to the existing data.
1324 if (nch
!= NULL
&& ((att
= mdoc_a2att(nch
->string
)) == NULL
))
1325 mandoc_vmsg(MANDOCERR_AT_BAD
, mdoc
->parse
,
1326 nch
->line
, nch
->pos
, "At %s", nch
->string
);
1328 mdoc
->next
= ROFF_NEXT_CHILD
;
1330 roff_word_alloc(mdoc
, nch
->line
, nch
->pos
, att
);
1331 nch
->flags
|= NODE_NOPRT
;
1333 roff_word_alloc(mdoc
, n
->line
, n
->pos
, "AT&T UNIX");
1334 mdoc
->last
->flags
|= NODE_NOSRC
;
1341 struct roff_node
*np
, *nch
;
1347 if (np
->norm
->An
.auth
== AUTH__NONE
) {
1349 mandoc_msg(MANDOCERR_MACRO_EMPTY
, mdoc
->parse
,
1350 np
->line
, np
->pos
, "An");
1353 } else if (nch
!= NULL
)
1354 mandoc_vmsg(MANDOCERR_ARG_EXCESS
, mdoc
->parse
,
1355 nch
->line
, nch
->pos
, "An ... %s", nch
->string
);
1362 post_obsolete(mdoc
);
1363 if (mdoc
->last
->type
== ROFFT_BLOCK
)
1364 mdoc
->last
->norm
->Es
= mdoc
->last_es
;
1371 post_obsolete(mdoc
);
1372 mdoc
->last_es
= mdoc
->last
;
1378 struct roff_node
*n
;
1406 mdoc
->next
= ROFF_NEXT_CHILD
;
1407 roff_word_alloc(mdoc
, n
->line
, n
->pos
, os
);
1408 mdoc
->last
->flags
|= NODE_NOSRC
;
1415 struct roff_node
*nbl
, *nit
, *nch
;
1422 if (nit
->type
!= ROFFT_BLOCK
)
1425 nbl
= nit
->parent
->parent
;
1426 lt
= nbl
->norm
->Bl
.type
;
1434 if (nit
->head
->child
== NULL
)
1435 mandoc_vmsg(MANDOCERR_IT_NOHEAD
,
1436 mdoc
->parse
, nit
->line
, nit
->pos
,
1438 mdoc_argnames
[nbl
->args
->argv
[0].arg
]);
1444 if (nit
->body
== NULL
|| nit
->body
->child
== NULL
)
1445 mandoc_vmsg(MANDOCERR_IT_NOBODY
,
1446 mdoc
->parse
, nit
->line
, nit
->pos
,
1448 mdoc_argnames
[nbl
->args
->argv
[0].arg
]);
1451 if ((nch
= nit
->head
->child
) != NULL
)
1452 mandoc_vmsg(MANDOCERR_ARG_SKIP
, mdoc
->parse
,
1453 nit
->line
, nit
->pos
, "It %s",
1454 nch
->string
== NULL
? roff_name
[nch
->tok
] :
1458 cols
= (int)nbl
->norm
->Bl
.ncols
;
1460 assert(nit
->head
->child
== NULL
);
1463 for (nch
= nit
->child
; nch
!= NULL
; nch
= nch
->next
)
1464 if (nch
->type
== ROFFT_BODY
)
1467 if (i
< cols
|| i
> cols
+ 1)
1468 mandoc_vmsg(MANDOCERR_BL_COL
,
1469 mdoc
->parse
, nit
->line
, nit
->pos
,
1470 "%d columns, %d cells", cols
, i
);
1478 post_bl_block(POST_ARGS
)
1480 struct roff_node
*n
, *ni
, *nc
;
1485 for (ni
= n
->body
->child
; ni
!= NULL
; ni
= ni
->next
) {
1486 if (ni
->body
== NULL
)
1488 nc
= ni
->body
->last
;
1489 while (nc
!= NULL
) {
1499 if (ni
->next
== NULL
) {
1500 mandoc_msg(MANDOCERR_PAR_MOVE
,
1501 mdoc
->parse
, nc
->line
, nc
->pos
,
1502 roff_name
[nc
->tok
]);
1503 mdoc_node_relink(mdoc
, nc
);
1504 } else if (n
->norm
->Bl
.comp
== 0 &&
1505 n
->norm
->Bl
.type
!= LIST_column
) {
1506 mandoc_vmsg(MANDOCERR_PAR_SKIP
,
1507 mdoc
->parse
, nc
->line
, nc
->pos
,
1508 "%s before It", roff_name
[nc
->tok
]);
1509 roff_node_delete(mdoc
, nc
);
1512 nc
= ni
->body
->last
;
1518 * If the argument of -offset or -width is a macro,
1519 * replace it with the associated default width.
1522 rewrite_macro2len(struct roff_man
*mdoc
, char **arg
)
1529 else if ( ! strcmp(*arg
, "Ds"))
1531 else if ((tok
= roffhash_find(mdoc
->mdocmac
, *arg
, 0)) == TOKEN_NONE
)
1534 width
= macro2len(tok
);
1537 mandoc_asprintf(arg
, "%zun", width
);
1541 post_bl_head(POST_ARGS
)
1543 struct roff_node
*nbl
, *nh
, *nch
, *nnext
;
1544 struct mdoc_argv
*argv
;
1550 if (nh
->norm
->Bl
.type
!= LIST_column
) {
1551 if ((nch
= nh
->child
) == NULL
)
1553 mandoc_vmsg(MANDOCERR_ARG_EXCESS
, mdoc
->parse
,
1554 nch
->line
, nch
->pos
, "Bl ... %s", nch
->string
);
1555 while (nch
!= NULL
) {
1556 roff_node_delete(mdoc
, nch
);
1563 * Append old-style lists, where the column width specifiers
1564 * trail as macro parameters, to the new-style ("normal-form")
1565 * lists where they're argument values following -column.
1568 if (nh
->child
== NULL
)
1572 for (j
= 0; j
< (int)nbl
->args
->argc
; j
++)
1573 if (nbl
->args
->argv
[j
].arg
== MDOC_Column
)
1576 assert(j
< (int)nbl
->args
->argc
);
1579 * Accommodate for new-style groff column syntax. Shuffle the
1580 * child nodes, all of which must be TEXT, as arguments for the
1581 * column field. Then, delete the head children.
1584 argv
= nbl
->args
->argv
+ j
;
1586 for (nch
= nh
->child
; nch
!= NULL
; nch
= nch
->next
)
1588 argv
->value
= mandoc_reallocarray(argv
->value
,
1589 argv
->sz
, sizeof(char *));
1591 nh
->norm
->Bl
.ncols
= argv
->sz
;
1592 nh
->norm
->Bl
.cols
= (void *)argv
->value
;
1594 for (nch
= nh
->child
; nch
!= NULL
; nch
= nnext
) {
1595 argv
->value
[i
++] = nch
->string
;
1598 roff_node_delete(NULL
, nch
);
1606 struct roff_node
*nparent
, *nprev
; /* of the Bl block */
1607 struct roff_node
*nblock
, *nbody
; /* of the Bl */
1608 struct roff_node
*nchild
, *nnext
; /* of the Bl body */
1609 const char *prev_Er
;
1613 switch (nbody
->type
) {
1615 post_bl_block(mdoc
);
1625 if (nbody
->end
!= ENDBODY_NOT
)
1628 nchild
= nbody
->child
;
1629 if (nchild
== NULL
) {
1630 mandoc_msg(MANDOCERR_BLK_EMPTY
, mdoc
->parse
,
1631 nbody
->line
, nbody
->pos
, "Bl");
1634 while (nchild
!= NULL
) {
1635 nnext
= nchild
->next
;
1636 if (nchild
->tok
== MDOC_It
||
1637 (nchild
->tok
== MDOC_Sm
&&
1638 nnext
!= NULL
&& nnext
->tok
== MDOC_It
)) {
1644 * In .Bl -column, the first rows may be implicit,
1645 * that is, they may not start with .It macros.
1646 * Such rows may be followed by nodes generated on the
1647 * roff level, for example .TS, which cannot be moved
1648 * out of the list. In that case, wrap such roff nodes
1649 * into an implicit row.
1652 if (nchild
->prev
!= NULL
) {
1653 mdoc
->last
= nchild
;
1654 mdoc
->next
= ROFF_NEXT_SIBLING
;
1655 roff_block_alloc(mdoc
, nchild
->line
,
1656 nchild
->pos
, MDOC_It
);
1657 roff_head_alloc(mdoc
, nchild
->line
,
1658 nchild
->pos
, MDOC_It
);
1659 mdoc
->next
= ROFF_NEXT_SIBLING
;
1660 roff_body_alloc(mdoc
, nchild
->line
,
1661 nchild
->pos
, MDOC_It
);
1662 while (nchild
->tok
!= MDOC_It
) {
1663 mdoc_node_relink(mdoc
, nchild
);
1664 if ((nchild
= nnext
) == NULL
)
1666 nnext
= nchild
->next
;
1667 mdoc
->next
= ROFF_NEXT_SIBLING
;
1673 mandoc_msg(MANDOCERR_BL_MOVE
, mdoc
->parse
,
1674 nchild
->line
, nchild
->pos
, roff_name
[nchild
->tok
]);
1677 * Move the node out of the Bl block.
1678 * First, collect all required node pointers.
1681 nblock
= nbody
->parent
;
1682 nprev
= nblock
->prev
;
1683 nparent
= nblock
->parent
;
1686 * Unlink this child.
1689 nbody
->child
= nnext
;
1696 * Relink this child.
1699 nchild
->parent
= nparent
;
1700 nchild
->prev
= nprev
;
1701 nchild
->next
= nblock
;
1703 nblock
->prev
= nchild
;
1705 nparent
->child
= nchild
;
1707 nprev
->next
= nchild
;
1712 if (mdoc
->meta
.os_e
!= MDOC_OS_NETBSD
)
1716 for (nchild
= nbody
->child
; nchild
!= NULL
; nchild
= nchild
->next
) {
1717 if (nchild
->tok
!= MDOC_It
)
1719 if ((nnext
= nchild
->head
->child
) == NULL
)
1721 if (nnext
->type
== ROFFT_BLOCK
)
1722 nnext
= nnext
->body
->child
;
1723 if (nnext
== NULL
|| nnext
->tok
!= MDOC_Er
)
1725 nnext
= nnext
->child
;
1726 if (prev_Er
!= NULL
) {
1727 order
= strcmp(prev_Er
, nnext
->string
);
1729 mandoc_vmsg(MANDOCERR_ER_ORDER
,
1730 mdoc
->parse
, nnext
->line
, nnext
->pos
,
1731 "Er %s %s", prev_Er
, nnext
->string
);
1732 else if (order
== 0)
1733 mandoc_vmsg(MANDOCERR_ER_REP
,
1734 mdoc
->parse
, nnext
->line
, nnext
->pos
,
1737 prev_Er
= nnext
->string
;
1744 struct roff_node
*n
;
1748 if (n
->type
== ROFFT_BLOCK
&& n
->body
->child
== NULL
) {
1749 mandoc_msg(MANDOCERR_BLK_EMPTY
,
1750 mdoc
->parse
, n
->line
, n
->pos
, "Bk");
1751 roff_node_delete(mdoc
, n
);
1758 struct roff_node
*nch
;
1760 nch
= mdoc
->last
->child
;
1763 mdoc
->flags
^= MDOC_SMOFF
;
1767 assert(nch
->type
== ROFFT_TEXT
);
1769 if ( ! strcmp(nch
->string
, "on")) {
1770 mdoc
->flags
&= ~MDOC_SMOFF
;
1773 if ( ! strcmp(nch
->string
, "off")) {
1774 mdoc
->flags
|= MDOC_SMOFF
;
1778 mandoc_vmsg(MANDOCERR_SM_BAD
,
1779 mdoc
->parse
, nch
->line
, nch
->pos
,
1780 "%s %s", roff_name
[mdoc
->last
->tok
], nch
->string
);
1781 mdoc_node_relink(mdoc
, nch
);
1786 post_root(POST_ARGS
)
1788 struct roff_node
*n
;
1790 /* Add missing prologue data. */
1792 if (mdoc
->meta
.date
== NULL
)
1793 mdoc
->meta
.date
= mdoc
->quick
?
1795 mandoc_normdate(mdoc
->parse
, NULL
, 0, 0);
1797 if (mdoc
->meta
.title
== NULL
) {
1798 mandoc_msg(MANDOCERR_DT_NOTITLE
,
1799 mdoc
->parse
, 0, 0, "EOF");
1800 mdoc
->meta
.title
= mandoc_strdup("UNTITLED");
1803 if (mdoc
->meta
.vol
== NULL
)
1804 mdoc
->meta
.vol
= mandoc_strdup("LOCAL");
1806 if (mdoc
->meta
.os
== NULL
) {
1807 mandoc_msg(MANDOCERR_OS_MISSING
,
1808 mdoc
->parse
, 0, 0, NULL
);
1809 mdoc
->meta
.os
= mandoc_strdup("");
1812 /* Check that we begin with a proper `Sh'. */
1814 n
= mdoc
->first
->child
;
1815 while (n
!= NULL
&& n
->tok
!= TOKEN_NONE
&&
1816 mdoc_macros
[n
->tok
].flags
& MDOC_PROLOGUE
)
1820 mandoc_msg(MANDOCERR_DOC_EMPTY
, mdoc
->parse
, 0, 0, NULL
);
1821 else if (n
->tok
!= MDOC_Sh
)
1822 mandoc_msg(MANDOCERR_SEC_BEFORE
, mdoc
->parse
,
1823 n
->line
, n
->pos
, roff_name
[n
->tok
]);
1829 struct roff_node
*np
, *nch
, *next
, *prev
;
1834 if (np
->type
!= ROFFT_BODY
)
1837 if (np
->child
== NULL
) {
1838 mandoc_msg(MANDOCERR_RS_EMPTY
, mdoc
->parse
,
1839 np
->line
, np
->pos
, "Rs");
1844 * The full `Rs' block needs special handling to order the
1845 * sub-elements according to `rsord'. Pick through each element
1846 * and correctly order it. This is an insertion sort.
1850 for (nch
= np
->child
->next
; nch
!= NULL
; nch
= next
) {
1851 /* Determine order number of this child. */
1852 for (i
= 0; i
< RSORD_MAX
; i
++)
1853 if (rsord
[i
] == nch
->tok
)
1856 if (i
== RSORD_MAX
) {
1857 mandoc_msg(MANDOCERR_RS_BAD
, mdoc
->parse
,
1858 nch
->line
, nch
->pos
, roff_name
[nch
->tok
]);
1860 } else if (nch
->tok
== MDOC__J
|| nch
->tok
== MDOC__B
)
1861 np
->norm
->Rs
.quote_T
++;
1864 * Remove this child from the chain. This somewhat
1865 * repeats roff_node_unlink(), but since we're
1866 * just re-ordering, there's no need for the
1867 * full unlink process.
1870 if ((next
= nch
->next
) != NULL
)
1871 next
->prev
= nch
->prev
;
1873 if ((prev
= nch
->prev
) != NULL
)
1874 prev
->next
= nch
->next
;
1876 nch
->prev
= nch
->next
= NULL
;
1879 * Scan back until we reach a node that's
1880 * to be ordered before this child.
1883 for ( ; prev
; prev
= prev
->prev
) {
1884 /* Determine order of `prev'. */
1885 for (j
= 0; j
< RSORD_MAX
; j
++)
1886 if (rsord
[j
] == prev
->tok
)
1896 * Set this child back into its correct place
1897 * in front of the `prev' node.
1903 np
->child
->prev
= nch
;
1904 nch
->next
= np
->child
;
1908 prev
->next
->prev
= nch
;
1909 nch
->next
= prev
->next
;
1916 * For some arguments of some macros,
1917 * convert all breakable hyphens into ASCII_HYPH.
1920 post_hyph(POST_ARGS
)
1922 struct roff_node
*nch
;
1925 for (nch
= mdoc
->last
->child
; nch
!= NULL
; nch
= nch
->next
) {
1926 if (nch
->type
!= ROFFT_TEXT
)
1931 while (*(++cp
) != '\0')
1933 isalpha((unsigned char)cp
[-1]) &&
1934 isalpha((unsigned char)cp
[1]))
1943 if (mdoc
->last
->flags
& NODE_LINE
)
1944 mandoc_msg(MANDOCERR_NS_SKIP
, mdoc
->parse
,
1945 mdoc
->last
->line
, mdoc
->last
->pos
, NULL
);
1954 switch (mdoc
->last
->type
) {
1959 switch (mdoc
->lastsec
) {
1964 post_sh_see_also(mdoc
);
1967 post_sh_authors(mdoc
);
1979 post_sh_name(POST_ARGS
)
1981 struct roff_node
*n
;
1986 for (n
= mdoc
->last
->child
; n
!= NULL
; n
= n
->next
) {
1989 if (hasnm
&& n
->child
!= NULL
)
1990 mandoc_vmsg(MANDOCERR_NAMESEC_PUNCT
,
1991 mdoc
->parse
, n
->line
, n
->pos
,
1992 "Nm %s", n
->child
->string
);
1997 if (n
->next
!= NULL
)
1998 mandoc_msg(MANDOCERR_NAMESEC_ND
,
1999 mdoc
->parse
, n
->line
, n
->pos
, NULL
);
2002 if (n
->type
== ROFFT_TEXT
&&
2003 n
->string
[0] == ',' && n
->string
[1] == '\0' &&
2004 n
->next
!= NULL
&& n
->next
->tok
== MDOC_Nm
) {
2010 mandoc_msg(MANDOCERR_NAMESEC_BAD
, mdoc
->parse
,
2011 n
->line
, n
->pos
, roff_name
[n
->tok
]);
2018 mandoc_msg(MANDOCERR_NAMESEC_NONM
, mdoc
->parse
,
2019 mdoc
->last
->line
, mdoc
->last
->pos
, NULL
);
2021 mandoc_msg(MANDOCERR_NAMESEC_NOND
, mdoc
->parse
,
2022 mdoc
->last
->line
, mdoc
->last
->pos
, NULL
);
2026 post_sh_see_also(POST_ARGS
)
2028 const struct roff_node
*n
;
2029 const char *name
, *sec
;
2030 const char *lastname
, *lastsec
, *lastpunct
;
2033 n
= mdoc
->last
->child
;
2034 lastname
= lastsec
= lastpunct
= NULL
;
2036 if (n
->tok
!= MDOC_Xr
||
2038 n
->child
->next
== NULL
)
2041 /* Process one .Xr node. */
2043 name
= n
->child
->string
;
2044 sec
= n
->child
->next
->string
;
2045 if (lastsec
!= NULL
) {
2046 if (lastpunct
[0] != ',' || lastpunct
[1] != '\0')
2047 mandoc_vmsg(MANDOCERR_XR_PUNCT
,
2048 mdoc
->parse
, n
->line
, n
->pos
,
2049 "%s before %s(%s)", lastpunct
,
2051 cmp
= strcmp(lastsec
, sec
);
2053 mandoc_vmsg(MANDOCERR_XR_ORDER
,
2054 mdoc
->parse
, n
->line
, n
->pos
,
2055 "%s(%s) after %s(%s)", name
,
2056 sec
, lastname
, lastsec
);
2057 else if (cmp
== 0 &&
2058 strcasecmp(lastname
, name
) > 0)
2059 mandoc_vmsg(MANDOCERR_XR_ORDER
,
2060 mdoc
->parse
, n
->line
, n
->pos
,
2061 "%s after %s", name
, lastname
);
2066 /* Process the following node. */
2071 if (n
->tok
== MDOC_Xr
) {
2075 if (n
->type
!= ROFFT_TEXT
)
2077 for (name
= n
->string
; *name
!= '\0'; name
++)
2078 if (isalpha((const unsigned char)*name
))
2080 lastpunct
= n
->string
;
2081 if (n
->next
== NULL
|| n
->next
->tok
== MDOC_Rs
)
2082 mandoc_vmsg(MANDOCERR_XR_PUNCT
, mdoc
->parse
,
2083 n
->line
, n
->pos
, "%s after %s(%s)",
2084 lastpunct
, lastname
, lastsec
);
2090 child_an(const struct roff_node
*n
)
2093 for (n
= n
->child
; n
!= NULL
; n
= n
->next
)
2094 if ((n
->tok
== MDOC_An
&& n
->child
!= NULL
) || child_an(n
))
2100 post_sh_authors(POST_ARGS
)
2103 if ( ! child_an(mdoc
->last
))
2104 mandoc_msg(MANDOCERR_AN_MISSING
, mdoc
->parse
,
2105 mdoc
->last
->line
, mdoc
->last
->pos
, NULL
);
2109 post_sh_head(POST_ARGS
)
2111 struct roff_node
*nch
;
2112 const char *goodsec
;
2116 * Process a new section. Sections are either "named" or
2117 * "custom". Custom sections are user-defined, while named ones
2118 * follow a conventional order and may only appear in certain
2122 sec
= mdoc
->last
->sec
;
2124 /* The NAME should be first. */
2126 if (sec
!= SEC_NAME
&& mdoc
->lastnamed
== SEC_NONE
)
2127 mandoc_vmsg(MANDOCERR_NAMESEC_FIRST
, mdoc
->parse
,
2128 mdoc
->last
->line
, mdoc
->last
->pos
, "Sh %s",
2129 sec
!= SEC_CUSTOM
? secnames
[sec
] :
2130 (nch
= mdoc
->last
->child
) == NULL
? "" :
2131 nch
->type
== ROFFT_TEXT
? nch
->string
:
2132 roff_name
[nch
->tok
]);
2134 /* The SYNOPSIS gets special attention in other areas. */
2136 if (sec
== SEC_SYNOPSIS
) {
2137 roff_setreg(mdoc
->roff
, "nS", 1, '=');
2138 mdoc
->flags
|= MDOC_SYNOPSIS
;
2140 roff_setreg(mdoc
->roff
, "nS", 0, '=');
2141 mdoc
->flags
&= ~MDOC_SYNOPSIS
;
2144 /* Mark our last section. */
2146 mdoc
->lastsec
= sec
;
2148 /* We don't care about custom sections after this. */
2150 if (sec
== SEC_CUSTOM
)
2154 * Check whether our non-custom section is being repeated or is
2158 if (sec
== mdoc
->lastnamed
)
2159 mandoc_vmsg(MANDOCERR_SEC_REP
, mdoc
->parse
,
2160 mdoc
->last
->line
, mdoc
->last
->pos
,
2161 "Sh %s", secnames
[sec
]);
2163 if (sec
< mdoc
->lastnamed
)
2164 mandoc_vmsg(MANDOCERR_SEC_ORDER
, mdoc
->parse
,
2165 mdoc
->last
->line
, mdoc
->last
->pos
,
2166 "Sh %s", secnames
[sec
]);
2168 /* Mark the last named section. */
2170 mdoc
->lastnamed
= sec
;
2172 /* Check particular section/manual conventions. */
2174 if (mdoc
->meta
.msec
== NULL
)
2180 if (*mdoc
->meta
.msec
== '4')
2182 goodsec
= "2, 3, 4, 9";
2184 case SEC_RETURN_VALUES
:
2186 if (*mdoc
->meta
.msec
== '2')
2188 if (*mdoc
->meta
.msec
== '3')
2190 if (NULL
== goodsec
)
2191 goodsec
= "2, 3, 9";
2194 if (*mdoc
->meta
.msec
== '9')
2196 if (NULL
== goodsec
)
2198 mandoc_vmsg(MANDOCERR_SEC_MSEC
, mdoc
->parse
,
2199 mdoc
->last
->line
, mdoc
->last
->pos
,
2200 "Sh %s for %s only", secnames
[sec
], goodsec
);
2210 struct roff_node
*n
, *nch
;
2214 if (nch
->next
== NULL
) {
2215 mandoc_vmsg(MANDOCERR_XR_NOSEC
, mdoc
->parse
,
2216 n
->line
, n
->pos
, "Xr %s", nch
->string
);
2218 assert(nch
->next
== n
->last
);
2223 post_ignpar(POST_ARGS
)
2225 struct roff_node
*np
;
2227 switch (mdoc
->last
->type
) {
2240 if ((np
= mdoc
->last
->child
) != NULL
)
2241 if (np
->tok
== MDOC_Pp
|| np
->tok
== MDOC_Lp
) {
2242 mandoc_vmsg(MANDOCERR_PAR_SKIP
,
2243 mdoc
->parse
, np
->line
, np
->pos
,
2244 "%s after %s", roff_name
[np
->tok
],
2245 roff_name
[mdoc
->last
->tok
]);
2246 roff_node_delete(mdoc
, np
);
2249 if ((np
= mdoc
->last
->last
) != NULL
)
2250 if (np
->tok
== MDOC_Pp
|| np
->tok
== MDOC_Lp
) {
2251 mandoc_vmsg(MANDOCERR_PAR_SKIP
, mdoc
->parse
,
2252 np
->line
, np
->pos
, "%s at the end of %s",
2254 roff_name
[mdoc
->last
->tok
]);
2255 roff_node_delete(mdoc
, np
);
2260 post_prevpar(POST_ARGS
)
2262 struct roff_node
*n
;
2265 if (NULL
== n
->prev
)
2267 if (n
->type
!= ROFFT_ELEM
&& n
->type
!= ROFFT_BLOCK
)
2271 * Don't allow prior `Lp' or `Pp' prior to a paragraph-type
2272 * block: `Lp', `Pp', or non-compact `Bd' or `Bl'.
2275 if (n
->prev
->tok
!= MDOC_Pp
&&
2276 n
->prev
->tok
!= MDOC_Lp
&&
2277 n
->prev
->tok
!= ROFF_br
)
2279 if (n
->tok
== MDOC_Bl
&& n
->norm
->Bl
.comp
)
2281 if (n
->tok
== MDOC_Bd
&& n
->norm
->Bd
.comp
)
2283 if (n
->tok
== MDOC_It
&& n
->parent
->norm
->Bl
.comp
)
2286 mandoc_vmsg(MANDOCERR_PAR_SKIP
, mdoc
->parse
,
2287 n
->prev
->line
, n
->prev
->pos
, "%s before %s",
2288 roff_name
[n
->prev
->tok
], roff_name
[n
->tok
]);
2289 roff_node_delete(mdoc
, n
->prev
);
2295 struct roff_node
*np
;
2298 if (np
->tok
!= ROFF_br
&& np
->tok
!= ROFF_sp
)
2301 if (np
->tok
== ROFF_sp
) {
2302 if (np
->child
!= NULL
&& np
->child
->next
!= NULL
)
2303 mandoc_vmsg(MANDOCERR_ARG_EXCESS
, mdoc
->parse
,
2304 np
->child
->next
->line
, np
->child
->next
->pos
,
2305 "sp ... %s", np
->child
->next
->string
);
2306 } else if (np
->child
!= NULL
)
2307 mandoc_vmsg(MANDOCERR_ARG_SKIP
,
2308 mdoc
->parse
, np
->line
, np
->pos
, "%s %s",
2309 roff_name
[np
->tok
], np
->child
->string
);
2311 if ((np
= mdoc
->last
->prev
) == NULL
) {
2312 np
= mdoc
->last
->parent
;
2313 if (np
->tok
!= MDOC_Sh
&& np
->tok
!= MDOC_Ss
)
2315 } else if (np
->tok
!= MDOC_Pp
&& np
->tok
!= MDOC_Lp
&&
2316 (mdoc
->last
->tok
!= ROFF_br
||
2317 (np
->tok
!= ROFF_sp
&& np
->tok
!= ROFF_br
)))
2320 mandoc_vmsg(MANDOCERR_PAR_SKIP
, mdoc
->parse
,
2321 mdoc
->last
->line
, mdoc
->last
->pos
, "%s after %s",
2322 roff_name
[mdoc
->last
->tok
], roff_name
[np
->tok
]);
2323 roff_node_delete(mdoc
, mdoc
->last
);
2329 struct roff_node
*n
;
2333 n
->flags
|= NODE_NOPRT
;
2335 if (mdoc
->meta
.date
!= NULL
) {
2336 mandoc_msg(MANDOCERR_PROLOG_REP
, mdoc
->parse
,
2337 n
->line
, n
->pos
, "Dd");
2338 free(mdoc
->meta
.date
);
2339 } else if (mdoc
->flags
& MDOC_PBODY
)
2340 mandoc_msg(MANDOCERR_PROLOG_LATE
, mdoc
->parse
,
2341 n
->line
, n
->pos
, "Dd");
2342 else if (mdoc
->meta
.title
!= NULL
)
2343 mandoc_msg(MANDOCERR_PROLOG_ORDER
, mdoc
->parse
,
2344 n
->line
, n
->pos
, "Dd after Dt");
2345 else if (mdoc
->meta
.os
!= NULL
)
2346 mandoc_msg(MANDOCERR_PROLOG_ORDER
, mdoc
->parse
,
2347 n
->line
, n
->pos
, "Dd after Os");
2349 if (n
->child
== NULL
|| n
->child
->string
[0] == '\0') {
2350 mdoc
->meta
.date
= mdoc
->quick
? mandoc_strdup("") :
2351 mandoc_normdate(mdoc
->parse
, NULL
, n
->line
, n
->pos
);
2356 deroff(&datestr
, n
);
2358 mdoc
->meta
.date
= datestr
;
2360 mdoc
->meta
.date
= mandoc_normdate(mdoc
->parse
,
2361 datestr
, n
->line
, n
->pos
);
2369 struct roff_node
*nn
, *n
;
2374 n
->flags
|= NODE_NOPRT
;
2376 if (mdoc
->flags
& MDOC_PBODY
) {
2377 mandoc_msg(MANDOCERR_DT_LATE
, mdoc
->parse
,
2378 n
->line
, n
->pos
, "Dt");
2382 if (mdoc
->meta
.title
!= NULL
)
2383 mandoc_msg(MANDOCERR_PROLOG_REP
, mdoc
->parse
,
2384 n
->line
, n
->pos
, "Dt");
2385 else if (mdoc
->meta
.os
!= NULL
)
2386 mandoc_msg(MANDOCERR_PROLOG_ORDER
, mdoc
->parse
,
2387 n
->line
, n
->pos
, "Dt after Os");
2389 free(mdoc
->meta
.title
);
2390 free(mdoc
->meta
.msec
);
2391 free(mdoc
->meta
.vol
);
2392 free(mdoc
->meta
.arch
);
2394 mdoc
->meta
.title
= NULL
;
2395 mdoc
->meta
.msec
= NULL
;
2396 mdoc
->meta
.vol
= NULL
;
2397 mdoc
->meta
.arch
= NULL
;
2399 /* Mandatory first argument: title. */
2402 if (nn
== NULL
|| *nn
->string
== '\0') {
2403 mandoc_msg(MANDOCERR_DT_NOTITLE
,
2404 mdoc
->parse
, n
->line
, n
->pos
, "Dt");
2405 mdoc
->meta
.title
= mandoc_strdup("UNTITLED");
2407 mdoc
->meta
.title
= mandoc_strdup(nn
->string
);
2409 /* Check that all characters are uppercase. */
2411 for (p
= nn
->string
; *p
!= '\0'; p
++)
2412 if (islower((unsigned char)*p
)) {
2413 mandoc_vmsg(MANDOCERR_TITLE_CASE
,
2414 mdoc
->parse
, nn
->line
,
2415 nn
->pos
+ (p
- nn
->string
),
2416 "Dt %s", nn
->string
);
2421 /* Mandatory second argument: section. */
2427 mandoc_vmsg(MANDOCERR_MSEC_MISSING
,
2428 mdoc
->parse
, n
->line
, n
->pos
,
2429 "Dt %s", mdoc
->meta
.title
);
2430 mdoc
->meta
.vol
= mandoc_strdup("LOCAL");
2431 return; /* msec and arch remain NULL. */
2434 mdoc
->meta
.msec
= mandoc_strdup(nn
->string
);
2436 /* Infer volume title from section number. */
2438 cp
= mandoc_a2msec(nn
->string
);
2440 mandoc_vmsg(MANDOCERR_MSEC_BAD
, mdoc
->parse
,
2441 nn
->line
, nn
->pos
, "Dt ... %s", nn
->string
);
2442 mdoc
->meta
.vol
= mandoc_strdup(nn
->string
);
2444 mdoc
->meta
.vol
= mandoc_strdup(cp
);
2446 /* Optional third argument: architecture. */
2448 if ((nn
= nn
->next
) == NULL
)
2451 for (p
= nn
->string
; *p
!= '\0'; p
++)
2452 *p
= tolower((unsigned char)*p
);
2453 mdoc
->meta
.arch
= mandoc_strdup(nn
->string
);
2455 /* Ignore fourth and later arguments. */
2457 if ((nn
= nn
->next
) != NULL
)
2458 mandoc_vmsg(MANDOCERR_ARG_EXCESS
, mdoc
->parse
,
2459 nn
->line
, nn
->pos
, "Dt ... %s", nn
->string
);
2465 struct roff_node
*n
, *nch
;
2474 macro
= !strcmp(nch
->string
, "Open") ? "Ox" :
2475 !strcmp(nch
->string
, "Net") ? "Nx" :
2476 !strcmp(nch
->string
, "Free") ? "Fx" :
2477 !strcmp(nch
->string
, "DragonFly") ? "Dx" : NULL
;
2479 mandoc_msg(MANDOCERR_BX
, mdoc
->parse
,
2480 n
->line
, n
->pos
, macro
);
2483 mdoc
->next
= ROFF_NEXT_SIBLING
;
2484 roff_elem_alloc(mdoc
, n
->line
, n
->pos
, MDOC_Ns
);
2485 mdoc
->last
->flags
|= NODE_NOSRC
;
2486 mdoc
->next
= ROFF_NEXT_SIBLING
;
2488 mdoc
->next
= ROFF_NEXT_CHILD
;
2489 roff_word_alloc(mdoc
, n
->line
, n
->pos
, "BSD");
2490 mdoc
->last
->flags
|= NODE_NOSRC
;
2497 roff_elem_alloc(mdoc
, n
->line
, n
->pos
, MDOC_Ns
);
2498 mdoc
->last
->flags
|= NODE_NOSRC
;
2499 mdoc
->next
= ROFF_NEXT_SIBLING
;
2500 roff_word_alloc(mdoc
, n
->line
, n
->pos
, "-");
2501 mdoc
->last
->flags
|= NODE_NOSRC
;
2502 roff_elem_alloc(mdoc
, n
->line
, n
->pos
, MDOC_Ns
);
2503 mdoc
->last
->flags
|= NODE_NOSRC
;
2507 * Make `Bx's second argument always start with an uppercase
2508 * letter. Groff checks if it's an "accepted" term, but we just
2509 * uppercase blindly.
2512 *nch
->string
= (char)toupper((unsigned char)*nch
->string
);
2519 struct utsname utsname
;
2520 static char *defbuf
;
2522 struct roff_node
*n
;
2525 n
->flags
|= NODE_NOPRT
;
2527 if (mdoc
->meta
.os
!= NULL
)
2528 mandoc_msg(MANDOCERR_PROLOG_REP
, mdoc
->parse
,
2529 n
->line
, n
->pos
, "Os");
2530 else if (mdoc
->flags
& MDOC_PBODY
)
2531 mandoc_msg(MANDOCERR_PROLOG_LATE
, mdoc
->parse
,
2532 n
->line
, n
->pos
, "Os");
2535 * Set the operating system by way of the `Os' macro.
2536 * The order of precedence is:
2537 * 1. the argument of the `Os' macro, unless empty
2538 * 2. the -Ios=foo command line argument, if provided
2539 * 3. -DOSNAME="\"foo\"", if provided during compilation
2540 * 4. "sysname release" from uname(3)
2543 free(mdoc
->meta
.os
);
2544 mdoc
->meta
.os
= NULL
;
2545 deroff(&mdoc
->meta
.os
, n
);
2550 mdoc
->meta
.os
= mandoc_strdup(mdoc
->defos
);
2555 mdoc
->meta
.os
= mandoc_strdup(OSNAME
);
2557 if (defbuf
== NULL
) {
2558 if (uname(&utsname
) == -1) {
2559 mandoc_msg(MANDOCERR_OS_UNAME
, mdoc
->parse
,
2560 n
->line
, n
->pos
, "Os");
2561 defbuf
= mandoc_strdup("UNKNOWN");
2563 mandoc_asprintf(&defbuf
, "%s %s",
2564 utsname
.sysname
, utsname
.release
);
2566 mdoc
->meta
.os
= mandoc_strdup(defbuf
);
2569 out
: mdoc
->meta
.os_e
= strstr(mdoc
->meta
.os
, "OpenBSD") != NULL
?
2570 MDOC_OS_OPENBSD
: strstr(mdoc
->meta
.os
, "NetBSD") != NULL
?
2571 MDOC_OS_NETBSD
: MDOC_OS_OTHER
;
2575 mdoc_a2sec(const char *p
)
2579 for (i
= 0; i
< (int)SEC__MAX
; i
++)
2580 if (secnames
[i
] && 0 == strcmp(p
, secnames
[i
]))
2581 return (enum roff_sec
)i
;
2587 macro2len(enum roff_tok macro
)