]>
git.cameronkatri.com Git - mandoc.git/blob - mdoc_validate.c
53c4ab5a62a5e6aaeb9029b056e8e42368a37f20
1 /* $Id: mdoc_validate.c,v 1.133 2010/11/30 14:27:37 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
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_eq0(POST_ARGS
);
82 static int eerr_eq1(POST_ARGS
);
83 static int eerr_ge1(POST_ARGS
);
84 static int eerr_le1(POST_ARGS
);
85 static int ewarn_eq0(POST_ARGS
);
86 static int ewarn_ge1(POST_ARGS
);
87 static int herr_eq0(POST_ARGS
);
88 static int herr_ge1(POST_ARGS
);
89 static int hwarn_eq0(POST_ARGS
);
90 static int hwarn_eq1(POST_ARGS
);
91 static int hwarn_le1(POST_ARGS
);
93 static int post_an(POST_ARGS
);
94 static int post_at(POST_ARGS
);
95 static int post_bf(POST_ARGS
);
96 static int post_bl(POST_ARGS
);
97 static int post_bl_block(POST_ARGS
);
98 static int post_bl_block_width(POST_ARGS
);
99 static int post_bl_block_tag(POST_ARGS
);
100 static int post_bl_head(POST_ARGS
);
101 static int post_dd(POST_ARGS
);
102 static int post_dt(POST_ARGS
);
103 static int post_defaults(POST_ARGS
);
104 static int post_literal(POST_ARGS
);
105 static int post_eoln(POST_ARGS
);
106 static int post_it(POST_ARGS
);
107 static int post_lb(POST_ARGS
);
108 static int post_nm(POST_ARGS
);
109 static int post_os(POST_ARGS
);
110 static int post_prol(POST_ARGS
);
111 static int post_root(POST_ARGS
);
112 static int post_rs(POST_ARGS
);
113 static int post_sh(POST_ARGS
);
114 static int post_sh_body(POST_ARGS
);
115 static int post_sh_head(POST_ARGS
);
116 static int post_st(POST_ARGS
);
117 static int post_std(POST_ARGS
);
118 static int post_vt(POST_ARGS
);
119 static int pre_an(PRE_ARGS
);
120 static int pre_bd(PRE_ARGS
);
121 static int pre_bl(PRE_ARGS
);
122 static int pre_dd(PRE_ARGS
);
123 static int pre_display(PRE_ARGS
);
124 static int pre_dt(PRE_ARGS
);
125 static int pre_it(PRE_ARGS
);
126 static int pre_literal(PRE_ARGS
);
127 static int pre_os(PRE_ARGS
);
128 static int pre_par(PRE_ARGS
);
129 static int pre_sh(PRE_ARGS
);
130 static int pre_ss(PRE_ARGS
);
131 static int pre_std(PRE_ARGS
);
133 static v_post posts_an
[] = { post_an
, NULL
};
134 static v_post posts_at
[] = { post_at
, post_defaults
, NULL
};
135 static v_post posts_bd
[] = { post_literal
, hwarn_eq0
, bwarn_ge1
, NULL
};
136 static v_post posts_bf
[] = { hwarn_le1
, post_bf
, NULL
};
137 static v_post posts_bk
[] = { hwarn_eq0
, bwarn_ge1
, NULL
};
138 static v_post posts_bl
[] = { bwarn_ge1
, post_bl
, NULL
};
139 static v_post posts_bool
[] = { eerr_eq1
, ebool
, NULL
};
140 static v_post posts_eoln
[] = { post_eoln
, NULL
};
141 static v_post posts_defaults
[] = { post_defaults
, NULL
};
142 static v_post posts_dd
[] = { ewarn_ge1
, post_dd
, post_prol
, NULL
};
143 static v_post posts_dl
[] = { post_literal
, bwarn_ge1
, herr_eq0
, NULL
};
144 static v_post posts_dt
[] = { post_dt
, post_prol
, NULL
};
145 static v_post posts_fo
[] = { hwarn_eq1
, bwarn_ge1
, NULL
};
146 static v_post posts_it
[] = { post_it
, NULL
};
147 static v_post posts_lb
[] = { eerr_eq1
, post_lb
, NULL
};
148 static v_post posts_nd
[] = { berr_ge1
, NULL
};
149 static v_post posts_nm
[] = { post_nm
, NULL
};
150 static v_post posts_notext
[] = { ewarn_eq0
, NULL
};
151 static v_post posts_os
[] = { post_os
, post_prol
, NULL
};
152 static v_post posts_rs
[] = { berr_ge1
, herr_eq0
, post_rs
, NULL
};
153 static v_post posts_sh
[] = { herr_ge1
, bwarn_ge1
, post_sh
, NULL
};
154 static v_post posts_sp
[] = { eerr_le1
, NULL
};
155 static v_post posts_ss
[] = { herr_ge1
, NULL
};
156 static v_post posts_st
[] = { eerr_eq1
, post_st
, NULL
};
157 static v_post posts_std
[] = { post_std
, NULL
};
158 static v_post posts_text
[] = { eerr_ge1
, NULL
};
159 static v_post posts_text1
[] = { eerr_eq1
, NULL
};
160 static v_post posts_vt
[] = { post_vt
, NULL
};
161 static v_post posts_wline
[] = { bwarn_ge1
, herr_eq0
, NULL
};
162 static v_post posts_wtext
[] = { ewarn_ge1
, NULL
};
163 static v_pre pres_an
[] = { pre_an
, NULL
};
164 static v_pre pres_bd
[] = { pre_display
, pre_bd
, pre_literal
, pre_par
, NULL
};
165 static v_pre pres_bl
[] = { pre_bl
, pre_par
, NULL
};
166 static v_pre pres_d1
[] = { pre_display
, NULL
};
167 static v_pre pres_dl
[] = { pre_literal
, pre_display
, NULL
};
168 static v_pre pres_dd
[] = { pre_dd
, NULL
};
169 static v_pre pres_dt
[] = { pre_dt
, NULL
};
170 static v_pre pres_er
[] = { NULL
, NULL
};
171 static v_pre pres_fd
[] = { NULL
, NULL
};
172 static v_pre pres_it
[] = { pre_it
, NULL
};
173 static v_pre pres_os
[] = { pre_os
, NULL
};
174 static v_pre pres_pp
[] = { pre_par
, NULL
};
175 static v_pre pres_sh
[] = { pre_sh
, NULL
};
176 static v_pre pres_ss
[] = { pre_ss
, NULL
};
177 static v_pre pres_std
[] = { pre_std
, NULL
};
179 const struct valids mdoc_valids
[MDOC_MAX
] = {
180 { NULL
, NULL
}, /* Ap */
181 { pres_dd
, posts_dd
}, /* Dd */
182 { pres_dt
, posts_dt
}, /* Dt */
183 { pres_os
, posts_os
}, /* Os */
184 { pres_sh
, posts_sh
}, /* Sh */
185 { pres_ss
, posts_ss
}, /* Ss */
186 { pres_pp
, posts_notext
}, /* Pp */
187 { pres_d1
, posts_wline
}, /* D1 */
188 { pres_dl
, posts_dl
}, /* Dl */
189 { pres_bd
, posts_bd
}, /* Bd */
190 { NULL
, NULL
}, /* Ed */
191 { pres_bl
, posts_bl
}, /* Bl */
192 { NULL
, NULL
}, /* El */
193 { pres_it
, posts_it
}, /* It */
194 { NULL
, posts_text
}, /* Ad */
195 { pres_an
, posts_an
}, /* An */
196 { NULL
, posts_defaults
}, /* Ar */
197 { NULL
, posts_text
}, /* Cd */
198 { NULL
, NULL
}, /* Cm */
199 { NULL
, NULL
}, /* Dv */
200 { pres_er
, posts_text
}, /* Er */
201 { NULL
, NULL
}, /* Ev */
202 { pres_std
, posts_std
}, /* Ex */
203 { NULL
, NULL
}, /* Fa */
204 { pres_fd
, posts_wtext
}, /* Fd */
205 { NULL
, NULL
}, /* Fl */
206 { NULL
, posts_text
}, /* Fn */
207 { NULL
, posts_wtext
}, /* Ft */
208 { NULL
, posts_text
}, /* Ic */
209 { NULL
, posts_text1
}, /* In */
210 { NULL
, posts_defaults
}, /* Li */
211 { NULL
, posts_nd
}, /* Nd */
212 { NULL
, posts_nm
}, /* Nm */
213 { NULL
, posts_wline
}, /* Op */
214 { NULL
, NULL
}, /* Ot */
215 { NULL
, posts_defaults
}, /* Pa */
216 { pres_std
, posts_std
}, /* Rv */
217 { NULL
, posts_st
}, /* St */
218 { NULL
, NULL
}, /* Va */
219 { NULL
, posts_vt
}, /* Vt */
220 { NULL
, posts_wtext
}, /* Xr */
221 { NULL
, posts_text
}, /* %A */
222 { NULL
, posts_text
}, /* %B */ /* FIXME: can be used outside Rs/Re. */
223 { NULL
, posts_text
}, /* %D */ /* FIXME: check date with mandoc_a2time(). */
224 { NULL
, posts_text
}, /* %I */
225 { NULL
, posts_text
}, /* %J */
226 { NULL
, posts_text
}, /* %N */
227 { NULL
, posts_text
}, /* %O */
228 { NULL
, posts_text
}, /* %P */
229 { NULL
, posts_text
}, /* %R */
230 { NULL
, posts_text
}, /* %T */ /* FIXME: can be used outside Rs/Re. */
231 { NULL
, posts_text
}, /* %V */
232 { NULL
, NULL
}, /* Ac */
233 { NULL
, NULL
}, /* Ao */
234 { NULL
, posts_wline
}, /* Aq */
235 { NULL
, posts_at
}, /* At */
236 { NULL
, NULL
}, /* Bc */
237 { NULL
, posts_bf
}, /* Bf */
238 { NULL
, NULL
}, /* Bo */
239 { NULL
, posts_wline
}, /* Bq */
240 { NULL
, NULL
}, /* Bsx */
241 { NULL
, NULL
}, /* Bx */
242 { NULL
, posts_bool
}, /* Db */
243 { NULL
, NULL
}, /* Dc */
244 { NULL
, NULL
}, /* Do */
245 { NULL
, posts_wline
}, /* Dq */
246 { NULL
, NULL
}, /* Ec */
247 { NULL
, NULL
}, /* Ef */
248 { NULL
, NULL
}, /* Em */
249 { NULL
, NULL
}, /* Eo */
250 { NULL
, NULL
}, /* Fx */
251 { NULL
, posts_text
}, /* Ms */
252 { NULL
, posts_notext
}, /* No */
253 { NULL
, posts_notext
}, /* Ns */
254 { NULL
, NULL
}, /* Nx */
255 { NULL
, NULL
}, /* Ox */
256 { NULL
, NULL
}, /* Pc */
257 { NULL
, posts_text1
}, /* Pf */
258 { NULL
, NULL
}, /* Po */
259 { NULL
, posts_wline
}, /* Pq */
260 { NULL
, NULL
}, /* Qc */
261 { NULL
, posts_wline
}, /* Ql */
262 { NULL
, NULL
}, /* Qo */
263 { NULL
, posts_wline
}, /* Qq */
264 { NULL
, NULL
}, /* Re */
265 { NULL
, posts_rs
}, /* Rs */
266 { NULL
, NULL
}, /* Sc */
267 { NULL
, NULL
}, /* So */
268 { NULL
, posts_wline
}, /* Sq */
269 { NULL
, posts_bool
}, /* Sm */
270 { NULL
, posts_text
}, /* Sx */
271 { NULL
, posts_text
}, /* Sy */
272 { NULL
, posts_text
}, /* Tn */
273 { NULL
, NULL
}, /* Ux */
274 { NULL
, NULL
}, /* Xc */
275 { NULL
, NULL
}, /* Xo */
276 { NULL
, posts_fo
}, /* Fo */
277 { NULL
, NULL
}, /* Fc */
278 { NULL
, NULL
}, /* Oo */
279 { NULL
, NULL
}, /* Oc */
280 { NULL
, posts_bk
}, /* Bk */
281 { NULL
, NULL
}, /* Ek */
282 { NULL
, posts_eoln
}, /* Bt */
283 { NULL
, NULL
}, /* Hf */
284 { NULL
, NULL
}, /* Fr */
285 { NULL
, posts_eoln
}, /* Ud */
286 { NULL
, posts_lb
}, /* Lb */
287 { NULL
, posts_notext
}, /* Lp */
288 { NULL
, posts_text
}, /* Lk */
289 { NULL
, posts_defaults
}, /* Mt */
290 { NULL
, posts_wline
}, /* Brq */
291 { NULL
, NULL
}, /* Bro */
292 { NULL
, NULL
}, /* Brc */
293 { NULL
, posts_text
}, /* %C */
294 { NULL
, NULL
}, /* Es */
295 { NULL
, NULL
}, /* En */
296 { NULL
, NULL
}, /* Dx */
297 { NULL
, posts_text
}, /* %Q */
298 { NULL
, posts_notext
}, /* br */
299 { pres_pp
, posts_sp
}, /* sp */
300 { NULL
, posts_text1
}, /* %U */
301 { NULL
, NULL
}, /* Ta */
304 #define RSORD_MAX 14 /* Number of `Rs' blocks. */
306 static const enum mdoct rsord
[RSORD_MAX
] = {
325 mdoc_valid_pre(struct mdoc
*mdoc
, struct mdoc_node
*n
)
331 if (MDOC_TEXT
== n
->type
) {
335 check_text(mdoc
, line
, pos
, tp
);
341 if (NULL
== mdoc_valids
[n
->tok
].pre
)
343 for (p
= mdoc_valids
[n
->tok
].pre
; *p
; p
++)
344 if ( ! (*p
)(mdoc
, n
))
351 mdoc_valid_post(struct mdoc
*mdoc
)
355 if (MDOC_VALID
& mdoc
->last
->flags
)
357 mdoc
->last
->flags
|= MDOC_VALID
;
359 if (MDOC_TEXT
== mdoc
->last
->type
)
361 if (MDOC_ROOT
== mdoc
->last
->type
)
362 return(post_root(mdoc
));
364 if (NULL
== mdoc_valids
[mdoc
->last
->tok
].post
)
366 for (p
= mdoc_valids
[mdoc
->last
->tok
].post
; *p
; p
++)
374 check_count(struct mdoc
*m
, enum mdoc_type type
,
375 enum check_lvl lvl
, enum check_ineq ineq
, int val
)
379 if (m
->last
->type
!= type
)
385 if (m
->last
->nchild
< val
)
390 if (m
->last
->nchild
> val
)
395 if (val
== m
->last
->nchild
)
403 if (CHECK_WARN
== lvl
) {
404 return(mdoc_vmsg(m
, MANDOCERR_ARGCOUNT
,
405 m
->last
->line
, m
->last
->pos
,
406 "want %s%d children (have %d)",
407 p
, val
, m
->last
->nchild
));
410 /* FIXME: THIS IS THE SAME AS THE ABOVE. */
412 return(mdoc_vmsg(m
, MANDOCERR_ARGCOUNT
,
413 m
->last
->line
, m
->last
->pos
,
414 "require %s%d children (have %d)",
415 p
, val
, m
->last
->nchild
));
422 return(check_count(mdoc
, MDOC_BODY
, CHECK_FATAL
, CHECK_GT
, 0));
428 return(check_count(mdoc
, MDOC_BODY
, CHECK_WARN
, CHECK_GT
, 0));
434 return(check_count(mdoc
, MDOC_ELEM
, CHECK_FATAL
, CHECK_EQ
, 0));
440 return(check_count(mdoc
, MDOC_ELEM
, CHECK_FATAL
, CHECK_EQ
, 1));
446 return(check_count(mdoc
, MDOC_ELEM
, CHECK_FATAL
, CHECK_GT
, 0));
452 return(check_count(mdoc
, MDOC_ELEM
, CHECK_FATAL
, CHECK_LT
, 2));
458 return(check_count(mdoc
, MDOC_ELEM
, CHECK_WARN
, CHECK_EQ
, 0));
464 return(check_count(mdoc
, MDOC_ELEM
, CHECK_WARN
, CHECK_GT
, 0));
470 return(check_count(mdoc
, MDOC_HEAD
, CHECK_FATAL
, CHECK_EQ
, 0));
476 return(check_count(mdoc
, MDOC_HEAD
, CHECK_FATAL
, CHECK_GT
, 0));
482 return(check_count(mdoc
, MDOC_HEAD
, CHECK_WARN
, CHECK_EQ
, 0));
488 return(check_count(mdoc
, MDOC_HEAD
, CHECK_WARN
, CHECK_EQ
, 1));
494 return(check_count(mdoc
, MDOC_HEAD
, CHECK_WARN
, CHECK_LT
, 2));
498 check_args(struct mdoc
*m
, struct mdoc_node
*n
)
505 assert(n
->args
->argc
);
506 for (i
= 0; i
< (int)n
->args
->argc
; i
++)
507 check_argv(m
, n
, &n
->args
->argv
[i
]);
511 check_argv(struct mdoc
*m
, struct mdoc_node
*n
, struct mdoc_argv
*v
)
515 for (i
= 0; i
< (int)v
->sz
; i
++)
516 check_text(m
, v
->line
, v
->pos
, v
->value
[i
]);
518 /* FIXME: move to post_std(). */
520 if (MDOC_Std
== v
->arg
)
521 if ( ! (v
->sz
|| m
->meta
.name
))
522 mdoc_nmsg(m
, n
, MANDOCERR_NONAME
);
526 check_text(struct mdoc
*m
, int ln
, int pos
, char *p
)
531 for ( ; *p
; p
++, pos
++) {
532 sz
= strcspn(p
, "\t\\");
541 if ( ! (MDOC_LITERAL
& m
->flags
))
542 mdoc_pmsg(m
, ln
, pos
, MANDOCERR_BADTAB
);
546 if (0 == (c
= mandoc_special(p
))) {
547 mdoc_pmsg(m
, ln
, pos
, MANDOCERR_BADESCAPE
);
557 check_parent(PRE_ARGS
, enum mdoct tok
, enum mdoc_type t
)
561 if ((MDOC_ROOT
== t
|| tok
== n
->parent
->tok
) &&
562 (t
== n
->parent
->type
))
565 mdoc_vmsg(mdoc
, MANDOCERR_SYNTCHILD
,
566 n
->line
, n
->pos
, "want parent %s",
567 MDOC_ROOT
== t
? "<root>" :
568 mdoc_macronames
[tok
]);
574 pre_display(PRE_ARGS
)
576 struct mdoc_node
*node
;
578 /* Display elements (`Bd', `D1'...) cannot be nested. */
580 if (MDOC_BLOCK
!= n
->type
)
584 for (node
= mdoc
->last
->parent
; node
; node
= node
->parent
)
585 if (MDOC_BLOCK
== node
->type
)
586 if (MDOC_Bd
== node
->tok
)
592 mdoc_nmsg(mdoc
, n
, MANDOCERR_NESTEDDISP
);
601 const char *offs
, *width
;
603 struct mdoc_node
*np
;
605 if (MDOC_BLOCK
!= n
->type
) {
606 if (ENDBODY_NOT
!= n
->end
) {
608 np
= n
->pending
->parent
;
613 assert(MDOC_BLOCK
== np
->type
);
614 assert(MDOC_Bl
== np
->tok
);
616 n
->data
.Bl
= np
->data
.Bl
;
621 * First figure out which kind of list to use: bind ourselves to
622 * the first mentioned list type and warn about any remaining
623 * ones. If we find no list type, we default to LIST_item.
626 assert(NULL
== n
->data
.Bl
);
627 n
->data
.Bl
= mandoc_calloc(1, sizeof(struct mdoc_bl
));
630 for (i
= 0; n
->args
&& i
< (int)n
->args
->argc
; i
++) {
634 switch (n
->args
->argv
[i
].arg
) {
635 /* Set list types. */
669 /* Set list arguments. */
671 dup
= n
->data
.Bl
->comp
;
675 dup
= (NULL
!= n
->data
.Bl
->width
);
676 width
= n
->args
->argv
[i
].value
[0];
679 /* NB: this can be empty! */
680 if (n
->args
->argv
[i
].sz
) {
681 offs
= n
->args
->argv
[i
].value
[0];
682 dup
= (NULL
!= n
->data
.Bl
->offs
);
685 mdoc_nmsg(mdoc
, n
, MANDOCERR_IGNARGV
);
691 /* Check: duplicate auxiliary arguments. */
694 mdoc_nmsg(mdoc
, n
, MANDOCERR_ARGVREP
);
697 n
->data
.Bl
->comp
= comp
;
699 n
->data
.Bl
->offs
= offs
;
701 n
->data
.Bl
->width
= width
;
703 /* Check: multiple list types. */
705 if (LIST__NONE
!= lt
&& n
->data
.Bl
->type
!= LIST__NONE
)
706 mdoc_nmsg(mdoc
, n
, MANDOCERR_LISTREP
);
708 /* Assign list type. */
710 if (LIST__NONE
!= lt
&& n
->data
.Bl
->type
== LIST__NONE
) {
711 n
->data
.Bl
->type
= lt
;
712 /* Set column information, too. */
713 if (LIST_column
== lt
) {
716 n
->data
.Bl
->cols
= (const char **)
717 n
->args
->argv
[i
].value
;
721 /* The list type should come first. */
723 if (n
->data
.Bl
->type
== LIST__NONE
)
724 if (n
->data
.Bl
->width
||
727 mdoc_nmsg(mdoc
, n
, MANDOCERR_LISTFIRST
);
732 /* Allow lists to default to LIST_item. */
734 if (LIST__NONE
== n
->data
.Bl
->type
) {
735 mdoc_nmsg(mdoc
, n
, MANDOCERR_LISTTYPE
);
736 n
->data
.Bl
->type
= LIST_item
;
740 * Validate the width field. Some list types don't need width
741 * types and should be warned about them. Others should have it
742 * and must also be warned.
745 switch (n
->data
.Bl
->type
) {
747 if (n
->data
.Bl
->width
)
749 mdoc_nmsg(mdoc
, n
, MANDOCERR_NOWIDTHARG
);
760 if (NULL
== n
->data
.Bl
->width
)
762 mdoc_nmsg(mdoc
, n
, MANDOCERR_WIDTHARG
);
778 struct mdoc_node
*np
;
780 if (MDOC_BLOCK
!= n
->type
) {
781 if (ENDBODY_NOT
!= n
->end
) {
783 np
= n
->pending
->parent
;
788 assert(MDOC_BLOCK
== np
->type
);
789 assert(MDOC_Bd
== np
->tok
);
791 n
->data
.Bd
= np
->data
.Bd
;
795 assert(NULL
== n
->data
.Bd
);
796 n
->data
.Bd
= mandoc_calloc(1, sizeof(struct mdoc_bd
));
799 for (i
= 0; n
->args
&& i
< (int)n
->args
->argc
; i
++) {
804 switch (n
->args
->argv
[i
].arg
) {
811 case (MDOC_Unfilled
):
821 mdoc_nmsg(mdoc
, n
, MANDOCERR_BADDISP
);
824 /* NB: this can be empty! */
825 if (n
->args
->argv
[i
].sz
) {
826 offs
= n
->args
->argv
[i
].value
[0];
827 dup
= (NULL
!= n
->data
.Bd
->offs
);
830 mdoc_nmsg(mdoc
, n
, MANDOCERR_IGNARGV
);
834 dup
= n
->data
.Bd
->comp
;
841 /* Check whether we have duplicates. */
844 mdoc_nmsg(mdoc
, n
, MANDOCERR_ARGVREP
);
846 /* Make our auxiliary assignments. */
849 n
->data
.Bd
->offs
= offs
;
851 n
->data
.Bd
->comp
= comp
;
853 /* Check whether a type has already been assigned. */
855 if (DISP__NONE
!= dt
&& n
->data
.Bd
->type
!= DISP__NONE
)
856 mdoc_nmsg(mdoc
, n
, MANDOCERR_DISPREP
);
858 /* Make our type assignment. */
860 if (DISP__NONE
!= dt
&& n
->data
.Bd
->type
== DISP__NONE
)
861 n
->data
.Bd
->type
= dt
;
864 if (DISP__NONE
== n
->data
.Bd
->type
) {
865 mdoc_nmsg(mdoc
, n
, MANDOCERR_DISPTYPE
);
866 n
->data
.Bd
->type
= DISP_ragged
;
877 if (MDOC_BLOCK
!= n
->type
)
879 return(check_parent(mdoc
, n
, MDOC_Sh
, MDOC_BODY
));
887 if (MDOC_BLOCK
!= n
->type
)
890 mdoc
->regs
->regs
[(int)REG_nS
].set
= 0;
891 return(check_parent(mdoc
, n
, MDOC_MAX
, MDOC_ROOT
));
899 if (MDOC_BLOCK
!= n
->type
)
903 * FIXME: this can probably be lifted if we make the It into
904 * something else on-the-fly?
906 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
->data
.An
.auth
= AUTH_split
;
924 else if (MDOC_Nosplit
== n
->args
->argv
[0].arg
)
925 n
->data
.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 assert(np
->data
.Bf
);
1008 mdoc
->last
->data
.Bf
= np
->data
.Bf
;
1013 assert(MDOC_BLOCK
== np
->parent
->type
);
1014 assert(MDOC_Bf
== np
->parent
->tok
);
1015 np
->data
.Bf
= mandoc_calloc(1, sizeof(struct mdoc_bf
));
1018 * Cannot have both argument and parameter.
1019 * If neither is specified, let it through with a warning.
1022 if (np
->parent
->args
&& np
->child
) {
1023 mdoc_nmsg(mdoc
, np
, MANDOCERR_SYNTARGVCOUNT
);
1025 } else if (NULL
== np
->parent
->args
&& NULL
== np
->child
) {
1026 mdoc_nmsg(mdoc
, np
, MANDOCERR_FONTTYPE
);
1030 /* Extract argument into data. */
1032 if (np
->parent
->args
) {
1033 arg
= np
->parent
->args
->argv
[0].arg
;
1034 if (MDOC_Emphasis
== arg
)
1035 np
->data
.Bf
->font
= FONT_Em
;
1036 else if (MDOC_Literal
== arg
)
1037 np
->data
.Bf
->font
= FONT_Li
;
1038 else if (MDOC_Symbolic
== arg
)
1039 np
->data
.Bf
->font
= FONT_Sy
;
1045 /* Extract parameter into data. */
1047 if (0 == strcmp(np
->child
->string
, "Em"))
1048 np
->data
.Bf
->font
= FONT_Em
;
1049 else if (0 == strcmp(np
->child
->string
, "Li"))
1050 np
->data
.Bf
->font
= FONT_Li
;
1051 else if (0 == strcmp(np
->child
->string
, "Sy"))
1052 np
->data
.Bf
->font
= FONT_Sy
;
1054 mdoc_nmsg(mdoc
, np
, MANDOCERR_FONTTYPE
);
1066 assert(mdoc
->last
->child
);
1067 assert(MDOC_TEXT
== mdoc
->last
->child
->type
);
1069 p
= mdoc_a2lib(mdoc
->last
->child
->string
);
1071 /* If lookup ok, replace with table value. */
1074 free(mdoc
->last
->child
->string
);
1075 mdoc
->last
->child
->string
= mandoc_strdup(p
);
1079 /* If not, use "library ``xxxx''. */
1081 sz
= strlen(mdoc
->last
->child
->string
) +
1082 2 + strlen("\\(lqlibrary\\(rq");
1083 buf
= mandoc_malloc(sz
);
1084 snprintf(buf
, sz
, "library \\(lq%s\\(rq",
1085 mdoc
->last
->child
->string
);
1086 free(mdoc
->last
->child
->string
);
1087 mdoc
->last
->child
->string
= buf
;
1092 post_eoln(POST_ARGS
)
1095 if (mdoc
->last
->child
)
1096 mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_ARGSLOST
);
1104 const struct mdoc_node
*n
;
1107 * The Vt macro comes in both ELEM and BLOCK form, both of which
1108 * have different syntaxes (yet more context-sensitive
1109 * behaviour). ELEM types must have a child; BLOCK types,
1110 * specifically the BODY, should only have TEXT children.
1113 if (MDOC_ELEM
== mdoc
->last
->type
)
1114 return(eerr_ge1(mdoc
));
1115 if (MDOC_BODY
!= mdoc
->last
->type
)
1118 for (n
= mdoc
->last
->child
; n
; n
= n
->next
)
1119 if (MDOC_TEXT
!= n
->type
)
1120 mdoc_nmsg(mdoc
, n
, MANDOCERR_CHILD
);
1131 /* If no child specified, make sure we have the meta name. */
1133 if (NULL
== mdoc
->last
->child
&& NULL
== mdoc
->meta
.name
) {
1134 mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_NONAME
);
1136 } else if (mdoc
->meta
.name
)
1139 /* If no meta name, set it from the child. */
1141 if ( ! concat(mdoc
, buf
, mdoc
->last
->child
, BUFSIZ
))
1144 mdoc
->meta
.name
= mandoc_strdup(buf
);
1150 post_literal(POST_ARGS
)
1154 * The `Dl' (note "el" not "one") and `Bd' macros unset the
1155 * MDOC_LITERAL flag as they leave. Note that `Bd' only sets
1156 * this in literal mode, but it doesn't hurt to just switch it
1157 * off in general since displays can't be nested.
1160 if (MDOC_BODY
== mdoc
->last
->type
)
1161 mdoc
->last
->flags
&= ~MDOC_LITERAL
;
1167 post_defaults(POST_ARGS
)
1169 struct mdoc_node
*nn
;
1172 * The `Ar' defaults to "file ..." if no value is provided as an
1173 * argument; the `Mt' and `Pa' macros use "~"; the `Li' just
1174 * gets an empty string.
1177 if (mdoc
->last
->child
)
1181 mdoc
->next
= MDOC_NEXT_CHILD
;
1185 if ( ! mdoc_word_alloc(mdoc
, nn
->line
, nn
->pos
, "file"))
1187 if ( ! mdoc_word_alloc(mdoc
, nn
->line
, nn
->pos
, "..."))
1191 if ( ! mdoc_word_alloc(mdoc
, nn
->line
, nn
->pos
, "AT&T"))
1193 if ( ! mdoc_word_alloc(mdoc
, nn
->line
, nn
->pos
, "UNIX"))
1197 if ( ! mdoc_word_alloc(mdoc
, nn
->line
, nn
->pos
, ""))
1203 if ( ! mdoc_word_alloc(mdoc
, nn
->line
, nn
->pos
, "~"))
1223 * If we have a child, look it up in the standard keys. If a
1224 * key exist, use that instead of the child; if it doesn't,
1225 * prefix "AT&T UNIX " to the existing data.
1228 if (NULL
== mdoc
->last
->child
)
1231 assert(MDOC_TEXT
== mdoc
->last
->child
->type
);
1232 p
= mdoc_a2att(mdoc
->last
->child
->string
);
1235 free(mdoc
->last
->child
->string
);
1236 mdoc
->last
->child
->string
= mandoc_strdup(p
);
1238 mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_BADATT
);
1240 q
= mdoc
->last
->child
->string
;
1241 sz
= strlen(p
) + strlen(q
) + 1;
1242 buf
= mandoc_malloc(sz
);
1243 strlcpy(buf
, p
, sz
);
1244 strlcat(buf
, q
, sz
);
1245 free(mdoc
->last
->child
->string
);
1246 mdoc
->last
->child
->string
= buf
;
1255 struct mdoc_node
*np
;
1258 if (AUTH__NONE
!= np
->data
.An
.auth
&& np
->child
)
1259 return(eerr_eq0(mdoc
));
1262 * FIXME: make this ewarn and make sure that the front-ends
1263 * don't print the arguments.
1265 if (AUTH__NONE
!= np
->data
.An
.auth
|| np
->child
)
1268 mdoc_nmsg(mdoc
, np
, MANDOCERR_NOARGS
);
1278 struct mdoc_node
*n
, *c
;
1281 if (MDOC_BLOCK
!= mdoc
->last
->type
)
1284 n
= mdoc
->last
->parent
->parent
;
1286 lt
= n
->data
.Bl
->type
;
1288 if (LIST__NONE
== lt
) {
1289 mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_LISTTYPE
);
1295 if (mdoc
->last
->head
->child
)
1297 /* FIXME: give this a dummy value. */
1298 mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_NOARGS
);
1307 if (NULL
== mdoc
->last
->head
->child
)
1308 mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_NOARGS
);
1317 if (NULL
== mdoc
->last
->body
->child
)
1318 mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_NOBODY
);
1321 if (mdoc
->last
->head
->child
)
1322 mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_ARGSLOST
);
1325 cols
= (int)n
->data
.Bl
->ncols
;
1327 assert(NULL
== mdoc
->last
->head
->child
);
1329 if (NULL
== mdoc
->last
->body
->child
)
1330 mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_NOBODY
);
1332 for (i
= 0, c
= mdoc
->last
->child
; c
; c
= c
->next
)
1333 if (MDOC_BODY
== c
->type
)
1337 er
= MANDOCERR_ARGCOUNT
;
1338 else if (i
== cols
|| i
== cols
+ 1)
1341 er
= MANDOCERR_SYNTARGCOUNT
;
1343 rc
= mdoc_vmsg(mdoc
, er
,
1344 mdoc
->last
->line
, mdoc
->last
->pos
,
1345 "columns == %d (have %d)", cols
, i
);
1355 post_bl_block(POST_ARGS
)
1357 struct mdoc_node
*n
;
1360 * These are fairly complicated, so we've broken them into two
1361 * functions. post_bl_block_tag() is called when a -tag is
1362 * specified, but no -width (it must be guessed). The second
1363 * when a -width is specified (macro indicators must be
1364 * rewritten into real lengths).
1369 if (LIST_tag
== n
->data
.Bl
->type
&&
1370 NULL
== n
->data
.Bl
->width
) {
1371 if ( ! post_bl_block_tag(mdoc
))
1373 } else if (NULL
!= n
->data
.Bl
->width
) {
1374 if ( ! post_bl_block_width(mdoc
))
1379 assert(n
->data
.Bl
->width
);
1384 post_bl_block_width(POST_ARGS
)
1389 struct mdoc_node
*n
;
1395 * Calculate the real width of a list from the -width string,
1396 * which may contain a macro (with a known default width), a
1397 * literal string, or a scaling width.
1399 * If the value to -width is a macro, then we re-write it to be
1400 * the macro's width as set in share/tmac/mdoc/doc-common.
1403 if (0 == strcmp(n
->data
.Bl
->width
, "Ds"))
1405 else if (MDOC_MAX
== (tok
= mdoc_hash_find(n
->data
.Bl
->width
)))
1407 else if (0 == (width
= mdoc_macro2len(tok
))) {
1408 mdoc_nmsg(mdoc
, n
, MANDOCERR_BADWIDTH
);
1412 /* The value already exists: free and reallocate it. */
1416 for (i
= 0; i
< (int)n
->args
->argc
; i
++)
1417 if (MDOC_Width
== n
->args
->argv
[i
].arg
)
1420 assert(i
< (int)n
->args
->argc
);
1422 snprintf(buf
, NUMSIZ
, "%zun", width
);
1423 free(n
->args
->argv
[i
].value
[0]);
1424 n
->args
->argv
[i
].value
[0] = mandoc_strdup(buf
);
1426 /* Set our width! */
1427 n
->data
.Bl
->width
= n
->args
->argv
[i
].value
[0];
1432 post_bl_block_tag(POST_ARGS
)
1434 struct mdoc_node
*n
, *nn
;
1440 * Calculate the -width for a `Bl -tag' list if it hasn't been
1441 * provided. Uses the first head macro. NOTE AGAIN: this is
1442 * ONLY if the -width argument has NOT been provided. See
1443 * post_bl_block_width() for converting the -width string.
1449 for (nn
= n
->body
->child
; nn
; nn
= nn
->next
) {
1450 if (MDOC_It
!= nn
->tok
)
1453 assert(MDOC_BLOCK
== nn
->type
);
1454 nn
= nn
->head
->child
;
1457 /* No -width for .Bl and first .It is emtpy */
1458 mdoc_nmsg(mdoc
, n
, MANDOCERR_NOWIDTHARG
);
1462 if (MDOC_TEXT
== nn
->type
) {
1463 sz
= strlen(nn
->string
) + 1;
1467 if (0 != (ssz
= mdoc_macro2len(nn
->tok
)))
1470 mdoc_nmsg(mdoc
, n
, MANDOCERR_NOWIDTHARG
);
1475 /* Defaults to ten ens. */
1477 snprintf(buf
, NUMSIZ
, "%zun", sz
);
1480 * We have to dynamically add this to the macro's argument list.
1481 * We're guaranteed that a MDOC_Width doesn't already exist.
1485 i
= (int)(n
->args
->argc
)++;
1487 n
->args
->argv
= mandoc_realloc(n
->args
->argv
,
1488 n
->args
->argc
* sizeof(struct mdoc_argv
));
1490 n
->args
->argv
[i
].arg
= MDOC_Width
;
1491 n
->args
->argv
[i
].line
= n
->line
;
1492 n
->args
->argv
[i
].pos
= n
->pos
;
1493 n
->args
->argv
[i
].sz
= 1;
1494 n
->args
->argv
[i
].value
= mandoc_malloc(sizeof(char *));
1495 n
->args
->argv
[i
].value
[0] = mandoc_strdup(buf
);
1497 /* Set our width! */
1498 n
->data
.Bl
->width
= n
->args
->argv
[i
].value
[0];
1504 post_bl_head(POST_ARGS
)
1506 struct mdoc_node
*np
, *nn
, *nnp
;
1509 if (LIST_column
!= mdoc
->last
->data
.Bl
->type
)
1510 /* FIXME: this should be ERROR class... */
1511 return(hwarn_eq0(mdoc
));
1514 * Convert old-style lists, where the column width specifiers
1515 * trail as macro parameters, to the new-style ("normal-form")
1516 * lists where they're argument values following -column.
1519 /* First, disallow both types and allow normal-form. */
1522 * TODO: technically, we can accept both and just merge the two
1523 * lists, but I'll leave that for another day.
1526 if (mdoc
->last
->data
.Bl
->ncols
&& mdoc
->last
->nchild
) {
1527 mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_COLUMNS
);
1529 } else if (NULL
== mdoc
->last
->child
)
1532 np
= mdoc
->last
->parent
;
1535 for (j
= 0; j
< (int)np
->args
->argc
; j
++)
1536 if (MDOC_Column
== np
->args
->argv
[j
].arg
)
1539 assert(j
< (int)np
->args
->argc
);
1540 assert(0 == np
->args
->argv
[j
].sz
);
1543 * Accomodate for new-style groff column syntax. Shuffle the
1544 * child nodes, all of which must be TEXT, as arguments for the
1545 * column field. Then, delete the head children.
1548 np
->args
->argv
[j
].sz
= (size_t)mdoc
->last
->nchild
;
1549 np
->args
->argv
[j
].value
= mandoc_malloc
1550 ((size_t)mdoc
->last
->nchild
* sizeof(char *));
1552 mdoc
->last
->data
.Bl
->ncols
= np
->args
->argv
[j
].sz
;
1553 mdoc
->last
->data
.Bl
->cols
= (const char **)np
->args
->argv
[j
].value
;
1555 for (i
= 0, nn
= mdoc
->last
->child
; nn
; i
++) {
1556 np
->args
->argv
[j
].value
[i
] = nn
->string
;
1560 mdoc_node_delete(NULL
, nnp
);
1563 mdoc
->last
->nchild
= 0;
1564 mdoc
->last
->child
= NULL
;
1572 struct mdoc_node
*n
;
1574 if (MDOC_HEAD
== mdoc
->last
->type
)
1575 return(post_bl_head(mdoc
));
1576 if (MDOC_BLOCK
== mdoc
->last
->type
)
1577 return(post_bl_block(mdoc
));
1578 if (MDOC_BODY
!= mdoc
->last
->type
)
1580 if (NULL
== mdoc
->last
->child
)
1584 * We only allow certain children of `Bl'. This is usually on
1585 * `It', but apparently `Sm' occurs here and there, so we let
1586 * that one through, too.
1590 for (n
= mdoc
->last
->child
; n
; n
= n
->next
) {
1591 if (MDOC_BLOCK
== n
->type
&& MDOC_It
== n
->tok
)
1593 if (MDOC_Sm
== n
->tok
)
1595 mdoc_nmsg(mdoc
, n
, MANDOCERR_SYNTCHILD
);
1603 ebool(struct mdoc
*mdoc
)
1606 if (NULL
== mdoc
->last
->child
)
1609 assert(MDOC_TEXT
== mdoc
->last
->child
->type
);
1611 if (0 == strcmp(mdoc
->last
->child
->string
, "on"))
1613 if (0 == strcmp(mdoc
->last
->child
->string
, "off"))
1616 mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_BADBOOL
);
1621 post_root(POST_ARGS
)
1624 struct mdoc_node
*n
;
1628 /* Check that we have a finished prologue. */
1630 if ( ! (MDOC_PBODY
& mdoc
->flags
)) {
1632 mdoc_nmsg(mdoc
, mdoc
->first
, MANDOCERR_NODOCPROLOG
);
1638 /* Check that we begin with a proper `Sh'. */
1640 if (NULL
== n
->child
) {
1642 mdoc_nmsg(mdoc
, n
, MANDOCERR_NODOCBODY
);
1643 } else if (MDOC_BLOCK
!= n
->child
->type
||
1644 MDOC_Sh
!= n
->child
->tok
) {
1646 /* Can this be lifted? See rxdebug.1 for example. */
1647 mdoc_nmsg(mdoc
, n
, MANDOCERR_NODOCBODY
);
1650 return(erc
? 0 : 1);
1658 assert(MDOC_TEXT
== mdoc
->last
->child
->type
);
1660 p
= mdoc_a2st(mdoc
->last
->child
->string
);
1663 mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_BADSTANDARD
);
1664 mdoc_node_delete(mdoc
, mdoc
->last
);
1666 free(mdoc
->last
->child
->string
);
1667 mdoc
->last
->child
->string
= mandoc_strdup(p
);
1676 struct mdoc_node
*nn
, *next
, *prev
;
1679 if (MDOC_BODY
!= mdoc
->last
->type
)
1683 * Make sure only certain types of nodes are allowed within the
1684 * the `Rs' body. Delete offending nodes and raise a warning.
1685 * Do this before re-ordering for the sake of clarity.
1689 for (nn
= mdoc
->last
->child
; nn
; nn
= next
) {
1690 for (i
= 0; i
< RSORD_MAX
; i
++)
1691 if (nn
->tok
== rsord
[i
])
1694 if (i
< RSORD_MAX
) {
1700 mdoc_nmsg(mdoc
, nn
, MANDOCERR_CHILD
);
1701 mdoc_node_delete(mdoc
, nn
);
1705 * The full `Rs' block needs special handling to order the
1706 * sub-elements according to `rsord'. Pick through each element
1707 * and correctly order it. This is a insertion sort.
1711 for (nn
= mdoc
->last
->child
->next
; nn
; nn
= next
) {
1712 /* Determine order of `nn'. */
1713 for (i
= 0; i
< RSORD_MAX
; i
++)
1714 if (rsord
[i
] == nn
->tok
)
1718 * Remove `nn' from the chain. This somewhat
1719 * repeats mdoc_node_unlink(), but since we're
1720 * just re-ordering, there's no need for the
1721 * full unlink process.
1724 if (NULL
!= (next
= nn
->next
))
1725 next
->prev
= nn
->prev
;
1727 if (NULL
!= (prev
= nn
->prev
))
1728 prev
->next
= nn
->next
;
1730 nn
->prev
= nn
->next
= NULL
;
1733 * Scan back until we reach a node that's
1734 * ordered before `nn'.
1737 for ( ; prev
; prev
= prev
->prev
) {
1738 /* Determine order of `prev'. */
1739 for (j
= 0; j
< RSORD_MAX
; j
++)
1740 if (rsord
[j
] == prev
->tok
)
1748 * Set `nn' back into its correct place in front
1749 * of the `prev' node.
1756 prev
->next
->prev
= nn
;
1757 nn
->next
= prev
->next
;
1760 mdoc
->last
->child
->prev
= nn
;
1761 nn
->next
= mdoc
->last
->child
;
1762 mdoc
->last
->child
= nn
;
1773 if (MDOC_HEAD
== mdoc
->last
->type
)
1774 return(post_sh_head(mdoc
));
1775 if (MDOC_BODY
== mdoc
->last
->type
)
1776 return(post_sh_body(mdoc
));
1782 post_sh_body(POST_ARGS
)
1784 struct mdoc_node
*n
;
1786 if (SEC_NAME
!= mdoc
->lastsec
)
1790 * Warn if the NAME section doesn't contain the `Nm' and `Nd'
1791 * macros (can have multiple `Nm' and one `Nd'). Note that the
1792 * children of the BODY declaration can also be "text".
1795 if (NULL
== (n
= mdoc
->last
->child
)) {
1796 mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_BADNAMESEC
);
1800 for ( ; n
&& n
->next
; n
= n
->next
) {
1801 if (MDOC_ELEM
== n
->type
&& MDOC_Nm
== n
->tok
)
1803 if (MDOC_TEXT
== n
->type
)
1805 mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_BADNAMESEC
);
1809 if (MDOC_BLOCK
== n
->type
&& MDOC_Nd
== n
->tok
)
1812 mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_BADNAMESEC
);
1817 post_sh_head(POST_ARGS
)
1823 * Process a new section. Sections are either "named" or
1824 * "custom". Custom sections are user-defined, while named ones
1825 * follow a conventional order and may only appear in certain
1829 if ( ! concat(mdoc
, buf
, mdoc
->last
->child
, BUFSIZ
))
1832 sec
= mdoc_str2sec(buf
);
1834 /* The NAME should be first. */
1836 if (SEC_NAME
!= sec
&& SEC_NONE
== mdoc
->lastnamed
)
1837 mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_NAMESECFIRST
);
1839 /* The SYNOPSIS gets special attention in other areas. */
1841 if (SEC_SYNOPSIS
== sec
)
1842 mdoc
->flags
|= MDOC_SYNOPSIS
;
1844 mdoc
->flags
&= ~MDOC_SYNOPSIS
;
1846 /* Mark our last section. */
1848 mdoc
->lastsec
= sec
;
1850 /* We don't care about custom sections after this. */
1852 if (SEC_CUSTOM
== sec
)
1856 * Check whether our non-custom section is being repeated or is
1860 if (sec
== mdoc
->lastnamed
)
1861 mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_SECREP
);
1863 if (sec
< mdoc
->lastnamed
)
1864 mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_SECOOO
);
1866 /* Mark the last named section. */
1868 mdoc
->lastnamed
= sec
;
1870 /* Check particular section/manual conventions. */
1872 assert(mdoc
->meta
.msec
);
1875 case (SEC_RETURN_VALUES
):
1880 if (*mdoc
->meta
.msec
== '2')
1882 if (*mdoc
->meta
.msec
== '3')
1884 if (*mdoc
->meta
.msec
== '9')
1886 mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_SECMSEC
);
1899 if (NULL
== mdoc
->last
)
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
)
1910 if (MDOC_Bl
== n
->tok
&& n
->data
.Bl
->comp
)
1912 if (MDOC_Bd
== n
->tok
&& n
->data
.Bd
->comp
)
1915 mdoc_nmsg(mdoc
, mdoc
->last
, MANDOCERR_IGNPAR
);
1916 mdoc_node_delete(mdoc
, mdoc
->last
);
1921 pre_literal(PRE_ARGS
)
1924 if (MDOC_BODY
!= n
->type
)
1928 * The `Dl' (note "el" not "one") and `Bd -literal' and `Bd
1929 * -unfilled' macros set MDOC_LITERAL on entrance to the body.
1934 mdoc
->flags
|= MDOC_LITERAL
;
1938 if (DISP_literal
== n
->data
.Bd
->type
)
1939 mdoc
->flags
|= MDOC_LITERAL
;
1940 if (DISP_unfilled
== n
->data
.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
, DATESIZ
))
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 (-1 == uname(&utsname
))
2138 return(mdoc_nmsg(mdoc
, n
, MANDOCERR_UTSNAME
));
2140 if (strlcat(buf
, utsname
.sysname
, BUFSIZ
) >= BUFSIZ
) {
2141 mdoc_nmsg(mdoc
, n
, MANDOCERR_MEM
);
2144 if (strlcat(buf
, " ", 64) >= BUFSIZ
) {
2145 mdoc_nmsg(mdoc
, n
, MANDOCERR_MEM
);
2148 if (strlcat(buf
, utsname
.release
, BUFSIZ
) >= BUFSIZ
) {
2149 mdoc_nmsg(mdoc
, n
, MANDOCERR_MEM
);
2155 mdoc
->meta
.os
= mandoc_strdup(buf
);
2162 struct mdoc_node
*nn
, *n
;
2167 * Macros accepting `-std' as an argument have the name of the
2168 * current document (`Nm') filled in as the argument if it's not
2175 if (NULL
== mdoc
->meta
.name
)
2179 mdoc
->next
= MDOC_NEXT_CHILD
;
2181 if ( ! mdoc_word_alloc(mdoc
, n
->line
, n
->pos
, mdoc
->meta
.name
))
2189 concat(struct mdoc
*m
, char *p
, const struct mdoc_node
*n
, size_t sz
)
2195 * Concatenate sibling nodes together. All siblings must be of
2196 * type MDOC_TEXT or an assertion is raised. Concatenation is
2197 * separated by a single whitespace. Returns 0 on fatal (string
2201 for ( ; n
; n
= n
->next
) {
2202 assert(MDOC_TEXT
== n
->type
);
2204 if (strlcat(p
, n
->string
, sz
) >= sz
) {
2205 mdoc_nmsg(m
, n
, MANDOCERR_MEM
);
2209 if (NULL
== n
->next
)
2212 if (strlcat(p
, " ", sz
) >= sz
) {
2213 mdoc_nmsg(m
, n
, MANDOCERR_MEM
);