1 /* $Id: mdoc_validate.c,v 1.332 2017/06/08 00:23:30 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 mandoc_vmsg(MANDOCERR_LB_BAD, mdoc->parse, n->child->line,
798 n->child->pos, "Lb %s", n->child->string);
800 roff_word_alloc(mdoc, n->line, n->pos, "library");
801 mdoc->last->flags = NODE_NOSRC;
802 roff_word_alloc(mdoc, n->line, n->pos, "\\(Lq");
803 mdoc->last->flags = NODE_DELIMO | NODE_NOSRC;
804 mdoc->last = mdoc->last->next;
805 roff_word_alloc(mdoc, n->line, n->pos, "\\(Rq");
806 mdoc->last->flags = NODE_DELIMC | NODE_NOSRC;
819 mdoc->next = ROFF_NEXT_CHILD;
820 if (n->child != NULL) {
821 roff_word_alloc(mdoc, n->line, n->pos, "The");
822 mdoc->last->flags |= NODE_NOSRC;
823 ic = build_list(mdoc, MDOC_Fn);
824 roff_word_alloc(mdoc, n->line, n->pos,
825 ic > 1 ? "functions return" : "function returns");
826 mdoc->last->flags |= NODE_NOSRC;
827 roff_word_alloc(mdoc, n->line, n->pos,
828 "the value\\~0 if successful;");
830 roff_word_alloc(mdoc, n->line, n->pos, "Upon successful "
831 "completion, the value\\~0 is returned;");
832 mdoc->last->flags |= NODE_NOSRC;
834 roff_word_alloc(mdoc, n->line, n->pos, "otherwise "
835 "the value\\~\\-1 is returned and the global variable");
836 mdoc->last->flags |= NODE_NOSRC;
837 roff_elem_alloc(mdoc, n->line, n->pos, MDOC_Va);
838 mdoc->last->flags |= NODE_NOSRC;
839 roff_word_alloc(mdoc, n->line, n->pos, "errno");
840 mdoc->last->flags |= NODE_NOSRC;
841 mdoc->last = mdoc->last->parent;
842 mdoc->next = ROFF_NEXT_SIBLING;
843 roff_word_alloc(mdoc, n->line, n->pos,
844 "is set to indicate the error.");
845 mdoc->last->flags |= NODE_EOS | NODE_NOSRC;
855 if (n->args && n->args->argc == 1)
856 if (n->args->argv[0].arg == MDOC_Std)
859 mandoc_msg(MANDOCERR_ARG_STD, mdoc->parse,
860 n->line, n->pos, roff_name[n->tok]);
866 struct roff_node *n, *nch;
871 assert(nch->type == ROFFT_TEXT);
873 if ((p = mdoc_a2st(nch->string)) == NULL) {
874 mandoc_vmsg(MANDOCERR_ST_BAD, mdoc->parse,
875 nch->line, nch->pos, "St %s", nch->string);
876 roff_node_delete(mdoc, n);
880 nch->flags |= NODE_NOPRT;
881 mdoc->next = ROFF_NEXT_CHILD;
882 roff_word_alloc(mdoc, nch->line, nch->pos, p);
883 mdoc->last->flags |= NODE_NOSRC;
888 post_obsolete(POST_ARGS)
893 if (n->type == ROFFT_ELEM || n->type == ROFFT_BLOCK)
894 mandoc_msg(MANDOCERR_MACRO_OBS, mdoc->parse,
895 n->line, n->pos, roff_name[n->tok]);
899 post_useless(POST_ARGS)
904 mandoc_msg(MANDOCERR_MACRO_USELESS, mdoc->parse,
905 n->line, n->pos, roff_name[n->tok]);
915 struct roff_node *np, *nch;
918 * Unlike other data pointers, these are "housed" by the HEAD
919 * element, which contains the goods.
923 if (np->type != ROFFT_HEAD)
926 assert(np->parent->type == ROFFT_BLOCK);
927 assert(np->parent->tok == MDOC_Bf);
929 /* Check the number of arguments. */
932 if (np->parent->args == NULL) {
934 mandoc_msg(MANDOCERR_BF_NOFONT, mdoc->parse,
935 np->line, np->pos, "Bf");
941 mandoc_vmsg(MANDOCERR_ARG_EXCESS, mdoc->parse,
942 nch->line, nch->pos, "Bf ... %s", nch->string);
944 /* Extract argument into data. */
946 if (np->parent->args != NULL) {
947 switch (np->parent->args->argv[0].arg) {
949 np->norm->Bf.font = FONT_Em;
952 np->norm->Bf.font = FONT_Li;
955 np->norm->Bf.font = FONT_Sy;
963 /* Extract parameter into data. */
965 if ( ! strcmp(np->child->string, "Em"))
966 np->norm->Bf.font = FONT_Em;
967 else if ( ! strcmp(np->child->string, "Li"))
968 np->norm->Bf.font = FONT_Li;
969 else if ( ! strcmp(np->child->string, "Sy"))
970 np->norm->Bf.font = FONT_Sy;
972 mandoc_vmsg(MANDOCERR_BF_BADFONT, mdoc->parse,
973 np->child->line, np->child->pos,
974 "Bf %s", np->child->string);
978 post_fname(POST_ARGS)
980 const struct roff_node *n;
984 n = mdoc->last->child;
985 pos = strcspn(n->string, "()");
986 cp = n->string + pos;
987 if ( ! (cp[0] == '\0' || (cp[0] == '(' && cp[1] == '*')))
988 mandoc_msg(MANDOCERR_FN_PAREN, mdoc->parse,
989 n->line, n->pos + pos, n->string);
1003 const struct roff_node *n;
1007 if (n->type != ROFFT_HEAD)
1010 if (n->child == NULL) {
1011 mandoc_msg(MANDOCERR_FO_NOHEAD, mdoc->parse,
1012 n->line, n->pos, "Fo");
1015 if (n->child != n->last) {
1016 mandoc_vmsg(MANDOCERR_ARG_EXCESS, mdoc->parse,
1017 n->child->next->line, n->child->next->pos,
1018 "Fo ... %s", n->child->next->string);
1019 while (n->child != n->last)
1020 roff_node_delete(mdoc, n->last);
1029 const struct roff_node *n;
1032 for (n = mdoc->last->child; n != NULL; n = n->next) {
1033 for (cp = n->string; *cp != '\0'; cp++) {
1034 /* Ignore callbacks and alterations. */
1035 if (*cp == '(' || *cp == '{')
1039 mandoc_msg(MANDOCERR_FA_COMMA, mdoc->parse,
1040 n->line, n->pos + (cp - n->string),
1050 struct roff_node *n;
1054 if (n->last != NULL &&
1055 (n->last->tok == MDOC_Pp ||
1056 n->last->tok == MDOC_Lp))
1057 mdoc_node_relink(mdoc, n->last);
1059 if (mdoc->meta.name == NULL)
1060 deroff(&mdoc->meta.name, n);
1062 if (mdoc->meta.name == NULL ||
1063 (mdoc->lastsec == SEC_NAME && n->child == NULL))
1064 mandoc_msg(MANDOCERR_NM_NONAME, mdoc->parse,
1065 n->line, n->pos, "Nm");
1067 if ((n->type != ROFFT_ELEM && n->type != ROFFT_HEAD) ||
1068 (n->child != NULL && n->child->type == ROFFT_TEXT) ||
1069 mdoc->meta.name == NULL)
1072 mdoc->next = ROFF_NEXT_CHILD;
1073 roff_word_alloc(mdoc, n->line, n->pos, mdoc->meta.name);
1074 mdoc->last->flags |= NODE_NOSRC;
1081 struct roff_node *n;
1086 if (n->type != ROFFT_BODY)
1089 if (n->sec != SEC_NAME)
1090 mandoc_msg(MANDOCERR_ND_LATE, mdoc->parse,
1091 n->line, n->pos, "Nd");
1093 if (n->child == NULL)
1094 mandoc_msg(MANDOCERR_ND_EMPTY, mdoc->parse,
1095 n->line, n->pos, "Nd");
1096 else if (n->last->type == ROFFT_TEXT &&
1097 (sz = strlen(n->last->string)) != 0 &&
1098 n->last->string[sz - 1] == '.')
1099 mandoc_msg(MANDOCERR_ND_DOT, mdoc->parse,
1100 n->last->line, n->last->pos + sz - 1, NULL);
1106 post_display(POST_ARGS)
1108 struct roff_node *n, *np;
1113 if (n->end != ENDBODY_NOT) {
1114 if (n->tok == MDOC_Bd &&
1115 n->body->parent->args == NULL)
1116 roff_node_delete(mdoc, n);
1117 } else if (n->child == NULL)
1118 mandoc_msg(MANDOCERR_BLK_EMPTY, mdoc->parse,
1119 n->line, n->pos, roff_name[n->tok]);
1120 else if (n->tok == MDOC_D1)
1124 if (n->tok == MDOC_Bd) {
1125 if (n->args == NULL) {
1126 mandoc_msg(MANDOCERR_BD_NOARG,
1127 mdoc->parse, n->line, n->pos, "Bd");
1128 mdoc->next = ROFF_NEXT_SIBLING;
1129 while (n->body->child != NULL)
1130 mdoc_node_relink(mdoc,
1132 roff_node_delete(mdoc, n);
1138 for (np = n->parent; np != NULL; np = np->parent) {
1139 if (np->type == ROFFT_BLOCK && np->tok == MDOC_Bd) {
1140 mandoc_vmsg(MANDOCERR_BD_NEST,
1141 mdoc->parse, n->line, n->pos,
1142 "%s in Bd", roff_name[n->tok]);
1153 post_defaults(POST_ARGS)
1155 struct roff_node *nn;
1158 * The `Ar' defaults to "file ..." if no value is provided as an
1159 * argument; the `Mt' and `Pa' macros use "~"; the `Li' just
1160 * gets an empty string.
1163 if (mdoc->last->child != NULL)
1170 mdoc->next = ROFF_NEXT_CHILD;
1171 roff_word_alloc(mdoc, nn->line, nn->pos, "file");
1172 mdoc->last->flags |= NODE_NOSRC;
1173 roff_word_alloc(mdoc, nn->line, nn->pos, "...");
1174 mdoc->last->flags |= NODE_NOSRC;
1178 mdoc->next = ROFF_NEXT_CHILD;
1179 roff_word_alloc(mdoc, nn->line, nn->pos, "~");
1180 mdoc->last->flags |= NODE_NOSRC;
1191 struct roff_node *n, *nch;
1198 * If we have a child, look it up in the standard keys. If a
1199 * key exist, use that instead of the child; if it doesn't,
1200 * prefix "AT&T UNIX " to the existing data.
1204 if (nch != NULL && ((att = mdoc_a2att(nch->string)) == NULL))
1205 mandoc_vmsg(MANDOCERR_AT_BAD, mdoc->parse,
1206 nch->line, nch->pos, "At %s", nch->string);
1208 mdoc->next = ROFF_NEXT_CHILD;
1210 roff_word_alloc(mdoc, nch->line, nch->pos, att);
1211 nch->flags |= NODE_NOPRT;
1213 roff_word_alloc(mdoc, n->line, n->pos, "AT&T UNIX");
1214 mdoc->last->flags |= NODE_NOSRC;
1221 struct roff_node *np, *nch;
1227 if (np->norm->An.auth == AUTH__NONE) {
1229 mandoc_msg(MANDOCERR_MACRO_EMPTY, mdoc->parse,
1230 np->line, np->pos, "An");
1231 } else if (nch != NULL)
1232 mandoc_vmsg(MANDOCERR_ARG_EXCESS, mdoc->parse,
1233 nch->line, nch->pos, "An ... %s", nch->string);
1240 post_obsolete(mdoc);
1241 if (mdoc->last->type == ROFFT_BLOCK)
1242 mdoc->last->norm->Es = mdoc->last_es;
1249 post_obsolete(mdoc);
1250 mdoc->last_es = mdoc->last;
1256 struct roff_node *n;
1282 mdoc->next = ROFF_NEXT_CHILD;
1283 roff_word_alloc(mdoc, n->line, n->pos, os);
1284 mdoc->last->flags |= NODE_NOSRC;
1291 struct roff_node *nbl, *nit, *nch;
1298 if (nit->type != ROFFT_BLOCK)
1301 nbl = nit->parent->parent;
1302 lt = nbl->norm->Bl.type;
1310 if (nit->head->child == NULL)
1311 mandoc_vmsg(MANDOCERR_IT_NOHEAD,
1312 mdoc->parse, nit->line, nit->pos,
1314 mdoc_argnames[nbl->args->argv[0].arg]);
1320 if (nit->body == NULL || nit->body->child == NULL)
1321 mandoc_vmsg(MANDOCERR_IT_NOBODY,
1322 mdoc->parse, nit->line, nit->pos,
1324 mdoc_argnames[nbl->args->argv[0].arg]);
1327 if ((nch = nit->head->child) != NULL)
1328 mandoc_vmsg(MANDOCERR_ARG_SKIP, mdoc->parse,
1329 nit->line, nit->pos, "It %s",
1330 nch->string == NULL ? roff_name[nch->tok] :
1334 cols = (int)nbl->norm->Bl.ncols;
1336 assert(nit->head->child == NULL);
1339 for (nch = nit->child; nch != NULL; nch = nch->next)
1340 if (nch->type == ROFFT_BODY)
1343 if (i < cols || i > cols + 1)
1344 mandoc_vmsg(MANDOCERR_BL_COL,
1345 mdoc->parse, nit->line, nit->pos,
1346 "%d columns, %d cells", cols, i);
1354 post_bl_block(POST_ARGS)
1356 struct roff_node *n, *ni, *nc;
1361 for (ni = n->body->child; ni != NULL; ni = ni->next) {
1362 if (ni->body == NULL)
1364 nc = ni->body->last;
1365 while (nc != NULL) {
1375 if (ni->next == NULL) {
1376 mandoc_msg(MANDOCERR_PAR_MOVE,
1377 mdoc->parse, nc->line, nc->pos,
1378 roff_name[nc->tok]);
1379 mdoc_node_relink(mdoc, nc);
1380 } else if (n->norm->Bl.comp == 0 &&
1381 n->norm->Bl.type != LIST_column) {
1382 mandoc_vmsg(MANDOCERR_PAR_SKIP,
1383 mdoc->parse, nc->line, nc->pos,
1384 "%s before It", roff_name[nc->tok]);
1385 roff_node_delete(mdoc, nc);
1388 nc = ni->body->last;
1394 * If the argument of -offset or -width is a macro,
1395 * replace it with the associated default width.
1398 rewrite_macro2len(struct roff_man *mdoc, char **arg)
1405 else if ( ! strcmp(*arg, "Ds"))
1407 else if ((tok = roffhash_find(mdoc->mdocmac, *arg, 0)) == TOKEN_NONE)
1410 width = macro2len(tok);
1413 mandoc_asprintf(arg, "%zun", width);
1417 post_bl_head(POST_ARGS)
1419 struct roff_node *nbl, *nh, *nch, *nnext;
1420 struct mdoc_argv *argv;
1426 if (nh->norm->Bl.type != LIST_column) {
1427 if ((nch = nh->child) == NULL)
1429 mandoc_vmsg(MANDOCERR_ARG_EXCESS, mdoc->parse,
1430 nch->line, nch->pos, "Bl ... %s", nch->string);
1431 while (nch != NULL) {
1432 roff_node_delete(mdoc, nch);
1439 * Append old-style lists, where the column width specifiers
1440 * trail as macro parameters, to the new-style ("normal-form")
1441 * lists where they're argument values following -column.
1444 if (nh->child == NULL)
1448 for (j = 0; j < (int)nbl->args->argc; j++)
1449 if (nbl->args->argv[j].arg == MDOC_Column)
1452 assert(j < (int)nbl->args->argc);
1455 * Accommodate for new-style groff column syntax. Shuffle the
1456 * child nodes, all of which must be TEXT, as arguments for the
1457 * column field. Then, delete the head children.
1460 argv = nbl->args->argv + j;
1462 for (nch = nh->child; nch != NULL; nch = nch->next)
1464 argv->value = mandoc_reallocarray(argv->value,
1465 argv->sz, sizeof(char *));
1467 nh->norm->Bl.ncols = argv->sz;
1468 nh->norm->Bl.cols = (void *)argv->value;
1470 for (nch = nh->child; nch != NULL; nch = nnext) {
1471 argv->value[i++] = nch->string;
1474 roff_node_delete(NULL, nch);
1482 struct roff_node *nparent, *nprev; /* of the Bl block */
1483 struct roff_node *nblock, *nbody; /* of the Bl */
1484 struct roff_node *nchild, *nnext; /* of the Bl body */
1485 const char *prev_Er;
1489 switch (nbody->type) {
1491 post_bl_block(mdoc);
1501 if (nbody->end != ENDBODY_NOT)
1504 nchild = nbody->child;
1505 if (nchild == NULL) {
1506 mandoc_msg(MANDOCERR_BLK_EMPTY, mdoc->parse,
1507 nbody->line, nbody->pos, "Bl");
1510 while (nchild != NULL) {
1511 nnext = nchild->next;
1512 if (nchild->tok == MDOC_It ||
1513 (nchild->tok == MDOC_Sm &&
1514 nnext != NULL && nnext->tok == MDOC_It)) {
1520 * In .Bl -column, the first rows may be implicit,
1521 * that is, they may not start with .It macros.
1522 * Such rows may be followed by nodes generated on the
1523 * roff level, for example .TS, which cannot be moved
1524 * out of the list. In that case, wrap such roff nodes
1525 * into an implicit row.
1528 if (nchild->prev != NULL) {
1529 mdoc->last = nchild;
1530 mdoc->next = ROFF_NEXT_SIBLING;
1531 roff_block_alloc(mdoc, nchild->line,
1532 nchild->pos, MDOC_It);
1533 roff_head_alloc(mdoc, nchild->line,
1534 nchild->pos, MDOC_It);
1535 mdoc->next = ROFF_NEXT_SIBLING;
1536 roff_body_alloc(mdoc, nchild->line,
1537 nchild->pos, MDOC_It);
1538 while (nchild->tok != MDOC_It) {
1539 mdoc_node_relink(mdoc, nchild);
1540 if ((nchild = nnext) == NULL)
1542 nnext = nchild->next;
1543 mdoc->next = ROFF_NEXT_SIBLING;
1549 mandoc_msg(MANDOCERR_BL_MOVE, mdoc->parse,
1550 nchild->line, nchild->pos, roff_name[nchild->tok]);
1553 * Move the node out of the Bl block.
1554 * First, collect all required node pointers.
1557 nblock = nbody->parent;
1558 nprev = nblock->prev;
1559 nparent = nblock->parent;
1562 * Unlink this child.
1565 nbody->child = nnext;
1572 * Relink this child.
1575 nchild->parent = nparent;
1576 nchild->prev = nprev;
1577 nchild->next = nblock;
1579 nblock->prev = nchild;
1581 nparent->child = nchild;
1583 nprev->next = nchild;
1588 if (mdoc->meta.os_e != MDOC_OS_NETBSD)
1592 for (nchild = nbody->child; nchild != NULL; nchild = nchild->next) {
1593 if (nchild->tok != MDOC_It)
1595 if ((nnext = nchild->head->child) == NULL)
1597 if (nnext->type == ROFFT_BLOCK)
1598 nnext = nnext->body->child;
1599 if (nnext == NULL || nnext->tok != MDOC_Er)
1601 nnext = nnext->child;
1602 if (prev_Er != NULL) {
1603 order = strcmp(prev_Er, nnext->string);
1605 mandoc_vmsg(MANDOCERR_ER_ORDER,
1606 mdoc->parse, nnext->line, nnext->pos,
1607 "Er %s %s", prev_Er, nnext->string);
1608 else if (order == 0)
1609 mandoc_vmsg(MANDOCERR_ER_REP,
1610 mdoc->parse, nnext->line, nnext->pos,
1613 prev_Er = nnext->string;
1620 struct roff_node *n;
1624 if (n->type == ROFFT_BLOCK && n->body->child == NULL) {
1625 mandoc_msg(MANDOCERR_BLK_EMPTY,
1626 mdoc->parse, n->line, n->pos, "Bk");
1627 roff_node_delete(mdoc, n);
1634 struct roff_node *nch;
1636 nch = mdoc->last->child;
1639 mdoc->flags ^= MDOC_SMOFF;
1643 assert(nch->type == ROFFT_TEXT);
1645 if ( ! strcmp(nch->string, "on")) {
1646 mdoc->flags &= ~MDOC_SMOFF;
1649 if ( ! strcmp(nch->string, "off")) {
1650 mdoc->flags |= MDOC_SMOFF;
1654 mandoc_vmsg(MANDOCERR_SM_BAD,
1655 mdoc->parse, nch->line, nch->pos,
1656 "%s %s", roff_name[mdoc->last->tok], nch->string);
1657 mdoc_node_relink(mdoc, nch);
1662 post_root(POST_ARGS)
1664 struct roff_node *n;
1666 /* Add missing prologue data. */
1668 if (mdoc->meta.date == NULL)
1669 mdoc->meta.date = mdoc->quick ?
1671 mandoc_normdate(mdoc->parse, NULL, 0, 0);
1673 if (mdoc->meta.title == NULL) {
1674 mandoc_msg(MANDOCERR_DT_NOTITLE,
1675 mdoc->parse, 0, 0, "EOF");
1676 mdoc->meta.title = mandoc_strdup("UNTITLED");
1679 if (mdoc->meta.vol == NULL)
1680 mdoc->meta.vol = mandoc_strdup("LOCAL");
1682 if (mdoc->meta.os == NULL) {
1683 mandoc_msg(MANDOCERR_OS_MISSING,
1684 mdoc->parse, 0, 0, NULL);
1685 mdoc->meta.os = mandoc_strdup("");
1688 /* Check that we begin with a proper `Sh'. */
1690 n = mdoc->first->child;
1691 while (n != NULL && n->tok != TOKEN_NONE &&
1692 mdoc_macros[n->tok].flags & MDOC_PROLOGUE)
1696 mandoc_msg(MANDOCERR_DOC_EMPTY, mdoc->parse, 0, 0, NULL);
1697 else if (n->tok != MDOC_Sh)
1698 mandoc_msg(MANDOCERR_SEC_BEFORE, mdoc->parse,
1699 n->line, n->pos, roff_name[n->tok]);
1705 struct roff_node *np, *nch, *next, *prev;
1710 if (np->type != ROFFT_BODY)
1713 if (np->child == NULL) {
1714 mandoc_msg(MANDOCERR_RS_EMPTY, mdoc->parse,
1715 np->line, np->pos, "Rs");
1720 * The full `Rs' block needs special handling to order the
1721 * sub-elements according to `rsord'. Pick through each element
1722 * and correctly order it. This is an insertion sort.
1726 for (nch = np->child->next; nch != NULL; nch = next) {
1727 /* Determine order number of this child. */
1728 for (i = 0; i < RSORD_MAX; i++)
1729 if (rsord[i] == nch->tok)
1732 if (i == RSORD_MAX) {
1733 mandoc_msg(MANDOCERR_RS_BAD, mdoc->parse,
1734 nch->line, nch->pos, roff_name[nch->tok]);
1736 } else if (nch->tok == MDOC__J || nch->tok == MDOC__B)
1737 np->norm->Rs.quote_T++;
1740 * Remove this child from the chain. This somewhat
1741 * repeats roff_node_unlink(), but since we're
1742 * just re-ordering, there's no need for the
1743 * full unlink process.
1746 if ((next = nch->next) != NULL)
1747 next->prev = nch->prev;
1749 if ((prev = nch->prev) != NULL)
1750 prev->next = nch->next;
1752 nch->prev = nch->next = NULL;
1755 * Scan back until we reach a node that's
1756 * to be ordered before this child.
1759 for ( ; prev ; prev = prev->prev) {
1760 /* Determine order of `prev'. */
1761 for (j = 0; j < RSORD_MAX; j++)
1762 if (rsord[j] == prev->tok)
1772 * Set this child back into its correct place
1773 * in front of the `prev' node.
1779 np->child->prev = nch;
1780 nch->next = np->child;
1784 prev->next->prev = nch;
1785 nch->next = prev->next;
1792 * For some arguments of some macros,
1793 * convert all breakable hyphens into ASCII_HYPH.
1796 post_hyph(POST_ARGS)
1798 struct roff_node *nch;
1801 for (nch = mdoc->last->child; nch != NULL; nch = nch->next) {
1802 if (nch->type != ROFFT_TEXT)
1807 while (*(++cp) != '\0')
1809 isalpha((unsigned char)cp[-1]) &&
1810 isalpha((unsigned char)cp[1]))
1819 if (mdoc->last->flags & NODE_LINE)
1820 mandoc_msg(MANDOCERR_NS_SKIP, mdoc->parse,
1821 mdoc->last->line, mdoc->last->pos, NULL);
1830 switch (mdoc->last->type) {
1835 switch (mdoc->lastsec) {
1840 post_sh_see_also(mdoc);
1843 post_sh_authors(mdoc);
1855 post_sh_name(POST_ARGS)
1857 struct roff_node *n;
1862 for (n = mdoc->last->child; n != NULL; n = n->next) {
1865 if (hasnm && n->child != NULL)
1866 mandoc_vmsg(MANDOCERR_NAMESEC_PUNCT,
1867 mdoc->parse, n->line, n->pos,
1868 "Nm %s", n->child->string);
1873 if (n->next != NULL)
1874 mandoc_msg(MANDOCERR_NAMESEC_ND,
1875 mdoc->parse, n->line, n->pos, NULL);
1878 if (n->type == ROFFT_TEXT &&
1879 n->string[0] == ',' && n->string[1] == '\0' &&
1880 n->next != NULL && n->next->tok == MDOC_Nm) {
1886 mandoc_msg(MANDOCERR_NAMESEC_BAD, mdoc->parse,
1887 n->line, n->pos, roff_name[n->tok]);
1894 mandoc_msg(MANDOCERR_NAMESEC_NONM, mdoc->parse,
1895 mdoc->last->line, mdoc->last->pos, NULL);
1897 mandoc_msg(MANDOCERR_NAMESEC_NOND, mdoc->parse,
1898 mdoc->last->line, mdoc->last->pos, NULL);
1902 post_sh_see_also(POST_ARGS)
1904 const struct roff_node *n;
1905 const char *name, *sec;
1906 const char *lastname, *lastsec, *lastpunct;
1909 n = mdoc->last->child;
1910 lastname = lastsec = lastpunct = NULL;
1912 if (n->tok != MDOC_Xr ||
1914 n->child->next == NULL)
1917 /* Process one .Xr node. */
1919 name = n->child->string;
1920 sec = n->child->next->string;
1921 if (lastsec != NULL) {
1922 if (lastpunct[0] != ',' || lastpunct[1] != '\0')
1923 mandoc_vmsg(MANDOCERR_XR_PUNCT,
1924 mdoc->parse, n->line, n->pos,
1925 "%s before %s(%s)", lastpunct,
1927 cmp = strcmp(lastsec, sec);
1929 mandoc_vmsg(MANDOCERR_XR_ORDER,
1930 mdoc->parse, n->line, n->pos,
1931 "%s(%s) after %s(%s)", name,
1932 sec, lastname, lastsec);
1933 else if (cmp == 0 &&
1934 strcasecmp(lastname, name) > 0)
1935 mandoc_vmsg(MANDOCERR_XR_ORDER,
1936 mdoc->parse, n->line, n->pos,
1937 "%s after %s", name, lastname);
1942 /* Process the following node. */
1947 if (n->tok == MDOC_Xr) {
1951 if (n->type != ROFFT_TEXT)
1953 for (name = n->string; *name != '\0'; name++)
1954 if (isalpha((const unsigned char)*name))
1956 lastpunct = n->string;
1957 if (n->next == NULL || n->next->tok == MDOC_Rs)
1958 mandoc_vmsg(MANDOCERR_XR_PUNCT, mdoc->parse,
1959 n->line, n->pos, "%s after %s(%s)",
1960 lastpunct, lastname, lastsec);
1966 child_an(const struct roff_node *n)
1969 for (n = n->child; n != NULL; n = n->next)
1970 if ((n->tok == MDOC_An && n->child != NULL) || child_an(n))
1976 post_sh_authors(POST_ARGS)
1979 if ( ! child_an(mdoc->last))
1980 mandoc_msg(MANDOCERR_AN_MISSING, mdoc->parse,
1981 mdoc->last->line, mdoc->last->pos, NULL);
1985 post_sh_head(POST_ARGS)
1987 struct roff_node *nch;
1988 const char *goodsec;
1992 * Process a new section. Sections are either "named" or
1993 * "custom". Custom sections are user-defined, while named ones
1994 * follow a conventional order and may only appear in certain
1998 sec = mdoc->last->sec;
2000 /* The NAME should be first. */
2002 if (sec != SEC_NAME && mdoc->lastnamed == SEC_NONE)
2003 mandoc_vmsg(MANDOCERR_NAMESEC_FIRST, mdoc->parse,
2004 mdoc->last->line, mdoc->last->pos, "Sh %s",
2005 sec != SEC_CUSTOM ? secnames[sec] :
2006 (nch = mdoc->last->child) == NULL ? "" :
2007 nch->type == ROFFT_TEXT ? nch->string :
2008 roff_name[nch->tok]);
2010 /* The SYNOPSIS gets special attention in other areas. */
2012 if (sec == SEC_SYNOPSIS) {
2013 roff_setreg(mdoc->roff, "nS", 1, '=');
2014 mdoc->flags |= MDOC_SYNOPSIS;
2016 roff_setreg(mdoc->roff, "nS", 0, '=');
2017 mdoc->flags &= ~MDOC_SYNOPSIS;
2020 /* Mark our last section. */
2022 mdoc->lastsec = sec;
2024 /* We don't care about custom sections after this. */
2026 if (sec == SEC_CUSTOM)
2030 * Check whether our non-custom section is being repeated or is
2034 if (sec == mdoc->lastnamed)
2035 mandoc_vmsg(MANDOCERR_SEC_REP, mdoc->parse,
2036 mdoc->last->line, mdoc->last->pos,
2037 "Sh %s", secnames[sec]);
2039 if (sec < mdoc->lastnamed)
2040 mandoc_vmsg(MANDOCERR_SEC_ORDER, mdoc->parse,
2041 mdoc->last->line, mdoc->last->pos,
2042 "Sh %s", secnames[sec]);
2044 /* Mark the last named section. */
2046 mdoc->lastnamed = sec;
2048 /* Check particular section/manual conventions. */
2050 if (mdoc->meta.msec == NULL)
2056 if (*mdoc->meta.msec == '4')
2058 goodsec = "2, 3, 4, 9";
2060 case SEC_RETURN_VALUES:
2062 if (*mdoc->meta.msec == '2')
2064 if (*mdoc->meta.msec == '3')
2066 if (NULL == goodsec)
2067 goodsec = "2, 3, 9";
2070 if (*mdoc->meta.msec == '9')
2072 if (NULL == goodsec)
2074 mandoc_vmsg(MANDOCERR_SEC_MSEC, mdoc->parse,
2075 mdoc->last->line, mdoc->last->pos,
2076 "Sh %s for %s only", secnames[sec], goodsec);
2086 struct roff_node *n, *nch;
2090 if (nch->next == NULL) {
2091 mandoc_vmsg(MANDOCERR_XR_NOSEC, mdoc->parse,
2092 n->line, n->pos, "Xr %s", nch->string);
2095 assert(nch->next == n->last);
2099 post_ignpar(POST_ARGS)
2101 struct roff_node *np;
2103 switch (mdoc->last->type) {
2116 if ((np = mdoc->last->child) != NULL)
2117 if (np->tok == MDOC_Pp || np->tok == MDOC_Lp) {
2118 mandoc_vmsg(MANDOCERR_PAR_SKIP,
2119 mdoc->parse, np->line, np->pos,
2120 "%s after %s", roff_name[np->tok],
2121 roff_name[mdoc->last->tok]);
2122 roff_node_delete(mdoc, np);
2125 if ((np = mdoc->last->last) != NULL)
2126 if (np->tok == MDOC_Pp || np->tok == MDOC_Lp) {
2127 mandoc_vmsg(MANDOCERR_PAR_SKIP, mdoc->parse,
2128 np->line, np->pos, "%s at the end of %s",
2130 roff_name[mdoc->last->tok]);
2131 roff_node_delete(mdoc, np);
2136 post_prevpar(POST_ARGS)
2138 struct roff_node *n;
2141 if (NULL == n->prev)
2143 if (n->type != ROFFT_ELEM && n->type != ROFFT_BLOCK)
2147 * Don't allow prior `Lp' or `Pp' prior to a paragraph-type
2148 * block: `Lp', `Pp', or non-compact `Bd' or `Bl'.
2151 if (n->prev->tok != MDOC_Pp &&
2152 n->prev->tok != MDOC_Lp &&
2153 n->prev->tok != ROFF_br)
2155 if (n->tok == MDOC_Bl && n->norm->Bl.comp)
2157 if (n->tok == MDOC_Bd && n->norm->Bd.comp)
2159 if (n->tok == MDOC_It && n->parent->norm->Bl.comp)
2162 mandoc_vmsg(MANDOCERR_PAR_SKIP, mdoc->parse,
2163 n->prev->line, n->prev->pos, "%s before %s",
2164 roff_name[n->prev->tok], roff_name[n->tok]);
2165 roff_node_delete(mdoc, n->prev);
2171 struct roff_node *np;
2174 if (np->tok != ROFF_br && np->tok != ROFF_sp)
2177 if (np->tok == ROFF_sp) {
2178 if (np->child != NULL && np->child->next != NULL)
2179 mandoc_vmsg(MANDOCERR_ARG_EXCESS, mdoc->parse,
2180 np->child->next->line, np->child->next->pos,
2181 "sp ... %s", np->child->next->string);
2182 } else if (np->child != NULL)
2183 mandoc_vmsg(MANDOCERR_ARG_SKIP,
2184 mdoc->parse, np->line, np->pos, "%s %s",
2185 roff_name[np->tok], np->child->string);
2187 if ((np = mdoc->last->prev) == NULL) {
2188 np = mdoc->last->parent;
2189 if (np->tok != MDOC_Sh && np->tok != MDOC_Ss)
2191 } else if (np->tok != MDOC_Pp && np->tok != MDOC_Lp &&
2192 (mdoc->last->tok != ROFF_br ||
2193 (np->tok != ROFF_sp && np->tok != ROFF_br)))
2196 mandoc_vmsg(MANDOCERR_PAR_SKIP, mdoc->parse,
2197 mdoc->last->line, mdoc->last->pos, "%s after %s",
2198 roff_name[mdoc->last->tok], roff_name[np->tok]);
2199 roff_node_delete(mdoc, mdoc->last);
2205 struct roff_node *n;
2209 n->flags |= NODE_NOPRT;
2211 if (mdoc->meta.date != NULL) {
2212 mandoc_msg(MANDOCERR_PROLOG_REP, mdoc->parse,
2213 n->line, n->pos, "Dd");
2214 free(mdoc->meta.date);
2215 } else if (mdoc->flags & MDOC_PBODY)
2216 mandoc_msg(MANDOCERR_PROLOG_LATE, mdoc->parse,
2217 n->line, n->pos, "Dd");
2218 else if (mdoc->meta.title != NULL)
2219 mandoc_msg(MANDOCERR_PROLOG_ORDER, mdoc->parse,
2220 n->line, n->pos, "Dd after Dt");
2221 else if (mdoc->meta.os != NULL)
2222 mandoc_msg(MANDOCERR_PROLOG_ORDER, mdoc->parse,
2223 n->line, n->pos, "Dd after Os");
2225 if (n->child == NULL || n->child->string[0] == '\0') {
2226 mdoc->meta.date = mdoc->quick ? mandoc_strdup("") :
2227 mandoc_normdate(mdoc->parse, NULL, n->line, n->pos);
2232 deroff(&datestr, n);
2234 mdoc->meta.date = datestr;
2236 mdoc->meta.date = mandoc_normdate(mdoc->parse,
2237 datestr, n->line, n->pos);
2245 struct roff_node *nn, *n;
2250 n->flags |= NODE_NOPRT;
2252 if (mdoc->flags & MDOC_PBODY) {
2253 mandoc_msg(MANDOCERR_DT_LATE, mdoc->parse,
2254 n->line, n->pos, "Dt");
2258 if (mdoc->meta.title != NULL)
2259 mandoc_msg(MANDOCERR_PROLOG_REP, mdoc->parse,
2260 n->line, n->pos, "Dt");
2261 else if (mdoc->meta.os != NULL)
2262 mandoc_msg(MANDOCERR_PROLOG_ORDER, mdoc->parse,
2263 n->line, n->pos, "Dt after Os");
2265 free(mdoc->meta.title);
2266 free(mdoc->meta.msec);
2267 free(mdoc->meta.vol);
2268 free(mdoc->meta.arch);
2270 mdoc->meta.title = NULL;
2271 mdoc->meta.msec = NULL;
2272 mdoc->meta.vol = NULL;
2273 mdoc->meta.arch = NULL;
2275 /* Mandatory first argument: title. */
2278 if (nn == NULL || *nn->string == '\0') {
2279 mandoc_msg(MANDOCERR_DT_NOTITLE,
2280 mdoc->parse, n->line, n->pos, "Dt");
2281 mdoc->meta.title = mandoc_strdup("UNTITLED");
2283 mdoc->meta.title = mandoc_strdup(nn->string);
2285 /* Check that all characters are uppercase. */
2287 for (p = nn->string; *p != '\0'; p++)
2288 if (islower((unsigned char)*p)) {
2289 mandoc_vmsg(MANDOCERR_TITLE_CASE,
2290 mdoc->parse, nn->line,
2291 nn->pos + (p - nn->string),
2292 "Dt %s", nn->string);
2297 /* Mandatory second argument: section. */
2303 mandoc_vmsg(MANDOCERR_MSEC_MISSING,
2304 mdoc->parse, n->line, n->pos,
2305 "Dt %s", mdoc->meta.title);
2306 mdoc->meta.vol = mandoc_strdup("LOCAL");
2307 return; /* msec and arch remain NULL. */
2310 mdoc->meta.msec = mandoc_strdup(nn->string);
2312 /* Infer volume title from section number. */
2314 cp = mandoc_a2msec(nn->string);
2316 mandoc_vmsg(MANDOCERR_MSEC_BAD, mdoc->parse,
2317 nn->line, nn->pos, "Dt ... %s", nn->string);
2318 mdoc->meta.vol = mandoc_strdup(nn->string);
2320 mdoc->meta.vol = mandoc_strdup(cp);
2322 /* Optional third argument: architecture. */
2324 if ((nn = nn->next) == NULL)
2327 for (p = nn->string; *p != '\0'; p++)
2328 *p = tolower((unsigned char)*p);
2329 mdoc->meta.arch = mandoc_strdup(nn->string);
2331 /* Ignore fourth and later arguments. */
2333 if ((nn = nn->next) != NULL)
2334 mandoc_vmsg(MANDOCERR_ARG_EXCESS, mdoc->parse,
2335 nn->line, nn->pos, "Dt ... %s", nn->string);
2341 struct roff_node *n, *nch;
2348 macro = !strcmp(nch->string, "Open") ? "Ox" :
2349 !strcmp(nch->string, "Net") ? "Nx" :
2350 !strcmp(nch->string, "Free") ? "Fx" :
2351 !strcmp(nch->string, "DragonFly") ? "Dx" : NULL;
2353 mandoc_msg(MANDOCERR_BX, mdoc->parse,
2354 n->line, n->pos, macro);
2357 mdoc->next = ROFF_NEXT_SIBLING;
2358 roff_elem_alloc(mdoc, n->line, n->pos, MDOC_Ns);
2359 mdoc->last->flags |= NODE_NOSRC;
2360 mdoc->next = ROFF_NEXT_SIBLING;
2362 mdoc->next = ROFF_NEXT_CHILD;
2363 roff_word_alloc(mdoc, n->line, n->pos, "BSD");
2364 mdoc->last->flags |= NODE_NOSRC;
2371 roff_elem_alloc(mdoc, n->line, n->pos, MDOC_Ns);
2372 mdoc->last->flags |= NODE_NOSRC;
2373 mdoc->next = ROFF_NEXT_SIBLING;
2374 roff_word_alloc(mdoc, n->line, n->pos, "-");
2375 mdoc->last->flags |= NODE_NOSRC;
2376 roff_elem_alloc(mdoc, n->line, n->pos, MDOC_Ns);
2377 mdoc->last->flags |= NODE_NOSRC;
2381 * Make `Bx's second argument always start with an uppercase
2382 * letter. Groff checks if it's an "accepted" term, but we just
2383 * uppercase blindly.
2386 *nch->string = (char)toupper((unsigned char)*nch->string);
2393 struct utsname utsname;
2394 static char *defbuf;
2396 struct roff_node *n;
2399 n->flags |= NODE_NOPRT;
2401 if (mdoc->meta.os != NULL)
2402 mandoc_msg(MANDOCERR_PROLOG_REP, mdoc->parse,
2403 n->line, n->pos, "Os");
2404 else if (mdoc->flags & MDOC_PBODY)
2405 mandoc_msg(MANDOCERR_PROLOG_LATE, mdoc->parse,
2406 n->line, n->pos, "Os");
2409 * Set the operating system by way of the `Os' macro.
2410 * The order of precedence is:
2411 * 1. the argument of the `Os' macro, unless empty
2412 * 2. the -Ios=foo command line argument, if provided
2413 * 3. -DOSNAME="\"foo\"", if provided during compilation
2414 * 4. "sysname release" from uname(3)
2417 free(mdoc->meta.os);
2418 mdoc->meta.os = NULL;
2419 deroff(&mdoc->meta.os, n);
2424 mdoc->meta.os = mandoc_strdup(mdoc->defos);
2429 mdoc->meta.os = mandoc_strdup(OSNAME);
2431 if (defbuf == NULL) {
2432 if (uname(&utsname) == -1) {
2433 mandoc_msg(MANDOCERR_OS_UNAME, mdoc->parse,
2434 n->line, n->pos, "Os");
2435 defbuf = mandoc_strdup("UNKNOWN");
2437 mandoc_asprintf(&defbuf, "%s %s",
2438 utsname.sysname, utsname.release);
2440 mdoc->meta.os = mandoc_strdup(defbuf);
2443 out: mdoc->meta.os_e = strstr(mdoc->meta.os, "OpenBSD") != NULL ?
2444 MDOC_OS_OPENBSD : strstr(mdoc->meta.os, "NetBSD") != NULL ?
2445 MDOC_OS_NETBSD : MDOC_OS_OTHER;
2449 mdoc_a2sec(const char *p)
2453 for (i = 0; i < (int)SEC__MAX; i++)
2454 if (secnames[i] && 0 == strcmp(p, secnames[i]))
2455 return (enum roff_sec)i;
2461 macro2len(enum roff_tok macro)