1 /* $Id: mdoc_validate.c,v 1.151 2011/01/03 23:53:51 schwarze Exp $ */
3 * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2010, 2011 Ingo Schwarze <schwarze@openbsd.org>
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23 #include <sys/utsname.h>
26 #include <sys/types.h>
38 #include "libmandoc.h"
40 /* FIXME: .Bl -diag can't have non-text children in HEAD. */
42 #define PRE_ARGS struct mdoc *mdoc, struct mdoc_node *n
43 #define POST_ARGS struct mdoc *mdoc
59 typedef int (*v_pre)(PRE_ARGS);
60 typedef int (*v_post)(POST_ARGS);
67 static int check_count(struct mdoc *, enum mdoc_type,
68 enum check_lvl, enum check_ineq, int);
69 static int check_parent(PRE_ARGS, enum mdoct, enum mdoc_type);
70 static void check_text(struct mdoc *, int, int, char *);
71 static void check_argv(struct mdoc *,
72 struct mdoc_node *, struct mdoc_argv *);
73 static void check_args(struct mdoc *, struct mdoc_node *);
75 static int concat(struct mdoc *, char *,
76 const struct mdoc_node *, size_t);
78 static int ebool(POST_ARGS);
79 static int berr_ge1(POST_ARGS);
80 static int bwarn_ge1(POST_ARGS);
81 static int eerr_ge1(POST_ARGS);
82 static int ewarn_eq0(POST_ARGS);
83 static int ewarn_eq1(POST_ARGS);
84 static int ewarn_ge1(POST_ARGS);
85 static int ewarn_le1(POST_ARGS);
86 static int hwarn_eq0(POST_ARGS);
87 static int hwarn_eq1(POST_ARGS);
88 static int hwarn_ge1(POST_ARGS);
89 static int hwarn_le1(POST_ARGS);
91 static int post_an(POST_ARGS);
92 static int post_at(POST_ARGS);
93 static int post_bf(POST_ARGS);
94 static int post_bl(POST_ARGS);
95 static int post_bl_block(POST_ARGS);
96 static int post_bl_block_width(POST_ARGS);
97 static int post_bl_block_tag(POST_ARGS);
98 static int post_bl_head(POST_ARGS);
99 static int post_dd(POST_ARGS);
100 static int post_dt(POST_ARGS);
101 static int post_defaults(POST_ARGS);
102 static int post_literal(POST_ARGS);
103 static int post_eoln(POST_ARGS);
104 static int post_it(POST_ARGS);
105 static int post_lb(POST_ARGS);
106 static int post_nm(POST_ARGS);
107 static int post_os(POST_ARGS);
108 static int post_ignpar(POST_ARGS);
109 static int post_prol(POST_ARGS);
110 static int post_root(POST_ARGS);
111 static int post_rs(POST_ARGS);
112 static int post_sh(POST_ARGS);
113 static int post_sh_body(POST_ARGS);
114 static int post_sh_head(POST_ARGS);
115 static int post_st(POST_ARGS);
116 static int post_std(POST_ARGS);
117 static int post_vt(POST_ARGS);
118 static int pre_an(PRE_ARGS);
119 static int pre_bd(PRE_ARGS);
120 static int pre_bl(PRE_ARGS);
121 static int pre_dd(PRE_ARGS);
122 static int pre_display(PRE_ARGS);
123 static int pre_dt(PRE_ARGS);
124 static int pre_it(PRE_ARGS);
125 static int pre_literal(PRE_ARGS);
126 static int pre_os(PRE_ARGS);
127 static int pre_par(PRE_ARGS);
128 static int pre_sh(PRE_ARGS);
129 static int pre_ss(PRE_ARGS);
130 static int pre_std(PRE_ARGS);
132 static v_post posts_an[] = { post_an, NULL };
133 static v_post posts_at[] = { post_at, post_defaults, NULL };
134 static v_post posts_bd[] = { post_literal, hwarn_eq0, bwarn_ge1, NULL };
135 static v_post posts_bf[] = { hwarn_le1, post_bf, NULL };
136 static v_post posts_bk[] = { hwarn_eq0, bwarn_ge1, NULL };
137 static v_post posts_bl[] = { bwarn_ge1, post_bl, NULL };
138 static v_post posts_bool[] = { ebool, NULL };
139 static v_post posts_eoln[] = { post_eoln, NULL };
140 static v_post posts_defaults[] = { post_defaults, NULL };
141 static v_post posts_dd[] = { ewarn_ge1, post_dd, post_prol, NULL };
142 static v_post posts_dl[] = { post_literal, bwarn_ge1, NULL };
143 static v_post posts_dt[] = { post_dt, post_prol, NULL };
144 static v_post posts_fo[] = { hwarn_eq1, bwarn_ge1, NULL };
145 static v_post posts_it[] = { post_it, NULL };
146 static v_post posts_lb[] = { post_lb, NULL };
147 static v_post posts_nd[] = { berr_ge1, NULL };
148 static v_post posts_nm[] = { post_nm, NULL };
149 static v_post posts_notext[] = { ewarn_eq0, NULL };
150 static v_post posts_os[] = { post_os, post_prol, NULL };
151 static v_post posts_rs[] = { post_rs, NULL };
152 static v_post posts_sh[] = { post_ignpar, hwarn_ge1, bwarn_ge1, post_sh, NULL };
153 static v_post posts_sp[] = { ewarn_le1, NULL };
154 static v_post posts_ss[] = { post_ignpar, hwarn_ge1, bwarn_ge1, NULL };
155 static v_post posts_st[] = { post_st, NULL };
156 static v_post posts_std[] = { post_std, NULL };
157 static v_post posts_text[] = { eerr_ge1, NULL };
158 static v_post posts_text1[] = { ewarn_eq1, NULL };
159 static v_post posts_vt[] = { post_vt, NULL };
160 static v_post posts_wline[] = { bwarn_ge1, NULL };
161 static v_post posts_wtext[] = { ewarn_ge1, NULL };
162 static v_pre pres_an[] = { pre_an, NULL };
163 static v_pre pres_bd[] = { pre_display, pre_bd, pre_literal, pre_par, NULL };
164 static v_pre pres_bl[] = { pre_bl, pre_par, NULL };
165 static v_pre pres_d1[] = { pre_display, NULL };
166 static v_pre pres_dl[] = { pre_literal, pre_display, NULL };
167 static v_pre pres_dd[] = { pre_dd, NULL };
168 static v_pre pres_dt[] = { pre_dt, NULL };
169 static v_pre pres_er[] = { NULL, NULL };
170 static v_pre pres_fd[] = { NULL, NULL };
171 static v_pre pres_it[] = { pre_it, pre_par, NULL };
172 static v_pre pres_os[] = { pre_os, NULL };
173 static v_pre pres_pp[] = { pre_par, NULL };
174 static v_pre pres_sh[] = { pre_sh, NULL };
175 static v_pre pres_ss[] = { pre_ss, NULL };
176 static v_pre pres_std[] = { pre_std, NULL };
178 const struct valids mdoc_valids[MDOC_MAX] = {
179 { NULL, NULL }, /* Ap */
180 { pres_dd, posts_dd }, /* Dd */
181 { pres_dt, posts_dt }, /* Dt */
182 { pres_os, posts_os }, /* Os */
183 { pres_sh, posts_sh }, /* Sh */
184 { pres_ss, posts_ss }, /* Ss */
185 { pres_pp, posts_notext }, /* Pp */
186 { pres_d1, posts_wline }, /* D1 */
187 { pres_dl, posts_dl }, /* Dl */
188 { pres_bd, posts_bd }, /* Bd */
189 { NULL, NULL }, /* Ed */
190 { pres_bl, posts_bl }, /* Bl */
191 { NULL, NULL }, /* El */
192 { pres_it, posts_it }, /* It */
193 { NULL, posts_text }, /* Ad */
194 { pres_an, posts_an }, /* An */
195 { NULL, posts_defaults }, /* Ar */
196 { NULL, posts_text }, /* Cd */
197 { NULL, NULL }, /* Cm */
198 { NULL, NULL }, /* Dv */
199 { pres_er, posts_text }, /* Er */
200 { NULL, NULL }, /* Ev */
201 { pres_std, posts_std }, /* Ex */
202 { NULL, NULL }, /* Fa */
203 { pres_fd, posts_wtext }, /* Fd */
204 { NULL, NULL }, /* Fl */
205 { NULL, posts_text }, /* Fn */
206 { NULL, posts_wtext }, /* Ft */
207 { NULL, posts_text }, /* Ic */
208 { NULL, posts_text1 }, /* In */
209 { NULL, posts_defaults }, /* Li */
210 { NULL, posts_nd }, /* Nd */
211 { NULL, posts_nm }, /* Nm */
212 { NULL, NULL }, /* Op */
213 { NULL, NULL }, /* Ot */
214 { NULL, posts_defaults }, /* Pa */
215 { pres_std, posts_std }, /* Rv */
216 { NULL, posts_st }, /* St */
217 { NULL, NULL }, /* Va */
218 { NULL, posts_vt }, /* Vt */
219 { NULL, posts_wtext }, /* Xr */
220 { NULL, posts_text }, /* %A */
221 { NULL, posts_text }, /* %B */ /* FIXME: can be used outside Rs/Re. */
222 { NULL, posts_text }, /* %D */ /* FIXME: check date with mandoc_a2time(). */
223 { NULL, posts_text }, /* %I */
224 { NULL, posts_text }, /* %J */
225 { NULL, posts_text }, /* %N */
226 { NULL, posts_text }, /* %O */
227 { NULL, posts_text }, /* %P */
228 { NULL, posts_text }, /* %R */
229 { NULL, posts_text }, /* %T */ /* FIXME: can be used outside Rs/Re. */
230 { NULL, posts_text }, /* %V */
231 { NULL, NULL }, /* Ac */
232 { NULL, NULL }, /* Ao */
233 { NULL, NULL }, /* Aq */
234 { NULL, posts_at }, /* At */
235 { NULL, NULL }, /* Bc */
236 { NULL, posts_bf }, /* Bf */
237 { NULL, NULL }, /* Bo */
238 { NULL, NULL }, /* Bq */
239 { NULL, NULL }, /* Bsx */
240 { NULL, NULL }, /* Bx */
241 { NULL, posts_bool }, /* Db */
242 { NULL, NULL }, /* Dc */
243 { NULL, NULL }, /* Do */
244 { NULL, NULL }, /* Dq */
245 { NULL, NULL }, /* Ec */
246 { NULL, NULL }, /* Ef */
247 { NULL, NULL }, /* Em */
248 { NULL, NULL }, /* Eo */
249 { NULL, NULL }, /* Fx */
250 { NULL, posts_text }, /* Ms */
251 { NULL, posts_notext }, /* No */
252 { NULL, posts_notext }, /* Ns */
253 { NULL, NULL }, /* Nx */
254 { NULL, NULL }, /* Ox */
255 { NULL, NULL }, /* Pc */
256 { NULL, posts_text1 }, /* Pf */
257 { NULL, NULL }, /* Po */
258 { NULL, NULL }, /* Pq */
259 { NULL, NULL }, /* Qc */
260 { NULL, NULL }, /* Ql */
261 { NULL, NULL }, /* Qo */
262 { NULL, NULL }, /* Qq */
263 { NULL, NULL }, /* Re */
264 { NULL, posts_rs }, /* Rs */
265 { NULL, NULL }, /* Sc */
266 { NULL, NULL }, /* So */
267 { NULL, NULL }, /* Sq */
268 { NULL, posts_bool }, /* Sm */
269 { NULL, posts_text }, /* Sx */
270 { NULL, posts_text }, /* Sy */
271 { NULL, posts_text }, /* Tn */
272 { NULL, NULL }, /* Ux */
273 { NULL, NULL }, /* Xc */
274 { NULL, NULL }, /* Xo */
275 { NULL, posts_fo }, /* Fo */
276 { NULL, NULL }, /* Fc */
277 { NULL, NULL }, /* Oo */
278 { NULL, NULL }, /* Oc */
279 { NULL, posts_bk }, /* Bk */
280 { NULL, NULL }, /* Ek */
281 { NULL, posts_eoln }, /* Bt */
282 { NULL, NULL }, /* Hf */
283 { NULL, NULL }, /* Fr */
284 { NULL, posts_eoln }, /* Ud */
285 { NULL, posts_lb }, /* Lb */
286 { NULL, posts_notext }, /* Lp */
287 { NULL, posts_text }, /* Lk */
288 { NULL, posts_defaults }, /* Mt */
289 { NULL, NULL }, /* Brq */
290 { NULL, NULL }, /* Bro */
291 { NULL, NULL }, /* Brc */
292 { NULL, posts_text }, /* %C */
293 { NULL, NULL }, /* Es */
294 { NULL, NULL }, /* En */
295 { NULL, NULL }, /* Dx */
296 { NULL, posts_text }, /* %Q */
297 { NULL, posts_notext }, /* br */
298 { pres_pp, posts_sp }, /* sp */
299 { NULL, posts_text1 }, /* %U */
300 { NULL, NULL }, /* Ta */
303 #define RSORD_MAX 14 /* Number of `Rs' blocks. */
305 static const enum mdoct rsord[RSORD_MAX] = {
324 mdoc_valid_pre(struct mdoc *mdoc, struct mdoc_node *n)
335 check_text(mdoc, line, pos, tp);
347 if (NULL == mdoc_valids[n->tok].pre)
349 for (p = mdoc_valids[n->tok].pre; *p; p++)
350 if ( ! (*p)(mdoc, n))
357 mdoc_valid_post(struct mdoc *mdoc)
361 if (MDOC_VALID & mdoc->last->flags)
363 mdoc->last->flags |= MDOC_VALID;
365 switch (mdoc->last->type) {
371 return(post_root(mdoc));
376 if (NULL == mdoc_valids[mdoc->last->tok].post)
378 for (p = mdoc_valids[mdoc->last->tok].post; *p; p++)
386 check_count(struct mdoc *m, enum mdoc_type type,
387 enum check_lvl lvl, enum check_ineq ineq, int val)
392 if (m->last->type != type)
398 if (m->last->nchild < val)
403 if (m->last->nchild > val)
408 if (val == m->last->nchild)
416 t = lvl == CHECK_WARN ? MANDOCERR_ARGCWARN : MANDOCERR_ARGCOUNT;
418 return(mdoc_vmsg(m, t, m->last->line, m->last->pos,
419 "want %s%d children (have %d)",
420 p, val, m->last->nchild));
427 return(check_count(mdoc, MDOC_BODY, CHECK_ERROR, CHECK_GT, 0));
433 return(check_count(mdoc, MDOC_BODY, CHECK_WARN, CHECK_GT, 0));
439 return(check_count(mdoc, MDOC_ELEM, CHECK_ERROR, CHECK_GT, 0));
445 return(check_count(mdoc, MDOC_ELEM, CHECK_WARN, CHECK_EQ, 0));
451 return(check_count(mdoc, MDOC_ELEM, CHECK_WARN, CHECK_EQ, 1));
457 return(check_count(mdoc, MDOC_ELEM, CHECK_WARN, CHECK_GT, 0));
463 return(check_count(mdoc, MDOC_ELEM, CHECK_WARN, CHECK_LT, 2));
469 return(check_count(mdoc, MDOC_HEAD, CHECK_WARN, CHECK_EQ, 0));
475 return(check_count(mdoc, MDOC_HEAD, CHECK_WARN, CHECK_EQ, 1));
481 return(check_count(mdoc, MDOC_HEAD, CHECK_WARN, CHECK_GT, 0));
487 return(check_count(mdoc, MDOC_HEAD, CHECK_WARN, CHECK_LT, 2));
491 check_args(struct mdoc *m, struct mdoc_node *n)
498 assert(n->args->argc);
499 for (i = 0; i < (int)n->args->argc; i++)
500 check_argv(m, n, &n->args->argv[i]);
504 check_argv(struct mdoc *m, struct mdoc_node *n, struct mdoc_argv *v)
508 for (i = 0; i < (int)v->sz; i++)
509 check_text(m, v->line, v->pos, v->value[i]);
511 /* FIXME: move to post_std(). */
513 if (MDOC_Std == v->arg)
514 if ( ! (v->sz || m->meta.name))
515 mdoc_nmsg(m, n, MANDOCERR_NONAME);
519 check_text(struct mdoc *m, int ln, int pos, char *p)
524 for ( ; *p; p++, pos++) {
525 sz = strcspn(p, "\t\\");
534 if ( ! (MDOC_LITERAL & m->flags))
535 mdoc_pmsg(m, ln, pos, MANDOCERR_BADTAB);
539 if (0 == (c = mandoc_special(p))) {
540 mdoc_pmsg(m, ln, pos, MANDOCERR_BADESCAPE);
550 check_parent(PRE_ARGS, enum mdoct tok, enum mdoc_type t)
554 if ((MDOC_ROOT == t || tok == n->parent->tok) &&
555 (t == n->parent->type))
558 mdoc_vmsg(mdoc, MANDOCERR_SYNTCHILD,
559 n->line, n->pos, "want parent %s",
560 MDOC_ROOT == t ? "<root>" :
561 mdoc_macronames[tok]);
567 pre_display(PRE_ARGS)
569 struct mdoc_node *node;
571 if (MDOC_BLOCK != n->type)
574 for (node = mdoc->last->parent; node; node = node->parent)
575 if (MDOC_BLOCK == node->type)
576 if (MDOC_Bd == node->tok)
580 mdoc_nmsg(mdoc, n, MANDOCERR_NESTEDDISP);
590 const char *offs, *width;
592 struct mdoc_node *np;
594 if (MDOC_BLOCK != n->type) {
595 if (ENDBODY_NOT != n->end) {
597 np = n->pending->parent;
602 assert(MDOC_BLOCK == np->type);
603 assert(MDOC_Bl == np->tok);
608 * First figure out which kind of list to use: bind ourselves to
609 * the first mentioned list type and warn about any remaining
610 * ones. If we find no list type, we default to LIST_item.
614 for (i = 0; n->args && i < (int)n->args->argc; i++) {
618 switch (n->args->argv[i].arg) {
619 /* Set list types. */
653 /* Set list arguments. */
655 dup = n->norm->Bl.comp;
659 dup = (NULL != n->norm->Bl.width);
660 width = n->args->argv[i].value[0];
663 /* NB: this can be empty! */
664 if (n->args->argv[i].sz) {
665 offs = n->args->argv[i].value[0];
666 dup = (NULL != n->norm->Bl.offs);
669 mdoc_nmsg(mdoc, n, MANDOCERR_IGNARGV);
675 /* Check: duplicate auxiliary arguments. */
678 mdoc_nmsg(mdoc, n, MANDOCERR_ARGVREP);
681 n->norm->Bl.comp = comp;
683 n->norm->Bl.offs = offs;
685 n->norm->Bl.width = width;
687 /* Check: multiple list types. */
689 if (LIST__NONE != lt && n->norm->Bl.type != LIST__NONE)
690 mdoc_nmsg(mdoc, n, MANDOCERR_LISTREP);
692 /* Assign list type. */
694 if (LIST__NONE != lt && n->norm->Bl.type == LIST__NONE) {
695 n->norm->Bl.type = lt;
696 /* Set column information, too. */
697 if (LIST_column == lt) {
700 n->norm->Bl.cols = (const char **)
701 n->args->argv[i].value;
705 /* The list type should come first. */
707 if (n->norm->Bl.type == LIST__NONE)
708 if (n->norm->Bl.width ||
711 mdoc_nmsg(mdoc, n, MANDOCERR_LISTFIRST);
716 /* Allow lists to default to LIST_item. */
718 if (LIST__NONE == n->norm->Bl.type) {
719 mdoc_nmsg(mdoc, n, MANDOCERR_LISTTYPE);
720 n->norm->Bl.type = LIST_item;
724 * Validate the width field. Some list types don't need width
725 * types and should be warned about them. Others should have it
726 * and must also be warned.
729 switch (n->norm->Bl.type) {
731 if (n->norm->Bl.width)
733 mdoc_nmsg(mdoc, n, MANDOCERR_NOWIDTHARG);
744 if (n->norm->Bl.width)
745 mdoc_nmsg(mdoc, n, MANDOCERR_IGNARGV);
761 struct mdoc_node *np;
763 if (MDOC_BLOCK != n->type) {
764 if (ENDBODY_NOT != n->end) {
766 np = n->pending->parent;
771 assert(MDOC_BLOCK == np->type);
772 assert(MDOC_Bd == np->tok);
777 for (i = 0; n->args && i < (int)n->args->argc; i++) {
782 switch (n->args->argv[i].arg) {
789 case (MDOC_Unfilled):
799 mdoc_nmsg(mdoc, n, MANDOCERR_BADDISP);
802 /* NB: this can be empty! */
803 if (n->args->argv[i].sz) {
804 offs = n->args->argv[i].value[0];
805 dup = (NULL != n->norm->Bd.offs);
808 mdoc_nmsg(mdoc, n, MANDOCERR_IGNARGV);
812 dup = n->norm->Bd.comp;
819 /* Check whether we have duplicates. */
822 mdoc_nmsg(mdoc, n, MANDOCERR_ARGVREP);
824 /* Make our auxiliary assignments. */
827 n->norm->Bd.offs = offs;
829 n->norm->Bd.comp = comp;
831 /* Check whether a type has already been assigned. */
833 if (DISP__NONE != dt && n->norm->Bd.type != DISP__NONE)
834 mdoc_nmsg(mdoc, n, MANDOCERR_DISPREP);
836 /* Make our type assignment. */
838 if (DISP__NONE != dt && n->norm->Bd.type == DISP__NONE)
839 n->norm->Bd.type = dt;
842 if (DISP__NONE == n->norm->Bd.type) {
843 mdoc_nmsg(mdoc, n, MANDOCERR_DISPTYPE);
844 n->norm->Bd.type = DISP_ragged;
855 if (MDOC_BLOCK != n->type)
857 return(check_parent(mdoc, n, MDOC_Sh, MDOC_BODY));
865 if (MDOC_BLOCK != n->type)
868 mdoc->regs->regs[(int)REG_nS].set = 0;
869 return(check_parent(mdoc, n, MDOC_MAX, MDOC_ROOT));
877 if (MDOC_BLOCK != n->type)
880 return(check_parent(mdoc, n, MDOC_Bl, MDOC_BODY));
892 for (i = 1; i < (int)n->args->argc; i++)
893 mdoc_pmsg(mdoc, n->args->argv[i].line,
894 n->args->argv[i].pos, MANDOCERR_IGNARGV);
896 if (MDOC_Split == n->args->argv[0].arg)
897 n->norm->An.auth = AUTH_split;
898 else if (MDOC_Nosplit == n->args->argv[0].arg)
899 n->norm->An.auth = AUTH_nosplit;
910 if (n->args && 1 == n->args->argc)
911 if (MDOC_Std == n->args->argv[0].arg)
914 mdoc_nmsg(mdoc, n, MANDOCERR_NOARGV);
922 if (0 == mdoc->meta.date || mdoc->meta.os)
923 mdoc_nmsg(mdoc, n, MANDOCERR_PROLOGOOO);
925 if (mdoc->meta.title)
926 mdoc_nmsg(mdoc, n, MANDOCERR_PROLOGREP);
935 if (NULL == mdoc->meta.title || 0 == mdoc->meta.date)
936 mdoc_nmsg(mdoc, n, MANDOCERR_PROLOGOOO);
939 mdoc_nmsg(mdoc, n, MANDOCERR_PROLOGREP);
948 if (mdoc->meta.title || mdoc->meta.os)
949 mdoc_nmsg(mdoc, n, MANDOCERR_PROLOGOOO);
952 mdoc_nmsg(mdoc, n, MANDOCERR_PROLOGREP);
961 struct mdoc_node *np;
965 * Unlike other data pointers, these are "housed" by the HEAD
966 * element, which contains the goods.
969 if (MDOC_HEAD != mdoc->last->type) {
970 if (ENDBODY_NOT != mdoc->last->end) {
971 assert(mdoc->last->pending);
972 np = mdoc->last->pending->parent->head;
973 } else if (MDOC_BLOCK != mdoc->last->type) {
974 np = mdoc->last->parent->head;
976 np = mdoc->last->head;
979 assert(MDOC_HEAD == np->type);
980 assert(MDOC_Bf == np->tok);
985 assert(MDOC_BLOCK == np->parent->type);
986 assert(MDOC_Bf == np->parent->tok);
989 * Cannot have both argument and parameter.
990 * If neither is specified, let it through with a warning.
993 if (np->parent->args && np->child) {
994 mdoc_nmsg(mdoc, np, MANDOCERR_SYNTARGVCOUNT);
996 } else if (NULL == np->parent->args && NULL == np->child) {
997 mdoc_nmsg(mdoc, np, MANDOCERR_FONTTYPE);
1001 /* Extract argument into data. */
1003 if (np->parent->args) {
1004 arg = np->parent->args->argv[0].arg;
1005 if (MDOC_Emphasis == arg)
1006 np->norm->Bf.font = FONT_Em;
1007 else if (MDOC_Literal == arg)
1008 np->norm->Bf.font = FONT_Li;
1009 else if (MDOC_Symbolic == arg)
1010 np->norm->Bf.font = FONT_Sy;
1016 /* Extract parameter into data. */
1018 if (0 == strcmp(np->child->string, "Em"))
1019 np->norm->Bf.font = FONT_Em;
1020 else if (0 == strcmp(np->child->string, "Li"))
1021 np->norm->Bf.font = FONT_Li;
1022 else if (0 == strcmp(np->child->string, "Sy"))
1023 np->norm->Bf.font = FONT_Sy;
1025 mdoc_nmsg(mdoc, np, MANDOCERR_FONTTYPE);
1037 check_count(mdoc, MDOC_ELEM, CHECK_WARN, CHECK_EQ, 1);
1039 assert(mdoc->last->child);
1040 assert(MDOC_TEXT == mdoc->last->child->type);
1042 p = mdoc_a2lib(mdoc->last->child->string);
1044 /* If lookup ok, replace with table value. */
1047 free(mdoc->last->child->string);
1048 mdoc->last->child->string = mandoc_strdup(p);
1052 /* If not, use "library ``xxxx''. */
1054 sz = strlen(mdoc->last->child->string) +
1055 2 + strlen("\\(lqlibrary\\(rq");
1056 buf = mandoc_malloc(sz);
1057 snprintf(buf, sz, "library \\(lq%s\\(rq",
1058 mdoc->last->child->string);
1059 free(mdoc->last->child->string);
1060 mdoc->last->child->string = buf;
1065 post_eoln(POST_ARGS)
1068 if (mdoc->last->child)
1069 mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_ARGSLOST);
1077 const struct mdoc_node *n;
1080 * The Vt macro comes in both ELEM and BLOCK form, both of which
1081 * have different syntaxes (yet more context-sensitive
1082 * behaviour). ELEM types must have a child; BLOCK types,
1083 * specifically the BODY, should only have TEXT children.
1086 if (MDOC_ELEM == mdoc->last->type)
1087 return(eerr_ge1(mdoc));
1088 if (MDOC_BODY != mdoc->last->type)
1091 for (n = mdoc->last->child; n; n = n->next)
1092 if (MDOC_TEXT != n->type)
1093 mdoc_nmsg(mdoc, n, MANDOCERR_CHILD);
1104 /* If no child specified, make sure we have the meta name. */
1106 if (NULL == mdoc->last->child && NULL == mdoc->meta.name) {
1107 mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_NONAME);
1109 } else if (mdoc->meta.name)
1112 /* If no meta name, set it from the child. */
1114 if ( ! concat(mdoc, buf, mdoc->last->child, BUFSIZ))
1117 mdoc->meta.name = mandoc_strdup(buf);
1123 post_literal(POST_ARGS)
1127 * The `Dl' (note "el" not "one") and `Bd' macros unset the
1128 * MDOC_LITERAL flag as they leave. Note that `Bd' only sets
1129 * this in literal mode, but it doesn't hurt to just switch it
1130 * off in general since displays can't be nested.
1133 if (MDOC_BODY == mdoc->last->type)
1134 mdoc->flags &= ~MDOC_LITERAL;
1140 post_defaults(POST_ARGS)
1142 struct mdoc_node *nn;
1145 * The `Ar' defaults to "file ..." if no value is provided as an
1146 * argument; the `Mt' and `Pa' macros use "~"; the `Li' just
1147 * gets an empty string.
1150 if (mdoc->last->child)
1154 mdoc->next = MDOC_NEXT_CHILD;
1158 if ( ! mdoc_word_alloc(mdoc, nn->line, nn->pos, "file"))
1160 if ( ! mdoc_word_alloc(mdoc, nn->line, nn->pos, "..."))
1164 if ( ! mdoc_word_alloc(mdoc, nn->line, nn->pos, "AT&T"))
1166 if ( ! mdoc_word_alloc(mdoc, nn->line, nn->pos, "UNIX"))
1170 if ( ! mdoc_word_alloc(mdoc, nn->line, nn->pos, ""))
1176 if ( ! mdoc_word_alloc(mdoc, nn->line, nn->pos, "~"))
1196 * If we have a child, look it up in the standard keys. If a
1197 * key exist, use that instead of the child; if it doesn't,
1198 * prefix "AT&T UNIX " to the existing data.
1201 if (NULL == mdoc->last->child)
1204 assert(MDOC_TEXT == mdoc->last->child->type);
1205 p = mdoc_a2att(mdoc->last->child->string);
1208 free(mdoc->last->child->string);
1209 mdoc->last->child->string = mandoc_strdup(p);
1211 mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_BADATT);
1213 q = mdoc->last->child->string;
1214 sz = strlen(p) + strlen(q) + 1;
1215 buf = mandoc_malloc(sz);
1216 strlcpy(buf, p, sz);
1217 strlcat(buf, q, sz);
1218 free(mdoc->last->child->string);
1219 mdoc->last->child->string = buf;
1228 struct mdoc_node *np;
1231 if (AUTH__NONE != np->norm->An.auth && np->child) {
1232 check_count(mdoc, MDOC_ELEM, CHECK_WARN, CHECK_EQ, 0);
1237 * FIXME: make this ewarn and make sure that the front-ends
1238 * don't print the arguments.
1240 if (AUTH__NONE != np->norm->An.auth || np->child)
1243 mdoc_nmsg(mdoc, np, MANDOCERR_NOARGS);
1253 struct mdoc_node *n, *c;
1256 if (MDOC_BLOCK != mdoc->last->type)
1259 n = mdoc->last->parent->parent;
1260 lt = n->norm->Bl.type;
1262 if (LIST__NONE == lt) {
1263 mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_LISTTYPE);
1269 if (mdoc->last->head->child)
1271 /* FIXME: give this a dummy value. */
1272 mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_NOARGS);
1281 if (NULL == mdoc->last->head->child)
1282 mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_NOARGS);
1291 if (NULL == mdoc->last->body->child)
1292 mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_NOBODY);
1295 if (mdoc->last->head->child)
1296 mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_ARGSLOST);
1299 cols = (int)n->norm->Bl.ncols;
1301 assert(NULL == mdoc->last->head->child);
1303 if (NULL == mdoc->last->body->child)
1304 mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_NOBODY);
1306 for (i = 0, c = mdoc->last->child; c; c = c->next)
1307 if (MDOC_BODY == c->type)
1311 er = MANDOCERR_ARGCOUNT;
1312 else if (i == cols || i == cols + 1)
1315 er = MANDOCERR_SYNTARGCOUNT;
1317 rc = mdoc_vmsg(mdoc, er,
1318 mdoc->last->line, mdoc->last->pos,
1319 "columns == %d (have %d)", cols, i);
1329 post_bl_block(POST_ARGS)
1331 struct mdoc_node *n;
1334 * These are fairly complicated, so we've broken them into two
1335 * functions. post_bl_block_tag() is called when a -tag is
1336 * specified, but no -width (it must be guessed). The second
1337 * when a -width is specified (macro indicators must be
1338 * rewritten into real lengths).
1343 if (LIST_tag == n->norm->Bl.type &&
1344 NULL == n->norm->Bl.width) {
1345 if ( ! post_bl_block_tag(mdoc))
1347 } else if (NULL != n->norm->Bl.width) {
1348 if ( ! post_bl_block_width(mdoc))
1353 assert(n->norm->Bl.width);
1358 post_bl_block_width(POST_ARGS)
1363 struct mdoc_node *n;
1369 * Calculate the real width of a list from the -width string,
1370 * which may contain a macro (with a known default width), a
1371 * literal string, or a scaling width.
1373 * If the value to -width is a macro, then we re-write it to be
1374 * the macro's width as set in share/tmac/mdoc/doc-common.
1377 if (0 == strcmp(n->norm->Bl.width, "Ds"))
1379 else if (MDOC_MAX == (tok = mdoc_hash_find(n->norm->Bl.width)))
1381 else if (0 == (width = mdoc_macro2len(tok))) {
1382 mdoc_nmsg(mdoc, n, MANDOCERR_BADWIDTH);
1386 /* The value already exists: free and reallocate it. */
1390 for (i = 0; i < (int)n->args->argc; i++)
1391 if (MDOC_Width == n->args->argv[i].arg)
1394 assert(i < (int)n->args->argc);
1396 snprintf(buf, NUMSIZ, "%zun", width);
1397 free(n->args->argv[i].value[0]);
1398 n->args->argv[i].value[0] = mandoc_strdup(buf);
1400 /* Set our width! */
1401 n->norm->Bl.width = n->args->argv[i].value[0];
1406 post_bl_block_tag(POST_ARGS)
1408 struct mdoc_node *n, *nn;
1414 * Calculate the -width for a `Bl -tag' list if it hasn't been
1415 * provided. Uses the first head macro. NOTE AGAIN: this is
1416 * ONLY if the -width argument has NOT been provided. See
1417 * post_bl_block_width() for converting the -width string.
1423 for (nn = n->body->child; nn; nn = nn->next) {
1424 if (MDOC_It != nn->tok)
1427 assert(MDOC_BLOCK == nn->type);
1428 nn = nn->head->child;
1433 if (MDOC_TEXT == nn->type) {
1434 sz = strlen(nn->string) + 1;
1438 if (0 != (ssz = mdoc_macro2len(nn->tok)))
1444 /* Defaults to ten ens. */
1446 snprintf(buf, NUMSIZ, "%zun", sz);
1449 * We have to dynamically add this to the macro's argument list.
1450 * We're guaranteed that a MDOC_Width doesn't already exist.
1454 i = (int)(n->args->argc)++;
1456 n->args->argv = mandoc_realloc(n->args->argv,
1457 n->args->argc * sizeof(struct mdoc_argv));
1459 n->args->argv[i].arg = MDOC_Width;
1460 n->args->argv[i].line = n->line;
1461 n->args->argv[i].pos = n->pos;
1462 n->args->argv[i].sz = 1;
1463 n->args->argv[i].value = mandoc_malloc(sizeof(char *));
1464 n->args->argv[i].value[0] = mandoc_strdup(buf);
1466 /* Set our width! */
1467 n->norm->Bl.width = n->args->argv[i].value[0];
1473 post_bl_head(POST_ARGS)
1475 struct mdoc_node *np, *nn, *nnp;
1478 if (LIST_column != mdoc->last->norm->Bl.type)
1479 /* FIXME: this should be ERROR class... */
1480 return(hwarn_eq0(mdoc));
1483 * Convert old-style lists, where the column width specifiers
1484 * trail as macro parameters, to the new-style ("normal-form")
1485 * lists where they're argument values following -column.
1488 /* First, disallow both types and allow normal-form. */
1491 * TODO: technically, we can accept both and just merge the two
1492 * lists, but I'll leave that for another day.
1495 if (mdoc->last->norm->Bl.ncols && mdoc->last->nchild) {
1496 mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_COLUMNS);
1498 } else if (NULL == mdoc->last->child)
1501 np = mdoc->last->parent;
1504 for (j = 0; j < (int)np->args->argc; j++)
1505 if (MDOC_Column == np->args->argv[j].arg)
1508 assert(j < (int)np->args->argc);
1509 assert(0 == np->args->argv[j].sz);
1512 * Accomodate for new-style groff column syntax. Shuffle the
1513 * child nodes, all of which must be TEXT, as arguments for the
1514 * column field. Then, delete the head children.
1517 np->args->argv[j].sz = (size_t)mdoc->last->nchild;
1518 np->args->argv[j].value = mandoc_malloc
1519 ((size_t)mdoc->last->nchild * sizeof(char *));
1521 mdoc->last->norm->Bl.ncols = np->args->argv[j].sz;
1522 mdoc->last->norm->Bl.cols = (const char **)np->args->argv[j].value;
1524 for (i = 0, nn = mdoc->last->child; nn; i++) {
1525 np->args->argv[j].value[i] = nn->string;
1529 mdoc_node_delete(NULL, nnp);
1532 mdoc->last->nchild = 0;
1533 mdoc->last->child = NULL;
1541 struct mdoc_node *n;
1543 if (MDOC_HEAD == mdoc->last->type)
1544 return(post_bl_head(mdoc));
1545 if (MDOC_BLOCK == mdoc->last->type)
1546 return(post_bl_block(mdoc));
1547 if (MDOC_BODY != mdoc->last->type)
1550 for (n = mdoc->last->child; n; n = n->next) {
1555 mdoc_nmsg(mdoc, n, MANDOCERR_CHILD);
1565 mdoc_nmsg(mdoc, n, MANDOCERR_SYNTCHILD);
1573 ebool(struct mdoc *mdoc)
1576 if (NULL == mdoc->last->child) {
1577 mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_MACROEMPTY);
1578 mdoc_node_delete(mdoc, mdoc->last);
1581 check_count(mdoc, MDOC_ELEM, CHECK_WARN, CHECK_EQ, 1);
1583 assert(MDOC_TEXT == mdoc->last->child->type);
1585 if (0 == strcmp(mdoc->last->child->string, "on"))
1587 if (0 == strcmp(mdoc->last->child->string, "off"))
1590 mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_BADBOOL);
1595 post_root(POST_ARGS)
1598 struct mdoc_node *n;
1602 /* Check that we have a finished prologue. */
1604 if ( ! (MDOC_PBODY & mdoc->flags)) {
1606 mdoc_nmsg(mdoc, mdoc->first, MANDOCERR_NODOCPROLOG);
1612 /* Check that we begin with a proper `Sh'. */
1614 if (NULL == n->child) {
1616 mdoc_nmsg(mdoc, n, MANDOCERR_NODOCBODY);
1617 } else if (MDOC_BLOCK != n->child->type ||
1618 MDOC_Sh != n->child->tok) {
1620 /* Can this be lifted? See rxdebug.1 for example. */
1621 mdoc_nmsg(mdoc, n, MANDOCERR_NODOCBODY);
1624 return(erc ? 0 : 1);
1630 struct mdoc_node *ch;
1633 if (NULL == (ch = mdoc->last->child)) {
1634 mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_MACROEMPTY);
1635 mdoc_node_delete(mdoc, mdoc->last);
1639 assert(MDOC_TEXT == ch->type);
1641 if (NULL == (p = mdoc_a2st(ch->string))) {
1642 mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_BADSTANDARD);
1643 mdoc_node_delete(mdoc, mdoc->last);
1646 ch->string = mandoc_strdup(p);
1655 struct mdoc_node *nn, *next, *prev;
1658 switch (mdoc->last->type) {
1660 check_count(mdoc, MDOC_HEAD, CHECK_WARN, CHECK_EQ, 0);
1663 if (mdoc->last->child)
1665 check_count(mdoc, MDOC_BODY, CHECK_WARN, CHECK_GT, 0);
1672 * Make sure only certain types of nodes are allowed within the
1673 * the `Rs' body. Delete offending nodes and raise a warning.
1674 * Do this before re-ordering for the sake of clarity.
1678 for (nn = mdoc->last->child; nn; nn = next) {
1679 for (i = 0; i < RSORD_MAX; i++)
1680 if (nn->tok == rsord[i])
1683 if (i < RSORD_MAX) {
1684 if (MDOC__J == rsord[i])
1685 mdoc->last->norm->Rs.child_J = nn;
1691 mdoc_nmsg(mdoc, nn, MANDOCERR_CHILD);
1692 mdoc_node_delete(mdoc, nn);
1696 * The full `Rs' block needs special handling to order the
1697 * sub-elements according to `rsord'. Pick through each element
1698 * and correctly order it. This is a insertion sort.
1702 for (nn = mdoc->last->child->next; nn; nn = next) {
1703 /* Determine order of `nn'. */
1704 for (i = 0; i < RSORD_MAX; i++)
1705 if (rsord[i] == nn->tok)
1709 * Remove `nn' from the chain. This somewhat
1710 * repeats mdoc_node_unlink(), but since we're
1711 * just re-ordering, there's no need for the
1712 * full unlink process.
1715 if (NULL != (next = nn->next))
1716 next->prev = nn->prev;
1718 if (NULL != (prev = nn->prev))
1719 prev->next = nn->next;
1721 nn->prev = nn->next = NULL;
1724 * Scan back until we reach a node that's
1725 * ordered before `nn'.
1728 for ( ; prev ; prev = prev->prev) {
1729 /* Determine order of `prev'. */
1730 for (j = 0; j < RSORD_MAX; j++)
1731 if (rsord[j] == prev->tok)
1739 * Set `nn' back into its correct place in front
1740 * of the `prev' node.
1747 prev->next->prev = nn;
1748 nn->next = prev->next;
1751 mdoc->last->child->prev = nn;
1752 nn->next = mdoc->last->child;
1753 mdoc->last->child = nn;
1764 if (MDOC_HEAD == mdoc->last->type)
1765 return(post_sh_head(mdoc));
1766 if (MDOC_BODY == mdoc->last->type)
1767 return(post_sh_body(mdoc));
1773 post_sh_body(POST_ARGS)
1775 struct mdoc_node *n;
1777 if (SEC_NAME != mdoc->lastsec)
1781 * Warn if the NAME section doesn't contain the `Nm' and `Nd'
1782 * macros (can have multiple `Nm' and one `Nd'). Note that the
1783 * children of the BODY declaration can also be "text".
1786 if (NULL == (n = mdoc->last->child)) {
1787 mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_BADNAMESEC);
1791 for ( ; n && n->next; n = n->next) {
1792 if (MDOC_ELEM == n->type && MDOC_Nm == n->tok)
1794 if (MDOC_TEXT == n->type)
1796 mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_BADNAMESEC);
1800 if (MDOC_BLOCK == n->type && MDOC_Nd == n->tok)
1803 mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_BADNAMESEC);
1808 post_sh_head(POST_ARGS)
1814 * Process a new section. Sections are either "named" or
1815 * "custom". Custom sections are user-defined, while named ones
1816 * follow a conventional order and may only appear in certain
1820 if ( ! concat(mdoc, buf, mdoc->last->child, BUFSIZ))
1823 sec = mdoc_str2sec(buf);
1825 /* The NAME should be first. */
1827 if (SEC_NAME != sec && SEC_NONE == mdoc->lastnamed)
1828 mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_NAMESECFIRST);
1830 /* The SYNOPSIS gets special attention in other areas. */
1832 if (SEC_SYNOPSIS == sec)
1833 mdoc->flags |= MDOC_SYNOPSIS;
1835 mdoc->flags &= ~MDOC_SYNOPSIS;
1837 /* Mark our last section. */
1839 mdoc->lastsec = sec;
1841 /* We don't care about custom sections after this. */
1843 if (SEC_CUSTOM == sec)
1847 * Check whether our non-custom section is being repeated or is
1851 if (sec == mdoc->lastnamed)
1852 mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_SECREP);
1854 if (sec < mdoc->lastnamed)
1855 mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_SECOOO);
1857 /* Mark the last named section. */
1859 mdoc->lastnamed = sec;
1861 /* Check particular section/manual conventions. */
1863 assert(mdoc->meta.msec);
1866 case (SEC_RETURN_VALUES):
1871 if (*mdoc->meta.msec == '2')
1873 if (*mdoc->meta.msec == '3')
1875 if (*mdoc->meta.msec == '9')
1877 mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_SECMSEC);
1887 post_ignpar(POST_ARGS)
1889 struct mdoc_node *np;
1891 if (MDOC_BODY != mdoc->last->type)
1894 if (NULL != (np = mdoc->last->child))
1895 if (MDOC_Pp == np->tok || MDOC_Lp == np->tok) {
1896 mdoc_nmsg(mdoc, np, MANDOCERR_IGNPAR);
1897 mdoc_node_delete(mdoc, np);
1900 if (NULL != (np = mdoc->last->last))
1901 if (MDOC_Pp == np->tok || MDOC_Lp == np->tok) {
1902 mdoc_nmsg(mdoc, np, MANDOCERR_IGNPAR);
1903 mdoc_node_delete(mdoc, np);
1913 if (NULL == mdoc->last)
1915 if (MDOC_ELEM != n->type && MDOC_BLOCK != n->type)
1919 * Don't allow prior `Lp' or `Pp' prior to a paragraph-type
1920 * block: `Lp', `Pp', or non-compact `Bd' or `Bl'.
1923 if (MDOC_Pp != mdoc->last->tok && MDOC_Lp != mdoc->last->tok)
1925 if (MDOC_Bl == n->tok && n->norm->Bl.comp)
1927 if (MDOC_Bd == n->tok && n->norm->Bd.comp)
1929 if (MDOC_It == n->tok && n->parent->norm->Bl.comp)
1932 mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_IGNPAR);
1933 mdoc_node_delete(mdoc, mdoc->last);
1938 pre_literal(PRE_ARGS)
1941 if (MDOC_BODY != n->type)
1945 * The `Dl' (note "el" not "one") and `Bd -literal' and `Bd
1946 * -unfilled' macros set MDOC_LITERAL on entrance to the body.
1951 mdoc->flags |= MDOC_LITERAL;
1954 if (DISP_literal == n->norm->Bd.type)
1955 mdoc->flags |= MDOC_LITERAL;
1956 if (DISP_unfilled == n->norm->Bd.type)
1957 mdoc->flags |= MDOC_LITERAL;
1971 struct mdoc_node *n;
1975 if (NULL == n->child) {
1976 mdoc->meta.date = time(NULL);
1980 if ( ! concat(mdoc, buf, n->child, DATESIZE))
1983 mdoc->meta.date = mandoc_a2time
1984 (MTIME_MDOCDATE | MTIME_CANONICAL, buf);
1986 if (0 == mdoc->meta.date) {
1987 mdoc_nmsg(mdoc, n, MANDOCERR_BADDATE);
1988 mdoc->meta.date = time(NULL);
1997 struct mdoc_node *nn, *n;
2003 if (mdoc->meta.title)
2004 free(mdoc->meta.title);
2006 free(mdoc->meta.vol);
2007 if (mdoc->meta.arch)
2008 free(mdoc->meta.arch);
2010 mdoc->meta.title = mdoc->meta.vol = mdoc->meta.arch = NULL;
2012 /* First make all characters uppercase. */
2014 if (NULL != (nn = n->child))
2015 for (p = nn->string; *p; p++) {
2016 if (toupper((u_char)*p) == *p)
2020 * FIXME: don't be lazy: have this make all
2021 * characters be uppercase and just warn once.
2023 mdoc_nmsg(mdoc, nn, MANDOCERR_UPPERCASE);
2028 * --> title = unknown, volume = local, msec = 0, arch = NULL
2031 if (NULL == (nn = n->child)) {
2032 /* XXX: make these macro values. */
2033 /* FIXME: warn about missing values. */
2034 mdoc->meta.title = mandoc_strdup("UNKNOWN");
2035 mdoc->meta.vol = mandoc_strdup("LOCAL");
2036 mdoc->meta.msec = mandoc_strdup("1");
2040 /* Handles: `.Dt TITLE'
2041 * --> title = TITLE, volume = local, msec = 0, arch = NULL
2044 mdoc->meta.title = mandoc_strdup
2045 ('\0' == nn->string[0] ? "UNKNOWN" : nn->string);
2047 if (NULL == (nn = nn->next)) {
2048 /* FIXME: warn about missing msec. */
2049 /* XXX: make this a macro value. */
2050 mdoc->meta.vol = mandoc_strdup("LOCAL");
2051 mdoc->meta.msec = mandoc_strdup("1");
2055 /* Handles: `.Dt TITLE SEC'
2056 * --> title = TITLE, volume = SEC is msec ?
2057 * format(msec) : SEC,
2058 * msec = SEC is msec ? atoi(msec) : 0,
2062 cp = mdoc_a2msec(nn->string);
2064 mdoc->meta.vol = mandoc_strdup(cp);
2065 mdoc->meta.msec = mandoc_strdup(nn->string);
2067 mdoc_nmsg(mdoc, n, MANDOCERR_BADMSEC);
2068 mdoc->meta.vol = mandoc_strdup(nn->string);
2069 mdoc->meta.msec = mandoc_strdup(nn->string);
2072 if (NULL == (nn = nn->next))
2075 /* Handles: `.Dt TITLE SEC VOL'
2076 * --> title = TITLE, volume = VOL is vol ?
2078 * VOL is arch ? format(arch) :
2082 cp = mdoc_a2vol(nn->string);
2084 free(mdoc->meta.vol);
2085 mdoc->meta.vol = mandoc_strdup(cp);
2087 /* FIXME: warn about bad arch. */
2088 cp = mdoc_a2arch(nn->string);
2090 free(mdoc->meta.vol);
2091 mdoc->meta.vol = mandoc_strdup(nn->string);
2093 mdoc->meta.arch = mandoc_strdup(cp);
2096 /* Ignore any subsequent parameters... */
2097 /* FIXME: warn about subsequent parameters. */
2103 post_prol(POST_ARGS)
2106 * Remove prologue macros from the document after they're
2107 * processed. The final document uses mdoc_meta for these
2108 * values and discards the originals.
2111 mdoc_node_delete(mdoc, mdoc->last);
2112 if (mdoc->meta.title && mdoc->meta.date && mdoc->meta.os)
2113 mdoc->flags |= MDOC_PBODY;
2121 struct mdoc_node *n;
2124 struct utsname utsname;
2130 * Set the operating system by way of the `Os' macro. Note that
2131 * if an argument isn't provided and -DOSNAME="\"foo\"" is
2132 * provided during compilation, this value will be used instead
2133 * of filling in "sysname release" from uname().
2137 free(mdoc->meta.os);
2139 if ( ! concat(mdoc, buf, n->child, BUFSIZ))
2142 /* XXX: yes, these can all be dynamically-adjusted buffers, but
2143 * it's really not worth the extra hackery.
2146 if ('\0' == buf[0]) {
2148 if (strlcat(buf, OSNAME, BUFSIZ) >= BUFSIZ) {
2149 mdoc_nmsg(mdoc, n, MANDOCERR_MEM);
2153 if (uname(&utsname)) {
2154 mdoc_nmsg(mdoc, n, MANDOCERR_UNAME);
2155 mdoc->meta.os = mandoc_strdup("UNKNOWN");
2156 return(post_prol(mdoc));
2159 if (strlcat(buf, utsname.sysname, BUFSIZ) >= BUFSIZ) {
2160 mdoc_nmsg(mdoc, n, MANDOCERR_MEM);
2163 if (strlcat(buf, " ", BUFSIZ) >= BUFSIZ) {
2164 mdoc_nmsg(mdoc, n, MANDOCERR_MEM);
2167 if (strlcat(buf, utsname.release, BUFSIZ) >= BUFSIZ) {
2168 mdoc_nmsg(mdoc, n, MANDOCERR_MEM);
2174 mdoc->meta.os = mandoc_strdup(buf);
2181 struct mdoc_node *nn, *n;
2186 * Macros accepting `-std' as an argument have the name of the
2187 * current document (`Nm') filled in as the argument if it's not
2194 if (NULL == mdoc->meta.name)
2198 mdoc->next = MDOC_NEXT_CHILD;
2200 if ( ! mdoc_word_alloc(mdoc, n->line, n->pos, mdoc->meta.name))
2208 concat(struct mdoc *m, char *p, const struct mdoc_node *n, size_t sz)
2214 * Concatenate sibling nodes together. All siblings must be of
2215 * type MDOC_TEXT or an assertion is raised. Concatenation is
2216 * separated by a single whitespace. Returns 0 on fatal (string
2220 for ( ; n; n = n->next) {
2221 assert(MDOC_TEXT == n->type);
2223 if (strlcat(p, n->string, sz) >= sz) {
2224 mdoc_nmsg(m, n, MANDOCERR_MEM);
2228 if (NULL == n->next)
2231 if (strlcat(p, " ", sz) >= sz) {
2232 mdoc_nmsg(m, n, MANDOCERR_MEM);