]>
git.cameronkatri.com Git - mandoc.git/blob - mdoc_term.c
1 /* $Id: mdoc_term.c,v 1.54 2009/07/21 15:03:37 kristaps Exp $ */
3 * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@kth.se>
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.
17 #include <sys/types.h>
29 /* FIXME: macro arguments can be escaped. */
32 #define TTYPE_CMD_FLAG 1
33 #define TTYPE_CMD_ARG 2
34 #define TTYPE_SECTION 3
35 #define TTYPE_FUNC_DECL 4
36 #define TTYPE_VAR_DECL 5
37 #define TTYPE_FUNC_TYPE 6
38 #define TTYPE_FUNC_NAME 7
39 #define TTYPE_FUNC_ARG 8
41 #define TTYPE_SSECTION 10
44 #define TTYPE_CONFIG 13
46 #define TTYPE_INCLUDE 15
48 #define TTYPE_SYMBOL 17
50 #define TTYPE_LINK_ANCHOR 19
51 #define TTYPE_LINK_TEXT 20
52 #define TTYPE_REF_JOURNAL 21
56 const int ttypes
[TTYPE_NMAX
] = {
57 TERMP_BOLD
, /* TTYPE_PROG */
58 TERMP_BOLD
, /* TTYPE_CMD_FLAG */
59 TERMP_UNDER
, /* TTYPE_CMD_ARG */
60 TERMP_BOLD
, /* TTYPE_SECTION */
61 TERMP_BOLD
, /* TTYPE_FUNC_DECL */
62 TERMP_UNDER
, /* TTYPE_VAR_DECL */
63 TERMP_UNDER
, /* TTYPE_FUNC_TYPE */
64 TERMP_BOLD
, /* TTYPE_FUNC_NAME */
65 TERMP_UNDER
, /* TTYPE_FUNC_ARG */
66 TERMP_UNDER
, /* TTYPE_LINK */
67 TERMP_BOLD
, /* TTYPE_SSECTION */
69 TERMP_UNDER
, /* TTYPE_EMPH */
70 TERMP_BOLD
, /* TTYPE_CONFIG */
71 TERMP_BOLD
, /* TTYPE_CMD */
72 TERMP_BOLD
, /* TTYPE_INCLUDE */
73 TERMP_BOLD
, /* TTYPE_SYMB */
74 TERMP_BOLD
, /* TTYPE_SYMBOL */
75 TERMP_BOLD
, /* TTYPE_DIAG */
76 TERMP_UNDER
, /* TTYPE_LINK_ANCHOR */
77 TERMP_BOLD
, /* TTYPE_LINK_TEXT */
78 TERMP_UNDER
, /* TTYPE_REF_JOURNAL */
79 TERMP_BOLD
/* TTYPE_LIST */
83 struct termpair
*ppair
;
88 #define DECL_ARGS struct termp *p, \
89 struct termpair *pair, \
90 const struct mdoc_meta *meta, \
91 const struct mdoc_node *node
94 int (*pre
)(DECL_ARGS
);
95 void (*post
)(DECL_ARGS
);
98 static void termp____post(DECL_ARGS
);
99 static void termp__t_post(DECL_ARGS
);
100 static void termp_aq_post(DECL_ARGS
);
101 static void termp_bd_post(DECL_ARGS
);
102 static void termp_bl_post(DECL_ARGS
);
103 static void termp_bq_post(DECL_ARGS
);
104 static void termp_brq_post(DECL_ARGS
);
105 static void termp_bx_post(DECL_ARGS
);
106 static void termp_d1_post(DECL_ARGS
);
107 static void termp_dq_post(DECL_ARGS
);
108 static void termp_fd_post(DECL_ARGS
);
109 static void termp_fn_post(DECL_ARGS
);
110 static void termp_fo_post(DECL_ARGS
);
111 static void termp_ft_post(DECL_ARGS
);
112 static void termp_in_post(DECL_ARGS
);
113 static void termp_it_post(DECL_ARGS
);
114 static void termp_lb_post(DECL_ARGS
);
115 static void termp_op_post(DECL_ARGS
);
116 static void termp_pf_post(DECL_ARGS
);
117 static void termp_pq_post(DECL_ARGS
);
118 static void termp_qq_post(DECL_ARGS
);
119 static void termp_sh_post(DECL_ARGS
);
120 static void termp_sq_post(DECL_ARGS
);
121 static void termp_ss_post(DECL_ARGS
);
122 static void termp_vt_post(DECL_ARGS
);
124 static int termp__j_pre(DECL_ARGS
);
125 static int termp__t_pre(DECL_ARGS
);
126 static int termp_ap_pre(DECL_ARGS
);
127 static int termp_aq_pre(DECL_ARGS
);
128 static int termp_ar_pre(DECL_ARGS
);
129 static int termp_bd_pre(DECL_ARGS
);
130 static int termp_bf_pre(DECL_ARGS
);
131 static int termp_bq_pre(DECL_ARGS
);
132 static int termp_br_pre(DECL_ARGS
);
133 static int termp_brq_pre(DECL_ARGS
);
134 static int termp_bt_pre(DECL_ARGS
);
135 static int termp_cd_pre(DECL_ARGS
);
136 static int termp_cm_pre(DECL_ARGS
);
137 static int termp_d1_pre(DECL_ARGS
);
138 static int termp_dq_pre(DECL_ARGS
);
139 static int termp_em_pre(DECL_ARGS
);
140 static int termp_ex_pre(DECL_ARGS
);
141 static int termp_fa_pre(DECL_ARGS
);
142 static int termp_fd_pre(DECL_ARGS
);
143 static int termp_fl_pre(DECL_ARGS
);
144 static int termp_fn_pre(DECL_ARGS
);
145 static int termp_fo_pre(DECL_ARGS
);
146 static int termp_ft_pre(DECL_ARGS
);
147 static int termp_ic_pre(DECL_ARGS
);
148 static int termp_in_pre(DECL_ARGS
);
149 static int termp_it_pre(DECL_ARGS
);
150 static int termp_lk_pre(DECL_ARGS
);
151 static int termp_ms_pre(DECL_ARGS
);
152 static int termp_mt_pre(DECL_ARGS
);
153 static int termp_nd_pre(DECL_ARGS
);
154 static int termp_nm_pre(DECL_ARGS
);
155 static int termp_ns_pre(DECL_ARGS
);
156 static int termp_op_pre(DECL_ARGS
);
157 static int termp_pf_pre(DECL_ARGS
);
158 static int termp_pp_pre(DECL_ARGS
);
159 static int termp_pq_pre(DECL_ARGS
);
160 static int termp_qq_pre(DECL_ARGS
);
161 static int termp_rs_pre(DECL_ARGS
);
162 static int termp_rv_pre(DECL_ARGS
);
163 static int termp_sh_pre(DECL_ARGS
);
164 static int termp_sm_pre(DECL_ARGS
);
165 static int termp_sp_pre(DECL_ARGS
);
166 static int termp_sq_pre(DECL_ARGS
);
167 static int termp_ss_pre(DECL_ARGS
);
168 static int termp_sx_pre(DECL_ARGS
);
169 static int termp_sy_pre(DECL_ARGS
);
170 static int termp_ud_pre(DECL_ARGS
);
171 static int termp_va_pre(DECL_ARGS
);
172 static int termp_vt_pre(DECL_ARGS
);
173 static int termp_xr_pre(DECL_ARGS
);
174 static int termp_xx_pre(DECL_ARGS
);
176 static const struct termact termacts
[MDOC_MAX
] = {
177 { termp_ap_pre
, NULL
}, /* Ap */
178 { NULL
, NULL
}, /* Dd */
179 { NULL
, NULL
}, /* Dt */
180 { NULL
, NULL
}, /* Os */
181 { termp_sh_pre
, termp_sh_post
}, /* Sh */
182 { termp_ss_pre
, termp_ss_post
}, /* Ss */
183 { termp_pp_pre
, NULL
}, /* Pp */
184 { termp_d1_pre
, termp_d1_post
}, /* D1 */
185 { termp_d1_pre
, termp_d1_post
}, /* Dl */
186 { termp_bd_pre
, termp_bd_post
}, /* Bd */
187 { NULL
, NULL
}, /* Ed */
188 { NULL
, termp_bl_post
}, /* Bl */
189 { NULL
, NULL
}, /* El */
190 { termp_it_pre
, termp_it_post
}, /* It */
191 { NULL
, NULL
}, /* Ad */
192 { NULL
, NULL
}, /* An */
193 { termp_ar_pre
, NULL
}, /* Ar */
194 { termp_cd_pre
, NULL
}, /* Cd */
195 { termp_cm_pre
, NULL
}, /* Cm */
196 { NULL
, NULL
}, /* Dv */
197 { NULL
, NULL
}, /* Er */
198 { NULL
, NULL
}, /* Ev */
199 { termp_ex_pre
, NULL
}, /* Ex */
200 { termp_fa_pre
, NULL
}, /* Fa */
201 { termp_fd_pre
, termp_fd_post
}, /* Fd */
202 { termp_fl_pre
, NULL
}, /* Fl */
203 { termp_fn_pre
, termp_fn_post
}, /* Fn */
204 { termp_ft_pre
, termp_ft_post
}, /* Ft */
205 { termp_ic_pre
, NULL
}, /* Ic */
206 { termp_in_pre
, termp_in_post
}, /* In */
207 { NULL
, NULL
}, /* Li */
208 { termp_nd_pre
, NULL
}, /* Nd */
209 { termp_nm_pre
, NULL
}, /* Nm */
210 { termp_op_pre
, termp_op_post
}, /* Op */
211 { NULL
, NULL
}, /* Ot */
212 { NULL
, NULL
}, /* Pa */
213 { termp_rv_pre
, NULL
}, /* Rv */
214 { NULL
, NULL
}, /* St */
215 { termp_va_pre
, NULL
}, /* Va */
216 { termp_vt_pre
, termp_vt_post
}, /* Vt */
217 { termp_xr_pre
, NULL
}, /* Xr */
218 { NULL
, termp____post
}, /* %A */
219 { NULL
, termp____post
}, /* %B */
220 { NULL
, termp____post
}, /* %D */
221 { NULL
, termp____post
}, /* %I */
222 { termp__j_pre
, termp____post
}, /* %J */
223 { NULL
, termp____post
}, /* %N */
224 { NULL
, termp____post
}, /* %O */
225 { NULL
, termp____post
}, /* %P */
226 { NULL
, termp____post
}, /* %R */
227 { termp__t_pre
, termp__t_post
}, /* %T */
228 { NULL
, termp____post
}, /* %V */
229 { NULL
, NULL
}, /* Ac */
230 { termp_aq_pre
, termp_aq_post
}, /* Ao */
231 { termp_aq_pre
, termp_aq_post
}, /* Aq */
232 { NULL
, NULL
}, /* At */
233 { NULL
, NULL
}, /* Bc */
234 { termp_bf_pre
, NULL
}, /* Bf */
235 { termp_bq_pre
, termp_bq_post
}, /* Bo */
236 { termp_bq_pre
, termp_bq_post
}, /* Bq */
237 { termp_xx_pre
, NULL
}, /* Bsx */
238 { NULL
, termp_bx_post
}, /* Bx */
239 { NULL
, NULL
}, /* Db */
240 { NULL
, NULL
}, /* Dc */
241 { termp_dq_pre
, termp_dq_post
}, /* Do */
242 { termp_dq_pre
, termp_dq_post
}, /* Dq */
243 { NULL
, NULL
}, /* Ec */
244 { NULL
, NULL
}, /* Ef */
245 { termp_em_pre
, NULL
}, /* Em */
246 { NULL
, NULL
}, /* Eo */
247 { termp_xx_pre
, NULL
}, /* Fx */
248 { termp_ms_pre
, NULL
}, /* Ms */
249 { NULL
, NULL
}, /* No */
250 { termp_ns_pre
, NULL
}, /* Ns */
251 { termp_xx_pre
, NULL
}, /* Nx */
252 { termp_xx_pre
, NULL
}, /* Ox */
253 { NULL
, NULL
}, /* Pc */
254 { termp_pf_pre
, termp_pf_post
}, /* Pf */
255 { termp_pq_pre
, termp_pq_post
}, /* Po */
256 { termp_pq_pre
, termp_pq_post
}, /* Pq */
257 { NULL
, NULL
}, /* Qc */
258 { termp_sq_pre
, termp_sq_post
}, /* Ql */
259 { termp_qq_pre
, termp_qq_post
}, /* Qo */
260 { termp_qq_pre
, termp_qq_post
}, /* Qq */
261 { NULL
, NULL
}, /* Re */
262 { termp_rs_pre
, NULL
}, /* Rs */
263 { NULL
, NULL
}, /* Sc */
264 { termp_sq_pre
, termp_sq_post
}, /* So */
265 { termp_sq_pre
, termp_sq_post
}, /* Sq */
266 { termp_sm_pre
, NULL
}, /* Sm */
267 { termp_sx_pre
, NULL
}, /* Sx */
268 { termp_sy_pre
, NULL
}, /* Sy */
269 { NULL
, NULL
}, /* Tn */
270 { termp_xx_pre
, NULL
}, /* Ux */
271 { NULL
, NULL
}, /* Xc */
272 { NULL
, NULL
}, /* Xo */
273 { termp_fo_pre
, termp_fo_post
}, /* Fo */
274 { NULL
, NULL
}, /* Fc */
275 { termp_op_pre
, termp_op_post
}, /* Oo */
276 { NULL
, NULL
}, /* Oc */
277 { NULL
, NULL
}, /* Bk */
278 { NULL
, NULL
}, /* Ek */
279 { termp_bt_pre
, NULL
}, /* Bt */
280 { NULL
, NULL
}, /* Hf */
281 { NULL
, NULL
}, /* Fr */
282 { termp_ud_pre
, NULL
}, /* Ud */
283 { NULL
, termp_lb_post
}, /* Lb */
284 { termp_pp_pre
, NULL
}, /* Lp */
285 { termp_lk_pre
, NULL
}, /* Lk */
286 { termp_mt_pre
, NULL
}, /* Mt */
287 { termp_brq_pre
, termp_brq_post
}, /* Brq */
288 { termp_brq_pre
, termp_brq_post
}, /* Bro */
289 { NULL
, NULL
}, /* Brc */
290 { NULL
, NULL
}, /* %C */
291 { NULL
, NULL
}, /* Es */
292 { NULL
, NULL
}, /* En */
293 { termp_xx_pre
, NULL
}, /* Dx */
294 { NULL
, NULL
}, /* %Q */
295 { termp_br_pre
, NULL
}, /* br */
296 { termp_sp_pre
, NULL
}, /* sp */
300 extern size_t strlcpy(char *, const char *, size_t);
301 extern size_t strlcat(char *, const char *, size_t);
304 static int arg_hasattr(int, const struct mdoc_node
*);
305 static int arg_getattrs(const int *, int *, size_t,
306 const struct mdoc_node
*);
307 static int arg_getattr(int, const struct mdoc_node
*);
308 static size_t arg_offset(const struct mdoc_argv
*);
309 static size_t arg_width(const struct mdoc_argv
*, int);
310 static int arg_listtype(const struct mdoc_node
*);
311 static void fmt_block_vspace(struct termp
*,
312 const struct mdoc_node
*,
313 const struct mdoc_node
*);
314 static void print_node(DECL_ARGS
);
315 static void print_head(struct termp
*,
316 const struct mdoc_meta
*);
317 static void print_body(DECL_ARGS
);
318 static void print_foot(struct termp
*,
319 const struct mdoc_meta
*);
323 mdoc_run(struct termp
*p
, const struct mdoc
*m
)
326 * Main output function. When this is called, assume that the
327 * tree is properly formed.
329 print_head(p
, mdoc_meta(m
));
330 assert(mdoc_node(m
));
331 assert(MDOC_ROOT
== mdoc_node(m
)->type
);
332 if (mdoc_node(m
)->child
)
333 print_body(p
, NULL
, mdoc_meta(m
), mdoc_node(m
)->child
);
334 print_foot(p
, mdoc_meta(m
));
340 print_body(DECL_ARGS
)
343 print_node(p
, pair
, meta
, node
);
346 print_body(p
, pair
, meta
, node
->next
);
351 print_node(DECL_ARGS
)
354 struct termpair npair
;
355 size_t offset
, rmargin
;
359 rmargin
= p
->rmargin
;
366 * Note on termpair. This allows a pre function to set a termp
367 * flag that is automatically unset after the body, but before
368 * the post function. Thus, if a pre uses a termpair flag, it
369 * must be reapplied in the post for use.
372 if (MDOC_TEXT
!= node
->type
) {
373 if (termacts
[node
->tok
].pre
)
374 if ( ! (*termacts
[node
->tok
].pre
)(p
, &npair
, meta
, node
))
376 } else /* MDOC_TEXT == node->type */
377 term_word(p
, node
->string
);
381 p
->flags
|= npair
.flag
;
383 if (dochild
&& node
->child
)
384 print_body(p
, &npair
, meta
, node
->child
);
386 p
->flags
&= ~npair
.flag
;
388 /* Post-processing. */
390 if (MDOC_TEXT
!= node
->type
)
391 if (termacts
[node
->tok
].post
)
392 (*termacts
[node
->tok
].post
)(p
, &npair
, meta
, node
);
395 p
->rmargin
= rmargin
;
400 print_foot(struct termp
*p
, const struct mdoc_meta
*meta
)
406 * Output the footer in new-groff style, that is, three columns
407 * with the middle being the manual date and flanking columns
408 * being the operating system:
413 if (NULL
== (buf
= malloc(p
->rmargin
)))
415 if (NULL
== (os
= malloc(p
->rmargin
)))
418 tm
= localtime(&meta
->date
);
420 if (0 == strftime(buf
, p
->rmargin
, "%B %e, %Y", tm
))
423 (void)strlcpy(os
, meta
->os
, p
->rmargin
);
428 p
->rmargin
= (p
->maxrmargin
- strlen(buf
) + 1) / 2;
429 p
->flags
|= TERMP_NOSPACE
| TERMP_NOBREAK
;
434 p
->offset
= p
->rmargin
;
435 p
->rmargin
= p
->maxrmargin
- strlen(os
);
436 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
441 p
->offset
= p
->rmargin
;
442 p
->rmargin
= p
->maxrmargin
;
443 p
->flags
&= ~TERMP_NOBREAK
;
444 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
450 p
->rmargin
= p
->maxrmargin
;
459 print_head(struct termp
*p
, const struct mdoc_meta
*meta
)
463 p
->rmargin
= p
->maxrmargin
;
466 if (NULL
== (buf
= malloc(p
->rmargin
)))
468 if (NULL
== (title
= malloc(p
->rmargin
)))
472 * The header is strange. It has three components, which are
473 * really two with the first duplicated. It goes like this:
475 * IDENTIFIER TITLE IDENTIFIER
477 * The IDENTIFIER is NAME(SECTION), which is the command-name
478 * (if given, or "unknown" if not) followed by the manual page
479 * section. These are given in `Dt'. The TITLE is a free-form
480 * string depending on the manual volume. If not specified, it
481 * switches on the manual section.
485 (void)strlcpy(buf
, meta
->vol
, p
->rmargin
);
488 (void)strlcat(buf
, " (", p
->rmargin
);
489 (void)strlcat(buf
, meta
->arch
, p
->rmargin
);
490 (void)strlcat(buf
, ")", p
->rmargin
);
493 (void)snprintf(title
, p
->rmargin
, "%s(%d)",
494 meta
->title
, meta
->msec
);
497 p
->rmargin
= (p
->maxrmargin
- strlen(buf
) + 1) / 2;
498 p
->flags
|= TERMP_NOBREAK
| TERMP_NOSPACE
;
503 p
->offset
= p
->rmargin
;
504 p
->rmargin
= p
->maxrmargin
- strlen(title
);
505 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
510 p
->offset
= p
->rmargin
;
511 p
->rmargin
= p
->maxrmargin
;
512 p
->flags
&= ~TERMP_NOBREAK
;
513 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
519 p
->rmargin
= p
->maxrmargin
;
520 p
->flags
&= ~TERMP_NOSPACE
;
528 arg_width(const struct mdoc_argv
*arg
, int pos
)
533 assert(pos
< (int)arg
->sz
&& pos
>= 0);
534 assert(arg
->value
[pos
]);
538 if (0 == (len
= (int)strlen(p
)))
541 for (i
= 0; i
< len
- 1; i
++)
542 if ( ! isdigit((u_char
)p
[i
]))
546 if ('n' == p
[len
- 1] || 'm' == p
[len
- 1])
547 return((size_t)atoi(p
) + 2);
549 return((size_t)len
+ 2);
554 arg_listtype(const struct mdoc_node
*n
)
558 assert(MDOC_BLOCK
== n
->type
);
560 len
= (int)(n
->args
? n
->args
->argc
: 0);
562 for (i
= 0; i
< len
; i
++)
563 switch (n
->args
->argv
[i
].arg
) {
585 return(n
->args
->argv
[i
].arg
);
595 arg_offset(const struct mdoc_argv
*arg
)
603 if (0 == strcmp(p
, "left"))
605 if (0 == strcmp(p
, "indent"))
607 if (0 == strcmp(p
, "indent-two"))
608 return((INDENT
+ 1) * 2);
610 if (0 == (len
= (int)strlen(p
)))
613 for (i
= 0; i
< len
- 1; i
++)
614 if ( ! isdigit((u_char
)p
[i
]))
618 if ('n' == p
[len
- 1] || 'm' == p
[len
- 1])
619 return((size_t)atoi(p
));
626 arg_hasattr(int arg
, const struct mdoc_node
*n
)
629 return(-1 != arg_getattr(arg
, n
));
634 arg_getattr(int v
, const struct mdoc_node
*n
)
638 return(arg_getattrs(&v
, &val
, 1, n
) ? val
: -1);
643 arg_getattrs(const int *keys
, int *vals
,
644 size_t sz
, const struct mdoc_node
*n
)
651 for (k
= i
= 0; i
< (int)n
->args
->argc
; i
++)
652 for (j
= 0; j
< (int)sz
; j
++)
653 if (n
->args
->argv
[i
].arg
== keys
[j
]) {
663 fmt_block_vspace(struct termp
*p
,
664 const struct mdoc_node
*bl
,
665 const struct mdoc_node
*node
)
667 const struct mdoc_node
*n
;
671 if (arg_hasattr(MDOC_Compact
, bl
))
675 * Search through our prior nodes. If we follow a `Ss' or `Sh',
679 for (n
= node
; n
; n
= n
->parent
) {
680 if (MDOC_BLOCK
!= n
->type
)
682 if (MDOC_Ss
== n
->tok
)
684 if (MDOC_Sh
== n
->tok
)
692 * XXX - not documented: a `-column' does not ever assert
693 * vertical space within the list.
696 if (arg_hasattr(MDOC_Column
, bl
))
697 if (node
->prev
&& MDOC_It
== node
->prev
->tok
)
706 termp_dq_pre(DECL_ARGS
)
709 if (MDOC_BODY
!= node
->type
)
712 term_word(p
, "\\(lq");
713 p
->flags
|= TERMP_NOSPACE
;
720 termp_dq_post(DECL_ARGS
)
723 if (MDOC_BODY
!= node
->type
)
726 p
->flags
|= TERMP_NOSPACE
;
727 term_word(p
, "\\(rq");
733 termp_it_pre(DECL_ARGS
)
735 const struct mdoc_node
*bl
, *n
;
737 int i
, type
, keys
[3], vals
[3];
738 size_t width
, offset
;
740 if (MDOC_BLOCK
== node
->type
) {
741 fmt_block_vspace(p
, node
->parent
->parent
, node
);
745 bl
= node
->parent
->parent
->parent
;
747 /* Save parent attributes. */
749 pair
->flag
= p
->flags
;
751 /* Get list width and offset. */
753 keys
[0] = MDOC_Width
;
754 keys
[1] = MDOC_Offset
;
755 keys
[2] = MDOC_Column
;
757 vals
[0] = vals
[1] = vals
[2] = -1;
761 (void)arg_getattrs(keys
, vals
, 3, bl
);
763 type
= arg_listtype(bl
);
766 /* Calculate real width and offset. */
770 if (MDOC_BODY
== node
->type
)
772 for (i
= 0, n
= node
->prev
; n
; n
= n
->prev
, i
++)
774 (&bl
->args
->argv
[vals
[2]], i
);
775 assert(i
< (int)bl
->args
->argv
[vals
[2]].sz
);
776 width
= arg_width(&bl
->args
->argv
[vals
[2]], i
);
778 offset
+= arg_offset(&bl
->args
->argv
[vals
[1]]);
782 width
= arg_width(&bl
->args
->argv
[vals
[0]], 0);
784 offset
+= arg_offset(&bl
->args
->argv
[vals
[1]]);
789 * List-type can override the width in the case of fixed-head
790 * values (bullet, dash/hyphen, enum). Tags need a non-zero
791 * offset. FIXME: double-check that correct.
822 * Whitespace control. Inset bodies need an initial space,
823 * while diagonal bodies need two.
826 p
->flags
|= TERMP_NOSPACE
;
830 term_word(p
, "\\ \\ ");
833 if (MDOC_BODY
== node
->type
)
840 p
->flags
|= TERMP_NOSPACE
;
843 * Style flags. Diagnostic heads need TTYPE_DIAG.
848 if (MDOC_HEAD
== node
->type
)
849 p
->flags
|= ttypes
[TTYPE_DIAG
];
856 * Pad and break control. This is the tricker part. Lists with
857 * set right-margins for the head get TERMP_NOBREAK because, if
858 * they overrun the margin, they wrap to the new margin.
859 * Correspondingly, the body for these types don't left-pad, as
860 * the head will pad out to to the right.
871 if (MDOC_HEAD
== node
->type
)
872 p
->flags
|= TERMP_NOBREAK
;
874 p
->flags
|= TERMP_NOLPAD
;
877 if (MDOC_HEAD
== node
->type
)
878 p
->flags
|= TERMP_NOBREAK
;
880 p
->flags
|= TERMP_NOLPAD
;
882 if (MDOC_HEAD
== node
->type
)
883 p
->flags
|= TERMP_HANG
;
886 if (MDOC_HEAD
== node
->type
)
887 p
->flags
|= TERMP_NOBREAK
| TERMP_TWOSPACE
;
889 p
->flags
|= TERMP_NOLPAD
;
891 if (MDOC_HEAD
!= node
->type
)
893 if (NULL
== node
->next
|| NULL
== node
->next
->child
)
894 p
->flags
|= TERMP_DANGLE
;
897 if (MDOC_HEAD
== node
->type
) {
899 if (MDOC_BODY
== node
->next
->type
)
900 p
->flags
&= ~TERMP_NOBREAK
;
902 p
->flags
|= TERMP_NOBREAK
;
904 p
->flags
|= TERMP_NOLPAD
;
908 if (MDOC_HEAD
== node
->type
)
909 p
->flags
|= TERMP_NOBREAK
;
916 * Margin control. Set-head-width lists have their right
917 * margins shortened. The body for these lists has the offset
918 * necessarily lengthened. Everybody gets the offset.
936 if (MDOC_HEAD
== node
->type
)
937 p
->rmargin
= p
->offset
+ width
;
943 p
->rmargin
= p
->offset
+ width
;
945 * XXX - this behaviour is not documented: the
946 * right-most column is filled to the right margin.
948 if (MDOC_HEAD
== node
->type
&&
949 MDOC_BODY
== node
->next
->type
)
950 p
->rmargin
= p
->maxrmargin
;
957 * The dash, hyphen, bullet and enum lists all have a special
958 * HEAD character (temporarily bold, in some cases).
961 if (MDOC_HEAD
== node
->type
)
964 p
->flags
|= TERMP_BOLD
;
965 term_word(p
, "\\[bu]");
966 p
->flags
&= ~TERMP_BOLD
;
971 p
->flags
|= TERMP_BOLD
;
972 term_word(p
, "\\(hy");
973 p
->flags
&= ~TERMP_BOLD
;
976 (pair
->ppair
->ppair
->count
)++;
977 (void)snprintf(buf
, sizeof(buf
), "%d.",
978 pair
->ppair
->ppair
->count
);
986 * If we're not going to process our children, indicate so here.
999 if (MDOC_HEAD
== node
->type
)
1003 if (MDOC_BODY
== node
->type
)
1016 termp_it_post(DECL_ARGS
)
1020 if (MDOC_BODY
!= node
->type
&& MDOC_HEAD
!= node
->type
)
1023 type
= arg_listtype(node
->parent
->parent
->parent
);
1032 if (MDOC_BODY
== node
->type
)
1036 if (MDOC_HEAD
== node
->type
)
1044 p
->flags
= pair
->flag
;
1050 termp_nm_pre(DECL_ARGS
)
1053 if (SEC_SYNOPSIS
== node
->sec
)
1056 pair
->flag
|= ttypes
[TTYPE_PROG
];
1057 p
->flags
|= ttypes
[TTYPE_PROG
];
1059 if (NULL
== node
->child
)
1060 term_word(p
, meta
->name
);
1068 termp_fl_pre(DECL_ARGS
)
1071 pair
->flag
|= ttypes
[TTYPE_CMD_FLAG
];
1072 p
->flags
|= ttypes
[TTYPE_CMD_FLAG
];
1073 term_word(p
, "\\-");
1074 p
->flags
|= TERMP_NOSPACE
;
1081 termp_ar_pre(DECL_ARGS
)
1084 pair
->flag
|= ttypes
[TTYPE_CMD_ARG
];
1091 termp_ns_pre(DECL_ARGS
)
1094 p
->flags
|= TERMP_NOSPACE
;
1101 termp_pp_pre(DECL_ARGS
)
1111 termp_rs_pre(DECL_ARGS
)
1114 if (MDOC_BLOCK
== node
->type
&& node
->prev
)
1122 termp_rv_pre(DECL_ARGS
)
1126 i
= arg_getattr(MDOC_Std
, node
);
1128 assert(node
->args
->argv
[i
].sz
);
1131 term_word(p
, "The");
1133 p
->flags
|= ttypes
[TTYPE_FUNC_NAME
];
1134 term_word(p
, *node
->args
->argv
[i
].value
);
1135 p
->flags
&= ~ttypes
[TTYPE_FUNC_NAME
];
1136 p
->flags
|= TERMP_NOSPACE
;
1138 term_word(p
, "() function returns the value 0 if successful;");
1139 term_word(p
, "otherwise the value -1 is returned and the");
1140 term_word(p
, "global variable");
1142 p
->flags
|= ttypes
[TTYPE_VAR_DECL
];
1143 term_word(p
, "errno");
1144 p
->flags
&= ~ttypes
[TTYPE_VAR_DECL
];
1146 term_word(p
, "is set to indicate the error.");
1154 termp_ex_pre(DECL_ARGS
)
1158 i
= arg_getattr(MDOC_Std
, node
);
1160 assert(node
->args
->argv
[i
].sz
);
1162 term_word(p
, "The");
1163 p
->flags
|= ttypes
[TTYPE_PROG
];
1164 term_word(p
, *node
->args
->argv
[i
].value
);
1165 p
->flags
&= ~ttypes
[TTYPE_PROG
];
1166 term_word(p
, "utility exits 0 on success, and >0 if an error occurs.");
1174 termp_nd_pre(DECL_ARGS
)
1177 if (MDOC_BODY
!= node
->type
)
1180 #if defined(__OpenBSD__) || defined(__linux__)
1181 term_word(p
, "\\(en");
1183 term_word(p
, "\\(em");
1191 termp_bl_post(DECL_ARGS
)
1194 if (MDOC_BLOCK
== node
->type
)
1201 termp_op_post(DECL_ARGS
)
1204 if (MDOC_BODY
!= node
->type
)
1206 p
->flags
|= TERMP_NOSPACE
;
1207 term_word(p
, "\\(rB");
1213 termp_xr_pre(DECL_ARGS
)
1215 const struct mdoc_node
*n
;
1217 assert(node
->child
&& MDOC_TEXT
== node
->child
->type
);
1220 term_word(p
, n
->string
);
1221 if (NULL
== (n
= n
->next
))
1223 p
->flags
|= TERMP_NOSPACE
;
1225 p
->flags
|= TERMP_NOSPACE
;
1226 term_word(p
, n
->string
);
1227 p
->flags
|= TERMP_NOSPACE
;
1235 termp_vt_pre(DECL_ARGS
)
1238 /* FIXME: this can be "type name". */
1239 pair
->flag
|= ttypes
[TTYPE_VAR_DECL
];
1246 termp_vt_post(DECL_ARGS
)
1249 if (node
->sec
!= SEC_SYNOPSIS
)
1251 if (node
->next
&& MDOC_Vt
== node
->next
->tok
)
1253 else if (node
->next
)
1260 termp_fd_pre(DECL_ARGS
)
1263 pair
->flag
|= ttypes
[TTYPE_FUNC_DECL
];
1270 termp_fd_post(DECL_ARGS
)
1273 if (node
->sec
!= SEC_SYNOPSIS
)
1277 if (node
->next
&& MDOC_Fd
!= node
->next
->tok
)
1284 termp_sh_pre(DECL_ARGS
)
1287 switch (node
->type
) {
1290 pair
->flag
|= ttypes
[TTYPE_SECTION
];
1304 termp_sh_post(DECL_ARGS
)
1307 switch (node
->type
) {
1323 termp_op_pre(DECL_ARGS
)
1326 switch (node
->type
) {
1328 term_word(p
, "\\(lB");
1329 p
->flags
|= TERMP_NOSPACE
;
1340 termp_bt_pre(DECL_ARGS
)
1343 term_word(p
, "is currently in beta test.");
1350 termp_lb_post(DECL_ARGS
)
1359 termp_ud_pre(DECL_ARGS
)
1362 term_word(p
, "currently under development.");
1369 termp_d1_pre(DECL_ARGS
)
1372 if (MDOC_BLOCK
!= node
->type
)
1375 p
->offset
+= (INDENT
+ 1);
1382 termp_d1_post(DECL_ARGS
)
1385 if (MDOC_BLOCK
!= node
->type
)
1393 termp_aq_pre(DECL_ARGS
)
1396 if (MDOC_BODY
!= node
->type
)
1398 term_word(p
, "\\(la");
1399 p
->flags
|= TERMP_NOSPACE
;
1406 termp_aq_post(DECL_ARGS
)
1409 if (MDOC_BODY
!= node
->type
)
1411 p
->flags
|= TERMP_NOSPACE
;
1412 term_word(p
, "\\(ra");
1418 termp_ft_pre(DECL_ARGS
)
1421 if (SEC_SYNOPSIS
== node
->sec
)
1422 if (node
->prev
&& MDOC_Fo
== node
->prev
->tok
)
1424 pair
->flag
|= ttypes
[TTYPE_FUNC_TYPE
];
1431 termp_ft_post(DECL_ARGS
)
1434 if (SEC_SYNOPSIS
== node
->sec
)
1441 termp_fn_pre(DECL_ARGS
)
1443 const struct mdoc_node
*n
;
1445 assert(node
->child
&& MDOC_TEXT
== node
->child
->type
);
1447 /* FIXME: can be "type funcname" "type varname"... */
1449 p
->flags
|= ttypes
[TTYPE_FUNC_NAME
];
1450 term_word(p
, node
->child
->string
);
1451 p
->flags
&= ~ttypes
[TTYPE_FUNC_NAME
];
1453 p
->flags
|= TERMP_NOSPACE
;
1456 for (n
= node
->child
->next
; n
; n
= n
->next
) {
1457 p
->flags
|= ttypes
[TTYPE_FUNC_ARG
];
1458 term_word(p
, n
->string
);
1459 p
->flags
&= ~ttypes
[TTYPE_FUNC_ARG
];
1466 if (SEC_SYNOPSIS
== node
->sec
)
1475 termp_fn_post(DECL_ARGS
)
1478 if (node
->sec
== SEC_SYNOPSIS
&& node
->next
)
1485 termp_sx_pre(DECL_ARGS
)
1488 pair
->flag
|= ttypes
[TTYPE_LINK
];
1495 termp_fa_pre(DECL_ARGS
)
1497 struct mdoc_node
*n
;
1499 if (node
->parent
->tok
!= MDOC_Fo
) {
1500 pair
->flag
|= ttypes
[TTYPE_FUNC_ARG
];
1504 for (n
= node
->child
; n
; n
= n
->next
) {
1505 p
->flags
|= ttypes
[TTYPE_FUNC_ARG
];
1506 term_word(p
, n
->string
);
1507 p
->flags
&= ~ttypes
[TTYPE_FUNC_ARG
];
1512 if (node
->child
&& node
->next
&& node
->next
->tok
== MDOC_Fa
)
1521 termp_va_pre(DECL_ARGS
)
1524 pair
->flag
|= ttypes
[TTYPE_VAR_DECL
];
1531 termp_bd_pre(DECL_ARGS
)
1536 * This is fairly tricky due primarily to crappy documentation.
1537 * If -ragged or -filled are specified, the block does nothing
1538 * but change the indentation.
1540 * If, on the other hand, -unfilled or -literal are specified,
1541 * then the game changes. Text is printed exactly as entered in
1542 * the display: if a macro line, a newline is appended to the
1543 * line. Blank lines are allowed.
1546 if (MDOC_BLOCK
== node
->type
) {
1547 fmt_block_vspace(p
, node
, node
);
1549 } else if (MDOC_BODY
!= node
->type
)
1552 /* FIXME: display type should be mandated by parser. */
1554 if (NULL
== node
->parent
->args
)
1555 errx(1, "missing display type");
1557 for (type
= -1, i
= 0;
1558 i
< (int)node
->parent
->args
->argc
; i
++) {
1559 switch (node
->parent
->args
->argv
[i
].arg
) {
1564 case (MDOC_Unfilled
):
1566 case (MDOC_Literal
):
1567 type
= node
->parent
->args
->argv
[i
].arg
;
1568 i
= (int)node
->parent
->args
->argc
;
1575 if (NULL
== node
->parent
->args
)
1576 errx(1, "missing display type");
1578 i
= arg_getattr(MDOC_Offset
, node
->parent
);
1580 if (1 != node
->parent
->args
->argv
[i
].sz
)
1581 errx(1, "expected single value");
1582 p
->offset
+= arg_offset(&node
->parent
->args
->argv
[i
]);
1586 case (MDOC_Literal
):
1588 case (MDOC_Unfilled
):
1595 * Tricky. Iterate through all children. If we're on a
1596 * different parse line, append a newline and then the contents.
1600 ln
= node
->child
? node
->child
->line
: 0;
1602 for (node
= node
->child
; node
; node
= node
->next
) {
1603 if (ln
< node
->line
) {
1605 p
->flags
|= TERMP_NOSPACE
;
1608 print_node(p
, pair
, meta
, node
);
1617 termp_bd_post(DECL_ARGS
)
1620 if (MDOC_BODY
!= node
->type
)
1628 termp_qq_pre(DECL_ARGS
)
1631 if (MDOC_BODY
!= node
->type
)
1634 p
->flags
|= TERMP_NOSPACE
;
1641 termp_qq_post(DECL_ARGS
)
1644 if (MDOC_BODY
!= node
->type
)
1646 p
->flags
|= TERMP_NOSPACE
;
1653 termp_bx_post(DECL_ARGS
)
1657 p
->flags
|= TERMP_NOSPACE
;
1658 term_word(p
, "BSD");
1664 termp_xx_pre(DECL_ARGS
)
1669 switch (node
->tok
) {
1674 pp
= "DragonFlyBSD";
1700 termp_sq_pre(DECL_ARGS
)
1703 if (MDOC_BODY
!= node
->type
)
1705 term_word(p
, "\\(oq");
1706 p
->flags
|= TERMP_NOSPACE
;
1713 termp_sq_post(DECL_ARGS
)
1716 if (MDOC_BODY
!= node
->type
)
1718 p
->flags
|= TERMP_NOSPACE
;
1719 term_word(p
, "\\(aq");
1725 termp_pf_pre(DECL_ARGS
)
1728 p
->flags
|= TERMP_IGNDELIM
;
1735 termp_pf_post(DECL_ARGS
)
1738 p
->flags
&= ~TERMP_IGNDELIM
;
1739 p
->flags
|= TERMP_NOSPACE
;
1745 termp_ss_pre(DECL_ARGS
)
1748 switch (node
->type
) {
1755 pair
->flag
|= ttypes
[TTYPE_SSECTION
];
1756 p
->offset
= HALFINDENT
;
1768 termp_ss_post(DECL_ARGS
)
1771 if (MDOC_HEAD
== node
->type
)
1778 termp_em_pre(DECL_ARGS
)
1781 pair
->flag
|= ttypes
[TTYPE_EMPH
];
1788 termp_cd_pre(DECL_ARGS
)
1791 pair
->flag
|= ttypes
[TTYPE_CONFIG
];
1799 termp_cm_pre(DECL_ARGS
)
1802 pair
->flag
|= ttypes
[TTYPE_CMD_FLAG
];
1809 termp_ic_pre(DECL_ARGS
)
1812 pair
->flag
|= ttypes
[TTYPE_CMD
];
1819 termp_in_pre(DECL_ARGS
)
1822 pair
->flag
|= ttypes
[TTYPE_INCLUDE
];
1823 p
->flags
|= ttypes
[TTYPE_INCLUDE
];
1825 if (SEC_SYNOPSIS
== node
->sec
)
1826 term_word(p
, "#include");
1829 p
->flags
|= TERMP_NOSPACE
;
1836 termp_in_post(DECL_ARGS
)
1839 p
->flags
|= TERMP_NOSPACE
| ttypes
[TTYPE_INCLUDE
];
1841 p
->flags
&= ~ttypes
[TTYPE_INCLUDE
];
1843 if (SEC_SYNOPSIS
!= node
->sec
)
1848 * XXX Not entirely correct. If `.In foo bar' is specified in
1849 * the SYNOPSIS section, then it produces a single break after
1850 * the <foo>; mandoc asserts a vertical space. Since this
1851 * construction is rarely used, I think it's fine.
1853 if (node
->next
&& MDOC_In
!= node
->next
->tok
)
1860 termp_sp_pre(DECL_ARGS
)
1864 if (NULL
== node
->child
) {
1869 len
= atoi(node
->child
->string
);
1872 for (i
= 0; i
< len
; i
++)
1881 termp_br_pre(DECL_ARGS
)
1891 termp_brq_pre(DECL_ARGS
)
1894 if (MDOC_BODY
!= node
->type
)
1896 term_word(p
, "\\(lC");
1897 p
->flags
|= TERMP_NOSPACE
;
1904 termp_brq_post(DECL_ARGS
)
1907 if (MDOC_BODY
!= node
->type
)
1909 p
->flags
|= TERMP_NOSPACE
;
1910 term_word(p
, "\\(rC");
1916 termp_bq_pre(DECL_ARGS
)
1919 if (MDOC_BODY
!= node
->type
)
1921 term_word(p
, "\\(lB");
1922 p
->flags
|= TERMP_NOSPACE
;
1929 termp_bq_post(DECL_ARGS
)
1932 if (MDOC_BODY
!= node
->type
)
1934 p
->flags
|= TERMP_NOSPACE
;
1935 term_word(p
, "\\(rB");
1941 termp_pq_pre(DECL_ARGS
)
1944 if (MDOC_BODY
!= node
->type
)
1946 term_word(p
, "\\&(");
1947 p
->flags
|= TERMP_NOSPACE
;
1954 termp_pq_post(DECL_ARGS
)
1957 if (MDOC_BODY
!= node
->type
)
1965 termp_fo_pre(DECL_ARGS
)
1967 const struct mdoc_node
*n
;
1969 if (MDOC_BODY
== node
->type
) {
1970 p
->flags
|= TERMP_NOSPACE
;
1972 p
->flags
|= TERMP_NOSPACE
;
1974 } else if (MDOC_HEAD
!= node
->type
)
1977 p
->flags
|= ttypes
[TTYPE_FUNC_NAME
];
1978 for (n
= node
->child
; n
; n
= n
->next
) {
1979 assert(MDOC_TEXT
== n
->type
);
1980 term_word(p
, n
->string
);
1982 p
->flags
&= ~ttypes
[TTYPE_FUNC_NAME
];
1990 termp_fo_post(DECL_ARGS
)
1993 if (MDOC_BODY
!= node
->type
)
1995 p
->flags
|= TERMP_NOSPACE
;
1997 p
->flags
|= TERMP_NOSPACE
;
2005 termp_bf_pre(DECL_ARGS
)
2007 const struct mdoc_node
*n
;
2009 if (MDOC_HEAD
== node
->type
)
2011 else if (MDOC_BLOCK
!= node
->type
)
2014 if (NULL
== (n
= node
->head
->child
)) {
2015 if (arg_hasattr(MDOC_Emphasis
, node
))
2016 pair
->flag
|= ttypes
[TTYPE_EMPH
];
2017 else if (arg_hasattr(MDOC_Symbolic
, node
))
2018 pair
->flag
|= ttypes
[TTYPE_SYMB
];
2023 assert(MDOC_TEXT
== n
->type
);
2024 if (0 == strcmp("Em", n
->string
))
2025 pair
->flag
|= ttypes
[TTYPE_EMPH
];
2026 else if (0 == strcmp("Sy", n
->string
))
2027 pair
->flag
|= ttypes
[TTYPE_SYMB
];
2035 termp_sy_pre(DECL_ARGS
)
2038 pair
->flag
|= ttypes
[TTYPE_SYMB
];
2045 termp_ms_pre(DECL_ARGS
)
2048 pair
->flag
|= ttypes
[TTYPE_SYMBOL
];
2056 termp_sm_pre(DECL_ARGS
)
2059 assert(node
->child
&& MDOC_TEXT
== node
->child
->type
);
2060 if (0 == strcmp("on", node
->child
->string
)) {
2061 p
->flags
&= ~TERMP_NONOSPACE
;
2062 p
->flags
&= ~TERMP_NOSPACE
;
2064 p
->flags
|= TERMP_NONOSPACE
;
2072 termp_ap_pre(DECL_ARGS
)
2075 p
->flags
|= TERMP_NOSPACE
;
2076 term_word(p
, "\\(aq");
2077 p
->flags
|= TERMP_NOSPACE
;
2084 termp__j_pre(DECL_ARGS
)
2087 pair
->flag
|= ttypes
[TTYPE_REF_JOURNAL
];
2094 termp__t_pre(DECL_ARGS
)
2098 p
->flags
|= TERMP_NOSPACE
;
2105 termp__t_post(DECL_ARGS
)
2108 p
->flags
|= TERMP_NOSPACE
;
2110 termp____post(p
, pair
, meta
, node
);
2116 termp____post(DECL_ARGS
)
2119 p
->flags
|= TERMP_NOSPACE
;
2120 term_word(p
, node
->next
? "," : ".");
2126 termp_lk_pre(DECL_ARGS
)
2128 const struct mdoc_node
*n
;
2130 assert(node
->child
);
2133 if (NULL
== n
->next
) {
2134 pair
->flag
|= ttypes
[TTYPE_LINK_ANCHOR
];
2138 p
->flags
|= ttypes
[TTYPE_LINK_ANCHOR
];
2139 term_word(p
, n
->string
);
2140 p
->flags
|= TERMP_NOSPACE
;
2142 p
->flags
&= ~ttypes
[TTYPE_LINK_ANCHOR
];
2144 p
->flags
|= ttypes
[TTYPE_LINK_TEXT
];
2145 for (n
= n
->next
; n
; n
= n
->next
)
2146 term_word(p
, n
->string
);
2148 p
->flags
&= ~ttypes
[TTYPE_LINK_TEXT
];
2155 termp_mt_pre(DECL_ARGS
)
2158 pair
->flag
|= ttypes
[TTYPE_LINK_ANCHOR
];