]>
git.cameronkatri.com Git - mandoc.git/blob - mdoc_term.c
1 /* $Id: mdoc_term.c,v 1.32 2009/07/12 19:28:46 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_at_pre(DECL_ARGS
);
131 static int termp_bd_pre(DECL_ARGS
);
132 static int termp_bf_pre(DECL_ARGS
);
133 static int termp_bq_pre(DECL_ARGS
);
134 static int termp_brq_pre(DECL_ARGS
);
135 static int termp_bt_pre(DECL_ARGS
);
136 static int termp_cd_pre(DECL_ARGS
);
137 static int termp_cm_pre(DECL_ARGS
);
138 static int termp_d1_pre(DECL_ARGS
);
139 static int termp_dq_pre(DECL_ARGS
);
140 static int termp_em_pre(DECL_ARGS
);
141 static int termp_ex_pre(DECL_ARGS
);
142 static int termp_fa_pre(DECL_ARGS
);
143 static int termp_fd_pre(DECL_ARGS
);
144 static int termp_fl_pre(DECL_ARGS
);
145 static int termp_fn_pre(DECL_ARGS
);
146 static int termp_fo_pre(DECL_ARGS
);
147 static int termp_ft_pre(DECL_ARGS
);
148 static int termp_ic_pre(DECL_ARGS
);
149 static int termp_in_pre(DECL_ARGS
);
150 static int termp_it_pre(DECL_ARGS
);
151 static int termp_lb_pre(DECL_ARGS
);
152 static int termp_lk_pre(DECL_ARGS
);
153 static int termp_ms_pre(DECL_ARGS
);
154 static int termp_mt_pre(DECL_ARGS
);
155 static int termp_nd_pre(DECL_ARGS
);
156 static int termp_nm_pre(DECL_ARGS
);
157 static int termp_ns_pre(DECL_ARGS
);
158 static int termp_op_pre(DECL_ARGS
);
159 static int termp_pa_pre(DECL_ARGS
);
160 static int termp_pf_pre(DECL_ARGS
);
161 static int termp_pp_pre(DECL_ARGS
);
162 static int termp_pq_pre(DECL_ARGS
);
163 static int termp_qq_pre(DECL_ARGS
);
164 static int termp_rs_pre(DECL_ARGS
);
165 static int termp_rv_pre(DECL_ARGS
);
166 static int termp_sh_pre(DECL_ARGS
);
167 static int termp_sm_pre(DECL_ARGS
);
168 static int termp_sq_pre(DECL_ARGS
);
169 static int termp_ss_pre(DECL_ARGS
);
170 static int termp_st_pre(DECL_ARGS
);
171 static int termp_sx_pre(DECL_ARGS
);
172 static int termp_sy_pre(DECL_ARGS
);
173 static int termp_ud_pre(DECL_ARGS
);
174 static int termp_va_pre(DECL_ARGS
);
175 static int termp_vt_pre(DECL_ARGS
);
176 static int termp_xr_pre(DECL_ARGS
);
177 static int termp_xx_pre(DECL_ARGS
);
179 static const struct termact termacts
[MDOC_MAX
] = {
180 { termp_ap_pre
, NULL
}, /* Ap */
181 { NULL
, NULL
}, /* Dd */
182 { NULL
, NULL
}, /* Dt */
183 { NULL
, NULL
}, /* Os */
184 { termp_sh_pre
, termp_sh_post
}, /* Sh */
185 { termp_ss_pre
, termp_ss_post
}, /* Ss */
186 { termp_pp_pre
, NULL
}, /* Pp */
187 { termp_d1_pre
, termp_d1_post
}, /* D1 */
188 { termp_d1_pre
, termp_d1_post
}, /* Dl */
189 { termp_bd_pre
, termp_bd_post
}, /* Bd */
190 { NULL
, NULL
}, /* Ed */
191 { NULL
, termp_bl_post
}, /* Bl */
192 { NULL
, NULL
}, /* El */
193 { termp_it_pre
, termp_it_post
}, /* It */
194 { NULL
, NULL
}, /* Ad */
195 { NULL
, NULL
}, /* An */
196 { termp_ar_pre
, NULL
}, /* Ar */
197 { termp_cd_pre
, NULL
}, /* Cd */
198 { termp_cm_pre
, NULL
}, /* Cm */
199 { NULL
, NULL
}, /* Dv */
200 { NULL
, NULL
}, /* Er */
201 { NULL
, NULL
}, /* Ev */
202 { termp_ex_pre
, NULL
}, /* Ex */
203 { termp_fa_pre
, NULL
}, /* Fa */
204 { termp_fd_pre
, termp_fd_post
}, /* Fd */
205 { termp_fl_pre
, NULL
}, /* Fl */
206 { termp_fn_pre
, termp_fn_post
}, /* Fn */
207 { termp_ft_pre
, termp_ft_post
}, /* Ft */
208 { termp_ic_pre
, NULL
}, /* Ic */
209 { termp_in_pre
, termp_in_post
}, /* In */
210 { NULL
, NULL
}, /* Li */
211 { termp_nd_pre
, NULL
}, /* Nd */
212 { termp_nm_pre
, NULL
}, /* Nm */
213 { termp_op_pre
, termp_op_post
}, /* Op */
214 { NULL
, NULL
}, /* Ot */
215 { termp_pa_pre
, NULL
}, /* Pa */
216 { termp_rv_pre
, NULL
}, /* Rv */
217 { termp_st_pre
, NULL
}, /* St */
218 { termp_va_pre
, NULL
}, /* Va */
219 { termp_vt_pre
, termp_vt_post
}, /* Vt */
220 { termp_xr_pre
, NULL
}, /* Xr */
221 { NULL
, termp____post
}, /* %A */
222 { NULL
, termp____post
}, /* %B */
223 { NULL
, termp____post
}, /* %D */
224 { NULL
, termp____post
}, /* %I */
225 { termp__j_pre
, termp____post
}, /* %J */
226 { NULL
, termp____post
}, /* %N */
227 { NULL
, termp____post
}, /* %O */
228 { NULL
, termp____post
}, /* %P */
229 { NULL
, termp____post
}, /* %R */
230 { termp__t_pre
, termp__t_post
}, /* %T */
231 { NULL
, termp____post
}, /* %V */
232 { NULL
, NULL
}, /* Ac */
233 { termp_aq_pre
, termp_aq_post
}, /* Ao */
234 { termp_aq_pre
, termp_aq_post
}, /* Aq */
235 { termp_at_pre
, NULL
}, /* At */
236 { NULL
, NULL
}, /* Bc */
237 { termp_bf_pre
, NULL
}, /* Bf */
238 { termp_bq_pre
, termp_bq_post
}, /* Bo */
239 { termp_bq_pre
, termp_bq_post
}, /* Bq */
240 { termp_xx_pre
, NULL
}, /* Bsx */
241 { NULL
, termp_bx_post
}, /* Bx */
242 { NULL
, NULL
}, /* Db */
243 { NULL
, NULL
}, /* Dc */
244 { termp_dq_pre
, termp_dq_post
}, /* Do */
245 { termp_dq_pre
, termp_dq_post
}, /* Dq */
246 { NULL
, NULL
}, /* Ec */
247 { NULL
, NULL
}, /* Ef */
248 { termp_em_pre
, NULL
}, /* Em */
249 { NULL
, NULL
}, /* Eo */
250 { termp_xx_pre
, NULL
}, /* Fx */
251 { termp_ms_pre
, NULL
}, /* Ms */
252 { NULL
, NULL
}, /* No */
253 { termp_ns_pre
, NULL
}, /* Ns */
254 { termp_xx_pre
, NULL
}, /* Nx */
255 { termp_xx_pre
, NULL
}, /* Ox */
256 { NULL
, NULL
}, /* Pc */
257 { termp_pf_pre
, termp_pf_post
}, /* Pf */
258 { termp_pq_pre
, termp_pq_post
}, /* Po */
259 { termp_pq_pre
, termp_pq_post
}, /* Pq */
260 { NULL
, NULL
}, /* Qc */
261 { termp_sq_pre
, termp_sq_post
}, /* Ql */
262 { termp_qq_pre
, termp_qq_post
}, /* Qo */
263 { termp_qq_pre
, termp_qq_post
}, /* Qq */
264 { NULL
, NULL
}, /* Re */
265 { termp_rs_pre
, NULL
}, /* Rs */
266 { NULL
, NULL
}, /* Sc */
267 { termp_sq_pre
, termp_sq_post
}, /* So */
268 { termp_sq_pre
, termp_sq_post
}, /* Sq */
269 { termp_sm_pre
, NULL
}, /* Sm */
270 { termp_sx_pre
, NULL
}, /* Sx */
271 { termp_sy_pre
, NULL
}, /* Sy */
272 { NULL
, NULL
}, /* Tn */
273 { termp_xx_pre
, NULL
}, /* Ux */
274 { NULL
, NULL
}, /* Xc */
275 { NULL
, NULL
}, /* Xo */
276 { termp_fo_pre
, termp_fo_post
}, /* Fo */
277 { NULL
, NULL
}, /* Fc */
278 { termp_op_pre
, termp_op_post
}, /* Oo */
279 { NULL
, NULL
}, /* Oc */
280 { NULL
, NULL
}, /* Bk */
281 { NULL
, NULL
}, /* Ek */
282 { termp_bt_pre
, NULL
}, /* Bt */
283 { NULL
, NULL
}, /* Hf */
284 { NULL
, NULL
}, /* Fr */
285 { termp_ud_pre
, NULL
}, /* Ud */
286 { termp_lb_pre
, termp_lb_post
}, /* Lb */
287 { termp_pp_pre
, NULL
}, /* Lp */
288 { termp_lk_pre
, NULL
}, /* Lk */
289 { termp_mt_pre
, NULL
}, /* Mt */
290 { termp_brq_pre
, termp_brq_post
}, /* Brq */
291 { termp_brq_pre
, termp_brq_post
}, /* Bro */
292 { NULL
, NULL
}, /* Brc */
293 { NULL
, NULL
}, /* %C */
294 { NULL
, NULL
}, /* Es */
295 { NULL
, NULL
}, /* En */
296 { termp_xx_pre
, NULL
}, /* Dx */
297 { NULL
, NULL
}, /* %Q */
301 extern size_t strlcpy(char *, const char *, size_t);
302 extern size_t strlcat(char *, const char *, size_t);
305 static int arg_hasattr(int, const struct mdoc_node
*);
306 static int arg_getattrs(const int *, int *, size_t,
307 const struct mdoc_node
*);
308 static int arg_getattr(int, const struct mdoc_node
*);
309 static size_t arg_offset(const struct mdoc_argv
*);
310 static size_t arg_width(const struct mdoc_argv
*, int);
311 static int arg_listtype(const struct mdoc_node
*);
312 static int fmt_block_vspace(struct termp
*,
313 const struct mdoc_node
*,
314 const struct mdoc_node
*);
315 static void print_node(DECL_ARGS
);
316 static void print_head(struct termp
*,
317 const struct mdoc_meta
*);
318 static void print_body(DECL_ARGS
);
319 static void print_foot(struct termp
*,
320 const struct mdoc_meta
*);
324 mdoc_run(struct termp
*p
, const struct mdoc
*m
)
327 * Main output function. When this is called, assume that the
328 * tree is properly formed.
330 print_head(p
, mdoc_meta(m
));
331 assert(mdoc_node(m
));
332 assert(MDOC_ROOT
== mdoc_node(m
)->type
);
333 if (mdoc_node(m
)->child
)
334 print_body(p
, NULL
, mdoc_meta(m
), mdoc_node(m
)->child
);
335 print_foot(p
, mdoc_meta(m
));
341 print_body(DECL_ARGS
)
344 print_node(p
, pair
, meta
, node
);
347 print_body(p
, pair
, meta
, node
->next
);
352 print_node(DECL_ARGS
)
355 struct termpair npair
;
356 size_t offset
, rmargin
;
360 rmargin
= p
->rmargin
;
366 if (MDOC_TEXT
!= node
->type
) {
367 if (termacts
[node
->tok
].pre
)
368 if ( ! (*termacts
[node
->tok
].pre
)(p
, &npair
, meta
, node
))
370 } else /* MDOC_TEXT == node->type */
371 term_word(p
, node
->string
);
375 p
->flags
|= npair
.flag
;
377 if (dochild
&& node
->child
)
378 print_body(p
, &npair
, meta
, node
->child
);
380 /* Post-processing. */
382 if (MDOC_TEXT
!= node
->type
)
383 if (termacts
[node
->tok
].post
)
384 (*termacts
[node
->tok
].post
)(p
, &npair
, meta
, node
);
387 p
->rmargin
= rmargin
;
388 p
->flags
&= ~npair
.flag
;
393 print_foot(struct termp
*p
, const struct mdoc_meta
*meta
)
399 * Output the footer in new-groff style, that is, three columns
400 * with the middle being the manual date and flanking columns
401 * being the operating system:
406 if (NULL
== (buf
= malloc(p
->rmargin
)))
408 if (NULL
== (os
= malloc(p
->rmargin
)))
411 tm
= localtime(&meta
->date
);
413 if (0 == strftime(buf
, p
->rmargin
, "%B %d, %Y", tm
))
416 (void)strlcpy(os
, meta
->os
, p
->rmargin
);
421 p
->rmargin
= (p
->maxrmargin
- strlen(buf
) + 1) / 2;
422 p
->flags
|= TERMP_NOSPACE
| TERMP_NOBREAK
;
427 p
->offset
= p
->rmargin
;
428 p
->rmargin
= p
->maxrmargin
- strlen(os
);
429 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
434 p
->offset
= p
->rmargin
;
435 p
->rmargin
= p
->maxrmargin
;
436 p
->flags
&= ~TERMP_NOBREAK
;
437 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
443 p
->rmargin
= p
->maxrmargin
;
452 print_head(struct termp
*p
, const struct mdoc_meta
*meta
)
456 p
->rmargin
= p
->maxrmargin
;
459 if (NULL
== (buf
= malloc(p
->rmargin
)))
461 if (NULL
== (title
= malloc(p
->rmargin
)))
465 * The header is strange. It has three components, which are
466 * really two with the first duplicated. It goes like this:
468 * IDENTIFIER TITLE IDENTIFIER
470 * The IDENTIFIER is NAME(SECTION), which is the command-name
471 * (if given, or "unknown" if not) followed by the manual page
472 * section. These are given in `Dt'. The TITLE is a free-form
473 * string depending on the manual volume. If not specified, it
474 * switches on the manual section.
478 (void)strlcpy(buf
, meta
->vol
, p
->rmargin
);
481 (void)strlcat(buf
, " (", p
->rmargin
);
482 (void)strlcat(buf
, meta
->arch
, p
->rmargin
);
483 (void)strlcat(buf
, ")", p
->rmargin
);
486 (void)snprintf(title
, p
->rmargin
, "%s(%d)",
487 meta
->title
, meta
->msec
);
490 p
->rmargin
= (p
->maxrmargin
- strlen(buf
) + 1) / 2;
491 p
->flags
|= TERMP_NOBREAK
| TERMP_NOSPACE
;
496 p
->offset
= p
->rmargin
;
497 p
->rmargin
= p
->maxrmargin
- strlen(title
);
498 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
503 p
->offset
= p
->rmargin
;
504 p
->rmargin
= p
->maxrmargin
;
505 p
->flags
&= ~TERMP_NOBREAK
;
506 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
512 p
->rmargin
= p
->maxrmargin
;
513 p
->flags
&= ~TERMP_NOSPACE
;
521 arg_width(const struct mdoc_argv
*arg
, int pos
)
526 assert(pos
< (int)arg
->sz
&& pos
>= 0);
527 assert(arg
->value
[pos
]);
529 if (0 == (len
= (int)strlen(arg
->value
[pos
])))
532 for (i
= 0; i
< len
- 1; i
++)
533 if ( ! isdigit((u_char
)arg
->value
[pos
][i
]))
537 if ('n' == arg
->value
[pos
][len
- 1] ||
538 'm' == arg
->value
[pos
][len
- 1]) {
539 v
= (size_t)atoi(arg
->value
[pos
]);
544 return(strlen(arg
->value
[pos
]) + 2);
549 arg_listtype(const struct mdoc_node
*n
)
553 assert(MDOC_BLOCK
== n
->type
);
555 len
= (int)(n
->args
? n
->args
->argc
: 0);
557 for (i
= 0; i
< len
; i
++)
558 switch (n
->args
->argv
[i
].arg
) {
578 return(n
->args
->argv
[i
].arg
);
583 /* FIXME: mandated by parser. */
585 errx(1, "list type not supported");
591 arg_offset(const struct mdoc_argv
*arg
)
595 if (0 == strcmp(*arg
->value
, "left"))
597 if (0 == strcmp(*arg
->value
, "indent"))
599 if (0 == strcmp(*arg
->value
, "indent-two"))
600 return((INDENT
+ 1) * 2);
602 /* FIXME: needs to support field-widths (10n, etc.). */
604 return(strlen(*arg
->value
));
609 arg_hasattr(int arg
, const struct mdoc_node
*n
)
612 return(-1 != arg_getattr(arg
, n
));
617 arg_getattr(int v
, const struct mdoc_node
*n
)
621 return(arg_getattrs(&v
, &val
, 1, n
) ? val
: -1);
626 arg_getattrs(const int *keys
, int *vals
,
627 size_t sz
, const struct mdoc_node
*n
)
634 for (k
= i
= 0; i
< (int)n
->args
->argc
; i
++)
635 for (j
= 0; j
< (int)sz
; j
++)
636 if (n
->args
->argv
[i
].arg
== keys
[j
]) {
646 fmt_block_vspace(struct termp
*p
,
647 const struct mdoc_node
*bl
,
648 const struct mdoc_node
*node
)
650 const struct mdoc_node
*n
;
654 if (arg_hasattr(MDOC_Compact
, bl
))
657 for (n
= node
; n
; n
= n
->parent
) {
658 if (MDOC_BLOCK
!= n
->type
)
660 if (MDOC_Ss
== n
->tok
)
662 if (MDOC_Sh
== n
->tok
)
676 termp_dq_pre(DECL_ARGS
)
679 if (MDOC_BODY
!= node
->type
)
682 term_word(p
, "\\(lq");
683 p
->flags
|= TERMP_NOSPACE
;
690 termp_dq_post(DECL_ARGS
)
693 if (MDOC_BODY
!= node
->type
)
696 p
->flags
|= TERMP_NOSPACE
;
697 term_word(p
, "\\(rq");
703 termp_it_pre(DECL_ARGS
)
705 const struct mdoc_node
*bl
, *n
;
707 int i
, type
, keys
[3], vals
[3], sv
;
708 size_t width
, offset
;
710 if (MDOC_BLOCK
== node
->type
)
711 return(fmt_block_vspace(p
, node
->parent
->parent
, node
));
713 bl
= node
->parent
->parent
->parent
;
715 /* Save parent attributes. */
717 pair
->flag
= p
->flags
;
719 /* Get list width and offset. */
721 keys
[0] = MDOC_Width
;
722 keys
[1] = MDOC_Offset
;
723 keys
[2] = MDOC_Column
;
725 vals
[0] = vals
[1] = vals
[2] = -1;
729 (void)arg_getattrs(keys
, vals
, 3, bl
);
731 type
= arg_listtype(bl
);
733 /* Calculate real width and offset. */
737 if (MDOC_BODY
== node
->type
)
739 for (i
= 0, n
= node
->prev
; n
; n
= n
->prev
, i
++)
741 (&bl
->args
->argv
[vals
[2]], i
);
742 assert(i
< (int)bl
->args
->argv
[vals
[2]].sz
);
743 width
= arg_width(&bl
->args
->argv
[vals
[2]], i
);
745 offset
+= arg_offset(&bl
->args
->argv
[vals
[1]]);
749 width
= arg_width(&bl
->args
->argv
[vals
[0]], 0);
751 offset
+= arg_offset(&bl
->args
->argv
[vals
[1]]);
756 * List-type can override the width in the case of fixed-head
757 * values (bullet, dash/hyphen, enum). Tags need a non-zero
783 * Whitespace control. Inset bodies need an initial space,
784 * while diagonal bodies need two.
792 if (MDOC_BODY
== node
->type
)
793 p
->flags
&= ~TERMP_NOSPACE
;
795 p
->flags
|= TERMP_NOSPACE
;
798 p
->flags
|= TERMP_NOSPACE
;
803 * Style flags. Diagnostic heads need TTYPE_DIAG.
808 if (MDOC_HEAD
== node
->type
)
809 p
->flags
|= ttypes
[TTYPE_DIAG
];
816 * Pad and break control. This is the tricker part. Lists with
817 * set right-margins for the head get TERMP_NOBREAK because, if
818 * they overrun the margin, they wrap to the new margin.
819 * Correspondingly, the body for these types don't left-pad, as
820 * the head will pad out to to the right.
833 if (MDOC_HEAD
== node
->type
)
834 p
->flags
|= TERMP_NOBREAK
;
836 p
->flags
|= TERMP_NOLPAD
;
837 if (MDOC_HEAD
== node
->type
&& MDOC_Tag
== type
)
838 if (NULL
== node
->next
||
839 NULL
== node
->next
->child
)
840 p
->flags
|= TERMP_NONOBREAK
;
843 if (MDOC_HEAD
== node
->type
) {
845 if (MDOC_BODY
== node
->next
->type
)
846 p
->flags
&= ~TERMP_NOBREAK
;
848 p
->flags
|= TERMP_NOBREAK
;
850 p
->flags
|= TERMP_NOLPAD
;
854 if (MDOC_HEAD
== node
->type
)
855 p
->flags
|= TERMP_NOBREAK
;
862 * Margin control. Set-head-width lists have their right
863 * margins shortened. The body for these lists has the offset
864 * necessarily lengthened. Everybody gets the offset.
879 if (MDOC_HEAD
== node
->type
)
880 p
->rmargin
= p
->offset
+ width
;
885 p
->rmargin
= p
->offset
+ width
;
892 * The dash, hyphen, bullet and enum lists all have a special
893 * HEAD character (temporarily bold, in some cases).
897 if (MDOC_HEAD
== node
->type
)
900 p
->flags
|= TERMP_BOLD
;
901 term_word(p
, "\\[bu]");
906 p
->flags
|= TERMP_BOLD
;
907 term_word(p
, "\\(hy");
910 (pair
->ppair
->ppair
->count
)++;
911 (void)snprintf(buf
, sizeof(buf
), "%d.",
912 pair
->ppair
->ppair
->count
);
919 p
->flags
= sv
; /* Restore saved flags. */
922 * If we're not going to process our children, indicate so here.
935 if (MDOC_HEAD
== node
->type
)
939 if (MDOC_BODY
== node
->type
)
952 termp_it_post(DECL_ARGS
)
956 if (MDOC_BODY
!= node
->type
&& MDOC_HEAD
!= node
->type
)
959 type
= arg_listtype(node
->parent
->parent
->parent
);
967 if (MDOC_BODY
== node
->type
)
971 if (MDOC_HEAD
== node
->type
)
979 p
->flags
= pair
->flag
;
985 termp_nm_pre(DECL_ARGS
)
988 if (SEC_SYNOPSIS
== node
->sec
)
991 pair
->flag
|= ttypes
[TTYPE_PROG
];
992 p
->flags
|= ttypes
[TTYPE_PROG
];
994 if (NULL
== node
->child
)
995 term_word(p
, meta
->name
);
1003 termp_fl_pre(DECL_ARGS
)
1006 pair
->flag
|= ttypes
[TTYPE_CMD_FLAG
];
1007 p
->flags
|= ttypes
[TTYPE_CMD_FLAG
];
1008 term_word(p
, "\\-");
1009 p
->flags
|= TERMP_NOSPACE
;
1016 termp_ar_pre(DECL_ARGS
)
1019 pair
->flag
|= ttypes
[TTYPE_CMD_ARG
];
1026 termp_ns_pre(DECL_ARGS
)
1029 p
->flags
|= TERMP_NOSPACE
;
1036 termp_pp_pre(DECL_ARGS
)
1046 termp_st_pre(DECL_ARGS
)
1050 if (node
->child
&& (cp
= mdoc_a2st(node
->child
->string
)))
1058 termp_rs_pre(DECL_ARGS
)
1061 if (MDOC_BLOCK
== node
->type
&& node
->prev
)
1069 termp_rv_pre(DECL_ARGS
)
1073 /* FIXME: mandated by parser. */
1075 if (-1 == (i
= arg_getattr(MDOC_Std
, node
)))
1076 errx(1, "expected -std argument");
1077 if (1 != node
->args
->argv
[i
].sz
)
1078 errx(1, "expected -std argument");
1081 term_word(p
, "The");
1083 p
->flags
|= ttypes
[TTYPE_FUNC_NAME
];
1084 term_word(p
, *node
->args
->argv
[i
].value
);
1085 p
->flags
&= ~ttypes
[TTYPE_FUNC_NAME
];
1086 p
->flags
|= TERMP_NOSPACE
;
1088 term_word(p
, "() function returns the value 0 if successful;");
1089 term_word(p
, "otherwise the value -1 is returned and the");
1090 term_word(p
, "global variable");
1092 p
->flags
|= ttypes
[TTYPE_VAR_DECL
];
1093 term_word(p
, "errno");
1094 p
->flags
&= ~ttypes
[TTYPE_VAR_DECL
];
1096 term_word(p
, "is set to indicate the error.");
1104 termp_ex_pre(DECL_ARGS
)
1108 /* FIXME: mandated by parser? */
1110 if (-1 == (i
= arg_getattr(MDOC_Std
, node
)))
1111 errx(1, "expected -std argument");
1112 if (1 != node
->args
->argv
[i
].sz
)
1113 errx(1, "expected -std argument");
1115 term_word(p
, "The");
1116 p
->flags
|= ttypes
[TTYPE_PROG
];
1117 term_word(p
, *node
->args
->argv
[i
].value
);
1118 p
->flags
&= ~ttypes
[TTYPE_PROG
];
1119 term_word(p
, "utility exits 0 on success, and >0 if an error occurs.");
1127 termp_nd_pre(DECL_ARGS
)
1130 if (MDOC_BODY
!= node
->type
)
1134 * XXX: signed off by jmc@openbsd.org. This technically
1135 * produces a minus sign after the Nd, which is wrong, but is
1136 * consistent with the historic OpenBSD tmac file.
1138 #if defined(__OpenBSD__) || defined(__linux__)
1139 term_word(p
, "\\-");
1141 term_word(p
, "\\(em");
1149 termp_bl_post(DECL_ARGS
)
1152 if (MDOC_BLOCK
== node
->type
)
1159 termp_op_post(DECL_ARGS
)
1162 if (MDOC_BODY
!= node
->type
)
1164 p
->flags
|= TERMP_NOSPACE
;
1165 term_word(p
, "\\(rB");
1171 termp_xr_pre(DECL_ARGS
)
1173 const struct mdoc_node
*n
;
1175 assert(node
->child
&& MDOC_TEXT
== node
->child
->type
);
1178 term_word(p
, n
->string
);
1179 if (NULL
== (n
= n
->next
))
1181 p
->flags
|= TERMP_NOSPACE
;
1183 p
->flags
|= TERMP_NOSPACE
;
1184 term_word(p
, n
->string
);
1185 p
->flags
|= TERMP_NOSPACE
;
1193 termp_vt_pre(DECL_ARGS
)
1196 /* FIXME: this can be "type name". */
1197 pair
->flag
|= ttypes
[TTYPE_VAR_DECL
];
1204 termp_vt_post(DECL_ARGS
)
1207 if (node
->sec
!= SEC_SYNOPSIS
)
1209 if (node
->next
&& MDOC_Vt
== node
->next
->tok
)
1211 else if (node
->next
)
1218 termp_fd_pre(DECL_ARGS
)
1221 pair
->flag
|= ttypes
[TTYPE_FUNC_DECL
];
1228 termp_fd_post(DECL_ARGS
)
1231 if (node
->sec
!= SEC_SYNOPSIS
)
1235 if (node
->next
&& MDOC_Fd
!= node
->next
->tok
)
1242 termp_sh_pre(DECL_ARGS
)
1245 switch (node
->type
) {
1248 pair
->flag
|= ttypes
[TTYPE_SECTION
];
1262 termp_sh_post(DECL_ARGS
)
1265 switch (node
->type
) {
1281 termp_op_pre(DECL_ARGS
)
1284 switch (node
->type
) {
1286 term_word(p
, "\\(lB");
1287 p
->flags
|= TERMP_NOSPACE
;
1298 termp_bt_pre(DECL_ARGS
)
1301 term_word(p
, "is currently in beta test.");
1308 termp_lb_pre(DECL_ARGS
)
1312 assert(node
->child
&& MDOC_TEXT
== node
->child
->type
);
1313 lb
= mdoc_a2lib(node
->child
->string
);
1318 term_word(p
, "library");
1325 termp_lb_post(DECL_ARGS
)
1334 termp_ud_pre(DECL_ARGS
)
1337 term_word(p
, "currently under development.");
1344 termp_d1_pre(DECL_ARGS
)
1347 if (MDOC_BLOCK
!= node
->type
)
1350 p
->offset
+= (INDENT
+ 1);
1357 termp_d1_post(DECL_ARGS
)
1360 if (MDOC_BLOCK
!= node
->type
)
1368 termp_aq_pre(DECL_ARGS
)
1371 if (MDOC_BODY
!= node
->type
)
1373 term_word(p
, "\\(la");
1374 p
->flags
|= TERMP_NOSPACE
;
1381 termp_aq_post(DECL_ARGS
)
1384 if (MDOC_BODY
!= node
->type
)
1386 p
->flags
|= TERMP_NOSPACE
;
1387 term_word(p
, "\\(ra");
1393 termp_ft_pre(DECL_ARGS
)
1396 if (SEC_SYNOPSIS
== node
->sec
)
1397 if (node
->prev
&& MDOC_Fo
== node
->prev
->tok
)
1399 pair
->flag
|= ttypes
[TTYPE_FUNC_TYPE
];
1406 termp_ft_post(DECL_ARGS
)
1409 if (SEC_SYNOPSIS
== node
->sec
)
1416 termp_fn_pre(DECL_ARGS
)
1418 const struct mdoc_node
*n
;
1420 assert(node
->child
&& MDOC_TEXT
== node
->child
->type
);
1422 /* FIXME: can be "type funcname" "type varname"... */
1424 p
->flags
|= ttypes
[TTYPE_FUNC_NAME
];
1425 term_word(p
, node
->child
->string
);
1426 p
->flags
&= ~ttypes
[TTYPE_FUNC_NAME
];
1428 p
->flags
|= TERMP_NOSPACE
;
1431 for (n
= node
->child
->next
; n
; n
= n
->next
) {
1432 p
->flags
|= ttypes
[TTYPE_FUNC_ARG
];
1433 term_word(p
, n
->string
);
1434 p
->flags
&= ~ttypes
[TTYPE_FUNC_ARG
];
1441 if (SEC_SYNOPSIS
== node
->sec
)
1450 termp_fn_post(DECL_ARGS
)
1453 if (node
->sec
== SEC_SYNOPSIS
&& node
->next
)
1460 termp_sx_pre(DECL_ARGS
)
1463 pair
->flag
|= ttypes
[TTYPE_LINK
];
1470 termp_fa_pre(DECL_ARGS
)
1472 struct mdoc_node
*n
;
1474 if (node
->parent
->tok
!= MDOC_Fo
) {
1475 pair
->flag
|= ttypes
[TTYPE_FUNC_ARG
];
1479 for (n
= node
->child
; n
; n
= n
->next
) {
1480 p
->flags
|= ttypes
[TTYPE_FUNC_ARG
];
1481 term_word(p
, n
->string
);
1482 p
->flags
&= ~ttypes
[TTYPE_FUNC_ARG
];
1487 if (node
->child
&& node
->next
&& node
->next
->tok
== MDOC_Fa
)
1496 termp_va_pre(DECL_ARGS
)
1499 pair
->flag
|= ttypes
[TTYPE_VAR_DECL
];
1506 termp_bd_pre(DECL_ARGS
)
1511 * This is fairly tricky due primarily to crappy documentation.
1512 * If -ragged or -filled are specified, the block does nothing
1513 * but change the indentation.
1515 * If, on the other hand, -unfilled or -literal are specified,
1516 * then the game changes. Text is printed exactly as entered in
1517 * the display: if a macro line, a newline is appended to the
1518 * line. Blank lines are allowed.
1521 if (MDOC_BLOCK
== node
->type
)
1522 return(fmt_block_vspace(p
, node
, node
));
1523 else if (MDOC_BODY
!= node
->type
)
1526 /* FIXME: display type should be mandated by parser. */
1528 if (NULL
== node
->parent
->args
)
1529 errx(1, "missing display type");
1531 for (type
= -1, i
= 0;
1532 i
< (int)node
->parent
->args
->argc
; i
++) {
1533 switch (node
->parent
->args
->argv
[i
].arg
) {
1538 case (MDOC_Unfilled
):
1540 case (MDOC_Literal
):
1541 type
= node
->parent
->args
->argv
[i
].arg
;
1542 i
= (int)node
->parent
->args
->argc
;
1549 if (NULL
== node
->parent
->args
)
1550 errx(1, "missing display type");
1552 i
= arg_getattr(MDOC_Offset
, node
->parent
);
1554 if (1 != node
->parent
->args
->argv
[i
].sz
)
1555 errx(1, "expected single value");
1556 p
->offset
+= arg_offset(&node
->parent
->args
->argv
[i
]);
1560 case (MDOC_Literal
):
1562 case (MDOC_Unfilled
):
1569 * Tricky. Iterate through all children. If we're on a
1570 * different parse line, append a newline and then the contents.
1574 p
->flags
|= TERMP_LITERAL
;
1575 ln
= node
->child
? node
->child
->line
: 0;
1577 for (node
= node
->child
; node
; node
= node
->next
) {
1578 if (ln
< node
->line
) {
1580 p
->flags
|= TERMP_NOSPACE
;
1583 print_node(p
, pair
, meta
, node
);
1592 termp_bd_post(DECL_ARGS
)
1595 if (MDOC_BODY
!= node
->type
)
1599 p
->flags
&= ~TERMP_LITERAL
;
1600 p
->flags
|= TERMP_NOSPACE
;
1606 termp_qq_pre(DECL_ARGS
)
1609 if (MDOC_BODY
!= node
->type
)
1612 p
->flags
|= TERMP_NOSPACE
;
1619 termp_qq_post(DECL_ARGS
)
1622 if (MDOC_BODY
!= node
->type
)
1624 p
->flags
|= TERMP_NOSPACE
;
1631 termp_bx_post(DECL_ARGS
)
1635 p
->flags
|= TERMP_NOSPACE
;
1636 term_word(p
, "BSD");
1642 termp_xx_pre(DECL_ARGS
)
1647 switch (node
->tok
) {
1652 pp
= "DragonFlyBSD";
1678 termp_sq_pre(DECL_ARGS
)
1681 if (MDOC_BODY
!= node
->type
)
1683 term_word(p
, "\\(oq");
1684 p
->flags
|= TERMP_NOSPACE
;
1691 termp_sq_post(DECL_ARGS
)
1694 if (MDOC_BODY
!= node
->type
)
1696 p
->flags
|= TERMP_NOSPACE
;
1697 term_word(p
, "\\(aq");
1703 termp_pf_pre(DECL_ARGS
)
1706 p
->flags
|= TERMP_IGNDELIM
;
1713 termp_pf_post(DECL_ARGS
)
1716 p
->flags
&= ~TERMP_IGNDELIM
;
1717 p
->flags
|= TERMP_NOSPACE
;
1723 termp_ss_pre(DECL_ARGS
)
1726 switch (node
->type
) {
1733 pair
->flag
|= ttypes
[TTYPE_SSECTION
];
1734 p
->offset
= HALFINDENT
;
1746 termp_ss_post(DECL_ARGS
)
1749 if (MDOC_HEAD
== node
->type
)
1756 termp_pa_pre(DECL_ARGS
)
1759 pair
->flag
|= ttypes
[TTYPE_FILE
];
1766 termp_em_pre(DECL_ARGS
)
1769 pair
->flag
|= ttypes
[TTYPE_EMPH
];
1776 termp_cd_pre(DECL_ARGS
)
1779 pair
->flag
|= ttypes
[TTYPE_CONFIG
];
1787 termp_cm_pre(DECL_ARGS
)
1790 pair
->flag
|= ttypes
[TTYPE_CMD_FLAG
];
1797 termp_ic_pre(DECL_ARGS
)
1800 pair
->flag
|= ttypes
[TTYPE_CMD
];
1807 termp_in_pre(DECL_ARGS
)
1810 pair
->flag
|= ttypes
[TTYPE_INCLUDE
];
1811 p
->flags
|= ttypes
[TTYPE_INCLUDE
];
1813 if (SEC_SYNOPSIS
== node
->sec
)
1814 term_word(p
, "#include");
1817 p
->flags
|= TERMP_NOSPACE
;
1824 termp_in_post(DECL_ARGS
)
1827 p
->flags
|= TERMP_NOSPACE
;
1830 if (SEC_SYNOPSIS
!= node
->sec
)
1835 * XXX Not entirely correct. If `.In foo bar' is specified in
1836 * the SYNOPSIS section, then it produces a single break after
1837 * the <foo>; mandoc asserts a vertical space. Since this
1838 * construction is rarely used, I think it's fine.
1840 if (node
->next
&& MDOC_In
!= node
->next
->tok
)
1847 termp_at_pre(DECL_ARGS
)
1854 att
= mdoc_a2att(node
->child
->string
);
1865 termp_brq_pre(DECL_ARGS
)
1868 if (MDOC_BODY
!= node
->type
)
1870 term_word(p
, "\\(lC");
1871 p
->flags
|= TERMP_NOSPACE
;
1878 termp_brq_post(DECL_ARGS
)
1881 if (MDOC_BODY
!= node
->type
)
1883 p
->flags
|= TERMP_NOSPACE
;
1884 term_word(p
, "\\(rC");
1890 termp_bq_pre(DECL_ARGS
)
1893 if (MDOC_BODY
!= node
->type
)
1895 term_word(p
, "\\(lB");
1896 p
->flags
|= TERMP_NOSPACE
;
1903 termp_bq_post(DECL_ARGS
)
1906 if (MDOC_BODY
!= node
->type
)
1908 p
->flags
|= TERMP_NOSPACE
;
1909 term_word(p
, "\\(rB");
1915 termp_pq_pre(DECL_ARGS
)
1918 if (MDOC_BODY
!= node
->type
)
1920 term_word(p
, "\\&(");
1921 p
->flags
|= TERMP_NOSPACE
;
1928 termp_pq_post(DECL_ARGS
)
1931 if (MDOC_BODY
!= node
->type
)
1939 termp_fo_pre(DECL_ARGS
)
1941 const struct mdoc_node
*n
;
1943 if (MDOC_BODY
== node
->type
) {
1945 p
->flags
|= TERMP_NOSPACE
;
1947 } else if (MDOC_HEAD
!= node
->type
)
1950 /* XXX - groff shows only first parameter */
1952 p
->flags
|= ttypes
[TTYPE_FUNC_NAME
];
1953 for (n
= node
->child
; n
; n
= n
->next
) {
1954 assert(MDOC_TEXT
== n
->type
);
1955 term_word(p
, n
->string
);
1957 p
->flags
&= ~ttypes
[TTYPE_FUNC_NAME
];
1965 termp_fo_post(DECL_ARGS
)
1968 if (MDOC_BODY
!= node
->type
)
1970 p
->flags
|= TERMP_NOSPACE
;
1972 p
->flags
|= TERMP_NOSPACE
;
1980 termp_bf_pre(DECL_ARGS
)
1982 const struct mdoc_node
*n
;
1984 if (MDOC_HEAD
== node
->type
)
1986 else if (MDOC_BLOCK
!= node
->type
)
1989 if (NULL
== (n
= node
->head
->child
)) {
1990 if (arg_hasattr(MDOC_Emphasis
, node
))
1991 pair
->flag
|= ttypes
[TTYPE_EMPH
];
1992 else if (arg_hasattr(MDOC_Symbolic
, node
))
1993 pair
->flag
|= ttypes
[TTYPE_SYMB
];
1998 assert(MDOC_TEXT
== n
->type
);
1999 if (0 == strcmp("Em", n
->string
))
2000 pair
->flag
|= ttypes
[TTYPE_EMPH
];
2001 else if (0 == strcmp("Sy", n
->string
))
2002 pair
->flag
|= ttypes
[TTYPE_SYMB
];
2010 termp_sy_pre(DECL_ARGS
)
2013 pair
->flag
|= ttypes
[TTYPE_SYMB
];
2020 termp_ms_pre(DECL_ARGS
)
2023 pair
->flag
|= ttypes
[TTYPE_SYMBOL
];
2031 termp_sm_pre(DECL_ARGS
)
2034 assert(node
->child
&& MDOC_TEXT
== node
->child
->type
);
2035 if (0 == strcmp("on", node
->child
->string
)) {
2036 p
->flags
&= ~TERMP_NONOSPACE
;
2037 p
->flags
&= ~TERMP_NOSPACE
;
2039 p
->flags
|= TERMP_NONOSPACE
;
2047 termp_ap_pre(DECL_ARGS
)
2050 p
->flags
|= TERMP_NOSPACE
;
2051 term_word(p
, "\\(aq");
2052 p
->flags
|= TERMP_NOSPACE
;
2059 termp__j_pre(DECL_ARGS
)
2062 pair
->flag
|= ttypes
[TTYPE_REF_JOURNAL
];
2069 termp__t_pre(DECL_ARGS
)
2073 p
->flags
|= TERMP_NOSPACE
;
2080 termp__t_post(DECL_ARGS
)
2083 p
->flags
|= TERMP_NOSPACE
;
2085 termp____post(p
, pair
, meta
, node
);
2091 termp____post(DECL_ARGS
)
2094 p
->flags
|= TERMP_NOSPACE
;
2095 term_word(p
, node
->next
? "," : ".");
2101 termp_lk_pre(DECL_ARGS
)
2103 const struct mdoc_node
*n
;
2105 assert(node
->child
);
2108 if (NULL
== n
->next
) {
2109 pair
->flag
|= ttypes
[TTYPE_LINK_ANCHOR
];
2113 p
->flags
|= ttypes
[TTYPE_LINK_ANCHOR
];
2114 term_word(p
, n
->string
);
2115 p
->flags
|= TERMP_NOSPACE
;
2117 p
->flags
&= ~ttypes
[TTYPE_LINK_ANCHOR
];
2119 p
->flags
|= ttypes
[TTYPE_LINK_TEXT
];
2120 for (n
= n
->next
; n
; n
= n
->next
)
2121 term_word(p
, n
->string
);
2123 p
->flags
&= ~ttypes
[TTYPE_LINK_TEXT
];
2130 termp_mt_pre(DECL_ARGS
)
2133 pair
->flag
|= ttypes
[TTYPE_LINK_ANCHOR
];