]>
git.cameronkatri.com Git - mandoc.git/blob - mdoc_term.c
1 /* $Id: mdoc_term.c,v 1.35 2009/07/12 20:24:24 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. */
30 /* FIXME: support more offset/width tokens. */
33 #define TTYPE_CMD_FLAG 1
34 #define TTYPE_CMD_ARG 2
35 #define TTYPE_SECTION 3
36 #define TTYPE_FUNC_DECL 4
37 #define TTYPE_VAR_DECL 5
38 #define TTYPE_FUNC_TYPE 6
39 #define TTYPE_FUNC_NAME 7
40 #define TTYPE_FUNC_ARG 8
42 #define TTYPE_SSECTION 10
45 #define TTYPE_CONFIG 13
47 #define TTYPE_INCLUDE 15
49 #define TTYPE_SYMBOL 17
51 #define TTYPE_LINK_ANCHOR 19
52 #define TTYPE_LINK_TEXT 20
53 #define TTYPE_REF_JOURNAL 21
57 const int ttypes
[TTYPE_NMAX
] = {
58 TERMP_BOLD
, /* TTYPE_PROG */
59 TERMP_BOLD
, /* TTYPE_CMD_FLAG */
60 TERMP_UNDER
, /* TTYPE_CMD_ARG */
61 TERMP_BOLD
, /* TTYPE_SECTION */
62 TERMP_BOLD
, /* TTYPE_FUNC_DECL */
63 TERMP_UNDER
, /* TTYPE_VAR_DECL */
64 TERMP_UNDER
, /* TTYPE_FUNC_TYPE */
65 TERMP_BOLD
, /* TTYPE_FUNC_NAME */
66 TERMP_UNDER
, /* TTYPE_FUNC_ARG */
67 TERMP_UNDER
, /* TTYPE_LINK */
68 TERMP_BOLD
, /* TTYPE_SSECTION */
69 TERMP_UNDER
, /* TTYPE_FILE */
70 TERMP_UNDER
, /* TTYPE_EMPH */
71 TERMP_BOLD
, /* TTYPE_CONFIG */
72 TERMP_BOLD
, /* TTYPE_CMD */
73 TERMP_BOLD
, /* TTYPE_INCLUDE */
74 TERMP_BOLD
, /* TTYPE_SYMB */
75 TERMP_BOLD
, /* TTYPE_SYMBOL */
76 TERMP_BOLD
, /* TTYPE_DIAG */
77 TERMP_UNDER
, /* TTYPE_LINK_ANCHOR */
78 TERMP_BOLD
, /* TTYPE_LINK_TEXT */
79 TERMP_UNDER
, /* TTYPE_REF_JOURNAL */
80 TERMP_BOLD
/* TTYPE_LIST */
84 struct termpair
*ppair
;
89 #define DECL_ARGS struct termp *p, \
90 struct termpair *pair, \
91 const struct mdoc_meta *meta, \
92 const struct mdoc_node *node
95 int (*pre
)(DECL_ARGS
);
96 void (*post
)(DECL_ARGS
);
99 static void termp____post(DECL_ARGS
);
100 static void termp__t_post(DECL_ARGS
);
101 static void termp_aq_post(DECL_ARGS
);
102 static void termp_bd_post(DECL_ARGS
);
103 static void termp_bl_post(DECL_ARGS
);
104 static void termp_bq_post(DECL_ARGS
);
105 static void termp_brq_post(DECL_ARGS
);
106 static void termp_bx_post(DECL_ARGS
);
107 static void termp_d1_post(DECL_ARGS
);
108 static void termp_dq_post(DECL_ARGS
);
109 static void termp_fd_post(DECL_ARGS
);
110 static void termp_fn_post(DECL_ARGS
);
111 static void termp_fo_post(DECL_ARGS
);
112 static void termp_ft_post(DECL_ARGS
);
113 static void termp_in_post(DECL_ARGS
);
114 static void termp_it_post(DECL_ARGS
);
115 static void termp_lb_post(DECL_ARGS
);
116 static void termp_op_post(DECL_ARGS
);
117 static void termp_pf_post(DECL_ARGS
);
118 static void termp_pq_post(DECL_ARGS
);
119 static void termp_qq_post(DECL_ARGS
);
120 static void termp_sh_post(DECL_ARGS
);
121 static void termp_sq_post(DECL_ARGS
);
122 static void termp_ss_post(DECL_ARGS
);
123 static void termp_vt_post(DECL_ARGS
);
125 static int termp__j_pre(DECL_ARGS
);
126 static int termp__t_pre(DECL_ARGS
);
127 static int termp_ap_pre(DECL_ARGS
);
128 static int termp_aq_pre(DECL_ARGS
);
129 static int termp_ar_pre(DECL_ARGS
);
130 static int termp_bd_pre(DECL_ARGS
);
131 static int termp_bf_pre(DECL_ARGS
);
132 static int termp_bq_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_lb_pre(DECL_ARGS
);
151 static int termp_lk_pre(DECL_ARGS
);
152 static int termp_ms_pre(DECL_ARGS
);
153 static int termp_mt_pre(DECL_ARGS
);
154 static int termp_nd_pre(DECL_ARGS
);
155 static int termp_nm_pre(DECL_ARGS
);
156 static int termp_ns_pre(DECL_ARGS
);
157 static int termp_op_pre(DECL_ARGS
);
158 static int termp_pa_pre(DECL_ARGS
);
159 static int termp_pf_pre(DECL_ARGS
);
160 static int termp_pp_pre(DECL_ARGS
);
161 static int termp_pq_pre(DECL_ARGS
);
162 static int termp_qq_pre(DECL_ARGS
);
163 static int termp_rs_pre(DECL_ARGS
);
164 static int termp_rv_pre(DECL_ARGS
);
165 static int termp_sh_pre(DECL_ARGS
);
166 static int termp_sm_pre(DECL_ARGS
);
167 static int termp_sq_pre(DECL_ARGS
);
168 static int termp_ss_pre(DECL_ARGS
);
169 static int termp_st_pre(DECL_ARGS
);
170 static int termp_sx_pre(DECL_ARGS
);
171 static int termp_sy_pre(DECL_ARGS
);
172 static int termp_ud_pre(DECL_ARGS
);
173 static int termp_va_pre(DECL_ARGS
);
174 static int termp_vt_pre(DECL_ARGS
);
175 static int termp_xr_pre(DECL_ARGS
);
176 static int termp_xx_pre(DECL_ARGS
);
178 static const struct termact termacts
[MDOC_MAX
] = {
179 { termp_ap_pre
, NULL
}, /* Ap */
180 { NULL
, NULL
}, /* Dd */
181 { NULL
, NULL
}, /* Dt */
182 { NULL
, NULL
}, /* Os */
183 { termp_sh_pre
, termp_sh_post
}, /* Sh */
184 { termp_ss_pre
, termp_ss_post
}, /* Ss */
185 { termp_pp_pre
, NULL
}, /* Pp */
186 { termp_d1_pre
, termp_d1_post
}, /* D1 */
187 { termp_d1_pre
, termp_d1_post
}, /* Dl */
188 { termp_bd_pre
, termp_bd_post
}, /* Bd */
189 { NULL
, NULL
}, /* Ed */
190 { NULL
, termp_bl_post
}, /* Bl */
191 { NULL
, NULL
}, /* El */
192 { termp_it_pre
, termp_it_post
}, /* It */
193 { NULL
, NULL
}, /* Ad */
194 { NULL
, NULL
}, /* An */
195 { termp_ar_pre
, NULL
}, /* Ar */
196 { termp_cd_pre
, NULL
}, /* Cd */
197 { termp_cm_pre
, NULL
}, /* Cm */
198 { NULL
, NULL
}, /* Dv */
199 { NULL
, NULL
}, /* Er */
200 { NULL
, NULL
}, /* Ev */
201 { termp_ex_pre
, NULL
}, /* Ex */
202 { termp_fa_pre
, NULL
}, /* Fa */
203 { termp_fd_pre
, termp_fd_post
}, /* Fd */
204 { termp_fl_pre
, NULL
}, /* Fl */
205 { termp_fn_pre
, termp_fn_post
}, /* Fn */
206 { termp_ft_pre
, termp_ft_post
}, /* Ft */
207 { termp_ic_pre
, NULL
}, /* Ic */
208 { termp_in_pre
, termp_in_post
}, /* In */
209 { NULL
, NULL
}, /* Li */
210 { termp_nd_pre
, NULL
}, /* Nd */
211 { termp_nm_pre
, NULL
}, /* Nm */
212 { termp_op_pre
, termp_op_post
}, /* Op */
213 { NULL
, NULL
}, /* Ot */
214 { termp_pa_pre
, NULL
}, /* Pa */
215 { termp_rv_pre
, NULL
}, /* Rv */
216 { termp_st_pre
, NULL
}, /* St */
217 { termp_va_pre
, NULL
}, /* Va */
218 { termp_vt_pre
, termp_vt_post
}, /* Vt */
219 { termp_xr_pre
, NULL
}, /* Xr */
220 { NULL
, termp____post
}, /* %A */
221 { NULL
, termp____post
}, /* %B */
222 { NULL
, termp____post
}, /* %D */
223 { NULL
, termp____post
}, /* %I */
224 { termp__j_pre
, termp____post
}, /* %J */
225 { NULL
, termp____post
}, /* %N */
226 { NULL
, termp____post
}, /* %O */
227 { NULL
, termp____post
}, /* %P */
228 { NULL
, termp____post
}, /* %R */
229 { termp__t_pre
, termp__t_post
}, /* %T */
230 { NULL
, termp____post
}, /* %V */
231 { NULL
, NULL
}, /* Ac */
232 { termp_aq_pre
, termp_aq_post
}, /* Ao */
233 { termp_aq_pre
, termp_aq_post
}, /* Aq */
234 { NULL
, NULL
}, /* At */
235 { NULL
, NULL
}, /* Bc */
236 { termp_bf_pre
, NULL
}, /* Bf */
237 { termp_bq_pre
, termp_bq_post
}, /* Bo */
238 { termp_bq_pre
, termp_bq_post
}, /* Bq */
239 { termp_xx_pre
, NULL
}, /* Bsx */
240 { NULL
, termp_bx_post
}, /* Bx */
241 { NULL
, NULL
}, /* Db */
242 { NULL
, NULL
}, /* Dc */
243 { termp_dq_pre
, termp_dq_post
}, /* Do */
244 { termp_dq_pre
, termp_dq_post
}, /* Dq */
245 { NULL
, NULL
}, /* Ec */
246 { NULL
, NULL
}, /* Ef */
247 { termp_em_pre
, NULL
}, /* Em */
248 { NULL
, NULL
}, /* Eo */
249 { termp_xx_pre
, NULL
}, /* Fx */
250 { termp_ms_pre
, NULL
}, /* Ms */
251 { NULL
, NULL
}, /* No */
252 { termp_ns_pre
, NULL
}, /* Ns */
253 { termp_xx_pre
, NULL
}, /* Nx */
254 { termp_xx_pre
, NULL
}, /* Ox */
255 { NULL
, NULL
}, /* Pc */
256 { termp_pf_pre
, termp_pf_post
}, /* Pf */
257 { termp_pq_pre
, termp_pq_post
}, /* Po */
258 { termp_pq_pre
, termp_pq_post
}, /* Pq */
259 { NULL
, NULL
}, /* Qc */
260 { termp_sq_pre
, termp_sq_post
}, /* Ql */
261 { termp_qq_pre
, termp_qq_post
}, /* Qo */
262 { termp_qq_pre
, termp_qq_post
}, /* Qq */
263 { NULL
, NULL
}, /* Re */
264 { termp_rs_pre
, NULL
}, /* Rs */
265 { NULL
, NULL
}, /* Sc */
266 { termp_sq_pre
, termp_sq_post
}, /* So */
267 { termp_sq_pre
, termp_sq_post
}, /* Sq */
268 { termp_sm_pre
, NULL
}, /* Sm */
269 { termp_sx_pre
, NULL
}, /* Sx */
270 { termp_sy_pre
, NULL
}, /* Sy */
271 { NULL
, NULL
}, /* Tn */
272 { termp_xx_pre
, NULL
}, /* Ux */
273 { NULL
, NULL
}, /* Xc */
274 { NULL
, NULL
}, /* Xo */
275 { termp_fo_pre
, termp_fo_post
}, /* Fo */
276 { NULL
, NULL
}, /* Fc */
277 { termp_op_pre
, termp_op_post
}, /* Oo */
278 { NULL
, NULL
}, /* Oc */
279 { NULL
, NULL
}, /* Bk */
280 { NULL
, NULL
}, /* Ek */
281 { termp_bt_pre
, NULL
}, /* Bt */
282 { NULL
, NULL
}, /* Hf */
283 { NULL
, NULL
}, /* Fr */
284 { termp_ud_pre
, NULL
}, /* Ud */
285 { termp_lb_pre
, termp_lb_post
}, /* Lb */
286 { termp_pp_pre
, NULL
}, /* Lp */
287 { termp_lk_pre
, NULL
}, /* Lk */
288 { termp_mt_pre
, NULL
}, /* Mt */
289 { termp_brq_pre
, termp_brq_post
}, /* Brq */
290 { termp_brq_pre
, termp_brq_post
}, /* Bro */
291 { NULL
, NULL
}, /* Brc */
292 { NULL
, NULL
}, /* %C */
293 { NULL
, NULL
}, /* Es */
294 { NULL
, NULL
}, /* En */
295 { termp_xx_pre
, NULL
}, /* Dx */
296 { NULL
, NULL
}, /* %Q */
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 int 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
;
365 if (MDOC_TEXT
!= node
->type
) {
366 if (termacts
[node
->tok
].pre
)
367 if ( ! (*termacts
[node
->tok
].pre
)(p
, &npair
, meta
, node
))
369 } else /* MDOC_TEXT == node->type */
370 term_word(p
, node
->string
);
374 p
->flags
|= npair
.flag
;
376 if (dochild
&& node
->child
)
377 print_body(p
, &npair
, meta
, node
->child
);
379 /* Post-processing. */
381 if (MDOC_TEXT
!= node
->type
)
382 if (termacts
[node
->tok
].post
)
383 (*termacts
[node
->tok
].post
)(p
, &npair
, meta
, node
);
386 p
->rmargin
= rmargin
;
387 p
->flags
&= ~npair
.flag
;
392 print_foot(struct termp
*p
, const struct mdoc_meta
*meta
)
398 * Output the footer in new-groff style, that is, three columns
399 * with the middle being the manual date and flanking columns
400 * being the operating system:
405 if (NULL
== (buf
= malloc(p
->rmargin
)))
407 if (NULL
== (os
= malloc(p
->rmargin
)))
410 tm
= localtime(&meta
->date
);
412 if (0 == strftime(buf
, p
->rmargin
, "%B %d, %Y", tm
))
415 (void)strlcpy(os
, meta
->os
, p
->rmargin
);
420 p
->rmargin
= (p
->maxrmargin
- strlen(buf
) + 1) / 2;
421 p
->flags
|= TERMP_NOSPACE
| TERMP_NOBREAK
;
426 p
->offset
= p
->rmargin
;
427 p
->rmargin
= p
->maxrmargin
- strlen(os
);
428 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
433 p
->offset
= p
->rmargin
;
434 p
->rmargin
= p
->maxrmargin
;
435 p
->flags
&= ~TERMP_NOBREAK
;
436 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
442 p
->rmargin
= p
->maxrmargin
;
451 print_head(struct termp
*p
, const struct mdoc_meta
*meta
)
455 p
->rmargin
= p
->maxrmargin
;
458 if (NULL
== (buf
= malloc(p
->rmargin
)))
460 if (NULL
== (title
= malloc(p
->rmargin
)))
464 * The header is strange. It has three components, which are
465 * really two with the first duplicated. It goes like this:
467 * IDENTIFIER TITLE IDENTIFIER
469 * The IDENTIFIER is NAME(SECTION), which is the command-name
470 * (if given, or "unknown" if not) followed by the manual page
471 * section. These are given in `Dt'. The TITLE is a free-form
472 * string depending on the manual volume. If not specified, it
473 * switches on the manual section.
477 (void)strlcpy(buf
, meta
->vol
, p
->rmargin
);
480 (void)strlcat(buf
, " (", p
->rmargin
);
481 (void)strlcat(buf
, meta
->arch
, p
->rmargin
);
482 (void)strlcat(buf
, ")", p
->rmargin
);
485 (void)snprintf(title
, p
->rmargin
, "%s(%d)",
486 meta
->title
, meta
->msec
);
489 p
->rmargin
= (p
->maxrmargin
- strlen(buf
) + 1) / 2;
490 p
->flags
|= TERMP_NOBREAK
| TERMP_NOSPACE
;
495 p
->offset
= p
->rmargin
;
496 p
->rmargin
= p
->maxrmargin
- strlen(title
);
497 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
502 p
->offset
= p
->rmargin
;
503 p
->rmargin
= p
->maxrmargin
;
504 p
->flags
&= ~TERMP_NOBREAK
;
505 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
511 p
->rmargin
= p
->maxrmargin
;
512 p
->flags
&= ~TERMP_NOSPACE
;
520 arg_width(const struct mdoc_argv
*arg
, int pos
)
525 assert(pos
< (int)arg
->sz
&& pos
>= 0);
526 assert(arg
->value
[pos
]);
528 if (0 == (len
= (int)strlen(arg
->value
[pos
])))
531 for (i
= 0; i
< len
- 1; i
++)
532 if ( ! isdigit((u_char
)arg
->value
[pos
][i
]))
536 if ('n' == arg
->value
[pos
][len
- 1] ||
537 'm' == arg
->value
[pos
][len
- 1]) {
538 v
= (size_t)atoi(arg
->value
[pos
]);
543 return(strlen(arg
->value
[pos
]) + 2);
548 arg_listtype(const struct mdoc_node
*n
)
552 assert(MDOC_BLOCK
== n
->type
);
554 len
= (int)(n
->args
? n
->args
->argc
: 0);
556 for (i
= 0; i
< len
; i
++)
557 switch (n
->args
->argv
[i
].arg
) {
577 return(n
->args
->argv
[i
].arg
);
582 /* FIXME: mandated by parser. */
584 errx(1, "list type not supported");
590 arg_offset(const struct mdoc_argv
*arg
)
594 if (0 == strcmp(*arg
->value
, "left"))
596 if (0 == strcmp(*arg
->value
, "indent"))
598 if (0 == strcmp(*arg
->value
, "indent-two"))
599 return((INDENT
+ 1) * 2);
601 /* FIXME: needs to support field-widths (10n, etc.). */
603 return(strlen(*arg
->value
));
608 arg_hasattr(int arg
, const struct mdoc_node
*n
)
611 return(-1 != arg_getattr(arg
, n
));
616 arg_getattr(int v
, const struct mdoc_node
*n
)
620 return(arg_getattrs(&v
, &val
, 1, n
) ? val
: -1);
625 arg_getattrs(const int *keys
, int *vals
,
626 size_t sz
, const struct mdoc_node
*n
)
633 for (k
= i
= 0; i
< (int)n
->args
->argc
; i
++)
634 for (j
= 0; j
< (int)sz
; j
++)
635 if (n
->args
->argv
[i
].arg
== keys
[j
]) {
645 fmt_block_vspace(struct termp
*p
,
646 const struct mdoc_node
*bl
,
647 const struct mdoc_node
*node
)
649 const struct mdoc_node
*n
;
653 if (arg_hasattr(MDOC_Compact
, bl
))
656 for (n
= node
; n
; n
= n
->parent
) {
657 if (MDOC_BLOCK
!= n
->type
)
659 if (MDOC_Ss
== n
->tok
)
661 if (MDOC_Sh
== n
->tok
)
675 termp_dq_pre(DECL_ARGS
)
678 if (MDOC_BODY
!= node
->type
)
681 term_word(p
, "\\(lq");
682 p
->flags
|= TERMP_NOSPACE
;
689 termp_dq_post(DECL_ARGS
)
692 if (MDOC_BODY
!= node
->type
)
695 p
->flags
|= TERMP_NOSPACE
;
696 term_word(p
, "\\(rq");
702 termp_it_pre(DECL_ARGS
)
704 const struct mdoc_node
*bl
, *n
;
706 int i
, type
, keys
[3], vals
[3], sv
;
707 size_t width
, offset
;
709 if (MDOC_BLOCK
== node
->type
)
710 return(fmt_block_vspace(p
, node
->parent
->parent
, node
));
712 bl
= node
->parent
->parent
->parent
;
714 /* Save parent attributes. */
716 pair
->flag
= p
->flags
;
718 /* Get list width and offset. */
720 keys
[0] = MDOC_Width
;
721 keys
[1] = MDOC_Offset
;
722 keys
[2] = MDOC_Column
;
724 vals
[0] = vals
[1] = vals
[2] = -1;
728 (void)arg_getattrs(keys
, vals
, 3, bl
);
730 type
= arg_listtype(bl
);
732 /* Calculate real width and offset. */
736 if (MDOC_BODY
== node
->type
)
738 for (i
= 0, n
= node
->prev
; n
; n
= n
->prev
, i
++)
740 (&bl
->args
->argv
[vals
[2]], i
);
741 assert(i
< (int)bl
->args
->argv
[vals
[2]].sz
);
742 width
= arg_width(&bl
->args
->argv
[vals
[2]], i
);
744 offset
+= arg_offset(&bl
->args
->argv
[vals
[1]]);
748 width
= arg_width(&bl
->args
->argv
[vals
[0]], 0);
750 offset
+= arg_offset(&bl
->args
->argv
[vals
[1]]);
755 * List-type can override the width in the case of fixed-head
756 * values (bullet, dash/hyphen, enum). Tags need a non-zero
782 * Whitespace control. Inset bodies need an initial space,
783 * while diagonal bodies need two.
791 if (MDOC_BODY
== node
->type
)
792 p
->flags
&= ~TERMP_NOSPACE
;
794 p
->flags
|= TERMP_NOSPACE
;
797 p
->flags
|= TERMP_NOSPACE
;
802 * Style flags. Diagnostic heads need TTYPE_DIAG.
807 if (MDOC_HEAD
== node
->type
)
808 p
->flags
|= ttypes
[TTYPE_DIAG
];
815 * Pad and break control. This is the tricker part. Lists with
816 * set right-margins for the head get TERMP_NOBREAK because, if
817 * they overrun the margin, they wrap to the new margin.
818 * Correspondingly, the body for these types don't left-pad, as
819 * the head will pad out to to the right.
832 if (MDOC_HEAD
== node
->type
)
833 p
->flags
|= TERMP_NOBREAK
;
835 p
->flags
|= TERMP_NOLPAD
;
836 if (MDOC_HEAD
== node
->type
&& MDOC_Tag
== type
)
837 if (NULL
== node
->next
||
838 NULL
== node
->next
->child
)
839 p
->flags
|= TERMP_NONOBREAK
;
842 if (MDOC_HEAD
== node
->type
) {
844 if (MDOC_BODY
== node
->next
->type
)
845 p
->flags
&= ~TERMP_NOBREAK
;
847 p
->flags
|= TERMP_NOBREAK
;
849 p
->flags
|= TERMP_NOLPAD
;
853 if (MDOC_HEAD
== node
->type
)
854 p
->flags
|= TERMP_NOBREAK
;
861 * Margin control. Set-head-width lists have their right
862 * margins shortened. The body for these lists has the offset
863 * necessarily lengthened. Everybody gets the offset.
878 if (MDOC_HEAD
== node
->type
)
879 p
->rmargin
= p
->offset
+ width
;
884 p
->rmargin
= p
->offset
+ width
;
891 * The dash, hyphen, bullet and enum lists all have a special
892 * HEAD character (temporarily bold, in some cases).
896 if (MDOC_HEAD
== node
->type
)
899 p
->flags
|= TERMP_BOLD
;
900 term_word(p
, "\\[bu]");
905 p
->flags
|= TERMP_BOLD
;
906 term_word(p
, "\\(hy");
909 (pair
->ppair
->ppair
->count
)++;
910 (void)snprintf(buf
, sizeof(buf
), "%d.",
911 pair
->ppair
->ppair
->count
);
918 p
->flags
= sv
; /* Restore saved flags. */
921 * If we're not going to process our children, indicate so here.
934 if (MDOC_HEAD
== node
->type
)
938 if (MDOC_BODY
== node
->type
)
951 termp_it_post(DECL_ARGS
)
955 if (MDOC_BODY
!= node
->type
&& MDOC_HEAD
!= node
->type
)
958 type
= arg_listtype(node
->parent
->parent
->parent
);
966 if (MDOC_BODY
== node
->type
)
970 if (MDOC_HEAD
== node
->type
)
978 p
->flags
= pair
->flag
;
984 termp_nm_pre(DECL_ARGS
)
987 if (SEC_SYNOPSIS
== node
->sec
)
990 pair
->flag
|= ttypes
[TTYPE_PROG
];
991 p
->flags
|= ttypes
[TTYPE_PROG
];
993 if (NULL
== node
->child
)
994 term_word(p
, meta
->name
);
1002 termp_fl_pre(DECL_ARGS
)
1005 pair
->flag
|= ttypes
[TTYPE_CMD_FLAG
];
1006 p
->flags
|= ttypes
[TTYPE_CMD_FLAG
];
1007 term_word(p
, "\\-");
1008 p
->flags
|= TERMP_NOSPACE
;
1015 termp_ar_pre(DECL_ARGS
)
1018 pair
->flag
|= ttypes
[TTYPE_CMD_ARG
];
1025 termp_ns_pre(DECL_ARGS
)
1028 p
->flags
|= TERMP_NOSPACE
;
1035 termp_pp_pre(DECL_ARGS
)
1045 termp_st_pre(DECL_ARGS
)
1049 if (node
->child
&& (cp
= mdoc_a2st(node
->child
->string
)))
1057 termp_rs_pre(DECL_ARGS
)
1060 if (MDOC_BLOCK
== node
->type
&& node
->prev
)
1068 termp_rv_pre(DECL_ARGS
)
1072 i
= arg_getattr(MDOC_Std
, node
);
1074 assert(node
->args
->argv
[i
].sz
);
1077 term_word(p
, "The");
1079 p
->flags
|= ttypes
[TTYPE_FUNC_NAME
];
1080 term_word(p
, *node
->args
->argv
[i
].value
);
1081 p
->flags
&= ~ttypes
[TTYPE_FUNC_NAME
];
1082 p
->flags
|= TERMP_NOSPACE
;
1084 term_word(p
, "() function returns the value 0 if successful;");
1085 term_word(p
, "otherwise the value -1 is returned and the");
1086 term_word(p
, "global variable");
1088 p
->flags
|= ttypes
[TTYPE_VAR_DECL
];
1089 term_word(p
, "errno");
1090 p
->flags
&= ~ttypes
[TTYPE_VAR_DECL
];
1092 term_word(p
, "is set to indicate the error.");
1100 termp_ex_pre(DECL_ARGS
)
1104 i
= arg_getattr(MDOC_Std
, node
);
1106 assert(node
->args
->argv
[i
].sz
);
1108 term_word(p
, "The");
1109 p
->flags
|= ttypes
[TTYPE_PROG
];
1110 term_word(p
, *node
->args
->argv
[i
].value
);
1111 p
->flags
&= ~ttypes
[TTYPE_PROG
];
1112 term_word(p
, "utility exits 0 on success, and >0 if an error occurs.");
1120 termp_nd_pre(DECL_ARGS
)
1123 if (MDOC_BODY
!= node
->type
)
1127 * XXX: signed off by jmc@openbsd.org. This technically
1128 * produces a minus sign after the Nd, which is wrong, but is
1129 * consistent with the historic OpenBSD tmac file.
1131 #if defined(__OpenBSD__) || defined(__linux__)
1132 term_word(p
, "\\-");
1134 term_word(p
, "\\(em");
1142 termp_bl_post(DECL_ARGS
)
1145 if (MDOC_BLOCK
== node
->type
)
1152 termp_op_post(DECL_ARGS
)
1155 if (MDOC_BODY
!= node
->type
)
1157 p
->flags
|= TERMP_NOSPACE
;
1158 term_word(p
, "\\(rB");
1164 termp_xr_pre(DECL_ARGS
)
1166 const struct mdoc_node
*n
;
1168 assert(node
->child
&& MDOC_TEXT
== node
->child
->type
);
1171 term_word(p
, n
->string
);
1172 if (NULL
== (n
= n
->next
))
1174 p
->flags
|= TERMP_NOSPACE
;
1176 p
->flags
|= TERMP_NOSPACE
;
1177 term_word(p
, n
->string
);
1178 p
->flags
|= TERMP_NOSPACE
;
1186 termp_vt_pre(DECL_ARGS
)
1189 /* FIXME: this can be "type name". */
1190 pair
->flag
|= ttypes
[TTYPE_VAR_DECL
];
1197 termp_vt_post(DECL_ARGS
)
1200 if (node
->sec
!= SEC_SYNOPSIS
)
1202 if (node
->next
&& MDOC_Vt
== node
->next
->tok
)
1204 else if (node
->next
)
1211 termp_fd_pre(DECL_ARGS
)
1214 pair
->flag
|= ttypes
[TTYPE_FUNC_DECL
];
1221 termp_fd_post(DECL_ARGS
)
1224 if (node
->sec
!= SEC_SYNOPSIS
)
1228 if (node
->next
&& MDOC_Fd
!= node
->next
->tok
)
1235 termp_sh_pre(DECL_ARGS
)
1238 switch (node
->type
) {
1241 pair
->flag
|= ttypes
[TTYPE_SECTION
];
1255 termp_sh_post(DECL_ARGS
)
1258 switch (node
->type
) {
1274 termp_op_pre(DECL_ARGS
)
1277 switch (node
->type
) {
1279 term_word(p
, "\\(lB");
1280 p
->flags
|= TERMP_NOSPACE
;
1291 termp_bt_pre(DECL_ARGS
)
1294 term_word(p
, "is currently in beta test.");
1301 termp_lb_pre(DECL_ARGS
)
1305 assert(node
->child
&& MDOC_TEXT
== node
->child
->type
);
1306 lb
= mdoc_a2lib(node
->child
->string
);
1311 term_word(p
, "library");
1318 termp_lb_post(DECL_ARGS
)
1327 termp_ud_pre(DECL_ARGS
)
1330 term_word(p
, "currently under development.");
1337 termp_d1_pre(DECL_ARGS
)
1340 if (MDOC_BLOCK
!= node
->type
)
1343 p
->offset
+= (INDENT
+ 1);
1350 termp_d1_post(DECL_ARGS
)
1353 if (MDOC_BLOCK
!= node
->type
)
1361 termp_aq_pre(DECL_ARGS
)
1364 if (MDOC_BODY
!= node
->type
)
1366 term_word(p
, "\\(la");
1367 p
->flags
|= TERMP_NOSPACE
;
1374 termp_aq_post(DECL_ARGS
)
1377 if (MDOC_BODY
!= node
->type
)
1379 p
->flags
|= TERMP_NOSPACE
;
1380 term_word(p
, "\\(ra");
1386 termp_ft_pre(DECL_ARGS
)
1389 if (SEC_SYNOPSIS
== node
->sec
)
1390 if (node
->prev
&& MDOC_Fo
== node
->prev
->tok
)
1392 pair
->flag
|= ttypes
[TTYPE_FUNC_TYPE
];
1399 termp_ft_post(DECL_ARGS
)
1402 if (SEC_SYNOPSIS
== node
->sec
)
1409 termp_fn_pre(DECL_ARGS
)
1411 const struct mdoc_node
*n
;
1413 assert(node
->child
&& MDOC_TEXT
== node
->child
->type
);
1415 /* FIXME: can be "type funcname" "type varname"... */
1417 p
->flags
|= ttypes
[TTYPE_FUNC_NAME
];
1418 term_word(p
, node
->child
->string
);
1419 p
->flags
&= ~ttypes
[TTYPE_FUNC_NAME
];
1421 p
->flags
|= TERMP_NOSPACE
;
1424 for (n
= node
->child
->next
; n
; n
= n
->next
) {
1425 p
->flags
|= ttypes
[TTYPE_FUNC_ARG
];
1426 term_word(p
, n
->string
);
1427 p
->flags
&= ~ttypes
[TTYPE_FUNC_ARG
];
1434 if (SEC_SYNOPSIS
== node
->sec
)
1443 termp_fn_post(DECL_ARGS
)
1446 if (node
->sec
== SEC_SYNOPSIS
&& node
->next
)
1453 termp_sx_pre(DECL_ARGS
)
1456 pair
->flag
|= ttypes
[TTYPE_LINK
];
1463 termp_fa_pre(DECL_ARGS
)
1465 struct mdoc_node
*n
;
1467 if (node
->parent
->tok
!= MDOC_Fo
) {
1468 pair
->flag
|= ttypes
[TTYPE_FUNC_ARG
];
1472 for (n
= node
->child
; n
; n
= n
->next
) {
1473 p
->flags
|= ttypes
[TTYPE_FUNC_ARG
];
1474 term_word(p
, n
->string
);
1475 p
->flags
&= ~ttypes
[TTYPE_FUNC_ARG
];
1480 if (node
->child
&& node
->next
&& node
->next
->tok
== MDOC_Fa
)
1489 termp_va_pre(DECL_ARGS
)
1492 pair
->flag
|= ttypes
[TTYPE_VAR_DECL
];
1499 termp_bd_pre(DECL_ARGS
)
1504 * This is fairly tricky due primarily to crappy documentation.
1505 * If -ragged or -filled are specified, the block does nothing
1506 * but change the indentation.
1508 * If, on the other hand, -unfilled or -literal are specified,
1509 * then the game changes. Text is printed exactly as entered in
1510 * the display: if a macro line, a newline is appended to the
1511 * line. Blank lines are allowed.
1514 if (MDOC_BLOCK
== node
->type
)
1515 return(fmt_block_vspace(p
, node
, node
));
1516 else if (MDOC_BODY
!= node
->type
)
1519 /* FIXME: display type should be mandated by parser. */
1521 if (NULL
== node
->parent
->args
)
1522 errx(1, "missing display type");
1524 for (type
= -1, i
= 0;
1525 i
< (int)node
->parent
->args
->argc
; i
++) {
1526 switch (node
->parent
->args
->argv
[i
].arg
) {
1531 case (MDOC_Unfilled
):
1533 case (MDOC_Literal
):
1534 type
= node
->parent
->args
->argv
[i
].arg
;
1535 i
= (int)node
->parent
->args
->argc
;
1542 if (NULL
== node
->parent
->args
)
1543 errx(1, "missing display type");
1545 i
= arg_getattr(MDOC_Offset
, node
->parent
);
1547 if (1 != node
->parent
->args
->argv
[i
].sz
)
1548 errx(1, "expected single value");
1549 p
->offset
+= arg_offset(&node
->parent
->args
->argv
[i
]);
1553 case (MDOC_Literal
):
1555 case (MDOC_Unfilled
):
1562 * Tricky. Iterate through all children. If we're on a
1563 * different parse line, append a newline and then the contents.
1567 p
->flags
|= TERMP_LITERAL
;
1568 ln
= node
->child
? node
->child
->line
: 0;
1570 for (node
= node
->child
; node
; node
= node
->next
) {
1571 if (ln
< node
->line
) {
1573 p
->flags
|= TERMP_NOSPACE
;
1576 print_node(p
, pair
, meta
, node
);
1585 termp_bd_post(DECL_ARGS
)
1588 if (MDOC_BODY
!= node
->type
)
1592 p
->flags
&= ~TERMP_LITERAL
;
1593 p
->flags
|= TERMP_NOSPACE
;
1599 termp_qq_pre(DECL_ARGS
)
1602 if (MDOC_BODY
!= node
->type
)
1605 p
->flags
|= TERMP_NOSPACE
;
1612 termp_qq_post(DECL_ARGS
)
1615 if (MDOC_BODY
!= node
->type
)
1617 p
->flags
|= TERMP_NOSPACE
;
1624 termp_bx_post(DECL_ARGS
)
1628 p
->flags
|= TERMP_NOSPACE
;
1629 term_word(p
, "BSD");
1635 termp_xx_pre(DECL_ARGS
)
1640 switch (node
->tok
) {
1645 pp
= "DragonFlyBSD";
1671 termp_sq_pre(DECL_ARGS
)
1674 if (MDOC_BODY
!= node
->type
)
1676 term_word(p
, "\\(oq");
1677 p
->flags
|= TERMP_NOSPACE
;
1684 termp_sq_post(DECL_ARGS
)
1687 if (MDOC_BODY
!= node
->type
)
1689 p
->flags
|= TERMP_NOSPACE
;
1690 term_word(p
, "\\(aq");
1696 termp_pf_pre(DECL_ARGS
)
1699 p
->flags
|= TERMP_IGNDELIM
;
1706 termp_pf_post(DECL_ARGS
)
1709 p
->flags
&= ~TERMP_IGNDELIM
;
1710 p
->flags
|= TERMP_NOSPACE
;
1716 termp_ss_pre(DECL_ARGS
)
1719 switch (node
->type
) {
1726 pair
->flag
|= ttypes
[TTYPE_SSECTION
];
1727 p
->offset
= HALFINDENT
;
1739 termp_ss_post(DECL_ARGS
)
1742 if (MDOC_HEAD
== node
->type
)
1749 termp_pa_pre(DECL_ARGS
)
1752 pair
->flag
|= ttypes
[TTYPE_FILE
];
1759 termp_em_pre(DECL_ARGS
)
1762 pair
->flag
|= ttypes
[TTYPE_EMPH
];
1769 termp_cd_pre(DECL_ARGS
)
1772 pair
->flag
|= ttypes
[TTYPE_CONFIG
];
1780 termp_cm_pre(DECL_ARGS
)
1783 pair
->flag
|= ttypes
[TTYPE_CMD_FLAG
];
1790 termp_ic_pre(DECL_ARGS
)
1793 pair
->flag
|= ttypes
[TTYPE_CMD
];
1800 termp_in_pre(DECL_ARGS
)
1803 pair
->flag
|= ttypes
[TTYPE_INCLUDE
];
1804 p
->flags
|= ttypes
[TTYPE_INCLUDE
];
1806 if (SEC_SYNOPSIS
== node
->sec
)
1807 term_word(p
, "#include");
1810 p
->flags
|= TERMP_NOSPACE
;
1817 termp_in_post(DECL_ARGS
)
1820 p
->flags
|= TERMP_NOSPACE
;
1823 if (SEC_SYNOPSIS
!= node
->sec
)
1828 * XXX Not entirely correct. If `.In foo bar' is specified in
1829 * the SYNOPSIS section, then it produces a single break after
1830 * the <foo>; mandoc asserts a vertical space. Since this
1831 * construction is rarely used, I think it's fine.
1833 if (node
->next
&& MDOC_In
!= node
->next
->tok
)
1840 termp_brq_pre(DECL_ARGS
)
1843 if (MDOC_BODY
!= node
->type
)
1845 term_word(p
, "\\(lC");
1846 p
->flags
|= TERMP_NOSPACE
;
1853 termp_brq_post(DECL_ARGS
)
1856 if (MDOC_BODY
!= node
->type
)
1858 p
->flags
|= TERMP_NOSPACE
;
1859 term_word(p
, "\\(rC");
1865 termp_bq_pre(DECL_ARGS
)
1868 if (MDOC_BODY
!= node
->type
)
1870 term_word(p
, "\\(lB");
1871 p
->flags
|= TERMP_NOSPACE
;
1878 termp_bq_post(DECL_ARGS
)
1881 if (MDOC_BODY
!= node
->type
)
1883 p
->flags
|= TERMP_NOSPACE
;
1884 term_word(p
, "\\(rB");
1890 termp_pq_pre(DECL_ARGS
)
1893 if (MDOC_BODY
!= node
->type
)
1895 term_word(p
, "\\&(");
1896 p
->flags
|= TERMP_NOSPACE
;
1903 termp_pq_post(DECL_ARGS
)
1906 if (MDOC_BODY
!= node
->type
)
1914 termp_fo_pre(DECL_ARGS
)
1916 const struct mdoc_node
*n
;
1918 if (MDOC_BODY
== node
->type
) {
1919 p
->flags
|= TERMP_NOSPACE
;
1921 p
->flags
|= TERMP_NOSPACE
;
1923 } else if (MDOC_HEAD
!= node
->type
)
1926 p
->flags
|= ttypes
[TTYPE_FUNC_NAME
];
1927 for (n
= node
->child
; n
; n
= n
->next
) {
1928 assert(MDOC_TEXT
== n
->type
);
1929 term_word(p
, n
->string
);
1931 p
->flags
&= ~ttypes
[TTYPE_FUNC_NAME
];
1939 termp_fo_post(DECL_ARGS
)
1942 if (MDOC_BODY
!= node
->type
)
1944 p
->flags
|= TERMP_NOSPACE
;
1946 p
->flags
|= TERMP_NOSPACE
;
1954 termp_bf_pre(DECL_ARGS
)
1956 const struct mdoc_node
*n
;
1958 if (MDOC_HEAD
== node
->type
)
1960 else if (MDOC_BLOCK
!= node
->type
)
1963 if (NULL
== (n
= node
->head
->child
)) {
1964 if (arg_hasattr(MDOC_Emphasis
, node
))
1965 pair
->flag
|= ttypes
[TTYPE_EMPH
];
1966 else if (arg_hasattr(MDOC_Symbolic
, node
))
1967 pair
->flag
|= ttypes
[TTYPE_SYMB
];
1972 assert(MDOC_TEXT
== n
->type
);
1973 if (0 == strcmp("Em", n
->string
))
1974 pair
->flag
|= ttypes
[TTYPE_EMPH
];
1975 else if (0 == strcmp("Sy", n
->string
))
1976 pair
->flag
|= ttypes
[TTYPE_SYMB
];
1984 termp_sy_pre(DECL_ARGS
)
1987 pair
->flag
|= ttypes
[TTYPE_SYMB
];
1994 termp_ms_pre(DECL_ARGS
)
1997 pair
->flag
|= ttypes
[TTYPE_SYMBOL
];
2005 termp_sm_pre(DECL_ARGS
)
2008 assert(node
->child
&& MDOC_TEXT
== node
->child
->type
);
2009 if (0 == strcmp("on", node
->child
->string
)) {
2010 p
->flags
&= ~TERMP_NONOSPACE
;
2011 p
->flags
&= ~TERMP_NOSPACE
;
2013 p
->flags
|= TERMP_NONOSPACE
;
2021 termp_ap_pre(DECL_ARGS
)
2024 p
->flags
|= TERMP_NOSPACE
;
2025 term_word(p
, "\\(aq");
2026 p
->flags
|= TERMP_NOSPACE
;
2033 termp__j_pre(DECL_ARGS
)
2036 pair
->flag
|= ttypes
[TTYPE_REF_JOURNAL
];
2043 termp__t_pre(DECL_ARGS
)
2047 p
->flags
|= TERMP_NOSPACE
;
2054 termp__t_post(DECL_ARGS
)
2057 p
->flags
|= TERMP_NOSPACE
;
2059 termp____post(p
, pair
, meta
, node
);
2065 termp____post(DECL_ARGS
)
2068 p
->flags
|= TERMP_NOSPACE
;
2069 term_word(p
, node
->next
? "," : ".");
2075 termp_lk_pre(DECL_ARGS
)
2077 const struct mdoc_node
*n
;
2079 assert(node
->child
);
2082 if (NULL
== n
->next
) {
2083 pair
->flag
|= ttypes
[TTYPE_LINK_ANCHOR
];
2087 p
->flags
|= ttypes
[TTYPE_LINK_ANCHOR
];
2088 term_word(p
, n
->string
);
2089 p
->flags
|= TERMP_NOSPACE
;
2091 p
->flags
&= ~ttypes
[TTYPE_LINK_ANCHOR
];
2093 p
->flags
|= ttypes
[TTYPE_LINK_TEXT
];
2094 for (n
= n
->next
; n
; n
= n
->next
)
2095 term_word(p
, n
->string
);
2097 p
->flags
&= ~ttypes
[TTYPE_LINK_TEXT
];
2104 termp_mt_pre(DECL_ARGS
)
2107 pair
->flag
|= ttypes
[TTYPE_LINK_ANCHOR
];