]>
git.cameronkatri.com Git - mandoc.git/blob - mdoc_validate.c
1 /* $Id: mdoc_validate.c,v 1.145 2010/12/22 11:15:16 kristaps Exp $ */
3 * Copyright (c) 2008, 2009, 2010 Kristaps Dzonsons <kristaps@bsd.lv>
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22 #include <sys/utsname.h>
25 #include <sys/types.h>
37 #include "libmandoc.h"
39 /* FIXME: .Bl -diag can't have non-text children in HEAD. */
41 #define PRE_ARGS struct mdoc *mdoc, struct mdoc_node *n
42 #define POST_ARGS struct mdoc *mdoc
44 #define NORM_BUMP(dst, src) do { \
45 (dst)->norm = (src)->norm; \
46 ((dst)->norm->refcnt)++; \
47 } while (/* CONSTCOND */ 0)
48 #define NORM_ALLOC(dst) do { \
50 mandoc_calloc(1, sizeof(struct mdoc_norm)); \
51 (dst)->norm->refcnt = 1; \
52 } while (/* CONSTCOND */ 0)
69 typedef int (*v_pre
)(PRE_ARGS
);
70 typedef int (*v_post
)(POST_ARGS
);
77 static int check_count(struct mdoc
*, enum mdoc_type
,
78 enum check_lvl
, enum check_ineq
, int);
79 static int check_parent(PRE_ARGS
, enum mdoct
, enum mdoc_type
);
80 static void check_text(struct mdoc
*, int, int, char *);
81 static void check_argv(struct mdoc
*,
82 struct mdoc_node
*, struct mdoc_argv
*);
83 static void check_args(struct mdoc
*, struct mdoc_node
*);
85 static int concat(struct mdoc
*, char *,
86 const struct mdoc_node
*, size_t);
88 static int ebool(POST_ARGS
);
89 static int berr_ge1(POST_ARGS
);
90 static int bwarn_ge1(POST_ARGS
);
91 static int eerr_eq0(POST_ARGS
);
92 static int eerr_eq1(POST_ARGS
);
93 static int eerr_ge1(POST_ARGS
);
94 static int eerr_le1(POST_ARGS
);
95 static int ewarn_eq0(POST_ARGS
);
96 static int ewarn_ge1(POST_ARGS
);
97 static int herr_eq0(POST_ARGS
);
98 static int herr_ge1(POST_ARGS
);
99 static int hwarn_eq0(POST_ARGS
);
100 static int hwarn_eq1(POST_ARGS
);
101 static int hwarn_le1(POST_ARGS
);
103 static int post_an(POST_ARGS
);
104 static int post_at(POST_ARGS
);
105 static int post_bf(POST_ARGS
);
106 static int post_bl(POST_ARGS
);
107 static int post_bl_block(POST_ARGS
);
108 static int post_bl_block_width(POST_ARGS
);
109 static int post_bl_block_tag(POST_ARGS
);
110 static int post_bl_head(POST_ARGS
);
111 static int post_dd(POST_ARGS
);
112 static int post_dt(POST_ARGS
);
113 static int post_defaults(POST_ARGS
);
114 static int post_literal(POST_ARGS
);
115 static int post_eoln(POST_ARGS
);
116 static int post_it(POST_ARGS
);
117 static int post_lb(POST_ARGS
);
118 static int post_nm(POST_ARGS
);
119 static int post_os(POST_ARGS
);
120 static int post_ignpar(POST_ARGS
);
121 static int post_prol(POST_ARGS
);
122 static int post_root(POST_ARGS
);
123 static int post_rs(POST_ARGS
);
124 static int post_sh(POST_ARGS
);
125 static int post_sh_body(POST_ARGS
);
126 static int post_sh_head(POST_ARGS
);
127 static int post_st(POST_ARGS
);
128 static int post_std(POST_ARGS
);
129 static int post_vt(POST_ARGS
);
130 static int pre_an(PRE_ARGS
);
131 static int pre_bd(PRE_ARGS
);
132 static int pre_bl(PRE_ARGS
);
133 static int pre_dd(PRE_ARGS
);
134 static int pre_display(PRE_ARGS
);
135 static int pre_dt(PRE_ARGS
);
136 static int pre_it(PRE_ARGS
);
137 static int pre_literal(PRE_ARGS
);
138 static int pre_os(PRE_ARGS
);
139 static int pre_par(PRE_ARGS
);
140 static int pre_sh(PRE_ARGS
);
141 static int pre_ss(PRE_ARGS
);
142 static int pre_std(PRE_ARGS
);
144 static v_post posts_an
[] = { post_an
, NULL
};
145 static v_post posts_at
[] = { post_at
, post_defaults
, NULL
};
146 static v_post posts_bd
[] = { post_literal
, hwarn_eq0
, bwarn_ge1
, NULL
};
147 static v_post posts_bf
[] = { hwarn_le1
, post_bf
, NULL
};
148 static v_post posts_bk
[] = { hwarn_eq0
, bwarn_ge1
, NULL
};
149 static v_post posts_bl
[] = { bwarn_ge1
, post_bl
, NULL
};
150 static v_post posts_bool
[] = { eerr_eq1
, ebool
, NULL
};
151 static v_post posts_eoln
[] = { post_eoln
, NULL
};
152 static v_post posts_defaults
[] = { post_defaults
, NULL
};
153 static v_post posts_dd
[] = { ewarn_ge1
, post_dd
, post_prol
, NULL
};
154 static v_post posts_dl
[] = { post_literal
, bwarn_ge1
, herr_eq0
, NULL
};
155 static v_post posts_dt
[] = { post_dt
, post_prol
, NULL
};
156 static v_post posts_fo
[] = { hwarn_eq1
, bwarn_ge1
, NULL
};
157 static v_post posts_it
[] = { post_it
, NULL
};
158 static v_post posts_lb
[] = { eerr_eq1
, post_lb
, NULL
};
159 static v_post posts_nd
[] = { berr_ge1
, NULL
};
160 static v_post posts_nm
[] = { post_nm
, NULL
};
161 static v_post posts_notext
[] = { ewarn_eq0
, NULL
};
162 static v_post posts_os
[] = { post_os
, post_prol
, NULL
};
163 static v_post posts_rs
[] = { berr_ge1
, herr_eq0
, post_rs
, NULL
};
164 static v_post posts_sh
[] = { post_ignpar
, herr_ge1
, bwarn_ge1
, post_sh
, NULL
};
165 static v_post posts_sp
[] = { eerr_le1
, NULL
};
166 static v_post posts_ss
[] = { post_ignpar
, herr_ge1
, bwarn_ge1
, NULL
};
167 static v_post posts_st
[] = { eerr_eq1
, post_st
, NULL
};
168 static v_post posts_std
[] = { post_std
, NULL
};
169 static v_post posts_text
[] = { eerr_ge1
, NULL
};
170 static v_post posts_text1
[] = { eerr_eq1
, NULL
};
171 static v_post posts_vt
[] = { post_vt
, NULL
};
172 static v_post posts_wline
[] = { bwarn_ge1
, herr_eq0
, NULL
};
173 static v_post posts_wtext
[] = { ewarn_ge1
, NULL
};
174 static v_pre pres_an
[] = { pre_an
, NULL
};
175 static v_pre pres_bd
[] = { pre_display
, pre_bd
, pre_literal
, pre_par
, NULL
};
176 static v_pre pres_bl
[] = { pre_bl
, pre_par
, NULL
};
177 static v_pre pres_d1
[] = { pre_display
, NULL
};
178 static v_pre pres_dl
[] = { pre_literal
, pre_display
, NULL
};
179 static v_pre pres_dd
[] = { pre_dd
, NULL
};
180 static v_pre pres_dt
[] = { pre_dt
, NULL
};
181 static v_pre pres_er
[] = { NULL
, NULL
};
182 static v_pre pres_fd
[] = { NULL
, NULL
};
183 static v_pre pres_it
[] = { pre_it
, pre_par
, NULL
};
184 static v_pre pres_os
[] = { pre_os
, NULL
};
185 static v_pre pres_pp
[] = { pre_par
, NULL
};
186 static v_pre pres_sh
[] = { pre_sh
, NULL
};
187 static v_pre pres_ss
[] = { pre_ss
, NULL
};
188 static v_pre pres_std
[] = { pre_std
, NULL
};
190 const struct valids mdoc_valids
[MDOC_MAX
] = {
191 { NULL
, NULL
}, /* Ap */
192 { pres_dd
, posts_dd
}, /* Dd */
193 { pres_dt
, posts_dt
}, /* Dt */
194 { pres_os
, posts_os
}, /* Os */
195 { pres_sh
, posts_sh
}, /* Sh */
196 { pres_ss
, posts_ss
}, /* Ss */
197 { pres_pp
, posts_notext
}, /* Pp */
198 { pres_d1
, posts_wline
}, /* D1 */
199 { pres_dl
, posts_dl
}, /* Dl */
200 { pres_bd
, posts_bd
}, /* Bd */
201 { NULL
, NULL
}, /* Ed */
202 { pres_bl
, posts_bl
}, /* Bl */
203 { NULL
, NULL
}, /* El */
204 { pres_it
, posts_it
}, /* It */
205 { NULL
, posts_text
}, /* Ad */
206 { pres_an
, posts_an
}, /* An */
207 { NULL
, posts_defaults
}, /* Ar */
208 { NULL
, posts_text
}, /* Cd */
209 { NULL
, NULL
}, /* Cm */
210 { NULL
, NULL
}, /* Dv */
211 { pres_er
, posts_text
}, /* Er */
212 { NULL
, NULL
}, /* Ev */
213 { pres_std
, posts_std
}, /* Ex */
214 { NULL
, NULL
}, /* Fa */
215 { pres_fd
, posts_wtext
}, /* Fd */
216 { NULL
, NULL
}, /* Fl */
217 { NULL
, posts_text
}, /* Fn */
218 { NULL
, posts_wtext
}, /* Ft */
219 { NULL
, posts_text
}, /* Ic */
220 { NULL
, posts_text1
}, /* In */
221 { NULL
, posts_defaults
}, /* Li */
222 { NULL
, posts_nd
}, /* Nd */
223 { NULL
, posts_nm
}, /* Nm */
224 { NULL
, NULL
}, /* Op */
225 { NULL
, NULL
}, /* Ot */
226 { NULL
, posts_defaults
}, /* Pa */
227 { pres_std
, posts_std
}, /* Rv */
228 { NULL
, posts_st
}, /* St */
229 { NULL
, NULL
}, /* Va */
230 { NULL
, posts_vt
}, /* Vt */
231 { NULL
, posts_wtext
}, /* Xr */
232 { NULL
, posts_text
}, /* %A */
233 { NULL
, posts_text
}, /* %B */ /* FIXME: can be used outside Rs/Re. */
234 { NULL
, posts_text
}, /* %D */ /* FIXME: check date with mandoc_a2time(). */
235 { NULL
, posts_text
}, /* %I */
236 { NULL
, posts_text
}, /* %J */
237 { NULL
, posts_text
}, /* %N */
238 { NULL
, posts_text
}, /* %O */
239 { NULL
, posts_text
}, /* %P */
240 { NULL
, posts_text
}, /* %R */
241 { NULL
, posts_text
}, /* %T */ /* FIXME: can be used outside Rs/Re. */
242 { NULL
, posts_text
}, /* %V */
243 { NULL
, NULL
}, /* Ac */
244 { NULL
, NULL
}, /* Ao */
245 { NULL
, NULL
}, /* Aq */
246 { NULL
, posts_at
}, /* At */
247 { NULL
, NULL
}, /* Bc */
248 { NULL
, posts_bf
}, /* Bf */
249 { NULL
, NULL
}, /* Bo */
250 { NULL
, NULL
}, /* Bq */
251 { NULL
, NULL
}, /* Bsx */
252 { NULL
, NULL
}, /* Bx */
253 { NULL
, posts_bool
}, /* Db */
254 { NULL
, NULL
}, /* Dc */
255 { NULL
, NULL
}, /* Do */
256 { NULL
, NULL
}, /* Dq */
257 { NULL
, NULL
}, /* Ec */
258 { NULL
, NULL
}, /* Ef */
259 { NULL
, NULL
}, /* Em */
260 { NULL
, NULL
}, /* Eo */
261 { NULL
, NULL
}, /* Fx */
262 { NULL
, posts_text
}, /* Ms */
263 { NULL
, posts_notext
}, /* No */
264 { NULL
, posts_notext
}, /* Ns */
265 { NULL
, NULL
}, /* Nx */
266 { NULL
, NULL
}, /* Ox */
267 { NULL
, NULL
}, /* Pc */
268 { NULL
, posts_text1
}, /* Pf */
269 { NULL
, NULL
}, /* Po */
270 { NULL
, NULL
}, /* Pq */
271 { NULL
, NULL
}, /* Qc */
272 { NULL
, NULL
}, /* Ql */
273 { NULL
, NULL
}, /* Qo */
274 { NULL
, NULL
}, /* Qq */
275 { NULL
, NULL
}, /* Re */
276 { NULL
, posts_rs
}, /* Rs */
277 { NULL
, NULL
}, /* Sc */
278 { NULL
, NULL
}, /* So */
279 { NULL
, NULL
}, /* Sq */
280 { NULL
, posts_bool
}, /* Sm */
281 { NULL
, posts_text
}, /* Sx */
282 { NULL
, posts_text
}, /* Sy */
283 { NULL
, posts_text
}, /* Tn */
284 { NULL
, NULL
}, /* Ux */
285 { NULL
, NULL
}, /* Xc */
286 { NULL
, NULL
}, /* Xo */
287 { NULL
, posts_fo
}, /* Fo */
288 { NULL
, NULL
}, /* Fc */
289 { NULL
, NULL
}, /* Oo */
290 { NULL
, NULL
}, /* Oc */
291 { NULL
, posts_bk
}, /* Bk */
292 { NULL
, NULL
}, /* Ek */
293 { NULL
, posts_eoln
}, /* Bt */
294 { NULL
, NULL
}, /* Hf */
295 { NULL
, NULL
}, /* Fr */
296 { NULL
, posts_eoln
}, /* Ud */
297 { NULL
, posts_lb
}, /* Lb */
298 { NULL
, posts_notext
}, /* Lp */
299 { NULL
, posts_text
}, /* Lk */
300 { NULL
, posts_defaults
}, /* Mt */
301 { NULL
, NULL
}, /* Brq */
302 { NULL
, NULL
}, /* Bro */
303 { NULL
, NULL
}, /* Brc */
304 { NULL
, posts_text
}, /* %C */
305 { NULL
, NULL
}, /* Es */
306 { NULL
, NULL
}, /* En */
307 { NULL
, NULL
}, /* Dx */
308 { NULL
, posts_text
}, /* %Q */
309 { NULL
, posts_notext
}, /* br */
310 { pres_pp
, posts_sp
}, /* sp */
311 { NULL
, posts_text1
}, /* %U */
312 { NULL
, NULL
}, /* Ta */
315 #define RSORD_MAX 14 /* Number of `Rs' blocks. */
317 static const enum mdoct rsord
[RSORD_MAX
] = {
336 mdoc_valid_pre(struct mdoc
*mdoc
, struct mdoc_node
*n
)
342 if (MDOC_TEXT
== n
->type
) {
346 check_text(mdoc
, line
, pos
, tp
);
352 if (NULL
== mdoc_valids
[n
->tok
].pre
)
354 for (p
= mdoc_valids
[n
->tok
].pre
; *p
; p
++)
355 if ( ! (*p
)(mdoc
, n
))
362 mdoc_valid_post(struct mdoc
*mdoc
)
366 if (MDOC_VALID
& mdoc
->last
->flags
)
368 mdoc
->last
->flags
|= MDOC_VALID
;
370 if (MDOC_TEXT
== mdoc
->last
->type
)
372 if (MDOC_ROOT
== mdoc
->last
->type
)
373 return(post_root(mdoc
));
375 if (NULL
== mdoc_valids
[mdoc
->last
->tok
].post
)
377 for (p
= mdoc_valids
[mdoc
->last
->tok
].post
; *p
; p
++)
385 check_count(struct mdoc
*m
, enum mdoc_type type
,
386 enum check_lvl lvl
, enum check_ineq ineq
, int val
)
390 if (m
->last
->type
!= type
)
396 if (m
->last
->nchild
< val
)
401 if (m
->last
->nchild
> val
)
406 if (val
== m
->last
->nchild
)
414 if (CHECK_WARN
== lvl
) {
415 return(mdoc_vmsg(m
, MANDOCERR_ARGCOUNT
,
416 m
->last
->line
, m
->last
->pos
,
417 "want %s%d children (have %d)",
418 p
, val
, m
->last
->nchild
));
421 /* FIXME: THIS IS THE SAME AS THE ABOVE. */
423 return(mdoc_vmsg(m
, MANDOCERR_ARGCOUNT
,
424 m
->last
->line
, m
->last
->pos
,
425 "require %s%d children (have %d)",
426 p
, val
, m
->last
->nchild
));
433 return(check_count(mdoc
, MDOC_BODY
, CHECK_FATAL
, CHECK_GT
, 0));
439 return(check_count(mdoc
, MDOC_BODY
, CHECK_WARN
, CHECK_GT
, 0));
445 return(check_count(mdoc
, MDOC_ELEM
, CHECK_FATAL
, CHECK_EQ
, 0));
451 return(check_count(mdoc
, MDOC_ELEM
, CHECK_FATAL
, CHECK_EQ
, 1));
457 return(check_count(mdoc
, MDOC_ELEM
, CHECK_FATAL
, CHECK_GT
, 0));
463 return(check_count(mdoc
, MDOC_ELEM
, CHECK_FATAL
, CHECK_LT
, 2));
469 return(check_count(mdoc
, MDOC_ELEM
, CHECK_WARN
, CHECK_EQ
, 0));
475 return(check_count(mdoc
, MDOC_ELEM
, CHECK_WARN
, CHECK_GT
, 0));
481 return(check_count(mdoc
, MDOC_HEAD
, CHECK_FATAL
, CHECK_EQ
, 0));
487 return(check_count(mdoc
, MDOC_HEAD
, CHECK_FATAL
, CHECK_GT
, 0));
493 return(check_count(mdoc
, MDOC_HEAD
, CHECK_WARN
, CHECK_EQ
, 0));
499 return(check_count(mdoc
, MDOC_HEAD
, CHECK_WARN
, CHECK_EQ
, 1));
505 return(check_count(mdoc
, MDOC_HEAD
, CHECK_WARN
, CHECK_LT
, 2));
509 check_args(struct mdoc
*m
, struct mdoc_node
*n
)
516 assert(n
->args
->argc
);
517 for (i
= 0; i
< (int)n
->args
->argc
; i
++)
518 check_argv(m
, n
, &n
->args
->argv
[i
]);
522 check_argv(struct mdoc
*m
, struct mdoc_node
*n
, struct mdoc_argv
*v
)
526 for (i
= 0; i
< (int)v
->sz
; i
++)
527 check_text(m
, v
->line
, v
->pos
, v
->value
[i
]);
529 /* FIXME: move to post_std(). */
531 if (MDOC_Std
== v
->arg
)
532 if ( ! (v
->sz
|| m
->meta
.name
))
533 mdoc_nmsg(m
, n
, MANDOCERR_NONAME
);
537 check_text(struct mdoc
*m
, int ln
, int pos
, char *p
)
542 for ( ; *p
; p
++, pos
++) {
543 sz
= strcspn(p
, "\t\\");
552 if ( ! (MDOC_LITERAL
& m
->flags
))
553 mdoc_pmsg(m
, ln
, pos
, MANDOCERR_BADTAB
);
557 if (0 == (c
= mandoc_special(p
))) {
558 mdoc_pmsg(m
, ln
, pos
, MANDOCERR_BADESCAPE
);
568 check_parent(PRE_ARGS
, enum mdoct tok
, enum mdoc_type t
)
572 if ((MDOC_ROOT
== t
|| tok
== n
->parent
->tok
) &&
573 (t
== n
->parent
->type
))
576 mdoc_vmsg(mdoc
, MANDOCERR_SYNTCHILD
,
577 n
->line
, n
->pos
, "want parent %s",
578 MDOC_ROOT
== t
? "<root>" :
579 mdoc_macronames
[tok
]);
585 pre_display(PRE_ARGS
)
587 struct mdoc_node
*node
;
589 if (MDOC_BLOCK
!= n
->type
)
592 for (node
= mdoc
->last
->parent
; node
; node
= node
->parent
)
593 if (MDOC_BLOCK
== node
->type
)
594 if (MDOC_Bd
== node
->tok
)
598 mdoc_nmsg(mdoc
, n
, MANDOCERR_NESTEDDISP
);
608 const char *offs
, *width
;
610 struct mdoc_node
*np
;
612 if (MDOC_BLOCK
!= n
->type
) {
613 if (ENDBODY_NOT
!= n
->end
) {
615 np
= n
->pending
->parent
;
620 assert(MDOC_BLOCK
== np
->type
);
621 assert(MDOC_Bl
== np
->tok
);
629 * First figure out which kind of list to use: bind ourselves to
630 * the first mentioned list type and warn about any remaining
631 * ones. If we find no list type, we default to LIST_item.
635 for (i
= 0; n
->args
&& i
< (int)n
->args
->argc
; i
++) {
639 switch (n
->args
->argv
[i
].arg
) {
640 /* Set list types. */
674 /* Set list arguments. */
676 dup
= n
->norm
->d
.Bl
.comp
;
680 dup
= (NULL
!= n
->norm
->d
.Bl
.width
);
681 width
= n
->args
->argv
[i
].value
[0];
684 /* NB: this can be empty! */
685 if (n
->args
->argv
[i
].sz
) {
686 offs
= n
->args
->argv
[i
].value
[0];
687 dup
= (NULL
!= n
->norm
->d
.Bl
.offs
);
690 mdoc_nmsg(mdoc
, n
, MANDOCERR_IGNARGV
);
696 /* Check: duplicate auxiliary arguments. */
699 mdoc_nmsg(mdoc
, n
, MANDOCERR_ARGVREP
);
702 n
->norm
->d
.Bl
.comp
= comp
;
704 n
->norm
->d
.Bl
.offs
= offs
;
706 n
->norm
->d
.Bl
.width
= width
;
708 /* Check: multiple list types. */
710 if (LIST__NONE
!= lt
&& n
->norm
->d
.Bl
.type
!= LIST__NONE
)
711 mdoc_nmsg(mdoc
, n
, MANDOCERR_LISTREP
);
713 /* Assign list type. */
715 if (LIST__NONE
!= lt
&& n
->norm
->d
.Bl
.type
== LIST__NONE
) {
716 n
->norm
->d
.Bl
.type
= lt
;
717 /* Set column information, too. */
718 if (LIST_column
== lt
) {
719 n
->norm
->d
.Bl
.ncols
=
721 n
->norm
->d
.Bl
.cols
= (const char **)
722 n
->args
->argv
[i
].value
;
726 /* The list type should come first. */
728 if (n
->norm
->d
.Bl
.type
== LIST__NONE
)
729 if (n
->norm
->d
.Bl
.width
||
730 n
->norm
->d
.Bl
.offs
||
732 mdoc_nmsg(mdoc
, n
, MANDOCERR_LISTFIRST
);
737 /* Allow lists to default to LIST_item. */
739 if (LIST__NONE
== n
->norm
->d
.Bl
.type
) {
740 mdoc_nmsg(mdoc
, n
, MANDOCERR_LISTTYPE
);
741 n
->norm
->d
.Bl
.type
= LIST_item
;
745 * Validate the width field. Some list types don't need width
746 * types and should be warned about them. Others should have it
747 * and must also be warned.
750 switch (n
->norm
->d
.Bl
.type
) {
752 if (n
->norm
->d
.Bl
.width
)
754 mdoc_nmsg(mdoc
, n
, MANDOCERR_NOWIDTHARG
);
765 if (n
->norm
->d
.Bl
.width
)
766 mdoc_nmsg(mdoc
, n
, MANDOCERR_IGNARGV
);
782 struct mdoc_node
*np
;
784 if (MDOC_BLOCK
!= n
->type
) {
785 if (ENDBODY_NOT
!= n
->end
) {
787 np
= n
->pending
->parent
;
792 assert(MDOC_BLOCK
== np
->type
);
793 assert(MDOC_Bd
== np
->tok
);
801 for (i
= 0; n
->args
&& i
< (int)n
->args
->argc
; i
++) {
806 switch (n
->args
->argv
[i
].arg
) {
813 case (MDOC_Unfilled
):
823 mdoc_nmsg(mdoc
, n
, MANDOCERR_BADDISP
);
826 /* NB: this can be empty! */
827 if (n
->args
->argv
[i
].sz
) {
828 offs
= n
->args
->argv
[i
].value
[0];
829 dup
= (NULL
!= n
->norm
->d
.Bd
.offs
);
832 mdoc_nmsg(mdoc
, n
, MANDOCERR_IGNARGV
);
836 dup
= n
->norm
->d
.Bd
.comp
;
843 /* Check whether we have duplicates. */
846 mdoc_nmsg(mdoc
, n
, MANDOCERR_ARGVREP
);
848 /* Make our auxiliary assignments. */
851 n
->norm
->d
.Bd
.offs
= offs
;
853 n
->norm
->d
.Bd
.comp
= comp
;
855 /* Check whether a type has already been assigned. */
857 if (DISP__NONE
!= dt
&& n
->norm
->d
.Bd
.type
!= DISP__NONE
)
858 mdoc_nmsg(mdoc
, n
, MANDOCERR_DISPREP
);
860 /* Make our type assignment. */
862 if (DISP__NONE
!= dt
&& n
->norm
->d
.Bd
.type
== DISP__NONE
)
863 n
->norm
->d
.Bd
.type
= dt
;
866 if (DISP__NONE
== n
->norm
->d
.Bd
.type
) {
867 mdoc_nmsg(mdoc
, n
, MANDOCERR_DISPTYPE
);
868 n
->norm
->d
.Bd
.type
= DISP_ragged
;
879 if (MDOC_BLOCK
!= n
->type
)
881 return(check_parent(mdoc
, n
, MDOC_Sh
, MDOC_BODY
));
889 if (MDOC_BLOCK
!= n
->type
)
892 mdoc
->regs
->regs
[(int)REG_nS
].set
= 0;
893 return(check_parent(mdoc
, n
, MDOC_MAX
, MDOC_ROOT
));
901 if (MDOC_BLOCK
!= n
->type
)
904 return(check_parent(mdoc
, n
, MDOC_Bl
, MDOC_BODY
));
918 for (i
= 1; i
< (int)n
->args
->argc
; i
++)
919 mdoc_pmsg(mdoc
, n
->args
->argv
[i
].line
,
920 n
->args
->argv
[i
].pos
, MANDOCERR_IGNARGV
);
922 if (MDOC_Split
== n
->args
->argv
[0].arg
)
923 n
->norm
->d
.An
.auth
= AUTH_split
;
924 else if (MDOC_Nosplit
== n
->args
->argv
[0].arg
)
925 n
->norm
->d
.An
.auth
= AUTH_nosplit
;
936 if (n
->args
&& 1 == n
->args
->argc
)
937 if (MDOC_Std
== n
->args
->argv
[0].arg
)
940 mdoc_nmsg(mdoc
, n
, MANDOCERR_NOARGV
);
948 if (0 == mdoc
->meta
.date
|| mdoc
->meta
.os
)
949 mdoc_nmsg(mdoc
, n
, MANDOCERR_PROLOGOOO
);
951 if (mdoc
->meta
.title
)
952 mdoc_nmsg(mdoc
, n
, MANDOCERR_PROLOGREP
);
961 if (NULL
== mdoc
->meta
.title
|| 0 == mdoc
->meta
.date
)
962 mdoc_nmsg(mdoc
, n
, MANDOCERR_PROLOGOOO
);
965 mdoc_nmsg(mdoc
, n
, MANDOCERR_PROLOGREP
);
974 if (mdoc
->meta
.title
|| mdoc
->meta
.os
)
975 mdoc_nmsg(mdoc
, n
, MANDOCERR_PROLOGOOO
);
978 mdoc_nmsg(mdoc
, n
, MANDOCERR_PROLOGREP
);
987 struct mdoc_node
*np
;
991 * Unlike other data pointers, these are "housed" by the HEAD
992 * element, which contains the goods.
995 if (MDOC_HEAD
!= mdoc
->last
->type
) {
996 if (ENDBODY_NOT
!= mdoc
->last
->end
) {
997 assert(mdoc
->last
->pending
);
998 np
= mdoc
->last
->pending
->parent
->head
;
999 } else if (MDOC_BLOCK
!= mdoc
->last
->type
) {
1000 np
= mdoc
->last
->parent
->head
;
1002 np
= mdoc
->last
->head
;
1005 assert(MDOC_HEAD
== np
->type
);
1006 assert(MDOC_Bf
== np
->tok
);
1007 NORM_BUMP(mdoc
->last
, np
);
1012 assert(MDOC_BLOCK
== np
->parent
->type
);
1013 assert(MDOC_Bf
== np
->parent
->tok
);
1017 * Cannot have both argument and parameter.
1018 * If neither is specified, let it through with a warning.
1021 if (np
->parent
->args
&& np
->child
) {
1022 mdoc_nmsg(mdoc
, np
, MANDOCERR_SYNTARGVCOUNT
);
1024 } else if (NULL
== np
->parent
->args
&& NULL
== np
->child
) {
1025 mdoc_nmsg(mdoc
, np
, MANDOCERR_FONTTYPE
);
1029 /* Extract argument into data. */
1031 if (np
->parent
->args
) {
1032 arg
= np
->parent
->args
->argv
[0].arg
;
1033 if (MDOC_Emphasis
== arg
)
1034 np
->norm
->d
.Bf
.font
= FONT_Em
;
1035 else if (MDOC_Literal
== arg
)
1036 np
->norm
->d
.Bf
.font
= FONT_Li
;
1037 else if (MDOC_Symbolic
== arg
)
1038 np
->norm
->d
.Bf
.font
= FONT_Sy
;
1044 /* Extract parameter into data. */
1046 if (0 == strcmp(np
->child
->string
, "Em"))
1047 np
->norm
->d
.Bf
.font
= FONT_Em
;
1048 else if (0 == strcmp(np
->child
->string
, "Li"))
1049 np
->norm
->d
.Bf
.font
= FONT_Li
;
1050 else if (0 == strcmp(np
->child
->string
, "Sy"))
1051 np
->norm
->d
.Bf
.font
= FONT_Sy
;
1053 mdoc_nmsg(mdoc
, np
, MANDOCERR_FONTTYPE
);
1065 assert(mdoc
->last
->child
);
1066 assert(MDOC_TEXT
== mdoc
->last
->child
->type
);
1068 p
= mdoc_a2lib(mdoc
->last
->child
->string
);
1070 /* If lookup ok, replace with table value. */
1073 free(mdoc
->last
->child
->string
);
1074 mdoc
->last
->child
->string
= mandoc_strdup(p
);
1078 /* If not, use "library ``xxxx''. */
1080 sz
= strlen(mdoc
->last
->child
->string
) +
1081 2 + strlen("\\(lqlibrary\\(rq");
1082 buf
= mandoc_malloc(sz
);
1083 snprintf(buf
, sz
, "library \\(lq%s\\(rq",
1084 mdoc
->last
->child
->string
);
1085 free(mdoc
->last
->child
->string
);
1086 mdoc
->last
->child
->string
= buf
;
1091 post_eoln(POST_ARGS
)
1094 if (mdoc
->last
->child
)
1095 mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_ARGSLOST
);
1103 const struct mdoc_node
*n
;
1106 * The Vt macro comes in both ELEM and BLOCK form, both of which
1107 * have different syntaxes (yet more context-sensitive
1108 * behaviour). ELEM types must have a child; BLOCK types,
1109 * specifically the BODY, should only have TEXT children.
1112 if (MDOC_ELEM
== mdoc
->last
->type
)
1113 return(eerr_ge1(mdoc
));
1114 if (MDOC_BODY
!= mdoc
->last
->type
)
1117 for (n
= mdoc
->last
->child
; n
; n
= n
->next
)
1118 if (MDOC_TEXT
!= n
->type
)
1119 mdoc_nmsg(mdoc
, n
, MANDOCERR_CHILD
);
1130 /* If no child specified, make sure we have the meta name. */
1132 if (NULL
== mdoc
->last
->child
&& NULL
== mdoc
->meta
.name
) {
1133 mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_NONAME
);
1135 } else if (mdoc
->meta
.name
)
1138 /* If no meta name, set it from the child. */
1140 if ( ! concat(mdoc
, buf
, mdoc
->last
->child
, BUFSIZ
))
1143 mdoc
->meta
.name
= mandoc_strdup(buf
);
1149 post_literal(POST_ARGS
)
1153 * The `Dl' (note "el" not "one") and `Bd' macros unset the
1154 * MDOC_LITERAL flag as they leave. Note that `Bd' only sets
1155 * this in literal mode, but it doesn't hurt to just switch it
1156 * off in general since displays can't be nested.
1159 if (MDOC_BODY
== mdoc
->last
->type
)
1160 mdoc
->flags
&= ~MDOC_LITERAL
;
1166 post_defaults(POST_ARGS
)
1168 struct mdoc_node
*nn
;
1171 * The `Ar' defaults to "file ..." if no value is provided as an
1172 * argument; the `Mt' and `Pa' macros use "~"; the `Li' just
1173 * gets an empty string.
1176 if (mdoc
->last
->child
)
1180 mdoc
->next
= MDOC_NEXT_CHILD
;
1184 if ( ! mdoc_word_alloc(mdoc
, nn
->line
, nn
->pos
, "file"))
1186 if ( ! mdoc_word_alloc(mdoc
, nn
->line
, nn
->pos
, "..."))
1190 if ( ! mdoc_word_alloc(mdoc
, nn
->line
, nn
->pos
, "AT&T"))
1192 if ( ! mdoc_word_alloc(mdoc
, nn
->line
, nn
->pos
, "UNIX"))
1196 if ( ! mdoc_word_alloc(mdoc
, nn
->line
, nn
->pos
, ""))
1202 if ( ! mdoc_word_alloc(mdoc
, nn
->line
, nn
->pos
, "~"))
1222 * If we have a child, look it up in the standard keys. If a
1223 * key exist, use that instead of the child; if it doesn't,
1224 * prefix "AT&T UNIX " to the existing data.
1227 if (NULL
== mdoc
->last
->child
)
1230 assert(MDOC_TEXT
== mdoc
->last
->child
->type
);
1231 p
= mdoc_a2att(mdoc
->last
->child
->string
);
1234 free(mdoc
->last
->child
->string
);
1235 mdoc
->last
->child
->string
= mandoc_strdup(p
);
1237 mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_BADATT
);
1239 q
= mdoc
->last
->child
->string
;
1240 sz
= strlen(p
) + strlen(q
) + 1;
1241 buf
= mandoc_malloc(sz
);
1242 strlcpy(buf
, p
, sz
);
1243 strlcat(buf
, q
, sz
);
1244 free(mdoc
->last
->child
->string
);
1245 mdoc
->last
->child
->string
= buf
;
1254 struct mdoc_node
*np
;
1257 if (AUTH__NONE
!= np
->norm
->d
.An
.auth
&& np
->child
)
1258 return(eerr_eq0(mdoc
));
1261 * FIXME: make this ewarn and make sure that the front-ends
1262 * don't print the arguments.
1264 if (AUTH__NONE
!= np
->norm
->d
.An
.auth
|| np
->child
)
1267 mdoc_nmsg(mdoc
, np
, MANDOCERR_NOARGS
);
1277 struct mdoc_node
*n
, *c
;
1280 if (MDOC_BLOCK
!= mdoc
->last
->type
)
1283 n
= mdoc
->last
->parent
->parent
;
1284 lt
= n
->norm
->d
.Bl
.type
;
1286 if (LIST__NONE
== lt
) {
1287 mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_LISTTYPE
);
1293 if (mdoc
->last
->head
->child
)
1295 /* FIXME: give this a dummy value. */
1296 mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_NOARGS
);
1305 if (NULL
== mdoc
->last
->head
->child
)
1306 mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_NOARGS
);
1315 if (NULL
== mdoc
->last
->body
->child
)
1316 mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_NOBODY
);
1319 if (mdoc
->last
->head
->child
)
1320 mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_ARGSLOST
);
1323 cols
= (int)n
->norm
->d
.Bl
.ncols
;
1325 assert(NULL
== mdoc
->last
->head
->child
);
1327 if (NULL
== mdoc
->last
->body
->child
)
1328 mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_NOBODY
);
1330 for (i
= 0, c
= mdoc
->last
->child
; c
; c
= c
->next
)
1331 if (MDOC_BODY
== c
->type
)
1335 er
= MANDOCERR_ARGCOUNT
;
1336 else if (i
== cols
|| i
== cols
+ 1)
1339 er
= MANDOCERR_SYNTARGCOUNT
;
1341 rc
= mdoc_vmsg(mdoc
, er
,
1342 mdoc
->last
->line
, mdoc
->last
->pos
,
1343 "columns == %d (have %d)", cols
, i
);
1353 post_bl_block(POST_ARGS
)
1355 struct mdoc_node
*n
;
1358 * These are fairly complicated, so we've broken them into two
1359 * functions. post_bl_block_tag() is called when a -tag is
1360 * specified, but no -width (it must be guessed). The second
1361 * when a -width is specified (macro indicators must be
1362 * rewritten into real lengths).
1367 if (LIST_tag
== n
->norm
->d
.Bl
.type
&&
1368 NULL
== n
->norm
->d
.Bl
.width
) {
1369 if ( ! post_bl_block_tag(mdoc
))
1371 } else if (NULL
!= n
->norm
->d
.Bl
.width
) {
1372 if ( ! post_bl_block_width(mdoc
))
1377 assert(n
->norm
->d
.Bl
.width
);
1382 post_bl_block_width(POST_ARGS
)
1387 struct mdoc_node
*n
;
1393 * Calculate the real width of a list from the -width string,
1394 * which may contain a macro (with a known default width), a
1395 * literal string, or a scaling width.
1397 * If the value to -width is a macro, then we re-write it to be
1398 * the macro's width as set in share/tmac/mdoc/doc-common.
1401 if (0 == strcmp(n
->norm
->d
.Bl
.width
, "Ds"))
1403 else if (MDOC_MAX
== (tok
= mdoc_hash_find(n
->norm
->d
.Bl
.width
)))
1405 else if (0 == (width
= mdoc_macro2len(tok
))) {
1406 mdoc_nmsg(mdoc
, n
, MANDOCERR_BADWIDTH
);
1410 /* The value already exists: free and reallocate it. */
1414 for (i
= 0; i
< (int)n
->args
->argc
; i
++)
1415 if (MDOC_Width
== n
->args
->argv
[i
].arg
)
1418 assert(i
< (int)n
->args
->argc
);
1420 snprintf(buf
, NUMSIZ
, "%zun", width
);
1421 free(n
->args
->argv
[i
].value
[0]);
1422 n
->args
->argv
[i
].value
[0] = mandoc_strdup(buf
);
1424 /* Set our width! */
1425 n
->norm
->d
.Bl
.width
= n
->args
->argv
[i
].value
[0];
1430 post_bl_block_tag(POST_ARGS
)
1432 struct mdoc_node
*n
, *nn
;
1438 * Calculate the -width for a `Bl -tag' list if it hasn't been
1439 * provided. Uses the first head macro. NOTE AGAIN: this is
1440 * ONLY if the -width argument has NOT been provided. See
1441 * post_bl_block_width() for converting the -width string.
1447 for (nn
= n
->body
->child
; nn
; nn
= nn
->next
) {
1448 if (MDOC_It
!= nn
->tok
)
1451 assert(MDOC_BLOCK
== nn
->type
);
1452 nn
= nn
->head
->child
;
1457 if (MDOC_TEXT
== nn
->type
) {
1458 sz
= strlen(nn
->string
) + 1;
1462 if (0 != (ssz
= mdoc_macro2len(nn
->tok
)))
1468 /* Defaults to ten ens. */
1470 snprintf(buf
, NUMSIZ
, "%zun", sz
);
1473 * We have to dynamically add this to the macro's argument list.
1474 * We're guaranteed that a MDOC_Width doesn't already exist.
1478 i
= (int)(n
->args
->argc
)++;
1480 n
->args
->argv
= mandoc_realloc(n
->args
->argv
,
1481 n
->args
->argc
* sizeof(struct mdoc_argv
));
1483 n
->args
->argv
[i
].arg
= MDOC_Width
;
1484 n
->args
->argv
[i
].line
= n
->line
;
1485 n
->args
->argv
[i
].pos
= n
->pos
;
1486 n
->args
->argv
[i
].sz
= 1;
1487 n
->args
->argv
[i
].value
= mandoc_malloc(sizeof(char *));
1488 n
->args
->argv
[i
].value
[0] = mandoc_strdup(buf
);
1490 /* Set our width! */
1491 n
->norm
->d
.Bl
.width
= n
->args
->argv
[i
].value
[0];
1497 post_bl_head(POST_ARGS
)
1499 struct mdoc_node
*np
, *nn
, *nnp
;
1502 if (LIST_column
!= mdoc
->last
->norm
->d
.Bl
.type
)
1503 /* FIXME: this should be ERROR class... */
1504 return(hwarn_eq0(mdoc
));
1507 * Convert old-style lists, where the column width specifiers
1508 * trail as macro parameters, to the new-style ("normal-form")
1509 * lists where they're argument values following -column.
1512 /* First, disallow both types and allow normal-form. */
1515 * TODO: technically, we can accept both and just merge the two
1516 * lists, but I'll leave that for another day.
1519 if (mdoc
->last
->norm
->d
.Bl
.ncols
&& mdoc
->last
->nchild
) {
1520 mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_COLUMNS
);
1522 } else if (NULL
== mdoc
->last
->child
)
1525 np
= mdoc
->last
->parent
;
1528 for (j
= 0; j
< (int)np
->args
->argc
; j
++)
1529 if (MDOC_Column
== np
->args
->argv
[j
].arg
)
1532 assert(j
< (int)np
->args
->argc
);
1533 assert(0 == np
->args
->argv
[j
].sz
);
1536 * Accomodate for new-style groff column syntax. Shuffle the
1537 * child nodes, all of which must be TEXT, as arguments for the
1538 * column field. Then, delete the head children.
1541 np
->args
->argv
[j
].sz
= (size_t)mdoc
->last
->nchild
;
1542 np
->args
->argv
[j
].value
= mandoc_malloc
1543 ((size_t)mdoc
->last
->nchild
* sizeof(char *));
1545 mdoc
->last
->norm
->d
.Bl
.ncols
= np
->args
->argv
[j
].sz
;
1546 mdoc
->last
->norm
->d
.Bl
.cols
= (const char **)np
->args
->argv
[j
].value
;
1548 for (i
= 0, nn
= mdoc
->last
->child
; nn
; i
++) {
1549 np
->args
->argv
[j
].value
[i
] = nn
->string
;
1553 mdoc_node_delete(NULL
, nnp
);
1556 mdoc
->last
->nchild
= 0;
1557 mdoc
->last
->child
= NULL
;
1565 struct mdoc_node
*n
;
1567 if (MDOC_HEAD
== mdoc
->last
->type
)
1568 return(post_bl_head(mdoc
));
1569 if (MDOC_BLOCK
== mdoc
->last
->type
)
1570 return(post_bl_block(mdoc
));
1571 if (MDOC_BODY
!= mdoc
->last
->type
)
1574 for (n
= mdoc
->last
->child
; n
; n
= n
->next
) {
1581 mdoc_nmsg(mdoc
, n
, MANDOCERR_CHILD
);
1587 mdoc_nmsg(mdoc
, n
, MANDOCERR_SYNTCHILD
);
1595 ebool(struct mdoc
*mdoc
)
1598 if (NULL
== mdoc
->last
->child
)
1601 assert(MDOC_TEXT
== mdoc
->last
->child
->type
);
1603 if (0 == strcmp(mdoc
->last
->child
->string
, "on"))
1605 if (0 == strcmp(mdoc
->last
->child
->string
, "off"))
1608 mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_BADBOOL
);
1613 post_root(POST_ARGS
)
1616 struct mdoc_node
*n
;
1620 /* Check that we have a finished prologue. */
1622 if ( ! (MDOC_PBODY
& mdoc
->flags
)) {
1624 mdoc_nmsg(mdoc
, mdoc
->first
, MANDOCERR_NODOCPROLOG
);
1630 /* Check that we begin with a proper `Sh'. */
1632 if (NULL
== n
->child
) {
1634 mdoc_nmsg(mdoc
, n
, MANDOCERR_NODOCBODY
);
1635 } else if (MDOC_BLOCK
!= n
->child
->type
||
1636 MDOC_Sh
!= n
->child
->tok
) {
1638 /* Can this be lifted? See rxdebug.1 for example. */
1639 mdoc_nmsg(mdoc
, n
, MANDOCERR_NODOCBODY
);
1642 return(erc
? 0 : 1);
1650 assert(MDOC_TEXT
== mdoc
->last
->child
->type
);
1652 p
= mdoc_a2st(mdoc
->last
->child
->string
);
1655 mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_BADSTANDARD
);
1656 mdoc_node_delete(mdoc
, mdoc
->last
);
1658 free(mdoc
->last
->child
->string
);
1659 mdoc
->last
->child
->string
= mandoc_strdup(p
);
1668 struct mdoc_node
*nn
, *next
, *prev
;
1671 if (MDOC_BODY
!= mdoc
->last
->type
)
1675 * Make sure only certain types of nodes are allowed within the
1676 * the `Rs' body. Delete offending nodes and raise a warning.
1677 * Do this before re-ordering for the sake of clarity.
1681 for (nn
= mdoc
->last
->child
; nn
; nn
= next
) {
1682 for (i
= 0; i
< RSORD_MAX
; i
++)
1683 if (nn
->tok
== rsord
[i
])
1686 if (i
< RSORD_MAX
) {
1692 mdoc_nmsg(mdoc
, nn
, MANDOCERR_CHILD
);
1693 mdoc_node_delete(mdoc
, nn
);
1697 * The full `Rs' block needs special handling to order the
1698 * sub-elements according to `rsord'. Pick through each element
1699 * and correctly order it. This is a insertion sort.
1703 for (nn
= mdoc
->last
->child
->next
; nn
; nn
= next
) {
1704 /* Determine order of `nn'. */
1705 for (i
= 0; i
< RSORD_MAX
; i
++)
1706 if (rsord
[i
] == nn
->tok
)
1710 * Remove `nn' from the chain. This somewhat
1711 * repeats mdoc_node_unlink(), but since we're
1712 * just re-ordering, there's no need for the
1713 * full unlink process.
1716 if (NULL
!= (next
= nn
->next
))
1717 next
->prev
= nn
->prev
;
1719 if (NULL
!= (prev
= nn
->prev
))
1720 prev
->next
= nn
->next
;
1722 nn
->prev
= nn
->next
= NULL
;
1725 * Scan back until we reach a node that's
1726 * ordered before `nn'.
1729 for ( ; prev
; prev
= prev
->prev
) {
1730 /* Determine order of `prev'. */
1731 for (j
= 0; j
< RSORD_MAX
; j
++)
1732 if (rsord
[j
] == prev
->tok
)
1740 * Set `nn' back into its correct place in front
1741 * of the `prev' node.
1748 prev
->next
->prev
= nn
;
1749 nn
->next
= prev
->next
;
1752 mdoc
->last
->child
->prev
= nn
;
1753 nn
->next
= mdoc
->last
->child
;
1754 mdoc
->last
->child
= nn
;
1765 if (MDOC_HEAD
== mdoc
->last
->type
)
1766 return(post_sh_head(mdoc
));
1767 if (MDOC_BODY
== mdoc
->last
->type
)
1768 return(post_sh_body(mdoc
));
1774 post_sh_body(POST_ARGS
)
1776 struct mdoc_node
*n
;
1778 if (SEC_NAME
!= mdoc
->lastsec
)
1782 * Warn if the NAME section doesn't contain the `Nm' and `Nd'
1783 * macros (can have multiple `Nm' and one `Nd'). Note that the
1784 * children of the BODY declaration can also be "text".
1787 if (NULL
== (n
= mdoc
->last
->child
)) {
1788 mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_BADNAMESEC
);
1792 for ( ; n
&& n
->next
; n
= n
->next
) {
1793 if (MDOC_ELEM
== n
->type
&& MDOC_Nm
== n
->tok
)
1795 if (MDOC_TEXT
== n
->type
)
1797 mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_BADNAMESEC
);
1801 if (MDOC_BLOCK
== n
->type
&& MDOC_Nd
== n
->tok
)
1804 mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_BADNAMESEC
);
1809 post_sh_head(POST_ARGS
)
1815 * Process a new section. Sections are either "named" or
1816 * "custom". Custom sections are user-defined, while named ones
1817 * follow a conventional order and may only appear in certain
1821 if ( ! concat(mdoc
, buf
, mdoc
->last
->child
, BUFSIZ
))
1824 sec
= mdoc_str2sec(buf
);
1826 /* The NAME should be first. */
1828 if (SEC_NAME
!= sec
&& SEC_NONE
== mdoc
->lastnamed
)
1829 mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_NAMESECFIRST
);
1831 /* The SYNOPSIS gets special attention in other areas. */
1833 if (SEC_SYNOPSIS
== sec
)
1834 mdoc
->flags
|= MDOC_SYNOPSIS
;
1836 mdoc
->flags
&= ~MDOC_SYNOPSIS
;
1838 /* Mark our last section. */
1840 mdoc
->lastsec
= sec
;
1842 /* We don't care about custom sections after this. */
1844 if (SEC_CUSTOM
== sec
)
1848 * Check whether our non-custom section is being repeated or is
1852 if (sec
== mdoc
->lastnamed
)
1853 mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_SECREP
);
1855 if (sec
< mdoc
->lastnamed
)
1856 mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_SECOOO
);
1858 /* Mark the last named section. */
1860 mdoc
->lastnamed
= sec
;
1862 /* Check particular section/manual conventions. */
1864 assert(mdoc
->meta
.msec
);
1867 case (SEC_RETURN_VALUES
):
1872 if (*mdoc
->meta
.msec
== '2')
1874 if (*mdoc
->meta
.msec
== '3')
1876 if (*mdoc
->meta
.msec
== '9')
1878 mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_SECMSEC
);
1888 post_ignpar(POST_ARGS
)
1890 struct mdoc_node
*np
;
1892 if (MDOC_BODY
!= mdoc
->last
->type
)
1895 if (NULL
!= (np
= mdoc
->last
->child
))
1896 if (MDOC_Pp
== np
->tok
|| MDOC_Lp
== np
->tok
) {
1897 mdoc_nmsg(mdoc
, np
, MANDOCERR_IGNPAR
);
1898 mdoc_node_delete(mdoc
, np
);
1901 if (NULL
!= (np
= mdoc
->last
->last
))
1902 if (MDOC_Pp
== np
->tok
|| MDOC_Lp
== np
->tok
) {
1903 mdoc_nmsg(mdoc
, np
, MANDOCERR_IGNPAR
);
1904 mdoc_node_delete(mdoc
, np
);
1914 if (NULL
== mdoc
->last
)
1916 if (MDOC_ELEM
!= n
->type
&& MDOC_BLOCK
!= n
->type
)
1920 * Don't allow prior `Lp' or `Pp' prior to a paragraph-type
1921 * block: `Lp', `Pp', or non-compact `Bd' or `Bl'.
1924 if (MDOC_Pp
!= mdoc
->last
->tok
&& MDOC_Lp
!= mdoc
->last
->tok
)
1926 if (MDOC_Bl
== n
->tok
&& n
->norm
->d
.Bl
.comp
)
1928 if (MDOC_Bd
== n
->tok
&& n
->norm
->d
.Bd
.comp
)
1930 if (MDOC_It
== n
->tok
&& n
->parent
->norm
->d
.Bl
.comp
)
1933 mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_IGNPAR
);
1934 mdoc_node_delete(mdoc
, mdoc
->last
);
1939 pre_literal(PRE_ARGS
)
1942 if (MDOC_BODY
!= n
->type
)
1946 * The `Dl' (note "el" not "one") and `Bd -literal' and `Bd
1947 * -unfilled' macros set MDOC_LITERAL on entrance to the body.
1952 mdoc
->flags
|= MDOC_LITERAL
;
1955 if (DISP_literal
== n
->norm
->d
.Bd
.type
)
1956 mdoc
->flags
|= MDOC_LITERAL
;
1957 if (DISP_unfilled
== n
->norm
->d
.Bd
.type
)
1958 mdoc
->flags
|= MDOC_LITERAL
;
1972 struct mdoc_node
*n
;
1976 if (NULL
== n
->child
) {
1977 mdoc
->meta
.date
= time(NULL
);
1981 if ( ! concat(mdoc
, buf
, n
->child
, DATESIZE
))
1984 mdoc
->meta
.date
= mandoc_a2time
1985 (MTIME_MDOCDATE
| MTIME_CANONICAL
, buf
);
1987 if (0 == mdoc
->meta
.date
) {
1988 mdoc_nmsg(mdoc
, n
, MANDOCERR_BADDATE
);
1989 mdoc
->meta
.date
= time(NULL
);
1998 struct mdoc_node
*nn
, *n
;
2004 if (mdoc
->meta
.title
)
2005 free(mdoc
->meta
.title
);
2007 free(mdoc
->meta
.vol
);
2008 if (mdoc
->meta
.arch
)
2009 free(mdoc
->meta
.arch
);
2011 mdoc
->meta
.title
= mdoc
->meta
.vol
= mdoc
->meta
.arch
= NULL
;
2013 /* First make all characters uppercase. */
2015 if (NULL
!= (nn
= n
->child
))
2016 for (p
= nn
->string
; *p
; p
++) {
2017 if (toupper((u_char
)*p
) == *p
)
2021 * FIXME: don't be lazy: have this make all
2022 * characters be uppercase and just warn once.
2024 mdoc_nmsg(mdoc
, nn
, MANDOCERR_UPPERCASE
);
2029 * --> title = unknown, volume = local, msec = 0, arch = NULL
2032 if (NULL
== (nn
= n
->child
)) {
2033 /* XXX: make these macro values. */
2034 /* FIXME: warn about missing values. */
2035 mdoc
->meta
.title
= mandoc_strdup("UNKNOWN");
2036 mdoc
->meta
.vol
= mandoc_strdup("LOCAL");
2037 mdoc
->meta
.msec
= mandoc_strdup("1");
2041 /* Handles: `.Dt TITLE'
2042 * --> title = TITLE, volume = local, msec = 0, arch = NULL
2045 mdoc
->meta
.title
= mandoc_strdup
2046 ('\0' == nn
->string
[0] ? "UNKNOWN" : nn
->string
);
2048 if (NULL
== (nn
= nn
->next
)) {
2049 /* FIXME: warn about missing msec. */
2050 /* XXX: make this a macro value. */
2051 mdoc
->meta
.vol
= mandoc_strdup("LOCAL");
2052 mdoc
->meta
.msec
= mandoc_strdup("1");
2056 /* Handles: `.Dt TITLE SEC'
2057 * --> title = TITLE, volume = SEC is msec ?
2058 * format(msec) : SEC,
2059 * msec = SEC is msec ? atoi(msec) : 0,
2063 cp
= mdoc_a2msec(nn
->string
);
2065 mdoc
->meta
.vol
= mandoc_strdup(cp
);
2066 mdoc
->meta
.msec
= mandoc_strdup(nn
->string
);
2068 mdoc_nmsg(mdoc
, n
, MANDOCERR_BADMSEC
);
2069 mdoc
->meta
.vol
= mandoc_strdup(nn
->string
);
2070 mdoc
->meta
.msec
= mandoc_strdup(nn
->string
);
2073 if (NULL
== (nn
= nn
->next
))
2076 /* Handles: `.Dt TITLE SEC VOL'
2077 * --> title = TITLE, volume = VOL is vol ?
2079 * VOL is arch ? format(arch) :
2083 cp
= mdoc_a2vol(nn
->string
);
2085 free(mdoc
->meta
.vol
);
2086 mdoc
->meta
.vol
= mandoc_strdup(cp
);
2088 /* FIXME: warn about bad arch. */
2089 cp
= mdoc_a2arch(nn
->string
);
2091 free(mdoc
->meta
.vol
);
2092 mdoc
->meta
.vol
= mandoc_strdup(nn
->string
);
2094 mdoc
->meta
.arch
= mandoc_strdup(cp
);
2097 /* Ignore any subsequent parameters... */
2098 /* FIXME: warn about subsequent parameters. */
2104 post_prol(POST_ARGS
)
2107 * Remove prologue macros from the document after they're
2108 * processed. The final document uses mdoc_meta for these
2109 * values and discards the originals.
2112 mdoc_node_delete(mdoc
, mdoc
->last
);
2113 if (mdoc
->meta
.title
&& mdoc
->meta
.date
&& mdoc
->meta
.os
)
2114 mdoc
->flags
|= MDOC_PBODY
;
2122 struct mdoc_node
*n
;
2125 struct utsname utsname
;
2131 * Set the operating system by way of the `Os' macro. Note that
2132 * if an argument isn't provided and -DOSNAME="\"foo\"" is
2133 * provided during compilation, this value will be used instead
2134 * of filling in "sysname release" from uname().
2138 free(mdoc
->meta
.os
);
2140 if ( ! concat(mdoc
, buf
, n
->child
, BUFSIZ
))
2143 /* XXX: yes, these can all be dynamically-adjusted buffers, but
2144 * it's really not worth the extra hackery.
2147 if ('\0' == buf
[0]) {
2149 if (strlcat(buf
, OSNAME
, BUFSIZ
) >= BUFSIZ
) {
2150 mdoc_nmsg(mdoc
, n
, MANDOCERR_MEM
);
2154 if (uname(&utsname
)) {
2155 mdoc_nmsg(mdoc
, n
, MANDOCERR_UNAME
);
2156 mdoc
->meta
.os
= mandoc_strdup("UNKNOWN");
2157 return(post_prol(mdoc
));
2160 if (strlcat(buf
, utsname
.sysname
, BUFSIZ
) >= BUFSIZ
) {
2161 mdoc_nmsg(mdoc
, n
, MANDOCERR_MEM
);
2164 if (strlcat(buf
, " ", BUFSIZ
) >= BUFSIZ
) {
2165 mdoc_nmsg(mdoc
, n
, MANDOCERR_MEM
);
2168 if (strlcat(buf
, utsname
.release
, BUFSIZ
) >= BUFSIZ
) {
2169 mdoc_nmsg(mdoc
, n
, MANDOCERR_MEM
);
2175 mdoc
->meta
.os
= mandoc_strdup(buf
);
2182 struct mdoc_node
*nn
, *n
;
2187 * Macros accepting `-std' as an argument have the name of the
2188 * current document (`Nm') filled in as the argument if it's not
2195 if (NULL
== mdoc
->meta
.name
)
2199 mdoc
->next
= MDOC_NEXT_CHILD
;
2201 if ( ! mdoc_word_alloc(mdoc
, n
->line
, n
->pos
, mdoc
->meta
.name
))
2209 concat(struct mdoc
*m
, char *p
, const struct mdoc_node
*n
, size_t sz
)
2215 * Concatenate sibling nodes together. All siblings must be of
2216 * type MDOC_TEXT or an assertion is raised. Concatenation is
2217 * separated by a single whitespace. Returns 0 on fatal (string
2221 for ( ; n
; n
= n
->next
) {
2222 assert(MDOC_TEXT
== n
->type
);
2224 if (strlcat(p
, n
->string
, sz
) >= sz
) {
2225 mdoc_nmsg(m
, n
, MANDOCERR_MEM
);
2229 if (NULL
== n
->next
)
2232 if (strlcat(p
, " ", sz
) >= sz
) {
2233 mdoc_nmsg(m
, n
, MANDOCERR_MEM
);