]>
git.cameronkatri.com Git - mandoc.git/blob - mdoc_validate.c
1 /* $Id: mdoc_validate.c,v 1.153 2011/01/25 10:37:49 kristaps 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 ewarn_eq0(POST_ARGS
);
82 static int ewarn_eq1(POST_ARGS
);
83 static int ewarn_ge1(POST_ARGS
);
84 static int ewarn_le1(POST_ARGS
);
85 static int hwarn_eq0(POST_ARGS
);
86 static int hwarn_eq1(POST_ARGS
);
87 static int hwarn_ge1(POST_ARGS
);
88 static int hwarn_le1(POST_ARGS
);
90 static int post_an(POST_ARGS
);
91 static int post_at(POST_ARGS
);
92 static int post_bf(POST_ARGS
);
93 static int post_bl(POST_ARGS
);
94 static int post_bl_block(POST_ARGS
);
95 static int post_bl_block_width(POST_ARGS
);
96 static int post_bl_block_tag(POST_ARGS
);
97 static int post_bl_head(POST_ARGS
);
98 static int post_dd(POST_ARGS
);
99 static int post_dt(POST_ARGS
);
100 static int post_defaults(POST_ARGS
);
101 static int post_literal(POST_ARGS
);
102 static int post_eoln(POST_ARGS
);
103 static int post_it(POST_ARGS
);
104 static int post_lb(POST_ARGS
);
105 static int post_nm(POST_ARGS
);
106 static int post_os(POST_ARGS
);
107 static int post_ignpar(POST_ARGS
);
108 static int post_prol(POST_ARGS
);
109 static int post_root(POST_ARGS
);
110 static int post_rs(POST_ARGS
);
111 static int post_sh(POST_ARGS
);
112 static int post_sh_body(POST_ARGS
);
113 static int post_sh_head(POST_ARGS
);
114 static int post_st(POST_ARGS
);
115 static int post_std(POST_ARGS
);
116 static int post_vt(POST_ARGS
);
117 static int pre_an(PRE_ARGS
);
118 static int pre_bd(PRE_ARGS
);
119 static int pre_bl(PRE_ARGS
);
120 static int pre_dd(PRE_ARGS
);
121 static int pre_display(PRE_ARGS
);
122 static int pre_dt(PRE_ARGS
);
123 static int pre_it(PRE_ARGS
);
124 static int pre_literal(PRE_ARGS
);
125 static int pre_os(PRE_ARGS
);
126 static int pre_par(PRE_ARGS
);
127 static int pre_sh(PRE_ARGS
);
128 static int pre_ss(PRE_ARGS
);
129 static int pre_std(PRE_ARGS
);
131 static v_post posts_an
[] = { post_an
, NULL
};
132 static v_post posts_at
[] = { post_at
, post_defaults
, NULL
};
133 static v_post posts_bd
[] = { post_literal
, hwarn_eq0
, bwarn_ge1
, NULL
};
134 static v_post posts_bf
[] = { hwarn_le1
, post_bf
, NULL
};
135 static v_post posts_bk
[] = { hwarn_eq0
, bwarn_ge1
, NULL
};
136 static v_post posts_bl
[] = { bwarn_ge1
, post_bl
, NULL
};
137 static v_post posts_bool
[] = { ebool
, NULL
};
138 static v_post posts_eoln
[] = { post_eoln
, NULL
};
139 static v_post posts_defaults
[] = { post_defaults
, NULL
};
140 static v_post posts_dd
[] = { ewarn_ge1
, post_dd
, post_prol
, NULL
};
141 static v_post posts_dl
[] = { post_literal
, bwarn_ge1
, NULL
};
142 static v_post posts_dt
[] = { post_dt
, post_prol
, NULL
};
143 static v_post posts_fo
[] = { hwarn_eq1
, bwarn_ge1
, NULL
};
144 static v_post posts_it
[] = { post_it
, NULL
};
145 static v_post posts_lb
[] = { post_lb
, NULL
};
146 static v_post posts_nd
[] = { berr_ge1
, NULL
};
147 static v_post posts_nm
[] = { post_nm
, NULL
};
148 static v_post posts_notext
[] = { ewarn_eq0
, NULL
};
149 static v_post posts_os
[] = { post_os
, post_prol
, NULL
};
150 static v_post posts_rs
[] = { post_rs
, NULL
};
151 static v_post posts_sh
[] = { post_ignpar
, hwarn_ge1
, bwarn_ge1
, post_sh
, NULL
};
152 static v_post posts_sp
[] = { ewarn_le1
, NULL
};
153 static v_post posts_ss
[] = { post_ignpar
, hwarn_ge1
, bwarn_ge1
, NULL
};
154 static v_post posts_st
[] = { post_st
, NULL
};
155 static v_post posts_std
[] = { post_std
, NULL
};
156 static v_post posts_text
[] = { ewarn_ge1
, NULL
};
157 static v_post posts_text1
[] = { ewarn_eq1
, NULL
};
158 static v_post posts_vt
[] = { post_vt
, NULL
};
159 static v_post posts_wline
[] = { bwarn_ge1
, NULL
};
160 static v_pre pres_an
[] = { pre_an
, NULL
};
161 static v_pre pres_bd
[] = { pre_display
, pre_bd
, pre_literal
, pre_par
, NULL
};
162 static v_pre pres_bl
[] = { pre_bl
, pre_par
, NULL
};
163 static v_pre pres_d1
[] = { pre_display
, NULL
};
164 static v_pre pres_dl
[] = { pre_literal
, pre_display
, NULL
};
165 static v_pre pres_dd
[] = { pre_dd
, NULL
};
166 static v_pre pres_dt
[] = { pre_dt
, NULL
};
167 static v_pre pres_er
[] = { NULL
, NULL
};
168 static v_pre pres_fd
[] = { NULL
, NULL
};
169 static v_pre pres_it
[] = { pre_it
, pre_par
, NULL
};
170 static v_pre pres_os
[] = { pre_os
, NULL
};
171 static v_pre pres_pp
[] = { pre_par
, NULL
};
172 static v_pre pres_sh
[] = { pre_sh
, NULL
};
173 static v_pre pres_ss
[] = { pre_ss
, NULL
};
174 static v_pre pres_std
[] = { pre_std
, NULL
};
176 const struct valids mdoc_valids
[MDOC_MAX
] = {
177 { NULL
, NULL
}, /* Ap */
178 { pres_dd
, posts_dd
}, /* Dd */
179 { pres_dt
, posts_dt
}, /* Dt */
180 { pres_os
, posts_os
}, /* Os */
181 { pres_sh
, posts_sh
}, /* Sh */
182 { pres_ss
, posts_ss
}, /* Ss */
183 { pres_pp
, posts_notext
}, /* Pp */
184 { pres_d1
, posts_wline
}, /* D1 */
185 { pres_dl
, posts_dl
}, /* Dl */
186 { pres_bd
, posts_bd
}, /* Bd */
187 { NULL
, NULL
}, /* Ed */
188 { pres_bl
, posts_bl
}, /* Bl */
189 { NULL
, NULL
}, /* El */
190 { pres_it
, posts_it
}, /* It */
191 { NULL
, NULL
}, /* Ad */
192 { pres_an
, posts_an
}, /* An */
193 { NULL
, posts_defaults
}, /* Ar */
194 { NULL
, NULL
}, /* Cd */
195 { NULL
, NULL
}, /* Cm */
196 { NULL
, NULL
}, /* Dv */
197 { pres_er
, NULL
}, /* Er */
198 { NULL
, NULL
}, /* Ev */
199 { pres_std
, posts_std
}, /* Ex */
200 { NULL
, NULL
}, /* Fa */
201 { pres_fd
, posts_text
}, /* Fd */
202 { NULL
, NULL
}, /* Fl */
203 { NULL
, NULL
}, /* Fn */
204 { NULL
, NULL
}, /* Ft */
205 { NULL
, NULL
}, /* Ic */
206 { NULL
, posts_text1
}, /* In */
207 { NULL
, posts_defaults
}, /* Li */
208 { NULL
, posts_nd
}, /* Nd */
209 { NULL
, posts_nm
}, /* Nm */
210 { NULL
, NULL
}, /* Op */
211 { NULL
, NULL
}, /* Ot */
212 { NULL
, posts_defaults
}, /* Pa */
213 { pres_std
, posts_std
}, /* Rv */
214 { NULL
, posts_st
}, /* St */
215 { NULL
, NULL
}, /* Va */
216 { NULL
, posts_vt
}, /* Vt */
217 { NULL
, posts_text
}, /* Xr */
218 { NULL
, posts_text
}, /* %A */
219 { NULL
, posts_text
}, /* %B */ /* FIXME: can be used outside Rs/Re. */
220 { NULL
, posts_text
}, /* %D */ /* FIXME: check date with mandoc_a2time(). */
221 { NULL
, posts_text
}, /* %I */
222 { NULL
, posts_text
}, /* %J */
223 { NULL
, posts_text
}, /* %N */
224 { NULL
, posts_text
}, /* %O */
225 { NULL
, posts_text
}, /* %P */
226 { NULL
, posts_text
}, /* %R */
227 { NULL
, posts_text
}, /* %T */ /* FIXME: can be used outside Rs/Re. */
228 { NULL
, posts_text
}, /* %V */
229 { NULL
, NULL
}, /* Ac */
230 { NULL
, NULL
}, /* Ao */
231 { NULL
, NULL
}, /* Aq */
232 { NULL
, posts_at
}, /* At */
233 { NULL
, NULL
}, /* Bc */
234 { NULL
, posts_bf
}, /* Bf */
235 { NULL
, NULL
}, /* Bo */
236 { NULL
, NULL
}, /* Bq */
237 { NULL
, NULL
}, /* Bsx */
238 { NULL
, NULL
}, /* Bx */
239 { NULL
, posts_bool
}, /* Db */
240 { NULL
, NULL
}, /* Dc */
241 { NULL
, NULL
}, /* Do */
242 { NULL
, NULL
}, /* Dq */
243 { NULL
, NULL
}, /* Ec */
244 { NULL
, NULL
}, /* Ef */
245 { NULL
, NULL
}, /* Em */
246 { NULL
, NULL
}, /* Eo */
247 { NULL
, NULL
}, /* Fx */
248 { NULL
, NULL
}, /* Ms */
249 { NULL
, posts_notext
}, /* No */
250 { NULL
, posts_notext
}, /* Ns */
251 { NULL
, NULL
}, /* Nx */
252 { NULL
, NULL
}, /* Ox */
253 { NULL
, NULL
}, /* Pc */
254 { NULL
, posts_text1
}, /* Pf */
255 { NULL
, NULL
}, /* Po */
256 { NULL
, NULL
}, /* Pq */
257 { NULL
, NULL
}, /* Qc */
258 { NULL
, NULL
}, /* Ql */
259 { NULL
, NULL
}, /* Qo */
260 { NULL
, NULL
}, /* Qq */
261 { NULL
, NULL
}, /* Re */
262 { NULL
, posts_rs
}, /* Rs */
263 { NULL
, NULL
}, /* Sc */
264 { NULL
, NULL
}, /* So */
265 { NULL
, NULL
}, /* Sq */
266 { NULL
, posts_bool
}, /* Sm */
267 { NULL
, NULL
}, /* Sx */
268 { NULL
, NULL
}, /* Sy */
269 { NULL
, NULL
}, /* Tn */
270 { NULL
, NULL
}, /* Ux */
271 { NULL
, NULL
}, /* Xc */
272 { NULL
, NULL
}, /* Xo */
273 { NULL
, posts_fo
}, /* Fo */
274 { NULL
, NULL
}, /* Fc */
275 { NULL
, NULL
}, /* Oo */
276 { NULL
, NULL
}, /* Oc */
277 { NULL
, posts_bk
}, /* Bk */
278 { NULL
, NULL
}, /* Ek */
279 { NULL
, posts_eoln
}, /* Bt */
280 { NULL
, NULL
}, /* Hf */
281 { NULL
, NULL
}, /* Fr */
282 { NULL
, posts_eoln
}, /* Ud */
283 { NULL
, posts_lb
}, /* Lb */
284 { NULL
, posts_notext
}, /* Lp */
285 { NULL
, NULL
}, /* Lk */
286 { NULL
, posts_defaults
}, /* Mt */
287 { NULL
, NULL
}, /* Brq */
288 { NULL
, NULL
}, /* Bro */
289 { NULL
, NULL
}, /* Brc */
290 { NULL
, posts_text
}, /* %C */
291 { NULL
, NULL
}, /* Es */
292 { NULL
, NULL
}, /* En */
293 { NULL
, NULL
}, /* Dx */
294 { NULL
, posts_text
}, /* %Q */
295 { NULL
, posts_notext
}, /* br */
296 { pres_pp
, posts_sp
}, /* sp */
297 { NULL
, posts_text1
}, /* %U */
298 { NULL
, NULL
}, /* Ta */
301 #define RSORD_MAX 14 /* Number of `Rs' blocks. */
303 static const enum mdoct rsord
[RSORD_MAX
] = {
322 mdoc_valid_pre(struct mdoc
*mdoc
, struct mdoc_node
*n
)
333 check_text(mdoc
, line
, pos
, tp
);
345 if (NULL
== mdoc_valids
[n
->tok
].pre
)
347 for (p
= mdoc_valids
[n
->tok
].pre
; *p
; p
++)
348 if ( ! (*p
)(mdoc
, n
))
355 mdoc_valid_post(struct mdoc
*mdoc
)
359 if (MDOC_VALID
& mdoc
->last
->flags
)
361 mdoc
->last
->flags
|= MDOC_VALID
;
363 switch (mdoc
->last
->type
) {
369 return(post_root(mdoc
));
374 if (NULL
== mdoc_valids
[mdoc
->last
->tok
].post
)
376 for (p
= mdoc_valids
[mdoc
->last
->tok
].post
; *p
; p
++)
384 check_count(struct mdoc
*m
, enum mdoc_type type
,
385 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 t
= lvl
== CHECK_WARN
? MANDOCERR_ARGCWARN
: MANDOCERR_ARGCOUNT
;
416 return(mdoc_vmsg(m
, t
, m
->last
->line
, m
->last
->pos
,
417 "want %s%d children (have %d)",
418 p
, val
, m
->last
->nchild
));
425 return(check_count(mdoc
, MDOC_BODY
, CHECK_ERROR
, CHECK_GT
, 0));
431 return(check_count(mdoc
, MDOC_BODY
, CHECK_WARN
, CHECK_GT
, 0));
437 return(check_count(mdoc
, MDOC_ELEM
, CHECK_WARN
, CHECK_EQ
, 0));
443 return(check_count(mdoc
, MDOC_ELEM
, CHECK_WARN
, CHECK_EQ
, 1));
449 return(check_count(mdoc
, MDOC_ELEM
, CHECK_WARN
, CHECK_GT
, 0));
455 return(check_count(mdoc
, MDOC_ELEM
, CHECK_WARN
, CHECK_LT
, 2));
461 return(check_count(mdoc
, MDOC_HEAD
, CHECK_WARN
, CHECK_EQ
, 0));
467 return(check_count(mdoc
, MDOC_HEAD
, CHECK_WARN
, CHECK_EQ
, 1));
473 return(check_count(mdoc
, MDOC_HEAD
, CHECK_WARN
, CHECK_GT
, 0));
479 return(check_count(mdoc
, MDOC_HEAD
, CHECK_WARN
, CHECK_LT
, 2));
483 check_args(struct mdoc
*m
, struct mdoc_node
*n
)
490 assert(n
->args
->argc
);
491 for (i
= 0; i
< (int)n
->args
->argc
; i
++)
492 check_argv(m
, n
, &n
->args
->argv
[i
]);
496 check_argv(struct mdoc
*m
, struct mdoc_node
*n
, struct mdoc_argv
*v
)
500 for (i
= 0; i
< (int)v
->sz
; i
++)
501 check_text(m
, v
->line
, v
->pos
, v
->value
[i
]);
503 /* FIXME: move to post_std(). */
505 if (MDOC_Std
== v
->arg
)
506 if ( ! (v
->sz
|| m
->meta
.name
))
507 mdoc_nmsg(m
, n
, MANDOCERR_NONAME
);
511 check_text(struct mdoc
*m
, int ln
, int pos
, char *p
)
516 for ( ; *p
; p
++, pos
++) {
517 sz
= strcspn(p
, "\t\\");
526 if ( ! (MDOC_LITERAL
& m
->flags
))
527 mdoc_pmsg(m
, ln
, pos
, MANDOCERR_BADTAB
);
531 if (0 == (c
= mandoc_special(p
))) {
532 mdoc_pmsg(m
, ln
, pos
, MANDOCERR_BADESCAPE
);
542 check_parent(PRE_ARGS
, enum mdoct tok
, enum mdoc_type t
)
546 if ((MDOC_ROOT
== t
|| tok
== n
->parent
->tok
) &&
547 (t
== n
->parent
->type
))
550 mdoc_vmsg(mdoc
, MANDOCERR_SYNTCHILD
,
551 n
->line
, n
->pos
, "want parent %s",
552 MDOC_ROOT
== t
? "<root>" :
553 mdoc_macronames
[tok
]);
559 pre_display(PRE_ARGS
)
561 struct mdoc_node
*node
;
563 if (MDOC_BLOCK
!= n
->type
)
566 for (node
= mdoc
->last
->parent
; node
; node
= node
->parent
)
567 if (MDOC_BLOCK
== node
->type
)
568 if (MDOC_Bd
== node
->tok
)
572 mdoc_nmsg(mdoc
, n
, MANDOCERR_NESTEDDISP
);
582 const char *offs
, *width
;
584 struct mdoc_node
*np
;
586 if (MDOC_BLOCK
!= n
->type
) {
587 if (ENDBODY_NOT
!= n
->end
) {
589 np
= n
->pending
->parent
;
594 assert(MDOC_BLOCK
== np
->type
);
595 assert(MDOC_Bl
== np
->tok
);
600 * First figure out which kind of list to use: bind ourselves to
601 * the first mentioned list type and warn about any remaining
602 * ones. If we find no list type, we default to LIST_item.
606 for (i
= 0; n
->args
&& i
< (int)n
->args
->argc
; i
++) {
610 switch (n
->args
->argv
[i
].arg
) {
611 /* Set list types. */
645 /* Set list arguments. */
647 dup
= n
->norm
->Bl
.comp
;
651 dup
= (NULL
!= n
->norm
->Bl
.width
);
652 width
= n
->args
->argv
[i
].value
[0];
655 /* NB: this can be empty! */
656 if (n
->args
->argv
[i
].sz
) {
657 offs
= n
->args
->argv
[i
].value
[0];
658 dup
= (NULL
!= n
->norm
->Bl
.offs
);
661 mdoc_nmsg(mdoc
, n
, MANDOCERR_IGNARGV
);
667 /* Check: duplicate auxiliary arguments. */
670 mdoc_nmsg(mdoc
, n
, MANDOCERR_ARGVREP
);
673 n
->norm
->Bl
.comp
= comp
;
675 n
->norm
->Bl
.offs
= offs
;
677 n
->norm
->Bl
.width
= width
;
679 /* Check: multiple list types. */
681 if (LIST__NONE
!= lt
&& n
->norm
->Bl
.type
!= LIST__NONE
)
682 mdoc_nmsg(mdoc
, n
, MANDOCERR_LISTREP
);
684 /* Assign list type. */
686 if (LIST__NONE
!= lt
&& n
->norm
->Bl
.type
== LIST__NONE
) {
687 n
->norm
->Bl
.type
= lt
;
688 /* Set column information, too. */
689 if (LIST_column
== lt
) {
692 n
->norm
->Bl
.cols
= (const char **)
693 n
->args
->argv
[i
].value
;
697 /* The list type should come first. */
699 if (n
->norm
->Bl
.type
== LIST__NONE
)
700 if (n
->norm
->Bl
.width
||
703 mdoc_nmsg(mdoc
, n
, MANDOCERR_LISTFIRST
);
708 /* Allow lists to default to LIST_item. */
710 if (LIST__NONE
== n
->norm
->Bl
.type
) {
711 mdoc_nmsg(mdoc
, n
, MANDOCERR_LISTTYPE
);
712 n
->norm
->Bl
.type
= LIST_item
;
716 * Validate the width field. Some list types don't need width
717 * types and should be warned about them. Others should have it
718 * and must also be warned.
721 switch (n
->norm
->Bl
.type
) {
723 if (n
->norm
->Bl
.width
)
725 mdoc_nmsg(mdoc
, n
, MANDOCERR_NOWIDTHARG
);
736 if (n
->norm
->Bl
.width
)
737 mdoc_nmsg(mdoc
, n
, MANDOCERR_IGNARGV
);
753 struct mdoc_node
*np
;
755 if (MDOC_BLOCK
!= n
->type
) {
756 if (ENDBODY_NOT
!= n
->end
) {
758 np
= n
->pending
->parent
;
763 assert(MDOC_BLOCK
== np
->type
);
764 assert(MDOC_Bd
== np
->tok
);
769 for (i
= 0; n
->args
&& i
< (int)n
->args
->argc
; i
++) {
774 switch (n
->args
->argv
[i
].arg
) {
781 case (MDOC_Unfilled
):
791 mdoc_nmsg(mdoc
, n
, MANDOCERR_BADDISP
);
794 /* NB: this can be empty! */
795 if (n
->args
->argv
[i
].sz
) {
796 offs
= n
->args
->argv
[i
].value
[0];
797 dup
= (NULL
!= n
->norm
->Bd
.offs
);
800 mdoc_nmsg(mdoc
, n
, MANDOCERR_IGNARGV
);
804 dup
= n
->norm
->Bd
.comp
;
811 /* Check whether we have duplicates. */
814 mdoc_nmsg(mdoc
, n
, MANDOCERR_ARGVREP
);
816 /* Make our auxiliary assignments. */
819 n
->norm
->Bd
.offs
= offs
;
821 n
->norm
->Bd
.comp
= comp
;
823 /* Check whether a type has already been assigned. */
825 if (DISP__NONE
!= dt
&& n
->norm
->Bd
.type
!= DISP__NONE
)
826 mdoc_nmsg(mdoc
, n
, MANDOCERR_DISPREP
);
828 /* Make our type assignment. */
830 if (DISP__NONE
!= dt
&& n
->norm
->Bd
.type
== DISP__NONE
)
831 n
->norm
->Bd
.type
= dt
;
834 if (DISP__NONE
== n
->norm
->Bd
.type
) {
835 mdoc_nmsg(mdoc
, n
, MANDOCERR_DISPTYPE
);
836 n
->norm
->Bd
.type
= DISP_ragged
;
847 if (MDOC_BLOCK
!= n
->type
)
849 return(check_parent(mdoc
, n
, MDOC_Sh
, MDOC_BODY
));
857 if (MDOC_BLOCK
!= n
->type
)
860 mdoc
->regs
->regs
[(int)REG_nS
].set
= 0;
861 return(check_parent(mdoc
, n
, MDOC_MAX
, MDOC_ROOT
));
869 if (MDOC_BLOCK
!= n
->type
)
872 return(check_parent(mdoc
, n
, MDOC_Bl
, MDOC_BODY
));
884 for (i
= 1; i
< (int)n
->args
->argc
; i
++)
885 mdoc_pmsg(mdoc
, n
->args
->argv
[i
].line
,
886 n
->args
->argv
[i
].pos
, MANDOCERR_IGNARGV
);
888 if (MDOC_Split
== n
->args
->argv
[0].arg
)
889 n
->norm
->An
.auth
= AUTH_split
;
890 else if (MDOC_Nosplit
== n
->args
->argv
[0].arg
)
891 n
->norm
->An
.auth
= AUTH_nosplit
;
902 if (n
->args
&& 1 == n
->args
->argc
)
903 if (MDOC_Std
== n
->args
->argv
[0].arg
)
906 mdoc_nmsg(mdoc
, n
, MANDOCERR_NOARGV
);
914 if (0 == mdoc
->meta
.date
|| mdoc
->meta
.os
)
915 mdoc_nmsg(mdoc
, n
, MANDOCERR_PROLOGOOO
);
917 if (mdoc
->meta
.title
)
918 mdoc_nmsg(mdoc
, n
, MANDOCERR_PROLOGREP
);
927 if (NULL
== mdoc
->meta
.title
|| 0 == mdoc
->meta
.date
)
928 mdoc_nmsg(mdoc
, n
, MANDOCERR_PROLOGOOO
);
931 mdoc_nmsg(mdoc
, n
, MANDOCERR_PROLOGREP
);
940 if (mdoc
->meta
.title
|| mdoc
->meta
.os
)
941 mdoc_nmsg(mdoc
, n
, MANDOCERR_PROLOGOOO
);
944 mdoc_nmsg(mdoc
, n
, MANDOCERR_PROLOGREP
);
953 struct mdoc_node
*np
;
957 * Unlike other data pointers, these are "housed" by the HEAD
958 * element, which contains the goods.
961 if (MDOC_HEAD
!= mdoc
->last
->type
) {
962 if (ENDBODY_NOT
!= mdoc
->last
->end
) {
963 assert(mdoc
->last
->pending
);
964 np
= mdoc
->last
->pending
->parent
->head
;
965 } else if (MDOC_BLOCK
!= mdoc
->last
->type
) {
966 np
= mdoc
->last
->parent
->head
;
968 np
= mdoc
->last
->head
;
971 assert(MDOC_HEAD
== np
->type
);
972 assert(MDOC_Bf
== np
->tok
);
977 assert(MDOC_BLOCK
== np
->parent
->type
);
978 assert(MDOC_Bf
== np
->parent
->tok
);
981 * Cannot have both argument and parameter.
982 * If neither is specified, let it through with a warning.
985 if (np
->parent
->args
&& np
->child
) {
986 mdoc_nmsg(mdoc
, np
, MANDOCERR_SYNTARGVCOUNT
);
988 } else if (NULL
== np
->parent
->args
&& NULL
== np
->child
) {
989 mdoc_nmsg(mdoc
, np
, MANDOCERR_FONTTYPE
);
993 /* Extract argument into data. */
995 if (np
->parent
->args
) {
996 arg
= np
->parent
->args
->argv
[0].arg
;
997 if (MDOC_Emphasis
== arg
)
998 np
->norm
->Bf
.font
= FONT_Em
;
999 else if (MDOC_Literal
== arg
)
1000 np
->norm
->Bf
.font
= FONT_Li
;
1001 else if (MDOC_Symbolic
== arg
)
1002 np
->norm
->Bf
.font
= FONT_Sy
;
1008 /* Extract parameter into data. */
1010 if (0 == strcmp(np
->child
->string
, "Em"))
1011 np
->norm
->Bf
.font
= FONT_Em
;
1012 else if (0 == strcmp(np
->child
->string
, "Li"))
1013 np
->norm
->Bf
.font
= FONT_Li
;
1014 else if (0 == strcmp(np
->child
->string
, "Sy"))
1015 np
->norm
->Bf
.font
= FONT_Sy
;
1017 mdoc_nmsg(mdoc
, np
, MANDOCERR_FONTTYPE
);
1029 check_count(mdoc
, MDOC_ELEM
, CHECK_WARN
, CHECK_EQ
, 1);
1031 assert(mdoc
->last
->child
);
1032 assert(MDOC_TEXT
== mdoc
->last
->child
->type
);
1034 p
= mdoc_a2lib(mdoc
->last
->child
->string
);
1036 /* If lookup ok, replace with table value. */
1039 free(mdoc
->last
->child
->string
);
1040 mdoc
->last
->child
->string
= mandoc_strdup(p
);
1044 /* If not, use "library ``xxxx''. */
1046 sz
= strlen(mdoc
->last
->child
->string
) +
1047 2 + strlen("\\(lqlibrary\\(rq");
1048 buf
= mandoc_malloc(sz
);
1049 snprintf(buf
, sz
, "library \\(lq%s\\(rq",
1050 mdoc
->last
->child
->string
);
1051 free(mdoc
->last
->child
->string
);
1052 mdoc
->last
->child
->string
= buf
;
1057 post_eoln(POST_ARGS
)
1060 if (mdoc
->last
->child
)
1061 mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_ARGSLOST
);
1069 const struct mdoc_node
*n
;
1072 * The Vt macro comes in both ELEM and BLOCK form, both of which
1073 * have different syntaxes (yet more context-sensitive
1074 * behaviour). ELEM types must have a child, which is already
1075 * guaranteed by the in_line parsing routine; BLOCK types,
1076 * specifically the BODY, should only have TEXT children.
1079 if (MDOC_BODY
!= mdoc
->last
->type
)
1082 for (n
= mdoc
->last
->child
; n
; n
= n
->next
)
1083 if (MDOC_TEXT
!= n
->type
)
1084 mdoc_nmsg(mdoc
, n
, MANDOCERR_CHILD
);
1095 /* If no child specified, make sure we have the meta name. */
1097 if (NULL
== mdoc
->last
->child
&& NULL
== mdoc
->meta
.name
) {
1098 mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_NONAME
);
1100 } else if (mdoc
->meta
.name
)
1103 /* If no meta name, set it from the child. */
1105 if ( ! concat(mdoc
, buf
, mdoc
->last
->child
, BUFSIZ
))
1108 mdoc
->meta
.name
= mandoc_strdup(buf
);
1114 post_literal(POST_ARGS
)
1118 * The `Dl' (note "el" not "one") and `Bd' macros unset the
1119 * MDOC_LITERAL flag as they leave. Note that `Bd' only sets
1120 * this in literal mode, but it doesn't hurt to just switch it
1121 * off in general since displays can't be nested.
1124 if (MDOC_BODY
== mdoc
->last
->type
)
1125 mdoc
->flags
&= ~MDOC_LITERAL
;
1131 post_defaults(POST_ARGS
)
1133 struct mdoc_node
*nn
;
1136 * The `Ar' defaults to "file ..." if no value is provided as an
1137 * argument; the `Mt' and `Pa' macros use "~"; the `Li' just
1138 * gets an empty string.
1141 if (mdoc
->last
->child
)
1145 mdoc
->next
= MDOC_NEXT_CHILD
;
1149 if ( ! mdoc_word_alloc(mdoc
, nn
->line
, nn
->pos
, "file"))
1151 if ( ! mdoc_word_alloc(mdoc
, nn
->line
, nn
->pos
, "..."))
1155 if ( ! mdoc_word_alloc(mdoc
, nn
->line
, nn
->pos
, "AT&T"))
1157 if ( ! mdoc_word_alloc(mdoc
, nn
->line
, nn
->pos
, "UNIX"))
1161 if ( ! mdoc_word_alloc(mdoc
, nn
->line
, nn
->pos
, ""))
1167 if ( ! mdoc_word_alloc(mdoc
, nn
->line
, nn
->pos
, "~"))
1187 * If we have a child, look it up in the standard keys. If a
1188 * key exist, use that instead of the child; if it doesn't,
1189 * prefix "AT&T UNIX " to the existing data.
1192 if (NULL
== mdoc
->last
->child
)
1195 assert(MDOC_TEXT
== mdoc
->last
->child
->type
);
1196 p
= mdoc_a2att(mdoc
->last
->child
->string
);
1199 free(mdoc
->last
->child
->string
);
1200 mdoc
->last
->child
->string
= mandoc_strdup(p
);
1202 mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_BADATT
);
1204 q
= mdoc
->last
->child
->string
;
1205 sz
= strlen(p
) + strlen(q
) + 1;
1206 buf
= mandoc_malloc(sz
);
1207 strlcpy(buf
, p
, sz
);
1208 strlcat(buf
, q
, sz
);
1209 free(mdoc
->last
->child
->string
);
1210 mdoc
->last
->child
->string
= buf
;
1219 struct mdoc_node
*np
;
1222 if (AUTH__NONE
== np
->norm
->An
.auth
) {
1224 check_count(mdoc
, MDOC_ELEM
, CHECK_WARN
, CHECK_GT
, 0);
1225 } else if (np
->child
)
1226 check_count(mdoc
, MDOC_ELEM
, CHECK_WARN
, CHECK_EQ
, 0);
1237 struct mdoc_node
*n
, *c
;
1240 if (MDOC_BLOCK
!= mdoc
->last
->type
)
1243 n
= mdoc
->last
->parent
->parent
;
1244 lt
= n
->norm
->Bl
.type
;
1246 if (LIST__NONE
== lt
) {
1247 mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_LISTTYPE
);
1253 if (mdoc
->last
->head
->child
)
1255 /* FIXME: give this a dummy value. */
1256 mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_NOARGS
);
1265 if (NULL
== mdoc
->last
->head
->child
)
1266 mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_NOARGS
);
1275 if (NULL
== mdoc
->last
->body
->child
)
1276 mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_NOBODY
);
1279 if (mdoc
->last
->head
->child
)
1280 mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_ARGSLOST
);
1283 cols
= (int)n
->norm
->Bl
.ncols
;
1285 assert(NULL
== mdoc
->last
->head
->child
);
1287 if (NULL
== mdoc
->last
->body
->child
)
1288 mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_NOBODY
);
1290 for (i
= 0, c
= mdoc
->last
->child
; c
; c
= c
->next
)
1291 if (MDOC_BODY
== c
->type
)
1295 er
= MANDOCERR_ARGCOUNT
;
1296 else if (i
== cols
|| i
== cols
+ 1)
1299 er
= MANDOCERR_SYNTARGCOUNT
;
1301 rc
= mdoc_vmsg(mdoc
, er
,
1302 mdoc
->last
->line
, mdoc
->last
->pos
,
1303 "columns == %d (have %d)", cols
, i
);
1313 post_bl_block(POST_ARGS
)
1315 struct mdoc_node
*n
;
1318 * These are fairly complicated, so we've broken them into two
1319 * functions. post_bl_block_tag() is called when a -tag is
1320 * specified, but no -width (it must be guessed). The second
1321 * when a -width is specified (macro indicators must be
1322 * rewritten into real lengths).
1327 if (LIST_tag
== n
->norm
->Bl
.type
&&
1328 NULL
== n
->norm
->Bl
.width
) {
1329 if ( ! post_bl_block_tag(mdoc
))
1331 } else if (NULL
!= n
->norm
->Bl
.width
) {
1332 if ( ! post_bl_block_width(mdoc
))
1337 assert(n
->norm
->Bl
.width
);
1342 post_bl_block_width(POST_ARGS
)
1347 struct mdoc_node
*n
;
1353 * Calculate the real width of a list from the -width string,
1354 * which may contain a macro (with a known default width), a
1355 * literal string, or a scaling width.
1357 * If the value to -width is a macro, then we re-write it to be
1358 * the macro's width as set in share/tmac/mdoc/doc-common.
1361 if (0 == strcmp(n
->norm
->Bl
.width
, "Ds"))
1363 else if (MDOC_MAX
== (tok
= mdoc_hash_find(n
->norm
->Bl
.width
)))
1365 else if (0 == (width
= mdoc_macro2len(tok
))) {
1366 mdoc_nmsg(mdoc
, n
, MANDOCERR_BADWIDTH
);
1370 /* The value already exists: free and reallocate it. */
1374 for (i
= 0; i
< (int)n
->args
->argc
; i
++)
1375 if (MDOC_Width
== n
->args
->argv
[i
].arg
)
1378 assert(i
< (int)n
->args
->argc
);
1380 snprintf(buf
, NUMSIZ
, "%zun", width
);
1381 free(n
->args
->argv
[i
].value
[0]);
1382 n
->args
->argv
[i
].value
[0] = mandoc_strdup(buf
);
1384 /* Set our width! */
1385 n
->norm
->Bl
.width
= n
->args
->argv
[i
].value
[0];
1390 post_bl_block_tag(POST_ARGS
)
1392 struct mdoc_node
*n
, *nn
;
1398 * Calculate the -width for a `Bl -tag' list if it hasn't been
1399 * provided. Uses the first head macro. NOTE AGAIN: this is
1400 * ONLY if the -width argument has NOT been provided. See
1401 * post_bl_block_width() for converting the -width string.
1407 for (nn
= n
->body
->child
; nn
; nn
= nn
->next
) {
1408 if (MDOC_It
!= nn
->tok
)
1411 assert(MDOC_BLOCK
== nn
->type
);
1412 nn
= nn
->head
->child
;
1417 if (MDOC_TEXT
== nn
->type
) {
1418 sz
= strlen(nn
->string
) + 1;
1422 if (0 != (ssz
= mdoc_macro2len(nn
->tok
)))
1428 /* Defaults to ten ens. */
1430 snprintf(buf
, NUMSIZ
, "%zun", sz
);
1433 * We have to dynamically add this to the macro's argument list.
1434 * We're guaranteed that a MDOC_Width doesn't already exist.
1438 i
= (int)(n
->args
->argc
)++;
1440 n
->args
->argv
= mandoc_realloc(n
->args
->argv
,
1441 n
->args
->argc
* sizeof(struct mdoc_argv
));
1443 n
->args
->argv
[i
].arg
= MDOC_Width
;
1444 n
->args
->argv
[i
].line
= n
->line
;
1445 n
->args
->argv
[i
].pos
= n
->pos
;
1446 n
->args
->argv
[i
].sz
= 1;
1447 n
->args
->argv
[i
].value
= mandoc_malloc(sizeof(char *));
1448 n
->args
->argv
[i
].value
[0] = mandoc_strdup(buf
);
1450 /* Set our width! */
1451 n
->norm
->Bl
.width
= n
->args
->argv
[i
].value
[0];
1457 post_bl_head(POST_ARGS
)
1459 struct mdoc_node
*np
, *nn
, *nnp
;
1462 if (LIST_column
!= mdoc
->last
->norm
->Bl
.type
)
1463 /* FIXME: this should be ERROR class... */
1464 return(hwarn_eq0(mdoc
));
1467 * Convert old-style lists, where the column width specifiers
1468 * trail as macro parameters, to the new-style ("normal-form")
1469 * lists where they're argument values following -column.
1472 /* First, disallow both types and allow normal-form. */
1475 * TODO: technically, we can accept both and just merge the two
1476 * lists, but I'll leave that for another day.
1479 if (mdoc
->last
->norm
->Bl
.ncols
&& mdoc
->last
->nchild
) {
1480 mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_COLUMNS
);
1482 } else if (NULL
== mdoc
->last
->child
)
1485 np
= mdoc
->last
->parent
;
1488 for (j
= 0; j
< (int)np
->args
->argc
; j
++)
1489 if (MDOC_Column
== np
->args
->argv
[j
].arg
)
1492 assert(j
< (int)np
->args
->argc
);
1493 assert(0 == np
->args
->argv
[j
].sz
);
1496 * Accomodate for new-style groff column syntax. Shuffle the
1497 * child nodes, all of which must be TEXT, as arguments for the
1498 * column field. Then, delete the head children.
1501 np
->args
->argv
[j
].sz
= (size_t)mdoc
->last
->nchild
;
1502 np
->args
->argv
[j
].value
= mandoc_malloc
1503 ((size_t)mdoc
->last
->nchild
* sizeof(char *));
1505 mdoc
->last
->norm
->Bl
.ncols
= np
->args
->argv
[j
].sz
;
1506 mdoc
->last
->norm
->Bl
.cols
= (const char **)np
->args
->argv
[j
].value
;
1508 for (i
= 0, nn
= mdoc
->last
->child
; nn
; i
++) {
1509 np
->args
->argv
[j
].value
[i
] = nn
->string
;
1513 mdoc_node_delete(NULL
, nnp
);
1516 mdoc
->last
->nchild
= 0;
1517 mdoc
->last
->child
= NULL
;
1525 struct mdoc_node
*n
;
1527 if (MDOC_HEAD
== mdoc
->last
->type
)
1528 return(post_bl_head(mdoc
));
1529 if (MDOC_BLOCK
== mdoc
->last
->type
)
1530 return(post_bl_block(mdoc
));
1531 if (MDOC_BODY
!= mdoc
->last
->type
)
1534 for (n
= mdoc
->last
->child
; n
; n
= n
->next
) {
1539 mdoc_nmsg(mdoc
, n
, MANDOCERR_CHILD
);
1549 mdoc_nmsg(mdoc
, n
, MANDOCERR_SYNTCHILD
);
1557 ebool(struct mdoc
*mdoc
)
1560 if (NULL
== mdoc
->last
->child
) {
1561 mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_MACROEMPTY
);
1562 mdoc_node_delete(mdoc
, mdoc
->last
);
1565 check_count(mdoc
, MDOC_ELEM
, CHECK_WARN
, CHECK_EQ
, 1);
1567 assert(MDOC_TEXT
== mdoc
->last
->child
->type
);
1569 if (0 == strcmp(mdoc
->last
->child
->string
, "on"))
1571 if (0 == strcmp(mdoc
->last
->child
->string
, "off"))
1574 mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_BADBOOL
);
1579 post_root(POST_ARGS
)
1582 struct mdoc_node
*n
;
1586 /* Check that we have a finished prologue. */
1588 if ( ! (MDOC_PBODY
& mdoc
->flags
)) {
1590 mdoc_nmsg(mdoc
, mdoc
->first
, MANDOCERR_NODOCPROLOG
);
1596 /* Check that we begin with a proper `Sh'. */
1598 if (NULL
== n
->child
) {
1600 mdoc_nmsg(mdoc
, n
, MANDOCERR_NODOCBODY
);
1601 } else if (MDOC_BLOCK
!= n
->child
->type
||
1602 MDOC_Sh
!= n
->child
->tok
) {
1604 /* Can this be lifted? See rxdebug.1 for example. */
1605 mdoc_nmsg(mdoc
, n
, MANDOCERR_NODOCBODY
);
1608 return(erc
? 0 : 1);
1614 struct mdoc_node
*ch
;
1617 if (NULL
== (ch
= mdoc
->last
->child
)) {
1618 mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_MACROEMPTY
);
1619 mdoc_node_delete(mdoc
, mdoc
->last
);
1623 assert(MDOC_TEXT
== ch
->type
);
1625 if (NULL
== (p
= mdoc_a2st(ch
->string
))) {
1626 mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_BADSTANDARD
);
1627 mdoc_node_delete(mdoc
, mdoc
->last
);
1630 ch
->string
= mandoc_strdup(p
);
1639 struct mdoc_node
*nn
, *next
, *prev
;
1642 switch (mdoc
->last
->type
) {
1644 check_count(mdoc
, MDOC_HEAD
, CHECK_WARN
, CHECK_EQ
, 0);
1647 if (mdoc
->last
->child
)
1649 check_count(mdoc
, MDOC_BODY
, CHECK_WARN
, CHECK_GT
, 0);
1656 * Make sure only certain types of nodes are allowed within the
1657 * the `Rs' body. Delete offending nodes and raise a warning.
1658 * Do this before re-ordering for the sake of clarity.
1662 for (nn
= mdoc
->last
->child
; nn
; nn
= next
) {
1663 for (i
= 0; i
< RSORD_MAX
; i
++)
1664 if (nn
->tok
== rsord
[i
])
1667 if (i
< RSORD_MAX
) {
1668 if (MDOC__J
== rsord
[i
] || MDOC__B
== rsord
[i
])
1669 mdoc
->last
->norm
->Rs
.quote_T
++;
1675 mdoc_nmsg(mdoc
, nn
, MANDOCERR_CHILD
);
1676 mdoc_node_delete(mdoc
, nn
);
1680 * The full `Rs' block needs special handling to order the
1681 * sub-elements according to `rsord'. Pick through each element
1682 * and correctly order it. This is a insertion sort.
1686 for (nn
= mdoc
->last
->child
->next
; nn
; nn
= next
) {
1687 /* Determine order of `nn'. */
1688 for (i
= 0; i
< RSORD_MAX
; i
++)
1689 if (rsord
[i
] == nn
->tok
)
1693 * Remove `nn' from the chain. This somewhat
1694 * repeats mdoc_node_unlink(), but since we're
1695 * just re-ordering, there's no need for the
1696 * full unlink process.
1699 if (NULL
!= (next
= nn
->next
))
1700 next
->prev
= nn
->prev
;
1702 if (NULL
!= (prev
= nn
->prev
))
1703 prev
->next
= nn
->next
;
1705 nn
->prev
= nn
->next
= NULL
;
1708 * Scan back until we reach a node that's
1709 * ordered before `nn'.
1712 for ( ; prev
; prev
= prev
->prev
) {
1713 /* Determine order of `prev'. */
1714 for (j
= 0; j
< RSORD_MAX
; j
++)
1715 if (rsord
[j
] == prev
->tok
)
1723 * Set `nn' back into its correct place in front
1724 * of the `prev' node.
1731 prev
->next
->prev
= nn
;
1732 nn
->next
= prev
->next
;
1735 mdoc
->last
->child
->prev
= nn
;
1736 nn
->next
= mdoc
->last
->child
;
1737 mdoc
->last
->child
= nn
;
1748 if (MDOC_HEAD
== mdoc
->last
->type
)
1749 return(post_sh_head(mdoc
));
1750 if (MDOC_BODY
== mdoc
->last
->type
)
1751 return(post_sh_body(mdoc
));
1757 post_sh_body(POST_ARGS
)
1759 struct mdoc_node
*n
;
1761 if (SEC_NAME
!= mdoc
->lastsec
)
1765 * Warn if the NAME section doesn't contain the `Nm' and `Nd'
1766 * macros (can have multiple `Nm' and one `Nd'). Note that the
1767 * children of the BODY declaration can also be "text".
1770 if (NULL
== (n
= mdoc
->last
->child
)) {
1771 mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_BADNAMESEC
);
1775 for ( ; n
&& n
->next
; n
= n
->next
) {
1776 if (MDOC_ELEM
== n
->type
&& MDOC_Nm
== n
->tok
)
1778 if (MDOC_TEXT
== n
->type
)
1780 mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_BADNAMESEC
);
1784 if (MDOC_BLOCK
== n
->type
&& MDOC_Nd
== n
->tok
)
1787 mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_BADNAMESEC
);
1792 post_sh_head(POST_ARGS
)
1798 * Process a new section. Sections are either "named" or
1799 * "custom". Custom sections are user-defined, while named ones
1800 * follow a conventional order and may only appear in certain
1804 if ( ! concat(mdoc
, buf
, mdoc
->last
->child
, BUFSIZ
))
1807 sec
= mdoc_str2sec(buf
);
1809 /* The NAME should be first. */
1811 if (SEC_NAME
!= sec
&& SEC_NONE
== mdoc
->lastnamed
)
1812 mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_NAMESECFIRST
);
1814 /* The SYNOPSIS gets special attention in other areas. */
1816 if (SEC_SYNOPSIS
== sec
)
1817 mdoc
->flags
|= MDOC_SYNOPSIS
;
1819 mdoc
->flags
&= ~MDOC_SYNOPSIS
;
1821 /* Mark our last section. */
1823 mdoc
->lastsec
= sec
;
1825 /* We don't care about custom sections after this. */
1827 if (SEC_CUSTOM
== sec
)
1831 * Check whether our non-custom section is being repeated or is
1835 if (sec
== mdoc
->lastnamed
)
1836 mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_SECREP
);
1838 if (sec
< mdoc
->lastnamed
)
1839 mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_SECOOO
);
1841 /* Mark the last named section. */
1843 mdoc
->lastnamed
= sec
;
1845 /* Check particular section/manual conventions. */
1847 assert(mdoc
->meta
.msec
);
1850 case (SEC_RETURN_VALUES
):
1855 if (*mdoc
->meta
.msec
== '2')
1857 if (*mdoc
->meta
.msec
== '3')
1859 if (*mdoc
->meta
.msec
== '9')
1861 mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_SECMSEC
);
1871 post_ignpar(POST_ARGS
)
1873 struct mdoc_node
*np
;
1875 if (MDOC_BODY
!= mdoc
->last
->type
)
1878 if (NULL
!= (np
= mdoc
->last
->child
))
1879 if (MDOC_Pp
== np
->tok
|| MDOC_Lp
== np
->tok
) {
1880 mdoc_nmsg(mdoc
, np
, MANDOCERR_IGNPAR
);
1881 mdoc_node_delete(mdoc
, np
);
1884 if (NULL
!= (np
= mdoc
->last
->last
))
1885 if (MDOC_Pp
== np
->tok
|| MDOC_Lp
== np
->tok
) {
1886 mdoc_nmsg(mdoc
, np
, MANDOCERR_IGNPAR
);
1887 mdoc_node_delete(mdoc
, np
);
1897 if (NULL
== mdoc
->last
)
1899 if (MDOC_ELEM
!= n
->type
&& MDOC_BLOCK
!= n
->type
)
1903 * Don't allow prior `Lp' or `Pp' prior to a paragraph-type
1904 * block: `Lp', `Pp', or non-compact `Bd' or `Bl'.
1907 if (MDOC_Pp
!= mdoc
->last
->tok
&& MDOC_Lp
!= mdoc
->last
->tok
)
1909 if (MDOC_Bl
== n
->tok
&& n
->norm
->Bl
.comp
)
1911 if (MDOC_Bd
== n
->tok
&& n
->norm
->Bd
.comp
)
1913 if (MDOC_It
== n
->tok
&& n
->parent
->norm
->Bl
.comp
)
1916 mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_IGNPAR
);
1917 mdoc_node_delete(mdoc
, mdoc
->last
);
1922 pre_literal(PRE_ARGS
)
1925 if (MDOC_BODY
!= n
->type
)
1929 * The `Dl' (note "el" not "one") and `Bd -literal' and `Bd
1930 * -unfilled' macros set MDOC_LITERAL on entrance to the body.
1935 mdoc
->flags
|= MDOC_LITERAL
;
1938 if (DISP_literal
== n
->norm
->Bd
.type
)
1939 mdoc
->flags
|= MDOC_LITERAL
;
1940 if (DISP_unfilled
== n
->norm
->Bd
.type
)
1941 mdoc
->flags
|= MDOC_LITERAL
;
1955 struct mdoc_node
*n
;
1959 if (NULL
== n
->child
) {
1960 mdoc
->meta
.date
= time(NULL
);
1964 if ( ! concat(mdoc
, buf
, n
->child
, DATESIZE
))
1967 mdoc
->meta
.date
= mandoc_a2time
1968 (MTIME_MDOCDATE
| MTIME_CANONICAL
, buf
);
1970 if (0 == mdoc
->meta
.date
) {
1971 mdoc_nmsg(mdoc
, n
, MANDOCERR_BADDATE
);
1972 mdoc
->meta
.date
= time(NULL
);
1981 struct mdoc_node
*nn
, *n
;
1987 if (mdoc
->meta
.title
)
1988 free(mdoc
->meta
.title
);
1990 free(mdoc
->meta
.vol
);
1991 if (mdoc
->meta
.arch
)
1992 free(mdoc
->meta
.arch
);
1994 mdoc
->meta
.title
= mdoc
->meta
.vol
= mdoc
->meta
.arch
= NULL
;
1996 /* First make all characters uppercase. */
1998 if (NULL
!= (nn
= n
->child
))
1999 for (p
= nn
->string
; *p
; p
++) {
2000 if (toupper((u_char
)*p
) == *p
)
2004 * FIXME: don't be lazy: have this make all
2005 * characters be uppercase and just warn once.
2007 mdoc_nmsg(mdoc
, nn
, MANDOCERR_UPPERCASE
);
2012 * --> title = unknown, volume = local, msec = 0, arch = NULL
2015 if (NULL
== (nn
= n
->child
)) {
2016 /* XXX: make these macro values. */
2017 /* FIXME: warn about missing values. */
2018 mdoc
->meta
.title
= mandoc_strdup("UNKNOWN");
2019 mdoc
->meta
.vol
= mandoc_strdup("LOCAL");
2020 mdoc
->meta
.msec
= mandoc_strdup("1");
2024 /* Handles: `.Dt TITLE'
2025 * --> title = TITLE, volume = local, msec = 0, arch = NULL
2028 mdoc
->meta
.title
= mandoc_strdup
2029 ('\0' == nn
->string
[0] ? "UNKNOWN" : nn
->string
);
2031 if (NULL
== (nn
= nn
->next
)) {
2032 /* FIXME: warn about missing msec. */
2033 /* XXX: make this a macro value. */
2034 mdoc
->meta
.vol
= mandoc_strdup("LOCAL");
2035 mdoc
->meta
.msec
= mandoc_strdup("1");
2039 /* Handles: `.Dt TITLE SEC'
2040 * --> title = TITLE, volume = SEC is msec ?
2041 * format(msec) : SEC,
2042 * msec = SEC is msec ? atoi(msec) : 0,
2046 cp
= mdoc_a2msec(nn
->string
);
2048 mdoc
->meta
.vol
= mandoc_strdup(cp
);
2049 mdoc
->meta
.msec
= mandoc_strdup(nn
->string
);
2051 mdoc_nmsg(mdoc
, n
, MANDOCERR_BADMSEC
);
2052 mdoc
->meta
.vol
= mandoc_strdup(nn
->string
);
2053 mdoc
->meta
.msec
= mandoc_strdup(nn
->string
);
2056 if (NULL
== (nn
= nn
->next
))
2059 /* Handles: `.Dt TITLE SEC VOL'
2060 * --> title = TITLE, volume = VOL is vol ?
2062 * VOL is arch ? format(arch) :
2066 cp
= mdoc_a2vol(nn
->string
);
2068 free(mdoc
->meta
.vol
);
2069 mdoc
->meta
.vol
= mandoc_strdup(cp
);
2071 /* FIXME: warn about bad arch. */
2072 cp
= mdoc_a2arch(nn
->string
);
2074 free(mdoc
->meta
.vol
);
2075 mdoc
->meta
.vol
= mandoc_strdup(nn
->string
);
2077 mdoc
->meta
.arch
= mandoc_strdup(cp
);
2080 /* Ignore any subsequent parameters... */
2081 /* FIXME: warn about subsequent parameters. */
2087 post_prol(POST_ARGS
)
2090 * Remove prologue macros from the document after they're
2091 * processed. The final document uses mdoc_meta for these
2092 * values and discards the originals.
2095 mdoc_node_delete(mdoc
, mdoc
->last
);
2096 if (mdoc
->meta
.title
&& mdoc
->meta
.date
&& mdoc
->meta
.os
)
2097 mdoc
->flags
|= MDOC_PBODY
;
2105 struct mdoc_node
*n
;
2108 struct utsname utsname
;
2114 * Set the operating system by way of the `Os' macro. Note that
2115 * if an argument isn't provided and -DOSNAME="\"foo\"" is
2116 * provided during compilation, this value will be used instead
2117 * of filling in "sysname release" from uname().
2121 free(mdoc
->meta
.os
);
2123 if ( ! concat(mdoc
, buf
, n
->child
, BUFSIZ
))
2126 /* XXX: yes, these can all be dynamically-adjusted buffers, but
2127 * it's really not worth the extra hackery.
2130 if ('\0' == buf
[0]) {
2132 if (strlcat(buf
, OSNAME
, BUFSIZ
) >= BUFSIZ
) {
2133 mdoc_nmsg(mdoc
, n
, MANDOCERR_MEM
);
2137 if (uname(&utsname
)) {
2138 mdoc_nmsg(mdoc
, n
, MANDOCERR_UNAME
);
2139 mdoc
->meta
.os
= mandoc_strdup("UNKNOWN");
2140 return(post_prol(mdoc
));
2143 if (strlcat(buf
, utsname
.sysname
, BUFSIZ
) >= BUFSIZ
) {
2144 mdoc_nmsg(mdoc
, n
, MANDOCERR_MEM
);
2147 if (strlcat(buf
, " ", BUFSIZ
) >= BUFSIZ
) {
2148 mdoc_nmsg(mdoc
, n
, MANDOCERR_MEM
);
2151 if (strlcat(buf
, utsname
.release
, BUFSIZ
) >= BUFSIZ
) {
2152 mdoc_nmsg(mdoc
, n
, MANDOCERR_MEM
);
2158 mdoc
->meta
.os
= mandoc_strdup(buf
);
2165 struct mdoc_node
*nn
, *n
;
2170 * Macros accepting `-std' as an argument have the name of the
2171 * current document (`Nm') filled in as the argument if it's not
2178 if (NULL
== mdoc
->meta
.name
)
2182 mdoc
->next
= MDOC_NEXT_CHILD
;
2184 if ( ! mdoc_word_alloc(mdoc
, n
->line
, n
->pos
, mdoc
->meta
.name
))
2192 concat(struct mdoc
*m
, char *p
, const struct mdoc_node
*n
, size_t sz
)
2198 * Concatenate sibling nodes together. All siblings must be of
2199 * type MDOC_TEXT or an assertion is raised. Concatenation is
2200 * separated by a single whitespace. Returns 0 on fatal (string
2204 for ( ; n
; n
= n
->next
) {
2205 assert(MDOC_TEXT
== n
->type
);
2207 if (strlcat(p
, n
->string
, sz
) >= sz
) {
2208 mdoc_nmsg(m
, n
, MANDOCERR_MEM
);
2212 if (NULL
== n
->next
)
2215 if (strlcat(p
, " ", sz
) >= sz
) {
2216 mdoc_nmsg(m
, n
, MANDOCERR_MEM
);