]>
git.cameronkatri.com Git - mandoc.git/blob - mdoc_term.c
208dc94c4a12b04b60149a2b8b0454db20cb11eb
1 /* $Id: mdoc_term.c,v 1.37 2009/07/12 20:50:08 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_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_pa_pre(DECL_ARGS
);
158 static int termp_pf_pre(DECL_ARGS
);
159 static int termp_pp_pre(DECL_ARGS
);
160 static int termp_pq_pre(DECL_ARGS
);
161 static int termp_qq_pre(DECL_ARGS
);
162 static int termp_rs_pre(DECL_ARGS
);
163 static int termp_rv_pre(DECL_ARGS
);
164 static int termp_sh_pre(DECL_ARGS
);
165 static int termp_sm_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 { termp_pa_pre
, 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 */
298 extern size_t strlcpy(char *, const char *, size_t);
299 extern size_t strlcat(char *, const char *, size_t);
302 static int arg_hasattr(int, const struct mdoc_node
*);
303 static int arg_getattrs(const int *, int *, size_t,
304 const struct mdoc_node
*);
305 static int arg_getattr(int, const struct mdoc_node
*);
306 static size_t arg_offset(const struct mdoc_argv
*);
307 static size_t arg_width(const struct mdoc_argv
*, int);
308 static int arg_listtype(const struct mdoc_node
*);
309 static int fmt_block_vspace(struct termp
*,
310 const struct mdoc_node
*,
311 const struct mdoc_node
*);
312 static void print_node(DECL_ARGS
);
313 static void print_head(struct termp
*,
314 const struct mdoc_meta
*);
315 static void print_body(DECL_ARGS
);
316 static void print_foot(struct termp
*,
317 const struct mdoc_meta
*);
321 mdoc_run(struct termp
*p
, const struct mdoc
*m
)
324 * Main output function. When this is called, assume that the
325 * tree is properly formed.
327 print_head(p
, mdoc_meta(m
));
328 assert(mdoc_node(m
));
329 assert(MDOC_ROOT
== mdoc_node(m
)->type
);
330 if (mdoc_node(m
)->child
)
331 print_body(p
, NULL
, mdoc_meta(m
), mdoc_node(m
)->child
);
332 print_foot(p
, mdoc_meta(m
));
338 print_body(DECL_ARGS
)
341 print_node(p
, pair
, meta
, node
);
344 print_body(p
, pair
, meta
, node
->next
);
349 print_node(DECL_ARGS
)
352 struct termpair npair
;
353 size_t offset
, rmargin
;
357 rmargin
= p
->rmargin
;
363 if (MDOC_TEXT
!= node
->type
) {
364 if (termacts
[node
->tok
].pre
)
365 if ( ! (*termacts
[node
->tok
].pre
)(p
, &npair
, meta
, node
))
367 } else /* MDOC_TEXT == node->type */
368 term_word(p
, node
->string
);
372 p
->flags
|= npair
.flag
;
374 if (dochild
&& node
->child
)
375 print_body(p
, &npair
, meta
, node
->child
);
377 /* Post-processing. */
379 if (MDOC_TEXT
!= node
->type
)
380 if (termacts
[node
->tok
].post
)
381 (*termacts
[node
->tok
].post
)(p
, &npair
, meta
, node
);
384 p
->rmargin
= rmargin
;
385 p
->flags
&= ~npair
.flag
;
390 print_foot(struct termp
*p
, const struct mdoc_meta
*meta
)
396 * Output the footer in new-groff style, that is, three columns
397 * with the middle being the manual date and flanking columns
398 * being the operating system:
403 if (NULL
== (buf
= malloc(p
->rmargin
)))
405 if (NULL
== (os
= malloc(p
->rmargin
)))
408 tm
= localtime(&meta
->date
);
410 if (0 == strftime(buf
, p
->rmargin
, "%B %d, %Y", tm
))
413 (void)strlcpy(os
, meta
->os
, p
->rmargin
);
418 p
->rmargin
= (p
->maxrmargin
- strlen(buf
) + 1) / 2;
419 p
->flags
|= TERMP_NOSPACE
| TERMP_NOBREAK
;
424 p
->offset
= p
->rmargin
;
425 p
->rmargin
= p
->maxrmargin
- strlen(os
);
426 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
431 p
->offset
= p
->rmargin
;
432 p
->rmargin
= p
->maxrmargin
;
433 p
->flags
&= ~TERMP_NOBREAK
;
434 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
440 p
->rmargin
= p
->maxrmargin
;
449 print_head(struct termp
*p
, const struct mdoc_meta
*meta
)
453 p
->rmargin
= p
->maxrmargin
;
456 if (NULL
== (buf
= malloc(p
->rmargin
)))
458 if (NULL
== (title
= malloc(p
->rmargin
)))
462 * The header is strange. It has three components, which are
463 * really two with the first duplicated. It goes like this:
465 * IDENTIFIER TITLE IDENTIFIER
467 * The IDENTIFIER is NAME(SECTION), which is the command-name
468 * (if given, or "unknown" if not) followed by the manual page
469 * section. These are given in `Dt'. The TITLE is a free-form
470 * string depending on the manual volume. If not specified, it
471 * switches on the manual section.
475 (void)strlcpy(buf
, meta
->vol
, p
->rmargin
);
478 (void)strlcat(buf
, " (", p
->rmargin
);
479 (void)strlcat(buf
, meta
->arch
, p
->rmargin
);
480 (void)strlcat(buf
, ")", p
->rmargin
);
483 (void)snprintf(title
, p
->rmargin
, "%s(%d)",
484 meta
->title
, meta
->msec
);
487 p
->rmargin
= (p
->maxrmargin
- strlen(buf
) + 1) / 2;
488 p
->flags
|= TERMP_NOBREAK
| TERMP_NOSPACE
;
493 p
->offset
= p
->rmargin
;
494 p
->rmargin
= p
->maxrmargin
- strlen(title
);
495 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
500 p
->offset
= p
->rmargin
;
501 p
->rmargin
= p
->maxrmargin
;
502 p
->flags
&= ~TERMP_NOBREAK
;
503 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
509 p
->rmargin
= p
->maxrmargin
;
510 p
->flags
&= ~TERMP_NOSPACE
;
518 arg_width(const struct mdoc_argv
*arg
, int pos
)
523 assert(pos
< (int)arg
->sz
&& pos
>= 0);
524 assert(arg
->value
[pos
]);
526 if (0 == (len
= (int)strlen(arg
->value
[pos
])))
529 for (i
= 0; i
< len
- 1; i
++)
530 if ( ! isdigit((u_char
)arg
->value
[pos
][i
]))
534 if ('n' == arg
->value
[pos
][len
- 1] ||
535 'm' == arg
->value
[pos
][len
- 1]) {
536 v
= (size_t)atoi(arg
->value
[pos
]);
541 return(strlen(arg
->value
[pos
]) + 2);
546 arg_listtype(const struct mdoc_node
*n
)
550 assert(MDOC_BLOCK
== n
->type
);
552 len
= (int)(n
->args
? n
->args
->argc
: 0);
554 for (i
= 0; i
< len
; i
++)
555 switch (n
->args
->argv
[i
].arg
) {
575 return(n
->args
->argv
[i
].arg
);
580 /* FIXME: mandated by parser. */
582 errx(1, "list type not supported");
588 arg_offset(const struct mdoc_argv
*arg
)
592 if (0 == strcmp(*arg
->value
, "left"))
594 if (0 == strcmp(*arg
->value
, "indent"))
596 if (0 == strcmp(*arg
->value
, "indent-two"))
597 return((INDENT
+ 1) * 2);
599 /* FIXME: needs to support field-widths (10n, etc.). */
601 return(strlen(*arg
->value
));
606 arg_hasattr(int arg
, const struct mdoc_node
*n
)
609 return(-1 != arg_getattr(arg
, n
));
614 arg_getattr(int v
, const struct mdoc_node
*n
)
618 return(arg_getattrs(&v
, &val
, 1, n
) ? val
: -1);
623 arg_getattrs(const int *keys
, int *vals
,
624 size_t sz
, const struct mdoc_node
*n
)
631 for (k
= i
= 0; i
< (int)n
->args
->argc
; i
++)
632 for (j
= 0; j
< (int)sz
; j
++)
633 if (n
->args
->argv
[i
].arg
== keys
[j
]) {
643 fmt_block_vspace(struct termp
*p
,
644 const struct mdoc_node
*bl
,
645 const struct mdoc_node
*node
)
647 const struct mdoc_node
*n
;
651 if (arg_hasattr(MDOC_Compact
, bl
))
654 for (n
= node
; n
; n
= n
->parent
) {
655 if (MDOC_BLOCK
!= n
->type
)
657 if (MDOC_Ss
== n
->tok
)
659 if (MDOC_Sh
== n
->tok
)
673 termp_dq_pre(DECL_ARGS
)
676 if (MDOC_BODY
!= node
->type
)
679 term_word(p
, "\\(lq");
680 p
->flags
|= TERMP_NOSPACE
;
687 termp_dq_post(DECL_ARGS
)
690 if (MDOC_BODY
!= node
->type
)
693 p
->flags
|= TERMP_NOSPACE
;
694 term_word(p
, "\\(rq");
700 termp_it_pre(DECL_ARGS
)
702 const struct mdoc_node
*bl
, *n
;
704 int i
, type
, keys
[3], vals
[3], sv
;
705 size_t width
, offset
;
707 if (MDOC_BLOCK
== node
->type
)
708 return(fmt_block_vspace(p
, node
->parent
->parent
, node
));
710 bl
= node
->parent
->parent
->parent
;
712 /* Save parent attributes. */
714 pair
->flag
= p
->flags
;
716 /* Get list width and offset. */
718 keys
[0] = MDOC_Width
;
719 keys
[1] = MDOC_Offset
;
720 keys
[2] = MDOC_Column
;
722 vals
[0] = vals
[1] = vals
[2] = -1;
726 (void)arg_getattrs(keys
, vals
, 3, bl
);
728 type
= arg_listtype(bl
);
730 /* Calculate real width and offset. */
734 if (MDOC_BODY
== node
->type
)
736 for (i
= 0, n
= node
->prev
; n
; n
= n
->prev
, i
++)
738 (&bl
->args
->argv
[vals
[2]], i
);
739 assert(i
< (int)bl
->args
->argv
[vals
[2]].sz
);
740 width
= arg_width(&bl
->args
->argv
[vals
[2]], i
);
742 offset
+= arg_offset(&bl
->args
->argv
[vals
[1]]);
746 width
= arg_width(&bl
->args
->argv
[vals
[0]], 0);
748 offset
+= arg_offset(&bl
->args
->argv
[vals
[1]]);
753 * List-type can override the width in the case of fixed-head
754 * values (bullet, dash/hyphen, enum). Tags need a non-zero
780 * Whitespace control. Inset bodies need an initial space,
781 * while diagonal bodies need two.
789 if (MDOC_BODY
== node
->type
)
790 p
->flags
&= ~TERMP_NOSPACE
;
792 p
->flags
|= TERMP_NOSPACE
;
795 p
->flags
|= TERMP_NOSPACE
;
800 * Style flags. Diagnostic heads need TTYPE_DIAG.
805 if (MDOC_HEAD
== node
->type
)
806 p
->flags
|= ttypes
[TTYPE_DIAG
];
813 * Pad and break control. This is the tricker part. Lists with
814 * set right-margins for the head get TERMP_NOBREAK because, if
815 * they overrun the margin, they wrap to the new margin.
816 * Correspondingly, the body for these types don't left-pad, as
817 * the head will pad out to to the right.
830 if (MDOC_HEAD
== node
->type
)
831 p
->flags
|= TERMP_NOBREAK
;
833 p
->flags
|= TERMP_NOLPAD
;
834 if (MDOC_HEAD
== node
->type
&& MDOC_Tag
== type
)
835 if (NULL
== node
->next
||
836 NULL
== node
->next
->child
)
837 p
->flags
|= TERMP_NONOBREAK
;
840 if (MDOC_HEAD
== node
->type
) {
842 if (MDOC_BODY
== node
->next
->type
)
843 p
->flags
&= ~TERMP_NOBREAK
;
845 p
->flags
|= TERMP_NOBREAK
;
847 p
->flags
|= TERMP_NOLPAD
;
851 if (MDOC_HEAD
== node
->type
)
852 p
->flags
|= TERMP_NOBREAK
;
859 * Margin control. Set-head-width lists have their right
860 * margins shortened. The body for these lists has the offset
861 * necessarily lengthened. Everybody gets the offset.
876 if (MDOC_HEAD
== node
->type
)
877 p
->rmargin
= p
->offset
+ width
;
882 p
->rmargin
= p
->offset
+ width
;
889 * The dash, hyphen, bullet and enum lists all have a special
890 * HEAD character (temporarily bold, in some cases).
894 if (MDOC_HEAD
== node
->type
)
897 p
->flags
|= TERMP_BOLD
;
898 term_word(p
, "\\[bu]");
903 p
->flags
|= TERMP_BOLD
;
904 term_word(p
, "\\(hy");
907 (pair
->ppair
->ppair
->count
)++;
908 (void)snprintf(buf
, sizeof(buf
), "%d.",
909 pair
->ppair
->ppair
->count
);
916 p
->flags
= sv
; /* Restore saved flags. */
919 * If we're not going to process our children, indicate so here.
932 if (MDOC_HEAD
== node
->type
)
936 if (MDOC_BODY
== node
->type
)
949 termp_it_post(DECL_ARGS
)
953 if (MDOC_BODY
!= node
->type
&& MDOC_HEAD
!= node
->type
)
956 type
= arg_listtype(node
->parent
->parent
->parent
);
964 if (MDOC_BODY
== node
->type
)
968 if (MDOC_HEAD
== node
->type
)
976 p
->flags
= pair
->flag
;
982 termp_nm_pre(DECL_ARGS
)
985 if (SEC_SYNOPSIS
== node
->sec
)
988 pair
->flag
|= ttypes
[TTYPE_PROG
];
989 p
->flags
|= ttypes
[TTYPE_PROG
];
991 if (NULL
== node
->child
)
992 term_word(p
, meta
->name
);
1000 termp_fl_pre(DECL_ARGS
)
1003 pair
->flag
|= ttypes
[TTYPE_CMD_FLAG
];
1004 p
->flags
|= ttypes
[TTYPE_CMD_FLAG
];
1005 term_word(p
, "\\-");
1006 p
->flags
|= TERMP_NOSPACE
;
1013 termp_ar_pre(DECL_ARGS
)
1016 pair
->flag
|= ttypes
[TTYPE_CMD_ARG
];
1023 termp_ns_pre(DECL_ARGS
)
1026 p
->flags
|= TERMP_NOSPACE
;
1033 termp_pp_pre(DECL_ARGS
)
1043 termp_rs_pre(DECL_ARGS
)
1046 if (MDOC_BLOCK
== node
->type
&& node
->prev
)
1054 termp_rv_pre(DECL_ARGS
)
1058 i
= arg_getattr(MDOC_Std
, node
);
1060 assert(node
->args
->argv
[i
].sz
);
1063 term_word(p
, "The");
1065 p
->flags
|= ttypes
[TTYPE_FUNC_NAME
];
1066 term_word(p
, *node
->args
->argv
[i
].value
);
1067 p
->flags
&= ~ttypes
[TTYPE_FUNC_NAME
];
1068 p
->flags
|= TERMP_NOSPACE
;
1070 term_word(p
, "() function returns the value 0 if successful;");
1071 term_word(p
, "otherwise the value -1 is returned and the");
1072 term_word(p
, "global variable");
1074 p
->flags
|= ttypes
[TTYPE_VAR_DECL
];
1075 term_word(p
, "errno");
1076 p
->flags
&= ~ttypes
[TTYPE_VAR_DECL
];
1078 term_word(p
, "is set to indicate the error.");
1086 termp_ex_pre(DECL_ARGS
)
1090 i
= arg_getattr(MDOC_Std
, node
);
1092 assert(node
->args
->argv
[i
].sz
);
1094 term_word(p
, "The");
1095 p
->flags
|= ttypes
[TTYPE_PROG
];
1096 term_word(p
, *node
->args
->argv
[i
].value
);
1097 p
->flags
&= ~ttypes
[TTYPE_PROG
];
1098 term_word(p
, "utility exits 0 on success, and >0 if an error occurs.");
1106 termp_nd_pre(DECL_ARGS
)
1109 if (MDOC_BODY
!= node
->type
)
1113 * XXX: signed off by jmc@openbsd.org. This technically
1114 * produces a minus sign after the Nd, which is wrong, but is
1115 * consistent with the historic OpenBSD tmac file.
1117 #if defined(__OpenBSD__) || defined(__linux__)
1118 term_word(p
, "\\-");
1120 term_word(p
, "\\(em");
1128 termp_bl_post(DECL_ARGS
)
1131 if (MDOC_BLOCK
== node
->type
)
1138 termp_op_post(DECL_ARGS
)
1141 if (MDOC_BODY
!= node
->type
)
1143 p
->flags
|= TERMP_NOSPACE
;
1144 term_word(p
, "\\(rB");
1150 termp_xr_pre(DECL_ARGS
)
1152 const struct mdoc_node
*n
;
1154 assert(node
->child
&& MDOC_TEXT
== node
->child
->type
);
1157 term_word(p
, n
->string
);
1158 if (NULL
== (n
= n
->next
))
1160 p
->flags
|= TERMP_NOSPACE
;
1162 p
->flags
|= TERMP_NOSPACE
;
1163 term_word(p
, n
->string
);
1164 p
->flags
|= TERMP_NOSPACE
;
1172 termp_vt_pre(DECL_ARGS
)
1175 /* FIXME: this can be "type name". */
1176 pair
->flag
|= ttypes
[TTYPE_VAR_DECL
];
1183 termp_vt_post(DECL_ARGS
)
1186 if (node
->sec
!= SEC_SYNOPSIS
)
1188 if (node
->next
&& MDOC_Vt
== node
->next
->tok
)
1190 else if (node
->next
)
1197 termp_fd_pre(DECL_ARGS
)
1200 pair
->flag
|= ttypes
[TTYPE_FUNC_DECL
];
1207 termp_fd_post(DECL_ARGS
)
1210 if (node
->sec
!= SEC_SYNOPSIS
)
1214 if (node
->next
&& MDOC_Fd
!= node
->next
->tok
)
1221 termp_sh_pre(DECL_ARGS
)
1224 switch (node
->type
) {
1227 pair
->flag
|= ttypes
[TTYPE_SECTION
];
1241 termp_sh_post(DECL_ARGS
)
1244 switch (node
->type
) {
1260 termp_op_pre(DECL_ARGS
)
1263 switch (node
->type
) {
1265 term_word(p
, "\\(lB");
1266 p
->flags
|= TERMP_NOSPACE
;
1277 termp_bt_pre(DECL_ARGS
)
1280 term_word(p
, "is currently in beta test.");
1287 termp_lb_post(DECL_ARGS
)
1296 termp_ud_pre(DECL_ARGS
)
1299 term_word(p
, "currently under development.");
1306 termp_d1_pre(DECL_ARGS
)
1309 if (MDOC_BLOCK
!= node
->type
)
1312 p
->offset
+= (INDENT
+ 1);
1319 termp_d1_post(DECL_ARGS
)
1322 if (MDOC_BLOCK
!= node
->type
)
1330 termp_aq_pre(DECL_ARGS
)
1333 if (MDOC_BODY
!= node
->type
)
1335 term_word(p
, "\\(la");
1336 p
->flags
|= TERMP_NOSPACE
;
1343 termp_aq_post(DECL_ARGS
)
1346 if (MDOC_BODY
!= node
->type
)
1348 p
->flags
|= TERMP_NOSPACE
;
1349 term_word(p
, "\\(ra");
1355 termp_ft_pre(DECL_ARGS
)
1358 if (SEC_SYNOPSIS
== node
->sec
)
1359 if (node
->prev
&& MDOC_Fo
== node
->prev
->tok
)
1361 pair
->flag
|= ttypes
[TTYPE_FUNC_TYPE
];
1368 termp_ft_post(DECL_ARGS
)
1371 if (SEC_SYNOPSIS
== node
->sec
)
1378 termp_fn_pre(DECL_ARGS
)
1380 const struct mdoc_node
*n
;
1382 assert(node
->child
&& MDOC_TEXT
== node
->child
->type
);
1384 /* FIXME: can be "type funcname" "type varname"... */
1386 p
->flags
|= ttypes
[TTYPE_FUNC_NAME
];
1387 term_word(p
, node
->child
->string
);
1388 p
->flags
&= ~ttypes
[TTYPE_FUNC_NAME
];
1390 p
->flags
|= TERMP_NOSPACE
;
1393 for (n
= node
->child
->next
; n
; n
= n
->next
) {
1394 p
->flags
|= ttypes
[TTYPE_FUNC_ARG
];
1395 term_word(p
, n
->string
);
1396 p
->flags
&= ~ttypes
[TTYPE_FUNC_ARG
];
1403 if (SEC_SYNOPSIS
== node
->sec
)
1412 termp_fn_post(DECL_ARGS
)
1415 if (node
->sec
== SEC_SYNOPSIS
&& node
->next
)
1422 termp_sx_pre(DECL_ARGS
)
1425 pair
->flag
|= ttypes
[TTYPE_LINK
];
1432 termp_fa_pre(DECL_ARGS
)
1434 struct mdoc_node
*n
;
1436 if (node
->parent
->tok
!= MDOC_Fo
) {
1437 pair
->flag
|= ttypes
[TTYPE_FUNC_ARG
];
1441 for (n
= node
->child
; n
; n
= n
->next
) {
1442 p
->flags
|= ttypes
[TTYPE_FUNC_ARG
];
1443 term_word(p
, n
->string
);
1444 p
->flags
&= ~ttypes
[TTYPE_FUNC_ARG
];
1449 if (node
->child
&& node
->next
&& node
->next
->tok
== MDOC_Fa
)
1458 termp_va_pre(DECL_ARGS
)
1461 pair
->flag
|= ttypes
[TTYPE_VAR_DECL
];
1468 termp_bd_pre(DECL_ARGS
)
1473 * This is fairly tricky due primarily to crappy documentation.
1474 * If -ragged or -filled are specified, the block does nothing
1475 * but change the indentation.
1477 * If, on the other hand, -unfilled or -literal are specified,
1478 * then the game changes. Text is printed exactly as entered in
1479 * the display: if a macro line, a newline is appended to the
1480 * line. Blank lines are allowed.
1483 if (MDOC_BLOCK
== node
->type
)
1484 return(fmt_block_vspace(p
, node
, node
));
1485 else if (MDOC_BODY
!= node
->type
)
1488 /* FIXME: display type should be mandated by parser. */
1490 if (NULL
== node
->parent
->args
)
1491 errx(1, "missing display type");
1493 for (type
= -1, i
= 0;
1494 i
< (int)node
->parent
->args
->argc
; i
++) {
1495 switch (node
->parent
->args
->argv
[i
].arg
) {
1500 case (MDOC_Unfilled
):
1502 case (MDOC_Literal
):
1503 type
= node
->parent
->args
->argv
[i
].arg
;
1504 i
= (int)node
->parent
->args
->argc
;
1511 if (NULL
== node
->parent
->args
)
1512 errx(1, "missing display type");
1514 i
= arg_getattr(MDOC_Offset
, node
->parent
);
1516 if (1 != node
->parent
->args
->argv
[i
].sz
)
1517 errx(1, "expected single value");
1518 p
->offset
+= arg_offset(&node
->parent
->args
->argv
[i
]);
1522 case (MDOC_Literal
):
1524 case (MDOC_Unfilled
):
1531 * Tricky. Iterate through all children. If we're on a
1532 * different parse line, append a newline and then the contents.
1536 p
->flags
|= TERMP_LITERAL
;
1537 ln
= node
->child
? node
->child
->line
: 0;
1539 for (node
= node
->child
; node
; node
= node
->next
) {
1540 if (ln
< node
->line
) {
1542 p
->flags
|= TERMP_NOSPACE
;
1545 print_node(p
, pair
, meta
, node
);
1554 termp_bd_post(DECL_ARGS
)
1557 if (MDOC_BODY
!= node
->type
)
1561 p
->flags
&= ~TERMP_LITERAL
;
1562 p
->flags
|= TERMP_NOSPACE
;
1568 termp_qq_pre(DECL_ARGS
)
1571 if (MDOC_BODY
!= node
->type
)
1574 p
->flags
|= TERMP_NOSPACE
;
1581 termp_qq_post(DECL_ARGS
)
1584 if (MDOC_BODY
!= node
->type
)
1586 p
->flags
|= TERMP_NOSPACE
;
1593 termp_bx_post(DECL_ARGS
)
1597 p
->flags
|= TERMP_NOSPACE
;
1598 term_word(p
, "BSD");
1604 termp_xx_pre(DECL_ARGS
)
1609 switch (node
->tok
) {
1614 pp
= "DragonFlyBSD";
1640 termp_sq_pre(DECL_ARGS
)
1643 if (MDOC_BODY
!= node
->type
)
1645 term_word(p
, "\\(oq");
1646 p
->flags
|= TERMP_NOSPACE
;
1653 termp_sq_post(DECL_ARGS
)
1656 if (MDOC_BODY
!= node
->type
)
1658 p
->flags
|= TERMP_NOSPACE
;
1659 term_word(p
, "\\(aq");
1665 termp_pf_pre(DECL_ARGS
)
1668 p
->flags
|= TERMP_IGNDELIM
;
1675 termp_pf_post(DECL_ARGS
)
1678 p
->flags
&= ~TERMP_IGNDELIM
;
1679 p
->flags
|= TERMP_NOSPACE
;
1685 termp_ss_pre(DECL_ARGS
)
1688 switch (node
->type
) {
1695 pair
->flag
|= ttypes
[TTYPE_SSECTION
];
1696 p
->offset
= HALFINDENT
;
1708 termp_ss_post(DECL_ARGS
)
1711 if (MDOC_HEAD
== node
->type
)
1718 termp_pa_pre(DECL_ARGS
)
1721 pair
->flag
|= ttypes
[TTYPE_FILE
];
1728 termp_em_pre(DECL_ARGS
)
1731 pair
->flag
|= ttypes
[TTYPE_EMPH
];
1738 termp_cd_pre(DECL_ARGS
)
1741 pair
->flag
|= ttypes
[TTYPE_CONFIG
];
1749 termp_cm_pre(DECL_ARGS
)
1752 pair
->flag
|= ttypes
[TTYPE_CMD_FLAG
];
1759 termp_ic_pre(DECL_ARGS
)
1762 pair
->flag
|= ttypes
[TTYPE_CMD
];
1769 termp_in_pre(DECL_ARGS
)
1772 pair
->flag
|= ttypes
[TTYPE_INCLUDE
];
1773 p
->flags
|= ttypes
[TTYPE_INCLUDE
];
1775 if (SEC_SYNOPSIS
== node
->sec
)
1776 term_word(p
, "#include");
1779 p
->flags
|= TERMP_NOSPACE
;
1786 termp_in_post(DECL_ARGS
)
1789 p
->flags
|= TERMP_NOSPACE
;
1792 if (SEC_SYNOPSIS
!= node
->sec
)
1797 * XXX Not entirely correct. If `.In foo bar' is specified in
1798 * the SYNOPSIS section, then it produces a single break after
1799 * the <foo>; mandoc asserts a vertical space. Since this
1800 * construction is rarely used, I think it's fine.
1802 if (node
->next
&& MDOC_In
!= node
->next
->tok
)
1809 termp_brq_pre(DECL_ARGS
)
1812 if (MDOC_BODY
!= node
->type
)
1814 term_word(p
, "\\(lC");
1815 p
->flags
|= TERMP_NOSPACE
;
1822 termp_brq_post(DECL_ARGS
)
1825 if (MDOC_BODY
!= node
->type
)
1827 p
->flags
|= TERMP_NOSPACE
;
1828 term_word(p
, "\\(rC");
1834 termp_bq_pre(DECL_ARGS
)
1837 if (MDOC_BODY
!= node
->type
)
1839 term_word(p
, "\\(lB");
1840 p
->flags
|= TERMP_NOSPACE
;
1847 termp_bq_post(DECL_ARGS
)
1850 if (MDOC_BODY
!= node
->type
)
1852 p
->flags
|= TERMP_NOSPACE
;
1853 term_word(p
, "\\(rB");
1859 termp_pq_pre(DECL_ARGS
)
1862 if (MDOC_BODY
!= node
->type
)
1864 term_word(p
, "\\&(");
1865 p
->flags
|= TERMP_NOSPACE
;
1872 termp_pq_post(DECL_ARGS
)
1875 if (MDOC_BODY
!= node
->type
)
1883 termp_fo_pre(DECL_ARGS
)
1885 const struct mdoc_node
*n
;
1887 if (MDOC_BODY
== node
->type
) {
1888 p
->flags
|= TERMP_NOSPACE
;
1890 p
->flags
|= TERMP_NOSPACE
;
1892 } else if (MDOC_HEAD
!= node
->type
)
1895 p
->flags
|= ttypes
[TTYPE_FUNC_NAME
];
1896 for (n
= node
->child
; n
; n
= n
->next
) {
1897 assert(MDOC_TEXT
== n
->type
);
1898 term_word(p
, n
->string
);
1900 p
->flags
&= ~ttypes
[TTYPE_FUNC_NAME
];
1908 termp_fo_post(DECL_ARGS
)
1911 if (MDOC_BODY
!= node
->type
)
1913 p
->flags
|= TERMP_NOSPACE
;
1915 p
->flags
|= TERMP_NOSPACE
;
1923 termp_bf_pre(DECL_ARGS
)
1925 const struct mdoc_node
*n
;
1927 if (MDOC_HEAD
== node
->type
)
1929 else if (MDOC_BLOCK
!= node
->type
)
1932 if (NULL
== (n
= node
->head
->child
)) {
1933 if (arg_hasattr(MDOC_Emphasis
, node
))
1934 pair
->flag
|= ttypes
[TTYPE_EMPH
];
1935 else if (arg_hasattr(MDOC_Symbolic
, node
))
1936 pair
->flag
|= ttypes
[TTYPE_SYMB
];
1941 assert(MDOC_TEXT
== n
->type
);
1942 if (0 == strcmp("Em", n
->string
))
1943 pair
->flag
|= ttypes
[TTYPE_EMPH
];
1944 else if (0 == strcmp("Sy", n
->string
))
1945 pair
->flag
|= ttypes
[TTYPE_SYMB
];
1953 termp_sy_pre(DECL_ARGS
)
1956 pair
->flag
|= ttypes
[TTYPE_SYMB
];
1963 termp_ms_pre(DECL_ARGS
)
1966 pair
->flag
|= ttypes
[TTYPE_SYMBOL
];
1974 termp_sm_pre(DECL_ARGS
)
1977 assert(node
->child
&& MDOC_TEXT
== node
->child
->type
);
1978 if (0 == strcmp("on", node
->child
->string
)) {
1979 p
->flags
&= ~TERMP_NONOSPACE
;
1980 p
->flags
&= ~TERMP_NOSPACE
;
1982 p
->flags
|= TERMP_NONOSPACE
;
1990 termp_ap_pre(DECL_ARGS
)
1993 p
->flags
|= TERMP_NOSPACE
;
1994 term_word(p
, "\\(aq");
1995 p
->flags
|= TERMP_NOSPACE
;
2002 termp__j_pre(DECL_ARGS
)
2005 pair
->flag
|= ttypes
[TTYPE_REF_JOURNAL
];
2012 termp__t_pre(DECL_ARGS
)
2016 p
->flags
|= TERMP_NOSPACE
;
2023 termp__t_post(DECL_ARGS
)
2026 p
->flags
|= TERMP_NOSPACE
;
2028 termp____post(p
, pair
, meta
, node
);
2034 termp____post(DECL_ARGS
)
2037 p
->flags
|= TERMP_NOSPACE
;
2038 term_word(p
, node
->next
? "," : ".");
2044 termp_lk_pre(DECL_ARGS
)
2046 const struct mdoc_node
*n
;
2048 assert(node
->child
);
2051 if (NULL
== n
->next
) {
2052 pair
->flag
|= ttypes
[TTYPE_LINK_ANCHOR
];
2056 p
->flags
|= ttypes
[TTYPE_LINK_ANCHOR
];
2057 term_word(p
, n
->string
);
2058 p
->flags
|= TERMP_NOSPACE
;
2060 p
->flags
&= ~ttypes
[TTYPE_LINK_ANCHOR
];
2062 p
->flags
|= ttypes
[TTYPE_LINK_TEXT
];
2063 for (n
= n
->next
; n
; n
= n
->next
)
2064 term_word(p
, n
->string
);
2066 p
->flags
&= ~ttypes
[TTYPE_LINK_TEXT
];
2073 termp_mt_pre(DECL_ARGS
)
2076 pair
->flag
|= ttypes
[TTYPE_LINK_ANCHOR
];