]>
git.cameronkatri.com Git - mandoc.git/blob - mdoc_term.c
1 /* $Id: mdoc_term.c,v 1.29 2009/07/12 17:45:17 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 * This is used to preserve a style of value across a macro, instead of
85 * losing it while the body is processed.
88 struct termpair
*ppair
;
89 int flag
; /* Whether being used. */
90 size_t rmargin
; /* Right margin. */
91 int count
; /* Enum count. */
95 struct termp *p, struct termpair *pair, \
96 const struct mdoc_meta *meta, \
97 const struct mdoc_node *node
99 #define DECL_PRE(name) \
100 static int name##_pre(DECL_ARGS)
101 #define DECL_POST(name) \
102 static void name##_post(DECL_ARGS)
103 #define DECL_PREPOST(name) \
107 DECL_PREPOST(termp__t
);
108 DECL_PREPOST(termp_aq
);
109 DECL_PREPOST(termp_bd
);
110 DECL_PREPOST(termp_bq
);
111 DECL_PREPOST(termp_brq
);
112 DECL_PREPOST(termp_d1
);
113 DECL_PREPOST(termp_dq
);
114 DECL_PREPOST(termp_fd
);
115 DECL_PREPOST(termp_fn
);
116 DECL_PREPOST(termp_fo
);
117 DECL_PREPOST(termp_ft
);
118 DECL_PREPOST(termp_in
);
119 DECL_PREPOST(termp_it
);
120 DECL_PREPOST(termp_lb
);
121 DECL_PREPOST(termp_op
);
122 DECL_PREPOST(termp_pf
);
123 DECL_PREPOST(termp_pq
);
124 DECL_PREPOST(termp_qq
);
125 DECL_PREPOST(termp_sh
);
126 DECL_PREPOST(termp_ss
);
127 DECL_PREPOST(termp_sq
);
128 DECL_PREPOST(termp_vt
);
167 int (*pre
)(DECL_ARGS
);
168 void (*post
)(DECL_ARGS
);
171 static const struct termact termacts
[MDOC_MAX
] = {
172 { termp_ap_pre
, NULL
}, /* Ap */
173 { NULL
, NULL
}, /* Dd */
174 { NULL
, NULL
}, /* Dt */
175 { NULL
, NULL
}, /* Os */
176 { termp_sh_pre
, termp_sh_post
}, /* Sh */
177 { termp_ss_pre
, termp_ss_post
}, /* Ss */
178 { termp_pp_pre
, NULL
}, /* Pp */
179 { termp_d1_pre
, termp_d1_post
}, /* D1 */
180 { termp_d1_pre
, termp_d1_post
}, /* Dl */
181 { termp_bd_pre
, termp_bd_post
}, /* Bd */
182 { NULL
, NULL
}, /* Ed */
183 { NULL
, termp_bl_post
}, /* Bl */
184 { NULL
, NULL
}, /* El */
185 { termp_it_pre
, termp_it_post
}, /* It */
186 { NULL
, NULL
}, /* Ad */
187 { NULL
, NULL
}, /* An */
188 { termp_ar_pre
, NULL
}, /* Ar */
189 { termp_cd_pre
, NULL
}, /* Cd */
190 { termp_cm_pre
, NULL
}, /* Cm */
191 { NULL
, NULL
}, /* Dv */
192 { NULL
, NULL
}, /* Er */
193 { NULL
, NULL
}, /* Ev */
194 { termp_ex_pre
, NULL
}, /* Ex */
195 { termp_fa_pre
, NULL
}, /* Fa */
196 { termp_fd_pre
, termp_fd_post
}, /* Fd */
197 { termp_fl_pre
, NULL
}, /* Fl */
198 { termp_fn_pre
, termp_fn_post
}, /* Fn */
199 { termp_ft_pre
, termp_ft_post
}, /* Ft */
200 { termp_ic_pre
, NULL
}, /* Ic */
201 { termp_in_pre
, termp_in_post
}, /* In */
202 { NULL
, NULL
}, /* Li */
203 { termp_nd_pre
, NULL
}, /* Nd */
204 { termp_nm_pre
, NULL
}, /* Nm */
205 { termp_op_pre
, termp_op_post
}, /* Op */
206 { NULL
, NULL
}, /* Ot */
207 { termp_pa_pre
, NULL
}, /* Pa */
208 { termp_rv_pre
, NULL
}, /* Rv */
209 { termp_st_pre
, NULL
}, /* St */
210 { termp_va_pre
, NULL
}, /* Va */
211 { termp_vt_pre
, termp_vt_post
}, /* Vt */
212 { termp_xr_pre
, NULL
}, /* Xr */
213 { NULL
, termp____post
}, /* %A */
214 { NULL
, termp____post
}, /* %B */
215 { NULL
, termp____post
}, /* %D */
216 { NULL
, termp____post
}, /* %I */
217 { termp__j_pre
, termp____post
}, /* %J */
218 { NULL
, termp____post
}, /* %N */
219 { NULL
, termp____post
}, /* %O */
220 { NULL
, termp____post
}, /* %P */
221 { NULL
, termp____post
}, /* %R */
222 { termp__t_pre
, termp__t_post
}, /* %T */
223 { NULL
, termp____post
}, /* %V */
224 { NULL
, NULL
}, /* Ac */
225 { termp_aq_pre
, termp_aq_post
}, /* Ao */
226 { termp_aq_pre
, termp_aq_post
}, /* Aq */
227 { termp_at_pre
, NULL
}, /* At */
228 { NULL
, NULL
}, /* Bc */
229 { termp_bf_pre
, NULL
}, /* Bf */
230 { termp_bq_pre
, termp_bq_post
}, /* Bo */
231 { termp_bq_pre
, termp_bq_post
}, /* Bq */
232 { termp_xx_pre
, NULL
}, /* Bsx */
233 { NULL
, termp_bx_post
}, /* Bx */
234 { NULL
, NULL
}, /* Db */
235 { NULL
, NULL
}, /* Dc */
236 { termp_dq_pre
, termp_dq_post
}, /* Do */
237 { termp_dq_pre
, termp_dq_post
}, /* Dq */
238 { NULL
, NULL
}, /* Ec */
239 { NULL
, NULL
}, /* Ef */
240 { termp_em_pre
, NULL
}, /* Em */
241 { NULL
, NULL
}, /* Eo */
242 { termp_xx_pre
, NULL
}, /* Fx */
243 { termp_ms_pre
, NULL
}, /* Ms */
244 { NULL
, NULL
}, /* No */
245 { termp_ns_pre
, NULL
}, /* Ns */
246 { termp_xx_pre
, NULL
}, /* Nx */
247 { termp_xx_pre
, NULL
}, /* Ox */
248 { NULL
, NULL
}, /* Pc */
249 { termp_pf_pre
, termp_pf_post
}, /* Pf */
250 { termp_pq_pre
, termp_pq_post
}, /* Po */
251 { termp_pq_pre
, termp_pq_post
}, /* Pq */
252 { NULL
, NULL
}, /* Qc */
253 { termp_sq_pre
, termp_sq_post
}, /* Ql */
254 { termp_qq_pre
, termp_qq_post
}, /* Qo */
255 { termp_qq_pre
, termp_qq_post
}, /* Qq */
256 { NULL
, NULL
}, /* Re */
257 { termp_rs_pre
, NULL
}, /* Rs */
258 { NULL
, NULL
}, /* Sc */
259 { termp_sq_pre
, termp_sq_post
}, /* So */
260 { termp_sq_pre
, termp_sq_post
}, /* Sq */
261 { termp_sm_pre
, NULL
}, /* Sm */
262 { termp_sx_pre
, NULL
}, /* Sx */
263 { termp_sy_pre
, NULL
}, /* Sy */
264 { NULL
, NULL
}, /* Tn */
265 { termp_xx_pre
, NULL
}, /* Ux */
266 { NULL
, NULL
}, /* Xc */
267 { NULL
, NULL
}, /* Xo */
268 { termp_fo_pre
, termp_fo_post
}, /* Fo */
269 { NULL
, NULL
}, /* Fc */
270 { termp_op_pre
, termp_op_post
}, /* Oo */
271 { NULL
, NULL
}, /* Oc */
272 { NULL
, NULL
}, /* Bk */
273 { NULL
, NULL
}, /* Ek */
274 { termp_bt_pre
, NULL
}, /* Bt */
275 { NULL
, NULL
}, /* Hf */
276 { NULL
, NULL
}, /* Fr */
277 { termp_ud_pre
, NULL
}, /* Ud */
278 { termp_lb_pre
, termp_lb_post
}, /* Lb */
279 { termp_pp_pre
, NULL
}, /* Lp */
280 { termp_lk_pre
, NULL
}, /* Lk */
281 { termp_mt_pre
, NULL
}, /* Mt */
282 { termp_brq_pre
, termp_brq_post
}, /* Brq */
283 { termp_brq_pre
, termp_brq_post
}, /* Bro */
284 { NULL
, NULL
}, /* Brc */
285 { NULL
, NULL
}, /* %C */
286 { NULL
, NULL
}, /* Es */
287 { NULL
, NULL
}, /* En */
288 { termp_xx_pre
, NULL
}, /* Dx */
289 { NULL
, NULL
}, /* %Q */
293 extern size_t strlcpy(char *, const char *, size_t);
294 extern size_t strlcat(char *, const char *, size_t);
297 static int arg_hasattr(int, const struct mdoc_node
*);
298 static int arg_getattrs(const int *, int *, size_t,
299 const struct mdoc_node
*);
300 static int arg_getattr(int, const struct mdoc_node
*);
301 static size_t arg_offset(const struct mdoc_argv
*);
302 static size_t arg_width(const struct mdoc_argv
*, int);
303 static int arg_listtype(const struct mdoc_node
*);
304 static int fmt_block_vspace(struct termp
*,
305 const struct mdoc_node
*,
306 const struct mdoc_node
*);
307 static void print_node(DECL_ARGS
);
308 static void print_head(struct termp
*,
309 const struct mdoc_meta
*);
310 static void print_body(DECL_ARGS
);
311 static void print_foot(struct termp
*,
312 const struct mdoc_meta
*);
316 mdoc_run(struct termp
*p
, const struct mdoc
*m
)
319 * Main output function. When this is called, assume that the
320 * tree is properly formed.
323 print_head(p
, mdoc_meta(m
));
324 assert(mdoc_node(m
));
325 assert(MDOC_ROOT
== mdoc_node(m
)->type
);
326 if (mdoc_node(m
)->child
)
327 print_body(p
, NULL
, mdoc_meta(m
), mdoc_node(m
)->child
);
328 print_foot(p
, mdoc_meta(m
));
334 print_body(DECL_ARGS
)
337 print_node(p
, pair
, meta
, node
);
340 print_body(p
, pair
, meta
, node
->next
);
345 print_node(DECL_ARGS
)
348 struct termpair npair
;
351 /* Pre-processing. */
361 if (MDOC_TEXT
!= node
->type
) {
362 if (termacts
[node
->tok
].pre
)
363 if ( ! (*termacts
[node
->tok
].pre
)(p
, &npair
, meta
, node
))
365 } else /* MDOC_TEXT == node->type */
366 term_word(p
, node
->string
);
370 p
->flags
|= npair
.flag
;
372 if (dochild
&& node
->child
)
373 print_body(p
, &npair
, meta
, node
->child
);
375 /* Post-processing. */
377 if (MDOC_TEXT
!= node
->type
)
378 if (termacts
[node
->tok
].post
)
379 (*termacts
[node
->tok
].post
)(p
, &npair
, meta
, node
);
382 p
->flags
&= ~npair
.flag
;
387 print_foot(struct termp
*p
, const struct mdoc_meta
*meta
)
393 * Output the footer in new-groff style, that is, three columns
394 * with the middle being the manual date and flanking columns
395 * being the operating system:
400 if (NULL
== (buf
= malloc(p
->rmargin
)))
402 if (NULL
== (os
= malloc(p
->rmargin
)))
405 tm
= localtime(&meta
->date
);
407 if (0 == strftime(buf
, p
->rmargin
, "%B %d, %Y", tm
))
410 (void)strlcpy(os
, meta
->os
, p
->rmargin
);
415 p
->rmargin
= (p
->maxrmargin
- strlen(buf
) + 1) / 2;
416 p
->flags
|= TERMP_NOSPACE
| TERMP_NOBREAK
;
421 p
->offset
= p
->rmargin
;
422 p
->rmargin
= p
->maxrmargin
- strlen(os
);
423 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
428 p
->offset
= p
->rmargin
;
429 p
->rmargin
= p
->maxrmargin
;
430 p
->flags
&= ~TERMP_NOBREAK
;
431 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
437 p
->rmargin
= p
->maxrmargin
;
446 print_head(struct termp
*p
, const struct mdoc_meta
*meta
)
450 p
->rmargin
= p
->maxrmargin
;
453 if (NULL
== (buf
= malloc(p
->rmargin
)))
455 if (NULL
== (title
= malloc(p
->rmargin
)))
459 * The header is strange. It has three components, which are
460 * really two with the first duplicated. It goes like this:
462 * IDENTIFIER TITLE IDENTIFIER
464 * The IDENTIFIER is NAME(SECTION), which is the command-name
465 * (if given, or "unknown" if not) followed by the manual page
466 * section. These are given in `Dt'. The TITLE is a free-form
467 * string depending on the manual volume. If not specified, it
468 * switches on the manual section.
472 (void)strlcpy(buf
, meta
->vol
, p
->rmargin
);
475 (void)strlcat(buf
, " (", p
->rmargin
);
476 (void)strlcat(buf
, meta
->arch
, p
->rmargin
);
477 (void)strlcat(buf
, ")", p
->rmargin
);
480 (void)snprintf(title
, p
->rmargin
, "%s(%d)",
481 meta
->title
, meta
->msec
);
484 p
->rmargin
= (p
->maxrmargin
- strlen(buf
) + 1) / 2;
485 p
->flags
|= TERMP_NOBREAK
| TERMP_NOSPACE
;
490 p
->offset
= p
->rmargin
;
491 p
->rmargin
= p
->maxrmargin
- strlen(title
);
492 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
497 p
->offset
= p
->rmargin
;
498 p
->rmargin
= p
->maxrmargin
;
499 p
->flags
&= ~TERMP_NOBREAK
;
500 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
506 p
->rmargin
= p
->maxrmargin
;
507 p
->flags
&= ~TERMP_NOSPACE
;
515 arg_width(const struct mdoc_argv
*arg
, int pos
)
520 assert(pos
< (int)arg
->sz
&& pos
>= 0);
521 assert(arg
->value
[pos
]);
523 if (0 == (len
= (int)strlen(arg
->value
[pos
])))
526 for (i
= 0; i
< len
- 1; i
++)
527 if ( ! isdigit((u_char
)arg
->value
[pos
][i
]))
531 if ('n' == arg
->value
[pos
][len
- 1] ||
532 'm' == arg
->value
[pos
][len
- 1]) {
533 v
= (size_t)atoi(arg
->value
[pos
]);
538 return(strlen(arg
->value
[pos
]) + 2);
543 arg_listtype(const struct mdoc_node
*n
)
547 assert(MDOC_BLOCK
== n
->type
);
549 len
= (int)(n
->args
? n
->args
->argc
: 0);
551 for (i
= 0; i
< len
; i
++)
552 switch (n
->args
->argv
[i
].arg
) {
572 return(n
->args
->argv
[i
].arg
);
577 /* FIXME: mandated by parser. */
579 errx(1, "list type not supported");
585 arg_offset(const struct mdoc_argv
*arg
)
589 if (0 == strcmp(*arg
->value
, "left"))
591 if (0 == strcmp(*arg
->value
, "indent"))
593 if (0 == strcmp(*arg
->value
, "indent-two"))
594 return((INDENT
+ 1) * 2);
596 /* FIXME: needs to support field-widths (10n, etc.). */
598 return(strlen(*arg
->value
));
603 arg_hasattr(int arg
, const struct mdoc_node
*n
)
606 return(-1 != arg_getattr(arg
, n
));
611 arg_getattr(int v
, const struct mdoc_node
*n
)
615 return(arg_getattrs(&v
, &val
, 1, n
) ? val
: -1);
620 arg_getattrs(const int *keys
, int *vals
,
621 size_t sz
, const struct mdoc_node
*n
)
628 for (k
= i
= 0; i
< (int)n
->args
->argc
; i
++)
629 for (j
= 0; j
< (int)sz
; j
++)
630 if (n
->args
->argv
[i
].arg
== keys
[j
]) {
640 fmt_block_vspace(struct termp
*p
,
641 const struct mdoc_node
*bl
,
642 const struct mdoc_node
*node
)
644 const struct mdoc_node
*n
;
648 if (arg_hasattr(MDOC_Compact
, bl
))
651 for (n
= node
; n
; n
= n
->parent
) {
652 if (MDOC_BLOCK
!= n
->type
)
654 if (MDOC_Ss
== n
->tok
)
656 if (MDOC_Sh
== n
->tok
)
670 termp_dq_pre(DECL_ARGS
)
673 if (MDOC_BODY
!= node
->type
)
676 term_word(p
, "\\(lq");
677 p
->flags
|= TERMP_NOSPACE
;
684 termp_dq_post(DECL_ARGS
)
687 if (MDOC_BODY
!= node
->type
)
690 p
->flags
|= TERMP_NOSPACE
;
691 term_word(p
, "\\(rq");
697 termp_it_pre(DECL_ARGS
)
699 const struct mdoc_node
*bl
, *n
;
701 int i
, type
, keys
[3], vals
[3], sv
;
702 size_t width
, offset
;
704 if (MDOC_BLOCK
== node
->type
)
705 return(fmt_block_vspace(p
, node
->parent
->parent
, node
));
707 bl
= node
->parent
->parent
->parent
;
709 /* Save parent attributes. */
711 pair
->rmargin
= p
->rmargin
;
712 pair
->flag
= p
->flags
;
714 /* Get list width and offset. */
716 keys
[0] = MDOC_Width
;
717 keys
[1] = MDOC_Offset
;
718 keys
[2] = MDOC_Column
;
720 vals
[0] = vals
[1] = vals
[2] = -1;
724 (void)arg_getattrs(keys
, vals
, 3, bl
);
726 type
= arg_listtype(bl
);
728 /* Calculate real width and offset. */
732 if (MDOC_BODY
== node
->type
)
734 for (i
= 0, n
= node
->prev
; n
; n
= n
->prev
, i
++)
736 (&bl
->args
->argv
[vals
[2]], i
);
737 assert(i
< (int)bl
->args
->argv
[vals
[2]].sz
);
738 width
= arg_width(&bl
->args
->argv
[vals
[2]], i
);
740 offset
+= arg_offset(&bl
->args
->argv
[vals
[1]]);
744 width
= arg_width(&bl
->args
->argv
[vals
[0]], 0);
746 offset
+= arg_offset(&bl
->args
->argv
[vals
[1]]);
751 * List-type can override the width in the case of fixed-head
752 * values (bullet, dash/hyphen, enum). Tags need a non-zero
778 * Whitespace control. Inset bodies need an initial space,
779 * while diagonal bodies need two.
787 if (MDOC_BODY
== node
->type
)
788 p
->flags
&= ~TERMP_NOSPACE
;
790 p
->flags
|= TERMP_NOSPACE
;
793 p
->flags
|= TERMP_NOSPACE
;
798 * Style flags. Diagnostic heads need TTYPE_DIAG.
803 if (MDOC_HEAD
== node
->type
)
804 p
->flags
|= ttypes
[TTYPE_DIAG
];
811 * Pad and break control. This is the tricker part. Lists with
812 * set right-margins for the head get TERMP_NOBREAK because, if
813 * they overrun the margin, they wrap to the new margin.
814 * Correspondingly, the body for these types don't left-pad, as
815 * the head will pad out to to the right.
828 if (MDOC_HEAD
== node
->type
)
829 p
->flags
|= TERMP_NOBREAK
;
831 p
->flags
|= TERMP_NOLPAD
;
832 if (MDOC_HEAD
== node
->type
&& MDOC_Tag
== type
)
833 if (NULL
== node
->next
||
834 NULL
== node
->next
->child
)
835 p
->flags
|= TERMP_NONOBREAK
;
838 if (MDOC_HEAD
== node
->type
) {
840 if (MDOC_BODY
== node
->next
->type
)
841 p
->flags
&= ~TERMP_NOBREAK
;
843 p
->flags
|= TERMP_NOBREAK
;
845 p
->flags
|= TERMP_NOLPAD
;
849 if (MDOC_HEAD
== node
->type
)
850 p
->flags
|= TERMP_NOBREAK
;
857 * Margin control. Set-head-width lists have their right
858 * margins shortened. The body for these lists has the offset
859 * necessarily lengthened. Everybody gets the offset.
874 if (MDOC_HEAD
== node
->type
)
875 p
->rmargin
= p
->offset
+ width
;
880 p
->rmargin
= p
->offset
+ width
;
887 * The dash, hyphen, bullet and enum lists all have a special
888 * HEAD character (temporarily bold, in some cases).
892 if (MDOC_HEAD
== node
->type
)
895 p
->flags
|= TERMP_BOLD
;
896 term_word(p
, "\\[bu]");
901 p
->flags
|= TERMP_BOLD
;
902 term_word(p
, "\\(hy");
905 (pair
->ppair
->ppair
->count
)++;
906 (void)snprintf(buf
, sizeof(buf
), "%d.",
907 pair
->ppair
->ppair
->count
);
914 p
->flags
= sv
; /* Restore saved flags. */
917 * If we're not going to process our children, indicate so here.
930 if (MDOC_HEAD
== node
->type
)
934 if (MDOC_BODY
== node
->type
)
947 termp_it_post(DECL_ARGS
)
951 if (MDOC_BODY
!= node
->type
&& MDOC_HEAD
!= node
->type
)
954 type
= arg_listtype(node
->parent
->parent
->parent
);
962 if (MDOC_BODY
== node
->type
)
966 if (MDOC_HEAD
== node
->type
)
974 p
->rmargin
= pair
->rmargin
;
975 p
->flags
= pair
->flag
;
981 termp_nm_pre(DECL_ARGS
)
984 if (SEC_SYNOPSIS
== node
->sec
)
987 pair
->flag
|= ttypes
[TTYPE_PROG
];
988 p
->flags
|= ttypes
[TTYPE_PROG
];
990 if (NULL
== node
->child
)
991 term_word(p
, meta
->name
);
999 termp_fl_pre(DECL_ARGS
)
1002 pair
->flag
|= ttypes
[TTYPE_CMD_FLAG
];
1003 p
->flags
|= ttypes
[TTYPE_CMD_FLAG
];
1004 term_word(p
, "\\-");
1005 p
->flags
|= TERMP_NOSPACE
;
1012 termp_ar_pre(DECL_ARGS
)
1015 pair
->flag
|= ttypes
[TTYPE_CMD_ARG
];
1022 termp_ns_pre(DECL_ARGS
)
1025 p
->flags
|= TERMP_NOSPACE
;
1032 termp_pp_pre(DECL_ARGS
)
1042 termp_st_pre(DECL_ARGS
)
1046 if (node
->child
&& (cp
= mdoc_a2st(node
->child
->string
)))
1054 termp_rs_pre(DECL_ARGS
)
1057 if (MDOC_BLOCK
== node
->type
&& node
->prev
)
1065 termp_rv_pre(DECL_ARGS
)
1069 /* FIXME: mandated by parser. */
1071 if (-1 == (i
= arg_getattr(MDOC_Std
, node
)))
1072 errx(1, "expected -std argument");
1073 if (1 != node
->args
->argv
[i
].sz
)
1074 errx(1, "expected -std argument");
1077 term_word(p
, "The");
1079 p
->flags
|= ttypes
[TTYPE_FUNC_NAME
];
1080 term_word(p
, *node
->args
->argv
[i
].value
);
1081 p
->flags
&= ~ttypes
[TTYPE_FUNC_NAME
];
1082 p
->flags
|= TERMP_NOSPACE
;
1084 term_word(p
, "() function returns the value 0 if successful;");
1085 term_word(p
, "otherwise the value -1 is returned and the");
1086 term_word(p
, "global variable");
1088 p
->flags
|= ttypes
[TTYPE_VAR_DECL
];
1089 term_word(p
, "errno");
1090 p
->flags
&= ~ttypes
[TTYPE_VAR_DECL
];
1092 term_word(p
, "is set to indicate the error.");
1100 termp_ex_pre(DECL_ARGS
)
1104 /* FIXME: mandated by parser? */
1106 if (-1 == (i
= arg_getattr(MDOC_Std
, node
)))
1107 errx(1, "expected -std argument");
1108 if (1 != node
->args
->argv
[i
].sz
)
1109 errx(1, "expected -std argument");
1111 term_word(p
, "The");
1112 p
->flags
|= ttypes
[TTYPE_PROG
];
1113 term_word(p
, *node
->args
->argv
[i
].value
);
1114 p
->flags
&= ~ttypes
[TTYPE_PROG
];
1115 term_word(p
, "utility exits 0 on success, and >0 if an error occurs.");
1123 termp_nd_pre(DECL_ARGS
)
1126 if (MDOC_BODY
!= node
->type
)
1129 * XXX: signed off by jmc@openbsd.org. This technically
1130 * produces a minus sign after the Nd, which is wrong, but is
1131 * consistent with the historic OpenBSD tmac file.
1133 #if defined(__OpenBSD__) || defined(__linux__)
1134 term_word(p
, "\\-");
1136 term_word(p
, "\\(em");
1144 termp_bl_post(DECL_ARGS
)
1147 if (MDOC_BLOCK
== node
->type
)
1154 termp_op_post(DECL_ARGS
)
1157 if (MDOC_BODY
!= node
->type
)
1159 p
->flags
|= TERMP_NOSPACE
;
1160 term_word(p
, "\\(rB");
1166 termp_xr_pre(DECL_ARGS
)
1168 const struct mdoc_node
*n
;
1170 assert(node
->child
&& MDOC_TEXT
== node
->child
->type
);
1173 term_word(p
, n
->string
);
1174 if (NULL
== (n
= n
->next
))
1176 p
->flags
|= TERMP_NOSPACE
;
1178 p
->flags
|= TERMP_NOSPACE
;
1179 term_word(p
, n
->string
);
1180 p
->flags
|= TERMP_NOSPACE
;
1188 termp_vt_pre(DECL_ARGS
)
1191 /* FIXME: this can be "type name". */
1192 pair
->flag
|= ttypes
[TTYPE_VAR_DECL
];
1199 termp_vt_post(DECL_ARGS
)
1202 if (node
->sec
== SEC_SYNOPSIS
)
1209 termp_fd_pre(DECL_ARGS
)
1212 pair
->flag
|= ttypes
[TTYPE_FUNC_DECL
];
1219 termp_fd_post(DECL_ARGS
)
1222 if (node
->sec
!= SEC_SYNOPSIS
)
1226 if (node
->next
&& MDOC_Fd
!= node
->next
->tok
)
1233 termp_sh_pre(DECL_ARGS
)
1236 switch (node
->type
) {
1239 pair
->flag
|= ttypes
[TTYPE_SECTION
];
1253 termp_sh_post(DECL_ARGS
)
1256 switch (node
->type
) {
1272 termp_op_pre(DECL_ARGS
)
1275 switch (node
->type
) {
1277 term_word(p
, "\\(lB");
1278 p
->flags
|= TERMP_NOSPACE
;
1289 termp_bt_pre(DECL_ARGS
)
1292 term_word(p
, "is currently in beta test.");
1299 termp_lb_pre(DECL_ARGS
)
1303 assert(node
->child
&& MDOC_TEXT
== node
->child
->type
);
1304 lb
= mdoc_a2lib(node
->child
->string
);
1309 term_word(p
, "library");
1316 termp_lb_post(DECL_ARGS
)
1325 termp_ud_pre(DECL_ARGS
)
1328 term_word(p
, "currently under development.");
1335 termp_d1_pre(DECL_ARGS
)
1338 if (MDOC_BLOCK
!= node
->type
)
1341 p
->offset
+= (INDENT
+ 1);
1348 termp_d1_post(DECL_ARGS
)
1351 if (MDOC_BLOCK
!= node
->type
)
1359 termp_aq_pre(DECL_ARGS
)
1362 if (MDOC_BODY
!= node
->type
)
1364 term_word(p
, "\\(la");
1365 p
->flags
|= TERMP_NOSPACE
;
1372 termp_aq_post(DECL_ARGS
)
1375 if (MDOC_BODY
!= node
->type
)
1377 p
->flags
|= TERMP_NOSPACE
;
1378 term_word(p
, "\\(ra");
1384 termp_ft_pre(DECL_ARGS
)
1387 if (SEC_SYNOPSIS
== node
->sec
)
1388 if (node
->prev
&& MDOC_Fo
== node
->prev
->tok
)
1390 pair
->flag
|= ttypes
[TTYPE_FUNC_TYPE
];
1397 termp_ft_post(DECL_ARGS
)
1400 if (SEC_SYNOPSIS
== node
->sec
)
1407 termp_fn_pre(DECL_ARGS
)
1409 const struct mdoc_node
*n
;
1411 assert(node
->child
&& MDOC_TEXT
== node
->child
->type
);
1413 /* FIXME: can be "type funcname" "type varname"... */
1415 p
->flags
|= ttypes
[TTYPE_FUNC_NAME
];
1416 term_word(p
, node
->child
->string
);
1417 p
->flags
&= ~ttypes
[TTYPE_FUNC_NAME
];
1419 p
->flags
|= TERMP_NOSPACE
;
1422 for (n
= node
->child
->next
; n
; n
= n
->next
) {
1423 p
->flags
|= ttypes
[TTYPE_FUNC_ARG
];
1424 term_word(p
, n
->string
);
1425 p
->flags
&= ~ttypes
[TTYPE_FUNC_ARG
];
1432 if (SEC_SYNOPSIS
== node
->sec
)
1441 termp_fn_post(DECL_ARGS
)
1444 if (node
->sec
== SEC_SYNOPSIS
&& node
->next
)
1451 termp_sx_pre(DECL_ARGS
)
1454 pair
->flag
|= ttypes
[TTYPE_LINK
];
1461 termp_fa_pre(DECL_ARGS
)
1463 struct mdoc_node
*n
;
1465 if (node
->parent
->tok
!= MDOC_Fo
) {
1466 pair
->flag
|= ttypes
[TTYPE_FUNC_ARG
];
1470 for (n
= node
->child
; n
; n
= n
->next
) {
1471 p
->flags
|= ttypes
[TTYPE_FUNC_ARG
];
1472 term_word(p
, n
->string
);
1473 p
->flags
&= ~ttypes
[TTYPE_FUNC_ARG
];
1478 if (node
->child
&& node
->next
&& node
->next
->tok
== MDOC_Fa
)
1487 termp_va_pre(DECL_ARGS
)
1490 pair
->flag
|= ttypes
[TTYPE_VAR_DECL
];
1497 termp_bd_pre(DECL_ARGS
)
1502 * This is fairly tricky due primarily to crappy documentation.
1503 * If -ragged or -filled are specified, the block does nothing
1504 * but change the indentation.
1506 * If, on the other hand, -unfilled or -literal are specified,
1507 * then the game changes. Text is printed exactly as entered in
1508 * the display: if a macro line, a newline is appended to the
1509 * line. Blank lines are allowed.
1512 if (MDOC_BLOCK
== node
->type
)
1513 return(fmt_block_vspace(p
, node
, node
));
1514 else if (MDOC_BODY
!= node
->type
)
1517 /* FIXME: display type should be mandated by parser. */
1519 if (NULL
== node
->parent
->args
)
1520 errx(1, "missing display type");
1522 for (type
= -1, i
= 0;
1523 i
< (int)node
->parent
->args
->argc
; i
++) {
1524 switch (node
->parent
->args
->argv
[i
].arg
) {
1529 case (MDOC_Unfilled
):
1531 case (MDOC_Literal
):
1532 type
= node
->parent
->args
->argv
[i
].arg
;
1533 i
= (int)node
->parent
->args
->argc
;
1540 if (NULL
== node
->parent
->args
)
1541 errx(1, "missing display type");
1543 i
= arg_getattr(MDOC_Offset
, node
->parent
);
1545 if (1 != node
->parent
->args
->argv
[i
].sz
)
1546 errx(1, "expected single value");
1547 p
->offset
+= arg_offset(&node
->parent
->args
->argv
[i
]);
1551 case (MDOC_Literal
):
1553 case (MDOC_Unfilled
):
1560 * Tricky. Iterate through all children. If we're on a
1561 * different parse line, append a newline and then the contents.
1565 p
->flags
|= TERMP_LITERAL
;
1566 ln
= node
->child
? node
->child
->line
: 0;
1568 for (node
= node
->child
; node
; node
= node
->next
) {
1569 if (ln
< node
->line
) {
1571 p
->flags
|= TERMP_NOSPACE
;
1574 print_node(p
, pair
, meta
, node
);
1583 termp_bd_post(DECL_ARGS
)
1586 if (MDOC_BODY
!= node
->type
)
1590 p
->flags
&= ~TERMP_LITERAL
;
1591 p
->flags
|= TERMP_NOSPACE
;
1597 termp_qq_pre(DECL_ARGS
)
1600 if (MDOC_BODY
!= node
->type
)
1603 p
->flags
|= TERMP_NOSPACE
;
1610 termp_qq_post(DECL_ARGS
)
1613 if (MDOC_BODY
!= node
->type
)
1615 p
->flags
|= TERMP_NOSPACE
;
1622 termp_bx_post(DECL_ARGS
)
1626 p
->flags
|= TERMP_NOSPACE
;
1627 term_word(p
, "BSD");
1633 termp_xx_pre(DECL_ARGS
)
1638 switch (node
->tok
) {
1643 pp
= "DragonFlyBSD";
1669 termp_sq_pre(DECL_ARGS
)
1672 if (MDOC_BODY
!= node
->type
)
1674 term_word(p
, "\\(oq");
1675 p
->flags
|= TERMP_NOSPACE
;
1682 termp_sq_post(DECL_ARGS
)
1685 if (MDOC_BODY
!= node
->type
)
1687 p
->flags
|= TERMP_NOSPACE
;
1688 term_word(p
, "\\(aq");
1694 termp_pf_pre(DECL_ARGS
)
1697 p
->flags
|= TERMP_IGNDELIM
;
1704 termp_pf_post(DECL_ARGS
)
1707 p
->flags
&= ~TERMP_IGNDELIM
;
1708 p
->flags
|= TERMP_NOSPACE
;
1714 termp_ss_pre(DECL_ARGS
)
1717 switch (node
->type
) {
1724 pair
->flag
|= ttypes
[TTYPE_SSECTION
];
1725 p
->offset
= HALFINDENT
;
1737 termp_ss_post(DECL_ARGS
)
1740 if (MDOC_HEAD
== node
->type
)
1747 termp_pa_pre(DECL_ARGS
)
1750 pair
->flag
|= ttypes
[TTYPE_FILE
];
1757 termp_em_pre(DECL_ARGS
)
1760 pair
->flag
|= ttypes
[TTYPE_EMPH
];
1767 termp_cd_pre(DECL_ARGS
)
1770 pair
->flag
|= ttypes
[TTYPE_CONFIG
];
1778 termp_cm_pre(DECL_ARGS
)
1781 pair
->flag
|= ttypes
[TTYPE_CMD_FLAG
];
1788 termp_ic_pre(DECL_ARGS
)
1791 pair
->flag
|= ttypes
[TTYPE_CMD
];
1798 termp_in_pre(DECL_ARGS
)
1801 pair
->flag
|= ttypes
[TTYPE_INCLUDE
];
1802 p
->flags
|= ttypes
[TTYPE_INCLUDE
];
1804 if (SEC_SYNOPSIS
== node
->sec
)
1805 term_word(p
, "#include");
1808 p
->flags
|= TERMP_NOSPACE
;
1815 termp_in_post(DECL_ARGS
)
1818 p
->flags
|= TERMP_NOSPACE
;
1821 if (SEC_SYNOPSIS
!= node
->sec
)
1826 * XXX Not entirely correct. If `.In foo bar' is specified in
1827 * the SYNOPSIS section, then it produces a single break after
1828 * the <foo>; mandoc asserts a vertical space. Since this
1829 * construction is rarely used, I think it's fine.
1831 if (node
->next
&& MDOC_In
!= node
->next
->tok
)
1838 termp_at_pre(DECL_ARGS
)
1845 att
= mdoc_a2att(node
->child
->string
);
1856 termp_brq_pre(DECL_ARGS
)
1859 if (MDOC_BODY
!= node
->type
)
1861 term_word(p
, "\\(lC");
1862 p
->flags
|= TERMP_NOSPACE
;
1869 termp_brq_post(DECL_ARGS
)
1872 if (MDOC_BODY
!= node
->type
)
1874 p
->flags
|= TERMP_NOSPACE
;
1875 term_word(p
, "\\(rC");
1881 termp_bq_pre(DECL_ARGS
)
1884 if (MDOC_BODY
!= node
->type
)
1886 term_word(p
, "\\(lB");
1887 p
->flags
|= TERMP_NOSPACE
;
1894 termp_bq_post(DECL_ARGS
)
1897 if (MDOC_BODY
!= node
->type
)
1899 p
->flags
|= TERMP_NOSPACE
;
1900 term_word(p
, "\\(rB");
1906 termp_pq_pre(DECL_ARGS
)
1909 if (MDOC_BODY
!= node
->type
)
1911 term_word(p
, "\\&(");
1912 p
->flags
|= TERMP_NOSPACE
;
1919 termp_pq_post(DECL_ARGS
)
1922 if (MDOC_BODY
!= node
->type
)
1930 termp_fo_pre(DECL_ARGS
)
1932 const struct mdoc_node
*n
;
1934 if (MDOC_BODY
== node
->type
) {
1936 p
->flags
|= TERMP_NOSPACE
;
1938 } else if (MDOC_HEAD
!= node
->type
)
1941 /* XXX - groff shows only first parameter */
1943 p
->flags
|= ttypes
[TTYPE_FUNC_NAME
];
1944 for (n
= node
->child
; n
; n
= n
->next
) {
1945 assert(MDOC_TEXT
== n
->type
);
1946 term_word(p
, n
->string
);
1948 p
->flags
&= ~ttypes
[TTYPE_FUNC_NAME
];
1956 termp_fo_post(DECL_ARGS
)
1959 if (MDOC_BODY
!= node
->type
)
1961 p
->flags
|= TERMP_NOSPACE
;
1963 p
->flags
|= TERMP_NOSPACE
;
1971 termp_bf_pre(DECL_ARGS
)
1973 const struct mdoc_node
*n
;
1975 if (MDOC_HEAD
== node
->type
)
1977 else if (MDOC_BLOCK
!= node
->type
)
1980 if (NULL
== (n
= node
->head
->child
)) {
1981 if (arg_hasattr(MDOC_Emphasis
, node
))
1982 pair
->flag
|= ttypes
[TTYPE_EMPH
];
1983 else if (arg_hasattr(MDOC_Symbolic
, node
))
1984 pair
->flag
|= ttypes
[TTYPE_SYMB
];
1989 assert(MDOC_TEXT
== n
->type
);
1990 if (0 == strcmp("Em", n
->string
))
1991 pair
->flag
|= ttypes
[TTYPE_EMPH
];
1992 else if (0 == strcmp("Sy", n
->string
))
1993 pair
->flag
|= ttypes
[TTYPE_SYMB
];
2001 termp_sy_pre(DECL_ARGS
)
2004 pair
->flag
|= ttypes
[TTYPE_SYMB
];
2011 termp_ms_pre(DECL_ARGS
)
2014 pair
->flag
|= ttypes
[TTYPE_SYMBOL
];
2022 termp_sm_pre(DECL_ARGS
)
2025 assert(node
->child
&& MDOC_TEXT
== node
->child
->type
);
2026 if (0 == strcmp("on", node
->child
->string
)) {
2027 p
->flags
&= ~TERMP_NONOSPACE
;
2028 p
->flags
&= ~TERMP_NOSPACE
;
2030 p
->flags
|= TERMP_NONOSPACE
;
2038 termp_ap_pre(DECL_ARGS
)
2041 p
->flags
|= TERMP_NOSPACE
;
2042 term_word(p
, "\\(aq");
2043 p
->flags
|= TERMP_NOSPACE
;
2050 termp__j_pre(DECL_ARGS
)
2053 pair
->flag
|= ttypes
[TTYPE_REF_JOURNAL
];
2060 termp__t_pre(DECL_ARGS
)
2064 p
->flags
|= TERMP_NOSPACE
;
2071 termp__t_post(DECL_ARGS
)
2074 p
->flags
|= TERMP_NOSPACE
;
2076 termp____post(p
, pair
, meta
, node
);
2082 termp____post(DECL_ARGS
)
2085 p
->flags
|= TERMP_NOSPACE
;
2086 term_word(p
, node
->next
? "," : ".");
2092 termp_lk_pre(DECL_ARGS
)
2094 const struct mdoc_node
*n
;
2096 assert(node
->child
);
2099 if (NULL
== n
->next
) {
2100 pair
->flag
|= ttypes
[TTYPE_LINK_ANCHOR
];
2104 p
->flags
|= ttypes
[TTYPE_LINK_ANCHOR
];
2105 term_word(p
, n
->string
);
2106 p
->flags
|= TERMP_NOSPACE
;
2108 p
->flags
&= ~ttypes
[TTYPE_LINK_ANCHOR
];
2110 p
->flags
|= ttypes
[TTYPE_LINK_TEXT
];
2111 for (n
= n
->next
; n
; n
= n
->next
)
2112 term_word(p
, n
->string
);
2114 p
->flags
&= ~ttypes
[TTYPE_LINK_TEXT
];
2121 termp_mt_pre(DECL_ARGS
)
2124 pair
->flag
|= ttypes
[TTYPE_LINK_ANCHOR
];