]>
git.cameronkatri.com Git - mandoc.git/blob - mdoc_term.c
122e353470f43cd5637f53e3998c44425bf3c243
1 /* $Id: mdoc_term.c,v 1.36 2009/07/12 20:30:35 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_sx_pre(DECL_ARGS
);
170 static int termp_sy_pre(DECL_ARGS
);
171 static int termp_ud_pre(DECL_ARGS
);
172 static int termp_va_pre(DECL_ARGS
);
173 static int termp_vt_pre(DECL_ARGS
);
174 static int termp_xr_pre(DECL_ARGS
);
175 static int termp_xx_pre(DECL_ARGS
);
177 static const struct termact termacts
[MDOC_MAX
] = {
178 { termp_ap_pre
, NULL
}, /* Ap */
179 { NULL
, NULL
}, /* Dd */
180 { NULL
, NULL
}, /* Dt */
181 { NULL
, NULL
}, /* Os */
182 { termp_sh_pre
, termp_sh_post
}, /* Sh */
183 { termp_ss_pre
, termp_ss_post
}, /* Ss */
184 { termp_pp_pre
, NULL
}, /* Pp */
185 { termp_d1_pre
, termp_d1_post
}, /* D1 */
186 { termp_d1_pre
, termp_d1_post
}, /* Dl */
187 { termp_bd_pre
, termp_bd_post
}, /* Bd */
188 { NULL
, NULL
}, /* Ed */
189 { NULL
, termp_bl_post
}, /* Bl */
190 { NULL
, NULL
}, /* El */
191 { termp_it_pre
, termp_it_post
}, /* It */
192 { NULL
, NULL
}, /* Ad */
193 { NULL
, NULL
}, /* An */
194 { termp_ar_pre
, NULL
}, /* Ar */
195 { termp_cd_pre
, NULL
}, /* Cd */
196 { termp_cm_pre
, NULL
}, /* Cm */
197 { NULL
, NULL
}, /* Dv */
198 { NULL
, NULL
}, /* Er */
199 { NULL
, NULL
}, /* Ev */
200 { termp_ex_pre
, NULL
}, /* Ex */
201 { termp_fa_pre
, NULL
}, /* Fa */
202 { termp_fd_pre
, termp_fd_post
}, /* Fd */
203 { termp_fl_pre
, NULL
}, /* Fl */
204 { termp_fn_pre
, termp_fn_post
}, /* Fn */
205 { termp_ft_pre
, termp_ft_post
}, /* Ft */
206 { termp_ic_pre
, NULL
}, /* Ic */
207 { termp_in_pre
, termp_in_post
}, /* In */
208 { NULL
, NULL
}, /* Li */
209 { termp_nd_pre
, NULL
}, /* Nd */
210 { termp_nm_pre
, NULL
}, /* Nm */
211 { termp_op_pre
, termp_op_post
}, /* Op */
212 { NULL
, NULL
}, /* Ot */
213 { termp_pa_pre
, NULL
}, /* Pa */
214 { termp_rv_pre
, NULL
}, /* Rv */
215 { NULL
, NULL
}, /* St */
216 { termp_va_pre
, NULL
}, /* Va */
217 { termp_vt_pre
, termp_vt_post
}, /* Vt */
218 { termp_xr_pre
, NULL
}, /* Xr */
219 { NULL
, termp____post
}, /* %A */
220 { NULL
, termp____post
}, /* %B */
221 { NULL
, termp____post
}, /* %D */
222 { NULL
, termp____post
}, /* %I */
223 { termp__j_pre
, termp____post
}, /* %J */
224 { NULL
, termp____post
}, /* %N */
225 { NULL
, termp____post
}, /* %O */
226 { NULL
, termp____post
}, /* %P */
227 { NULL
, termp____post
}, /* %R */
228 { termp__t_pre
, termp__t_post
}, /* %T */
229 { NULL
, termp____post
}, /* %V */
230 { NULL
, NULL
}, /* Ac */
231 { termp_aq_pre
, termp_aq_post
}, /* Ao */
232 { termp_aq_pre
, termp_aq_post
}, /* Aq */
233 { NULL
, NULL
}, /* At */
234 { NULL
, NULL
}, /* Bc */
235 { termp_bf_pre
, NULL
}, /* Bf */
236 { termp_bq_pre
, termp_bq_post
}, /* Bo */
237 { termp_bq_pre
, termp_bq_post
}, /* Bq */
238 { termp_xx_pre
, NULL
}, /* Bsx */
239 { NULL
, termp_bx_post
}, /* Bx */
240 { NULL
, NULL
}, /* Db */
241 { NULL
, NULL
}, /* Dc */
242 { termp_dq_pre
, termp_dq_post
}, /* Do */
243 { termp_dq_pre
, termp_dq_post
}, /* Dq */
244 { NULL
, NULL
}, /* Ec */
245 { NULL
, NULL
}, /* Ef */
246 { termp_em_pre
, NULL
}, /* Em */
247 { NULL
, NULL
}, /* Eo */
248 { termp_xx_pre
, NULL
}, /* Fx */
249 { termp_ms_pre
, NULL
}, /* Ms */
250 { NULL
, NULL
}, /* No */
251 { termp_ns_pre
, NULL
}, /* Ns */
252 { termp_xx_pre
, NULL
}, /* Nx */
253 { termp_xx_pre
, NULL
}, /* Ox */
254 { NULL
, NULL
}, /* Pc */
255 { termp_pf_pre
, termp_pf_post
}, /* Pf */
256 { termp_pq_pre
, termp_pq_post
}, /* Po */
257 { termp_pq_pre
, termp_pq_post
}, /* Pq */
258 { NULL
, NULL
}, /* Qc */
259 { termp_sq_pre
, termp_sq_post
}, /* Ql */
260 { termp_qq_pre
, termp_qq_post
}, /* Qo */
261 { termp_qq_pre
, termp_qq_post
}, /* Qq */
262 { NULL
, NULL
}, /* Re */
263 { termp_rs_pre
, NULL
}, /* Rs */
264 { NULL
, NULL
}, /* Sc */
265 { termp_sq_pre
, termp_sq_post
}, /* So */
266 { termp_sq_pre
, termp_sq_post
}, /* Sq */
267 { termp_sm_pre
, NULL
}, /* Sm */
268 { termp_sx_pre
, NULL
}, /* Sx */
269 { termp_sy_pre
, NULL
}, /* Sy */
270 { NULL
, NULL
}, /* Tn */
271 { termp_xx_pre
, NULL
}, /* Ux */
272 { NULL
, NULL
}, /* Xc */
273 { NULL
, NULL
}, /* Xo */
274 { termp_fo_pre
, termp_fo_post
}, /* Fo */
275 { NULL
, NULL
}, /* Fc */
276 { termp_op_pre
, termp_op_post
}, /* Oo */
277 { NULL
, NULL
}, /* Oc */
278 { NULL
, NULL
}, /* Bk */
279 { NULL
, NULL
}, /* Ek */
280 { termp_bt_pre
, NULL
}, /* Bt */
281 { NULL
, NULL
}, /* Hf */
282 { NULL
, NULL
}, /* Fr */
283 { termp_ud_pre
, NULL
}, /* Ud */
284 { termp_lb_pre
, termp_lb_post
}, /* Lb */
285 { termp_pp_pre
, NULL
}, /* Lp */
286 { termp_lk_pre
, NULL
}, /* Lk */
287 { termp_mt_pre
, NULL
}, /* Mt */
288 { termp_brq_pre
, termp_brq_post
}, /* Brq */
289 { termp_brq_pre
, termp_brq_post
}, /* Bro */
290 { NULL
, NULL
}, /* Brc */
291 { NULL
, NULL
}, /* %C */
292 { NULL
, NULL
}, /* Es */
293 { NULL
, NULL
}, /* En */
294 { termp_xx_pre
, NULL
}, /* Dx */
295 { NULL
, NULL
}, /* %Q */
299 extern size_t strlcpy(char *, const char *, size_t);
300 extern size_t strlcat(char *, const char *, size_t);
303 static int arg_hasattr(int, const struct mdoc_node
*);
304 static int arg_getattrs(const int *, int *, size_t,
305 const struct mdoc_node
*);
306 static int arg_getattr(int, const struct mdoc_node
*);
307 static size_t arg_offset(const struct mdoc_argv
*);
308 static size_t arg_width(const struct mdoc_argv
*, int);
309 static int arg_listtype(const struct mdoc_node
*);
310 static int fmt_block_vspace(struct termp
*,
311 const struct mdoc_node
*,
312 const struct mdoc_node
*);
313 static void print_node(DECL_ARGS
);
314 static void print_head(struct termp
*,
315 const struct mdoc_meta
*);
316 static void print_body(DECL_ARGS
);
317 static void print_foot(struct termp
*,
318 const struct mdoc_meta
*);
322 mdoc_run(struct termp
*p
, const struct mdoc
*m
)
325 * Main output function. When this is called, assume that the
326 * tree is properly formed.
328 print_head(p
, mdoc_meta(m
));
329 assert(mdoc_node(m
));
330 assert(MDOC_ROOT
== mdoc_node(m
)->type
);
331 if (mdoc_node(m
)->child
)
332 print_body(p
, NULL
, mdoc_meta(m
), mdoc_node(m
)->child
);
333 print_foot(p
, mdoc_meta(m
));
339 print_body(DECL_ARGS
)
342 print_node(p
, pair
, meta
, node
);
345 print_body(p
, pair
, meta
, node
->next
);
350 print_node(DECL_ARGS
)
353 struct termpair npair
;
354 size_t offset
, rmargin
;
358 rmargin
= p
->rmargin
;
364 if (MDOC_TEXT
!= node
->type
) {
365 if (termacts
[node
->tok
].pre
)
366 if ( ! (*termacts
[node
->tok
].pre
)(p
, &npair
, meta
, node
))
368 } else /* MDOC_TEXT == node->type */
369 term_word(p
, node
->string
);
373 p
->flags
|= npair
.flag
;
375 if (dochild
&& node
->child
)
376 print_body(p
, &npair
, meta
, node
->child
);
378 /* Post-processing. */
380 if (MDOC_TEXT
!= node
->type
)
381 if (termacts
[node
->tok
].post
)
382 (*termacts
[node
->tok
].post
)(p
, &npair
, meta
, node
);
385 p
->rmargin
= rmargin
;
386 p
->flags
&= ~npair
.flag
;
391 print_foot(struct termp
*p
, const struct mdoc_meta
*meta
)
397 * Output the footer in new-groff style, that is, three columns
398 * with the middle being the manual date and flanking columns
399 * being the operating system:
404 if (NULL
== (buf
= malloc(p
->rmargin
)))
406 if (NULL
== (os
= malloc(p
->rmargin
)))
409 tm
= localtime(&meta
->date
);
411 if (0 == strftime(buf
, p
->rmargin
, "%B %d, %Y", tm
))
414 (void)strlcpy(os
, meta
->os
, p
->rmargin
);
419 p
->rmargin
= (p
->maxrmargin
- strlen(buf
) + 1) / 2;
420 p
->flags
|= TERMP_NOSPACE
| TERMP_NOBREAK
;
425 p
->offset
= p
->rmargin
;
426 p
->rmargin
= p
->maxrmargin
- strlen(os
);
427 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
432 p
->offset
= p
->rmargin
;
433 p
->rmargin
= p
->maxrmargin
;
434 p
->flags
&= ~TERMP_NOBREAK
;
435 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
441 p
->rmargin
= p
->maxrmargin
;
450 print_head(struct termp
*p
, const struct mdoc_meta
*meta
)
454 p
->rmargin
= p
->maxrmargin
;
457 if (NULL
== (buf
= malloc(p
->rmargin
)))
459 if (NULL
== (title
= malloc(p
->rmargin
)))
463 * The header is strange. It has three components, which are
464 * really two with the first duplicated. It goes like this:
466 * IDENTIFIER TITLE IDENTIFIER
468 * The IDENTIFIER is NAME(SECTION), which is the command-name
469 * (if given, or "unknown" if not) followed by the manual page
470 * section. These are given in `Dt'. The TITLE is a free-form
471 * string depending on the manual volume. If not specified, it
472 * switches on the manual section.
476 (void)strlcpy(buf
, meta
->vol
, p
->rmargin
);
479 (void)strlcat(buf
, " (", p
->rmargin
);
480 (void)strlcat(buf
, meta
->arch
, p
->rmargin
);
481 (void)strlcat(buf
, ")", p
->rmargin
);
484 (void)snprintf(title
, p
->rmargin
, "%s(%d)",
485 meta
->title
, meta
->msec
);
488 p
->rmargin
= (p
->maxrmargin
- strlen(buf
) + 1) / 2;
489 p
->flags
|= TERMP_NOBREAK
| TERMP_NOSPACE
;
494 p
->offset
= p
->rmargin
;
495 p
->rmargin
= p
->maxrmargin
- strlen(title
);
496 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
501 p
->offset
= p
->rmargin
;
502 p
->rmargin
= p
->maxrmargin
;
503 p
->flags
&= ~TERMP_NOBREAK
;
504 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
510 p
->rmargin
= p
->maxrmargin
;
511 p
->flags
&= ~TERMP_NOSPACE
;
519 arg_width(const struct mdoc_argv
*arg
, int pos
)
524 assert(pos
< (int)arg
->sz
&& pos
>= 0);
525 assert(arg
->value
[pos
]);
527 if (0 == (len
= (int)strlen(arg
->value
[pos
])))
530 for (i
= 0; i
< len
- 1; i
++)
531 if ( ! isdigit((u_char
)arg
->value
[pos
][i
]))
535 if ('n' == arg
->value
[pos
][len
- 1] ||
536 'm' == arg
->value
[pos
][len
- 1]) {
537 v
= (size_t)atoi(arg
->value
[pos
]);
542 return(strlen(arg
->value
[pos
]) + 2);
547 arg_listtype(const struct mdoc_node
*n
)
551 assert(MDOC_BLOCK
== n
->type
);
553 len
= (int)(n
->args
? n
->args
->argc
: 0);
555 for (i
= 0; i
< len
; i
++)
556 switch (n
->args
->argv
[i
].arg
) {
576 return(n
->args
->argv
[i
].arg
);
581 /* FIXME: mandated by parser. */
583 errx(1, "list type not supported");
589 arg_offset(const struct mdoc_argv
*arg
)
593 if (0 == strcmp(*arg
->value
, "left"))
595 if (0 == strcmp(*arg
->value
, "indent"))
597 if (0 == strcmp(*arg
->value
, "indent-two"))
598 return((INDENT
+ 1) * 2);
600 /* FIXME: needs to support field-widths (10n, etc.). */
602 return(strlen(*arg
->value
));
607 arg_hasattr(int arg
, const struct mdoc_node
*n
)
610 return(-1 != arg_getattr(arg
, n
));
615 arg_getattr(int v
, const struct mdoc_node
*n
)
619 return(arg_getattrs(&v
, &val
, 1, n
) ? val
: -1);
624 arg_getattrs(const int *keys
, int *vals
,
625 size_t sz
, const struct mdoc_node
*n
)
632 for (k
= i
= 0; i
< (int)n
->args
->argc
; i
++)
633 for (j
= 0; j
< (int)sz
; j
++)
634 if (n
->args
->argv
[i
].arg
== keys
[j
]) {
644 fmt_block_vspace(struct termp
*p
,
645 const struct mdoc_node
*bl
,
646 const struct mdoc_node
*node
)
648 const struct mdoc_node
*n
;
652 if (arg_hasattr(MDOC_Compact
, bl
))
655 for (n
= node
; n
; n
= n
->parent
) {
656 if (MDOC_BLOCK
!= n
->type
)
658 if (MDOC_Ss
== n
->tok
)
660 if (MDOC_Sh
== n
->tok
)
674 termp_dq_pre(DECL_ARGS
)
677 if (MDOC_BODY
!= node
->type
)
680 term_word(p
, "\\(lq");
681 p
->flags
|= TERMP_NOSPACE
;
688 termp_dq_post(DECL_ARGS
)
691 if (MDOC_BODY
!= node
->type
)
694 p
->flags
|= TERMP_NOSPACE
;
695 term_word(p
, "\\(rq");
701 termp_it_pre(DECL_ARGS
)
703 const struct mdoc_node
*bl
, *n
;
705 int i
, type
, keys
[3], vals
[3], sv
;
706 size_t width
, offset
;
708 if (MDOC_BLOCK
== node
->type
)
709 return(fmt_block_vspace(p
, node
->parent
->parent
, node
));
711 bl
= node
->parent
->parent
->parent
;
713 /* Save parent attributes. */
715 pair
->flag
= p
->flags
;
717 /* Get list width and offset. */
719 keys
[0] = MDOC_Width
;
720 keys
[1] = MDOC_Offset
;
721 keys
[2] = MDOC_Column
;
723 vals
[0] = vals
[1] = vals
[2] = -1;
727 (void)arg_getattrs(keys
, vals
, 3, bl
);
729 type
= arg_listtype(bl
);
731 /* Calculate real width and offset. */
735 if (MDOC_BODY
== node
->type
)
737 for (i
= 0, n
= node
->prev
; n
; n
= n
->prev
, i
++)
739 (&bl
->args
->argv
[vals
[2]], i
);
740 assert(i
< (int)bl
->args
->argv
[vals
[2]].sz
);
741 width
= arg_width(&bl
->args
->argv
[vals
[2]], i
);
743 offset
+= arg_offset(&bl
->args
->argv
[vals
[1]]);
747 width
= arg_width(&bl
->args
->argv
[vals
[0]], 0);
749 offset
+= arg_offset(&bl
->args
->argv
[vals
[1]]);
754 * List-type can override the width in the case of fixed-head
755 * values (bullet, dash/hyphen, enum). Tags need a non-zero
781 * Whitespace control. Inset bodies need an initial space,
782 * while diagonal bodies need two.
790 if (MDOC_BODY
== node
->type
)
791 p
->flags
&= ~TERMP_NOSPACE
;
793 p
->flags
|= TERMP_NOSPACE
;
796 p
->flags
|= TERMP_NOSPACE
;
801 * Style flags. Diagnostic heads need TTYPE_DIAG.
806 if (MDOC_HEAD
== node
->type
)
807 p
->flags
|= ttypes
[TTYPE_DIAG
];
814 * Pad and break control. This is the tricker part. Lists with
815 * set right-margins for the head get TERMP_NOBREAK because, if
816 * they overrun the margin, they wrap to the new margin.
817 * Correspondingly, the body for these types don't left-pad, as
818 * the head will pad out to to the right.
831 if (MDOC_HEAD
== node
->type
)
832 p
->flags
|= TERMP_NOBREAK
;
834 p
->flags
|= TERMP_NOLPAD
;
835 if (MDOC_HEAD
== node
->type
&& MDOC_Tag
== type
)
836 if (NULL
== node
->next
||
837 NULL
== node
->next
->child
)
838 p
->flags
|= TERMP_NONOBREAK
;
841 if (MDOC_HEAD
== node
->type
) {
843 if (MDOC_BODY
== node
->next
->type
)
844 p
->flags
&= ~TERMP_NOBREAK
;
846 p
->flags
|= TERMP_NOBREAK
;
848 p
->flags
|= TERMP_NOLPAD
;
852 if (MDOC_HEAD
== node
->type
)
853 p
->flags
|= TERMP_NOBREAK
;
860 * Margin control. Set-head-width lists have their right
861 * margins shortened. The body for these lists has the offset
862 * necessarily lengthened. Everybody gets the offset.
877 if (MDOC_HEAD
== node
->type
)
878 p
->rmargin
= p
->offset
+ width
;
883 p
->rmargin
= p
->offset
+ width
;
890 * The dash, hyphen, bullet and enum lists all have a special
891 * HEAD character (temporarily bold, in some cases).
895 if (MDOC_HEAD
== node
->type
)
898 p
->flags
|= TERMP_BOLD
;
899 term_word(p
, "\\[bu]");
904 p
->flags
|= TERMP_BOLD
;
905 term_word(p
, "\\(hy");
908 (pair
->ppair
->ppair
->count
)++;
909 (void)snprintf(buf
, sizeof(buf
), "%d.",
910 pair
->ppair
->ppair
->count
);
917 p
->flags
= sv
; /* Restore saved flags. */
920 * If we're not going to process our children, indicate so here.
933 if (MDOC_HEAD
== node
->type
)
937 if (MDOC_BODY
== node
->type
)
950 termp_it_post(DECL_ARGS
)
954 if (MDOC_BODY
!= node
->type
&& MDOC_HEAD
!= node
->type
)
957 type
= arg_listtype(node
->parent
->parent
->parent
);
965 if (MDOC_BODY
== node
->type
)
969 if (MDOC_HEAD
== node
->type
)
977 p
->flags
= pair
->flag
;
983 termp_nm_pre(DECL_ARGS
)
986 if (SEC_SYNOPSIS
== node
->sec
)
989 pair
->flag
|= ttypes
[TTYPE_PROG
];
990 p
->flags
|= ttypes
[TTYPE_PROG
];
992 if (NULL
== node
->child
)
993 term_word(p
, meta
->name
);
1001 termp_fl_pre(DECL_ARGS
)
1004 pair
->flag
|= ttypes
[TTYPE_CMD_FLAG
];
1005 p
->flags
|= ttypes
[TTYPE_CMD_FLAG
];
1006 term_word(p
, "\\-");
1007 p
->flags
|= TERMP_NOSPACE
;
1014 termp_ar_pre(DECL_ARGS
)
1017 pair
->flag
|= ttypes
[TTYPE_CMD_ARG
];
1024 termp_ns_pre(DECL_ARGS
)
1027 p
->flags
|= TERMP_NOSPACE
;
1034 termp_pp_pre(DECL_ARGS
)
1044 termp_rs_pre(DECL_ARGS
)
1047 if (MDOC_BLOCK
== node
->type
&& node
->prev
)
1055 termp_rv_pre(DECL_ARGS
)
1059 i
= arg_getattr(MDOC_Std
, node
);
1061 assert(node
->args
->argv
[i
].sz
);
1064 term_word(p
, "The");
1066 p
->flags
|= ttypes
[TTYPE_FUNC_NAME
];
1067 term_word(p
, *node
->args
->argv
[i
].value
);
1068 p
->flags
&= ~ttypes
[TTYPE_FUNC_NAME
];
1069 p
->flags
|= TERMP_NOSPACE
;
1071 term_word(p
, "() function returns the value 0 if successful;");
1072 term_word(p
, "otherwise the value -1 is returned and the");
1073 term_word(p
, "global variable");
1075 p
->flags
|= ttypes
[TTYPE_VAR_DECL
];
1076 term_word(p
, "errno");
1077 p
->flags
&= ~ttypes
[TTYPE_VAR_DECL
];
1079 term_word(p
, "is set to indicate the error.");
1087 termp_ex_pre(DECL_ARGS
)
1091 i
= arg_getattr(MDOC_Std
, node
);
1093 assert(node
->args
->argv
[i
].sz
);
1095 term_word(p
, "The");
1096 p
->flags
|= ttypes
[TTYPE_PROG
];
1097 term_word(p
, *node
->args
->argv
[i
].value
);
1098 p
->flags
&= ~ttypes
[TTYPE_PROG
];
1099 term_word(p
, "utility exits 0 on success, and >0 if an error occurs.");
1107 termp_nd_pre(DECL_ARGS
)
1110 if (MDOC_BODY
!= node
->type
)
1114 * XXX: signed off by jmc@openbsd.org. This technically
1115 * produces a minus sign after the Nd, which is wrong, but is
1116 * consistent with the historic OpenBSD tmac file.
1118 #if defined(__OpenBSD__) || defined(__linux__)
1119 term_word(p
, "\\-");
1121 term_word(p
, "\\(em");
1129 termp_bl_post(DECL_ARGS
)
1132 if (MDOC_BLOCK
== node
->type
)
1139 termp_op_post(DECL_ARGS
)
1142 if (MDOC_BODY
!= node
->type
)
1144 p
->flags
|= TERMP_NOSPACE
;
1145 term_word(p
, "\\(rB");
1151 termp_xr_pre(DECL_ARGS
)
1153 const struct mdoc_node
*n
;
1155 assert(node
->child
&& MDOC_TEXT
== node
->child
->type
);
1158 term_word(p
, n
->string
);
1159 if (NULL
== (n
= n
->next
))
1161 p
->flags
|= TERMP_NOSPACE
;
1163 p
->flags
|= TERMP_NOSPACE
;
1164 term_word(p
, n
->string
);
1165 p
->flags
|= TERMP_NOSPACE
;
1173 termp_vt_pre(DECL_ARGS
)
1176 /* FIXME: this can be "type name". */
1177 pair
->flag
|= ttypes
[TTYPE_VAR_DECL
];
1184 termp_vt_post(DECL_ARGS
)
1187 if (node
->sec
!= SEC_SYNOPSIS
)
1189 if (node
->next
&& MDOC_Vt
== node
->next
->tok
)
1191 else if (node
->next
)
1198 termp_fd_pre(DECL_ARGS
)
1201 pair
->flag
|= ttypes
[TTYPE_FUNC_DECL
];
1208 termp_fd_post(DECL_ARGS
)
1211 if (node
->sec
!= SEC_SYNOPSIS
)
1215 if (node
->next
&& MDOC_Fd
!= node
->next
->tok
)
1222 termp_sh_pre(DECL_ARGS
)
1225 switch (node
->type
) {
1228 pair
->flag
|= ttypes
[TTYPE_SECTION
];
1242 termp_sh_post(DECL_ARGS
)
1245 switch (node
->type
) {
1261 termp_op_pre(DECL_ARGS
)
1264 switch (node
->type
) {
1266 term_word(p
, "\\(lB");
1267 p
->flags
|= TERMP_NOSPACE
;
1278 termp_bt_pre(DECL_ARGS
)
1281 term_word(p
, "is currently in beta test.");
1288 termp_lb_pre(DECL_ARGS
)
1292 assert(node
->child
&& MDOC_TEXT
== node
->child
->type
);
1293 lb
= mdoc_a2lib(node
->child
->string
);
1298 term_word(p
, "library");
1305 termp_lb_post(DECL_ARGS
)
1314 termp_ud_pre(DECL_ARGS
)
1317 term_word(p
, "currently under development.");
1324 termp_d1_pre(DECL_ARGS
)
1327 if (MDOC_BLOCK
!= node
->type
)
1330 p
->offset
+= (INDENT
+ 1);
1337 termp_d1_post(DECL_ARGS
)
1340 if (MDOC_BLOCK
!= node
->type
)
1348 termp_aq_pre(DECL_ARGS
)
1351 if (MDOC_BODY
!= node
->type
)
1353 term_word(p
, "\\(la");
1354 p
->flags
|= TERMP_NOSPACE
;
1361 termp_aq_post(DECL_ARGS
)
1364 if (MDOC_BODY
!= node
->type
)
1366 p
->flags
|= TERMP_NOSPACE
;
1367 term_word(p
, "\\(ra");
1373 termp_ft_pre(DECL_ARGS
)
1376 if (SEC_SYNOPSIS
== node
->sec
)
1377 if (node
->prev
&& MDOC_Fo
== node
->prev
->tok
)
1379 pair
->flag
|= ttypes
[TTYPE_FUNC_TYPE
];
1386 termp_ft_post(DECL_ARGS
)
1389 if (SEC_SYNOPSIS
== node
->sec
)
1396 termp_fn_pre(DECL_ARGS
)
1398 const struct mdoc_node
*n
;
1400 assert(node
->child
&& MDOC_TEXT
== node
->child
->type
);
1402 /* FIXME: can be "type funcname" "type varname"... */
1404 p
->flags
|= ttypes
[TTYPE_FUNC_NAME
];
1405 term_word(p
, node
->child
->string
);
1406 p
->flags
&= ~ttypes
[TTYPE_FUNC_NAME
];
1408 p
->flags
|= TERMP_NOSPACE
;
1411 for (n
= node
->child
->next
; n
; n
= n
->next
) {
1412 p
->flags
|= ttypes
[TTYPE_FUNC_ARG
];
1413 term_word(p
, n
->string
);
1414 p
->flags
&= ~ttypes
[TTYPE_FUNC_ARG
];
1421 if (SEC_SYNOPSIS
== node
->sec
)
1430 termp_fn_post(DECL_ARGS
)
1433 if (node
->sec
== SEC_SYNOPSIS
&& node
->next
)
1440 termp_sx_pre(DECL_ARGS
)
1443 pair
->flag
|= ttypes
[TTYPE_LINK
];
1450 termp_fa_pre(DECL_ARGS
)
1452 struct mdoc_node
*n
;
1454 if (node
->parent
->tok
!= MDOC_Fo
) {
1455 pair
->flag
|= ttypes
[TTYPE_FUNC_ARG
];
1459 for (n
= node
->child
; n
; n
= n
->next
) {
1460 p
->flags
|= ttypes
[TTYPE_FUNC_ARG
];
1461 term_word(p
, n
->string
);
1462 p
->flags
&= ~ttypes
[TTYPE_FUNC_ARG
];
1467 if (node
->child
&& node
->next
&& node
->next
->tok
== MDOC_Fa
)
1476 termp_va_pre(DECL_ARGS
)
1479 pair
->flag
|= ttypes
[TTYPE_VAR_DECL
];
1486 termp_bd_pre(DECL_ARGS
)
1491 * This is fairly tricky due primarily to crappy documentation.
1492 * If -ragged or -filled are specified, the block does nothing
1493 * but change the indentation.
1495 * If, on the other hand, -unfilled or -literal are specified,
1496 * then the game changes. Text is printed exactly as entered in
1497 * the display: if a macro line, a newline is appended to the
1498 * line. Blank lines are allowed.
1501 if (MDOC_BLOCK
== node
->type
)
1502 return(fmt_block_vspace(p
, node
, node
));
1503 else if (MDOC_BODY
!= node
->type
)
1506 /* FIXME: display type should be mandated by parser. */
1508 if (NULL
== node
->parent
->args
)
1509 errx(1, "missing display type");
1511 for (type
= -1, i
= 0;
1512 i
< (int)node
->parent
->args
->argc
; i
++) {
1513 switch (node
->parent
->args
->argv
[i
].arg
) {
1518 case (MDOC_Unfilled
):
1520 case (MDOC_Literal
):
1521 type
= node
->parent
->args
->argv
[i
].arg
;
1522 i
= (int)node
->parent
->args
->argc
;
1529 if (NULL
== node
->parent
->args
)
1530 errx(1, "missing display type");
1532 i
= arg_getattr(MDOC_Offset
, node
->parent
);
1534 if (1 != node
->parent
->args
->argv
[i
].sz
)
1535 errx(1, "expected single value");
1536 p
->offset
+= arg_offset(&node
->parent
->args
->argv
[i
]);
1540 case (MDOC_Literal
):
1542 case (MDOC_Unfilled
):
1549 * Tricky. Iterate through all children. If we're on a
1550 * different parse line, append a newline and then the contents.
1554 p
->flags
|= TERMP_LITERAL
;
1555 ln
= node
->child
? node
->child
->line
: 0;
1557 for (node
= node
->child
; node
; node
= node
->next
) {
1558 if (ln
< node
->line
) {
1560 p
->flags
|= TERMP_NOSPACE
;
1563 print_node(p
, pair
, meta
, node
);
1572 termp_bd_post(DECL_ARGS
)
1575 if (MDOC_BODY
!= node
->type
)
1579 p
->flags
&= ~TERMP_LITERAL
;
1580 p
->flags
|= TERMP_NOSPACE
;
1586 termp_qq_pre(DECL_ARGS
)
1589 if (MDOC_BODY
!= node
->type
)
1592 p
->flags
|= TERMP_NOSPACE
;
1599 termp_qq_post(DECL_ARGS
)
1602 if (MDOC_BODY
!= node
->type
)
1604 p
->flags
|= TERMP_NOSPACE
;
1611 termp_bx_post(DECL_ARGS
)
1615 p
->flags
|= TERMP_NOSPACE
;
1616 term_word(p
, "BSD");
1622 termp_xx_pre(DECL_ARGS
)
1627 switch (node
->tok
) {
1632 pp
= "DragonFlyBSD";
1658 termp_sq_pre(DECL_ARGS
)
1661 if (MDOC_BODY
!= node
->type
)
1663 term_word(p
, "\\(oq");
1664 p
->flags
|= TERMP_NOSPACE
;
1671 termp_sq_post(DECL_ARGS
)
1674 if (MDOC_BODY
!= node
->type
)
1676 p
->flags
|= TERMP_NOSPACE
;
1677 term_word(p
, "\\(aq");
1683 termp_pf_pre(DECL_ARGS
)
1686 p
->flags
|= TERMP_IGNDELIM
;
1693 termp_pf_post(DECL_ARGS
)
1696 p
->flags
&= ~TERMP_IGNDELIM
;
1697 p
->flags
|= TERMP_NOSPACE
;
1703 termp_ss_pre(DECL_ARGS
)
1706 switch (node
->type
) {
1713 pair
->flag
|= ttypes
[TTYPE_SSECTION
];
1714 p
->offset
= HALFINDENT
;
1726 termp_ss_post(DECL_ARGS
)
1729 if (MDOC_HEAD
== node
->type
)
1736 termp_pa_pre(DECL_ARGS
)
1739 pair
->flag
|= ttypes
[TTYPE_FILE
];
1746 termp_em_pre(DECL_ARGS
)
1749 pair
->flag
|= ttypes
[TTYPE_EMPH
];
1756 termp_cd_pre(DECL_ARGS
)
1759 pair
->flag
|= ttypes
[TTYPE_CONFIG
];
1767 termp_cm_pre(DECL_ARGS
)
1770 pair
->flag
|= ttypes
[TTYPE_CMD_FLAG
];
1777 termp_ic_pre(DECL_ARGS
)
1780 pair
->flag
|= ttypes
[TTYPE_CMD
];
1787 termp_in_pre(DECL_ARGS
)
1790 pair
->flag
|= ttypes
[TTYPE_INCLUDE
];
1791 p
->flags
|= ttypes
[TTYPE_INCLUDE
];
1793 if (SEC_SYNOPSIS
== node
->sec
)
1794 term_word(p
, "#include");
1797 p
->flags
|= TERMP_NOSPACE
;
1804 termp_in_post(DECL_ARGS
)
1807 p
->flags
|= TERMP_NOSPACE
;
1810 if (SEC_SYNOPSIS
!= node
->sec
)
1815 * XXX Not entirely correct. If `.In foo bar' is specified in
1816 * the SYNOPSIS section, then it produces a single break after
1817 * the <foo>; mandoc asserts a vertical space. Since this
1818 * construction is rarely used, I think it's fine.
1820 if (node
->next
&& MDOC_In
!= node
->next
->tok
)
1827 termp_brq_pre(DECL_ARGS
)
1830 if (MDOC_BODY
!= node
->type
)
1832 term_word(p
, "\\(lC");
1833 p
->flags
|= TERMP_NOSPACE
;
1840 termp_brq_post(DECL_ARGS
)
1843 if (MDOC_BODY
!= node
->type
)
1845 p
->flags
|= TERMP_NOSPACE
;
1846 term_word(p
, "\\(rC");
1852 termp_bq_pre(DECL_ARGS
)
1855 if (MDOC_BODY
!= node
->type
)
1857 term_word(p
, "\\(lB");
1858 p
->flags
|= TERMP_NOSPACE
;
1865 termp_bq_post(DECL_ARGS
)
1868 if (MDOC_BODY
!= node
->type
)
1870 p
->flags
|= TERMP_NOSPACE
;
1871 term_word(p
, "\\(rB");
1877 termp_pq_pre(DECL_ARGS
)
1880 if (MDOC_BODY
!= node
->type
)
1882 term_word(p
, "\\&(");
1883 p
->flags
|= TERMP_NOSPACE
;
1890 termp_pq_post(DECL_ARGS
)
1893 if (MDOC_BODY
!= node
->type
)
1901 termp_fo_pre(DECL_ARGS
)
1903 const struct mdoc_node
*n
;
1905 if (MDOC_BODY
== node
->type
) {
1906 p
->flags
|= TERMP_NOSPACE
;
1908 p
->flags
|= TERMP_NOSPACE
;
1910 } else if (MDOC_HEAD
!= node
->type
)
1913 p
->flags
|= ttypes
[TTYPE_FUNC_NAME
];
1914 for (n
= node
->child
; n
; n
= n
->next
) {
1915 assert(MDOC_TEXT
== n
->type
);
1916 term_word(p
, n
->string
);
1918 p
->flags
&= ~ttypes
[TTYPE_FUNC_NAME
];
1926 termp_fo_post(DECL_ARGS
)
1929 if (MDOC_BODY
!= node
->type
)
1931 p
->flags
|= TERMP_NOSPACE
;
1933 p
->flags
|= TERMP_NOSPACE
;
1941 termp_bf_pre(DECL_ARGS
)
1943 const struct mdoc_node
*n
;
1945 if (MDOC_HEAD
== node
->type
)
1947 else if (MDOC_BLOCK
!= node
->type
)
1950 if (NULL
== (n
= node
->head
->child
)) {
1951 if (arg_hasattr(MDOC_Emphasis
, node
))
1952 pair
->flag
|= ttypes
[TTYPE_EMPH
];
1953 else if (arg_hasattr(MDOC_Symbolic
, node
))
1954 pair
->flag
|= ttypes
[TTYPE_SYMB
];
1959 assert(MDOC_TEXT
== n
->type
);
1960 if (0 == strcmp("Em", n
->string
))
1961 pair
->flag
|= ttypes
[TTYPE_EMPH
];
1962 else if (0 == strcmp("Sy", n
->string
))
1963 pair
->flag
|= ttypes
[TTYPE_SYMB
];
1971 termp_sy_pre(DECL_ARGS
)
1974 pair
->flag
|= ttypes
[TTYPE_SYMB
];
1981 termp_ms_pre(DECL_ARGS
)
1984 pair
->flag
|= ttypes
[TTYPE_SYMBOL
];
1992 termp_sm_pre(DECL_ARGS
)
1995 assert(node
->child
&& MDOC_TEXT
== node
->child
->type
);
1996 if (0 == strcmp("on", node
->child
->string
)) {
1997 p
->flags
&= ~TERMP_NONOSPACE
;
1998 p
->flags
&= ~TERMP_NOSPACE
;
2000 p
->flags
|= TERMP_NONOSPACE
;
2008 termp_ap_pre(DECL_ARGS
)
2011 p
->flags
|= TERMP_NOSPACE
;
2012 term_word(p
, "\\(aq");
2013 p
->flags
|= TERMP_NOSPACE
;
2020 termp__j_pre(DECL_ARGS
)
2023 pair
->flag
|= ttypes
[TTYPE_REF_JOURNAL
];
2030 termp__t_pre(DECL_ARGS
)
2034 p
->flags
|= TERMP_NOSPACE
;
2041 termp__t_post(DECL_ARGS
)
2044 p
->flags
|= TERMP_NOSPACE
;
2046 termp____post(p
, pair
, meta
, node
);
2052 termp____post(DECL_ARGS
)
2055 p
->flags
|= TERMP_NOSPACE
;
2056 term_word(p
, node
->next
? "," : ".");
2062 termp_lk_pre(DECL_ARGS
)
2064 const struct mdoc_node
*n
;
2066 assert(node
->child
);
2069 if (NULL
== n
->next
) {
2070 pair
->flag
|= ttypes
[TTYPE_LINK_ANCHOR
];
2074 p
->flags
|= ttypes
[TTYPE_LINK_ANCHOR
];
2075 term_word(p
, n
->string
);
2076 p
->flags
|= TERMP_NOSPACE
;
2078 p
->flags
&= ~ttypes
[TTYPE_LINK_ANCHOR
];
2080 p
->flags
|= ttypes
[TTYPE_LINK_TEXT
];
2081 for (n
= n
->next
; n
; n
= n
->next
)
2082 term_word(p
, n
->string
);
2084 p
->flags
&= ~ttypes
[TTYPE_LINK_TEXT
];
2091 termp_mt_pre(DECL_ARGS
)
2094 pair
->flag
|= ttypes
[TTYPE_LINK_ANCHOR
];