]>
git.cameronkatri.com Git - mandoc.git/blob - mdoc_term.c
1 /* $Id: mdoc_term.c,v 1.12 2009/06/12 09:18:00 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 */
83 /* XXX - clean this up. */
86 struct termpair
*ppair
;
88 #define TERMPAIR_FLAG (1 << 0)
89 int flag
; /* Whether being used. */
90 size_t offset
; /* Left margin. */
91 size_t rmargin
; /* Right margin. */
92 int count
; /* Enum count. */
95 #define TERMPAIR_SETFLAG(termp, p, fl) \
97 assert(! (TERMPAIR_FLAG & (p)->type)); \
98 (termp)->flags |= (fl); \
100 (p)->type |= TERMPAIR_FLAG; \
101 } while ( /* CONSTCOND */ 0)
104 struct termp *p, struct termpair *pair, \
105 const struct mdoc_meta *meta, \
106 const struct mdoc_node *node
108 #define DECL_PRE(name) \
109 static int name##_pre(DECL_ARGS)
110 #define DECL_POST(name) \
111 static void name##_post(DECL_ARGS)
112 #define DECL_PREPOST(name) \
116 DECL_PREPOST(termp__t
);
117 DECL_PREPOST(termp_aq
);
118 DECL_PREPOST(termp_bd
);
119 DECL_PREPOST(termp_bq
);
120 DECL_PREPOST(termp_brq
);
121 DECL_PREPOST(termp_d1
);
122 DECL_PREPOST(termp_dq
);
123 DECL_PREPOST(termp_fd
);
124 DECL_PREPOST(termp_fn
);
125 DECL_PREPOST(termp_fo
);
126 DECL_PREPOST(termp_ft
);
127 DECL_PREPOST(termp_in
);
128 DECL_PREPOST(termp_it
);
129 DECL_PREPOST(termp_lb
);
130 DECL_PREPOST(termp_op
);
131 DECL_PREPOST(termp_pf
);
132 DECL_PREPOST(termp_pq
);
133 DECL_PREPOST(termp_qq
);
134 DECL_PREPOST(termp_sh
);
135 DECL_PREPOST(termp_ss
);
136 DECL_PREPOST(termp_sq
);
137 DECL_PREPOST(termp_vt
);
181 int (*pre
)(DECL_ARGS
);
182 void (*post
)(DECL_ARGS
);
185 static const struct termact termacts
[MDOC_MAX
] = {
186 { NULL
, NULL
}, /* \" */
187 { NULL
, NULL
}, /* Dd */
188 { NULL
, NULL
}, /* Dt */
189 { NULL
, NULL
}, /* Os */
190 { termp_sh_pre
, termp_sh_post
}, /* Sh */
191 { termp_ss_pre
, termp_ss_post
}, /* Ss */
192 { termp_pp_pre
, NULL
}, /* Pp */
193 { termp_d1_pre
, termp_d1_post
}, /* D1 */
194 { termp_d1_pre
, termp_d1_post
}, /* Dl */
195 { termp_bd_pre
, termp_bd_post
}, /* Bd */
196 { NULL
, NULL
}, /* Ed */
197 { NULL
, termp_bl_post
}, /* Bl */
198 { NULL
, NULL
}, /* El */
199 { termp_it_pre
, termp_it_post
}, /* It */
200 { NULL
, NULL
}, /* Ad */
201 { NULL
, NULL
}, /* An */
202 { termp_ar_pre
, NULL
}, /* Ar */
203 { termp_cd_pre
, NULL
}, /* Cd */
204 { termp_cm_pre
, NULL
}, /* Cm */
205 { NULL
, NULL
}, /* Dv */
206 { NULL
, NULL
}, /* Er */
207 { NULL
, NULL
}, /* Ev */
208 { termp_ex_pre
, NULL
}, /* Ex */
209 { termp_fa_pre
, NULL
}, /* Fa */
210 { termp_fd_pre
, termp_fd_post
}, /* Fd */
211 { termp_fl_pre
, NULL
}, /* Fl */
212 { termp_fn_pre
, termp_fn_post
}, /* Fn */
213 { termp_ft_pre
, termp_ft_post
}, /* Ft */
214 { termp_ic_pre
, NULL
}, /* Ic */
215 { termp_in_pre
, termp_in_post
}, /* In */
216 { NULL
, NULL
}, /* Li */
217 { termp_nd_pre
, NULL
}, /* Nd */
218 { termp_nm_pre
, NULL
}, /* Nm */
219 { termp_op_pre
, termp_op_post
}, /* Op */
220 { NULL
, NULL
}, /* Ot */
221 { termp_pa_pre
, NULL
}, /* Pa */
222 { termp_rv_pre
, NULL
}, /* Rv */
223 { termp_st_pre
, NULL
}, /* St */
224 { termp_va_pre
, NULL
}, /* Va */
225 { termp_vt_pre
, termp_vt_post
}, /* Vt */
226 { termp_xr_pre
, NULL
}, /* Xr */
227 { NULL
, termp____post
}, /* %A */
228 { NULL
, termp____post
}, /* %B */
229 { NULL
, termp____post
}, /* %D */
230 { NULL
, termp____post
}, /* %I */
231 { termp__j_pre
, termp____post
}, /* %J */
232 { NULL
, termp____post
}, /* %N */
233 { NULL
, termp____post
}, /* %O */
234 { NULL
, termp____post
}, /* %P */
235 { NULL
, termp____post
}, /* %R */
236 { termp__t_pre
, termp__t_post
}, /* %T */
237 { NULL
, termp____post
}, /* %V */
238 { NULL
, NULL
}, /* Ac */
239 { termp_aq_pre
, termp_aq_post
}, /* Ao */
240 { termp_aq_pre
, termp_aq_post
}, /* Aq */
241 { termp_at_pre
, NULL
}, /* At */
242 { NULL
, NULL
}, /* Bc */
243 { termp_bf_pre
, NULL
}, /* Bf */
244 { termp_bq_pre
, termp_bq_post
}, /* Bo */
245 { termp_bq_pre
, termp_bq_post
}, /* Bq */
246 { termp_bsx_pre
, NULL
}, /* Bsx */
247 { NULL
, termp_bx_post
}, /* Bx */
248 { NULL
, NULL
}, /* Db */
249 { NULL
, NULL
}, /* Dc */
250 { termp_dq_pre
, termp_dq_post
}, /* Do */
251 { termp_dq_pre
, termp_dq_post
}, /* Dq */
252 { NULL
, NULL
}, /* Ec */
253 { NULL
, NULL
}, /* Ef */
254 { termp_em_pre
, NULL
}, /* Em */
255 { NULL
, NULL
}, /* Eo */
256 { termp_fx_pre
, NULL
}, /* Fx */
257 { termp_ms_pre
, NULL
}, /* Ms */
258 { NULL
, NULL
}, /* No */
259 { termp_ns_pre
, NULL
}, /* Ns */
260 { termp_nx_pre
, NULL
}, /* Nx */
261 { termp_ox_pre
, NULL
}, /* Ox */
262 { NULL
, NULL
}, /* Pc */
263 { termp_pf_pre
, termp_pf_post
}, /* Pf */
264 { termp_pq_pre
, termp_pq_post
}, /* Po */
265 { termp_pq_pre
, termp_pq_post
}, /* Pq */
266 { NULL
, NULL
}, /* Qc */
267 { termp_sq_pre
, termp_sq_post
}, /* Ql */
268 { termp_qq_pre
, termp_qq_post
}, /* Qo */
269 { termp_qq_pre
, termp_qq_post
}, /* Qq */
270 { NULL
, NULL
}, /* Re */
271 { termp_rs_pre
, NULL
}, /* Rs */
272 { NULL
, NULL
}, /* Sc */
273 { termp_sq_pre
, termp_sq_post
}, /* So */
274 { termp_sq_pre
, termp_sq_post
}, /* Sq */
275 { termp_sm_pre
, NULL
}, /* Sm */
276 { termp_sx_pre
, NULL
}, /* Sx */
277 { termp_sy_pre
, NULL
}, /* Sy */
278 { NULL
, NULL
}, /* Tn */
279 { termp_ux_pre
, NULL
}, /* Ux */
280 { NULL
, NULL
}, /* Xc */
281 { NULL
, NULL
}, /* Xo */
282 { termp_fo_pre
, termp_fo_post
}, /* Fo */
283 { NULL
, NULL
}, /* Fc */
284 { termp_op_pre
, termp_op_post
}, /* Oo */
285 { NULL
, NULL
}, /* Oc */
286 { NULL
, NULL
}, /* Bk */
287 { NULL
, NULL
}, /* Ek */
288 { termp_bt_pre
, NULL
}, /* Bt */
289 { NULL
, NULL
}, /* Hf */
290 { NULL
, NULL
}, /* Fr */
291 { termp_ud_pre
, NULL
}, /* Ud */
292 { termp_lb_pre
, termp_lb_post
}, /* Lb */
293 { termp_ap_pre
, NULL
}, /* Lb */
294 { termp_pp_pre
, NULL
}, /* Pp */
295 { termp_lk_pre
, NULL
}, /* Lk */
296 { termp_mt_pre
, NULL
}, /* Mt */
297 { termp_brq_pre
, termp_brq_post
}, /* Brq */
298 { termp_brq_pre
, termp_brq_post
}, /* Bro */
299 { NULL
, NULL
}, /* Brc */
300 { NULL
, NULL
}, /* %C */
301 { NULL
, NULL
}, /* Es */
302 { NULL
, NULL
}, /* En */
303 { termp_dx_pre
, NULL
}, /* Dx */
304 { NULL
, NULL
}, /* %Q */
308 extern size_t strlcpy(char *, const char *, size_t);
309 extern size_t strlcat(char *, const char *, size_t);
312 static int arg_hasattr(int, const struct mdoc_node
*);
313 static int arg_getattrs(const int *, int *, size_t,
314 const struct mdoc_node
*);
315 static int arg_getattr(int, const struct mdoc_node
*);
316 static size_t arg_offset(const struct mdoc_argv
*);
317 static size_t arg_width(const struct mdoc_argv
*, int);
318 static int arg_listtype(const struct mdoc_node
*);
319 static int fmt_block_vspace(struct termp
*,
320 const struct mdoc_node
*,
321 const struct mdoc_node
*);
322 static void print_node(DECL_ARGS
);
323 static void print_head(struct termp
*,
324 const struct mdoc_meta
*);
325 static void print_body(DECL_ARGS
);
326 static void print_foot(struct termp
*,
327 const struct mdoc_meta
*);
331 mdoc_run(struct termp
*p
, const struct mdoc
*m
)
334 * Main output function. When this is called, assume that the
335 * tree is properly formed.
338 print_head(p
, mdoc_meta(m
));
339 print_body(p
, NULL
, mdoc_meta(m
), mdoc_node(m
));
340 print_foot(p
, mdoc_meta(m
));
346 print_body(DECL_ARGS
)
349 print_node(p
, pair
, meta
, node
);
352 print_body(p
, pair
, meta
, node
->next
);
357 print_node(DECL_ARGS
)
360 struct termpair npair
;
362 /* Pre-processing. */
367 npair
.offset
= npair
.rmargin
= 0;
371 if (MDOC_TEXT
!= node
->type
) {
372 if (termacts
[node
->tok
].pre
)
373 if ( ! (*termacts
[node
->tok
].pre
)(p
, &npair
, meta
, node
))
375 } else /* MDOC_TEXT == node->type */
376 term_word(p
, node
->string
);
380 if (TERMPAIR_FLAG
& npair
.type
)
381 p
->flags
|= npair
.flag
;
383 if (dochild
&& node
->child
)
384 print_body(p
, &npair
, meta
, node
->child
);
386 if (TERMPAIR_FLAG
& npair
.type
)
387 p
->flags
&= ~npair
.flag
;
389 /* Post-processing. */
391 if (MDOC_TEXT
!= node
->type
)
392 if (termacts
[node
->tok
].post
)
393 (*termacts
[node
->tok
].post
)(p
, &npair
, meta
, node
);
398 print_foot(struct termp
*p
, const struct mdoc_meta
*meta
)
404 * Output the footer in new-groff style, that is, three columns
405 * with the middle being the manual date and flanking columns
406 * being the operating system:
411 if (NULL
== (buf
= malloc(p
->rmargin
)))
413 if (NULL
== (os
= malloc(p
->rmargin
)))
416 tm
= localtime(&meta
->date
);
419 if (NULL
== strftime(buf
, p
->rmargin
, "%B %d, %Y", tm
))
421 if (0 == strftime(buf
, p
->rmargin
, "%B %d, %Y", tm
))
425 (void)strlcpy(os
, meta
->os
, p
->rmargin
);
430 p
->rmargin
= (p
->maxrmargin
- strlen(buf
) + 1) / 2;
431 p
->flags
|= TERMP_NOSPACE
| TERMP_NOBREAK
;
436 p
->offset
= p
->rmargin
;
437 p
->rmargin
= p
->maxrmargin
- strlen(os
);
438 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
443 p
->offset
= p
->rmargin
;
444 p
->rmargin
= p
->maxrmargin
;
445 p
->flags
&= ~TERMP_NOBREAK
;
446 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
452 p
->rmargin
= p
->maxrmargin
;
461 print_head(struct termp
*p
, const struct mdoc_meta
*meta
)
465 p
->rmargin
= p
->maxrmargin
;
468 if (NULL
== (buf
= malloc(p
->rmargin
)))
470 if (NULL
== (title
= malloc(p
->rmargin
)))
474 * The header is strange. It has three components, which are
475 * really two with the first duplicated. It goes like this:
477 * IDENTIFIER TITLE IDENTIFIER
479 * The IDENTIFIER is NAME(SECTION), which is the command-name
480 * (if given, or "unknown" if not) followed by the manual page
481 * section. These are given in `Dt'. The TITLE is a free-form
482 * string depending on the manual volume. If not specified, it
483 * switches on the manual section.
487 (void)strlcpy(buf
, meta
->vol
, p
->rmargin
);
490 (void)strlcat(buf
, " (", p
->rmargin
);
491 (void)strlcat(buf
, meta
->arch
, p
->rmargin
);
492 (void)strlcat(buf
, ")", p
->rmargin
);
495 (void)snprintf(title
, p
->rmargin
, "%s(%d)",
496 meta
->title
, meta
->msec
);
499 p
->rmargin
= (p
->maxrmargin
- strlen(buf
) + 1) / 2;
500 p
->flags
|= TERMP_NOBREAK
| TERMP_NOSPACE
;
505 p
->offset
= p
->rmargin
;
506 p
->rmargin
= p
->maxrmargin
- strlen(title
);
507 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
512 p
->offset
= p
->rmargin
;
513 p
->rmargin
= p
->maxrmargin
;
514 p
->flags
&= ~TERMP_NOBREAK
;
515 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
521 p
->rmargin
= p
->maxrmargin
;
522 p
->flags
&= ~TERMP_NOSPACE
;
530 arg_width(const struct mdoc_argv
*arg
, int pos
)
535 assert(pos
< (int)arg
->sz
&& pos
>= 0);
536 assert(arg
->value
[pos
]);
537 if (0 == strcmp(arg
->value
[pos
], "indent"))
539 if (0 == strcmp(arg
->value
[pos
], "indent-two"))
542 if (0 == (len
= (int)strlen(arg
->value
[pos
])))
545 for (i
= 0; i
< len
- 1; i
++)
546 if ( ! isdigit((u_char
)arg
->value
[pos
][i
]))
550 if ('n' == arg
->value
[pos
][len
- 1]) {
551 v
= (size_t)atoi(arg
->value
[pos
]);
556 return(strlen(arg
->value
[pos
]) + 1);
561 arg_listtype(const struct mdoc_node
*n
)
565 assert(MDOC_BLOCK
== n
->type
);
567 len
= (int)(n
->args
? n
->args
->argc
: 0);
569 for (i
= 0; i
< len
; i
++)
570 switch (n
->args
->argv
[i
].arg
) {
590 return(n
->args
->argv
[i
].arg
);
595 /* FIXME: mandated by parser. */
597 errx(1, "list type not supported");
603 arg_offset(const struct mdoc_argv
*arg
)
607 if (0 == strcmp(*arg
->value
, "left"))
609 if (0 == strcmp(*arg
->value
, "indent"))
611 if (0 == strcmp(*arg
->value
, "indent-two"))
614 /* FIXME: needs to support field-widths (10n, etc.). */
616 return(strlen(*arg
->value
));
621 arg_hasattr(int arg
, const struct mdoc_node
*n
)
624 return(-1 != arg_getattr(arg
, n
));
629 arg_getattr(int v
, const struct mdoc_node
*n
)
633 return(arg_getattrs(&v
, &val
, 1, n
) ? val
: -1);
638 arg_getattrs(const int *keys
, int *vals
,
639 size_t sz
, const struct mdoc_node
*n
)
646 for (k
= i
= 0; i
< (int)n
->args
->argc
; i
++)
647 for (j
= 0; j
< (int)sz
; j
++)
648 if (n
->args
->argv
[i
].arg
== keys
[j
]) {
658 fmt_block_vspace(struct termp
*p
,
659 const struct mdoc_node
*bl
,
660 const struct mdoc_node
*node
)
662 const struct mdoc_node
*n
;
666 if (arg_hasattr(MDOC_Compact
, bl
))
669 for (n
= node
; n
; n
= n
->parent
) {
670 if (MDOC_BLOCK
!= n
->type
)
672 if (MDOC_Ss
== n
->tok
)
674 if (MDOC_Sh
== n
->tok
)
688 termp_dq_pre(DECL_ARGS
)
691 if (MDOC_BODY
!= node
->type
)
694 term_word(p
, "\\(lq");
695 p
->flags
|= TERMP_NOSPACE
;
702 termp_dq_post(DECL_ARGS
)
705 if (MDOC_BODY
!= node
->type
)
708 p
->flags
|= TERMP_NOSPACE
;
709 term_word(p
, "\\(rq");
715 termp_it_pre(DECL_ARGS
)
717 const struct mdoc_node
*bl
, *n
;
719 int i
, type
, keys
[3], vals
[3];
720 size_t width
, offset
;
722 if (MDOC_BLOCK
== node
->type
)
723 return(fmt_block_vspace(p
, node
->parent
->parent
, node
));
725 bl
= node
->parent
->parent
->parent
;
727 /* Save parent attributes. */
729 pair
->offset
= p
->offset
;
730 pair
->rmargin
= p
->rmargin
;
731 pair
->flag
= p
->flags
;
733 /* Get list width and offset. */
735 keys
[0] = MDOC_Width
;
736 keys
[1] = MDOC_Offset
;
737 keys
[2] = MDOC_Column
;
739 vals
[0] = vals
[1] = vals
[2] = -1;
743 (void)arg_getattrs(keys
, vals
, 3, bl
);
745 type
= arg_listtype(bl
);
747 /* Calculate real width and offset. */
751 if (MDOC_BODY
== node
->type
)
753 for (i
= 0, n
= node
->prev
; n
; n
= n
->prev
, i
++)
755 (&bl
->args
->argv
[vals
[2]], i
);
756 assert(i
< (int)bl
->args
->argv
[vals
[2]].sz
);
757 width
= arg_width(&bl
->args
->argv
[vals
[2]], i
);
759 offset
+= arg_offset(&bl
->args
->argv
[vals
[1]]);
763 width
= arg_width(&bl
->args
->argv
[vals
[0]], 0);
765 offset
= arg_offset(&bl
->args
->argv
[vals
[1]]);
770 * List-type can override the width in the case of fixed-head
771 * values (bullet, dash/hyphen, enum). Tags need a non-zero
795 * Whitespace control. Inset bodies need an initial space.
802 if (MDOC_BODY
== node
->type
)
803 p
->flags
&= ~TERMP_NOSPACE
;
805 p
->flags
|= TERMP_NOSPACE
;
808 p
->flags
|= TERMP_NOSPACE
;
813 * Style flags. Diagnostic heads need TTYPE_DIAG.
818 if (MDOC_HEAD
== node
->type
)
819 p
->flags
|= ttypes
[TTYPE_DIAG
];
826 * Pad and break control. This is the tricker part. Lists with
827 * set right-margins for the head get TERMP_NOBREAK because, if
828 * they overrun the margin, they wrap to the new margin.
829 * Correspondingly, the body for these types don't left-pad, as
830 * the head will pad out to to the right.
843 if (MDOC_HEAD
== node
->type
)
844 p
->flags
|= TERMP_NOBREAK
;
846 p
->flags
|= TERMP_NOLPAD
;
847 if (MDOC_HEAD
== node
->type
&& MDOC_Tag
== type
)
848 if (NULL
== node
->next
||
849 NULL
== node
->next
->child
)
850 p
->flags
|= TERMP_NONOBREAK
;
853 if (MDOC_HEAD
== node
->type
) {
855 if (MDOC_BODY
== node
->next
->type
)
856 p
->flags
&= ~TERMP_NOBREAK
;
858 p
->flags
|= TERMP_NOBREAK
;
860 p
->flags
|= TERMP_NOLPAD
;
864 if (MDOC_HEAD
== node
->type
)
865 p
->flags
|= TERMP_NOBREAK
;
872 * Margin control. Set-head-width lists have their right
873 * margins shortened. The body for these lists has the offset
874 * necessarily lengthened. Everybody gets the offset.
889 if (MDOC_HEAD
== node
->type
)
890 p
->rmargin
= p
->offset
+ width
;
895 p
->rmargin
= p
->offset
+ width
;
902 * The dash, hyphen, bullet and enum lists all have a special
903 * HEAD character. Print it now.
906 if (MDOC_HEAD
== node
->type
)
909 term_word(p
, "\\[bu]");
917 (pair
->ppair
->ppair
->count
)++;
918 (void)snprintf(buf
, sizeof(buf
), "%d.",
919 pair
->ppair
->ppair
->count
);
927 * If we're not going to process our children, indicate so here.
940 if (MDOC_HEAD
== node
->type
)
944 if (MDOC_BODY
== node
->type
)
957 termp_it_post(DECL_ARGS
)
961 if (MDOC_BODY
!= node
->type
&& MDOC_HEAD
!= node
->type
)
964 type
= arg_listtype(node
->parent
->parent
->parent
);
972 if (MDOC_BODY
== node
->type
)
976 if (MDOC_HEAD
== node
->type
)
984 p
->offset
= pair
->offset
;
985 p
->rmargin
= pair
->rmargin
;
986 p
->flags
= pair
->flag
;
992 termp_nm_pre(DECL_ARGS
)
995 if (SEC_SYNOPSIS
== node
->sec
)
998 TERMPAIR_SETFLAG(p
, pair
, ttypes
[TTYPE_PROG
]);
999 if (NULL
== node
->child
)
1000 term_word(p
, meta
->name
);
1008 termp_fl_pre(DECL_ARGS
)
1011 TERMPAIR_SETFLAG(p
, pair
, ttypes
[TTYPE_CMD_FLAG
]);
1012 term_word(p
, "\\-");
1013 p
->flags
|= TERMP_NOSPACE
;
1020 termp_ar_pre(DECL_ARGS
)
1023 TERMPAIR_SETFLAG(p
, pair
, ttypes
[TTYPE_CMD_ARG
]);
1030 termp_ns_pre(DECL_ARGS
)
1033 p
->flags
|= TERMP_NOSPACE
;
1040 termp_pp_pre(DECL_ARGS
)
1050 termp_st_pre(DECL_ARGS
)
1054 if (node
->child
&& (cp
= mdoc_a2st(node
->child
->string
)))
1062 termp_rs_pre(DECL_ARGS
)
1065 if (MDOC_BLOCK
== node
->type
&& node
->prev
)
1073 termp_rv_pre(DECL_ARGS
)
1077 /* FIXME: mandated by parser. */
1079 if (-1 == (i
= arg_getattr(MDOC_Std
, node
)))
1080 errx(1, "expected -std argument");
1081 if (1 != node
->args
->argv
[i
].sz
)
1082 errx(1, "expected -std argument");
1085 term_word(p
, "The");
1087 p
->flags
|= ttypes
[TTYPE_FUNC_NAME
];
1088 term_word(p
, *node
->args
->argv
[i
].value
);
1089 p
->flags
&= ~ttypes
[TTYPE_FUNC_NAME
];
1090 p
->flags
|= TERMP_NOSPACE
;
1092 term_word(p
, "() function returns the value 0 if successful;");
1093 term_word(p
, "otherwise the value -1 is returned and the");
1094 term_word(p
, "global variable");
1096 p
->flags
|= ttypes
[TTYPE_VAR_DECL
];
1097 term_word(p
, "errno");
1098 p
->flags
&= ~ttypes
[TTYPE_VAR_DECL
];
1100 term_word(p
, "is set to indicate the error.");
1108 termp_ex_pre(DECL_ARGS
)
1112 /* FIXME: mandated by parser? */
1114 if (-1 == (i
= arg_getattr(MDOC_Std
, node
)))
1115 errx(1, "expected -std argument");
1116 if (1 != node
->args
->argv
[i
].sz
)
1117 errx(1, "expected -std argument");
1119 term_word(p
, "The");
1120 p
->flags
|= ttypes
[TTYPE_PROG
];
1121 term_word(p
, *node
->args
->argv
[i
].value
);
1122 p
->flags
&= ~ttypes
[TTYPE_PROG
];
1123 term_word(p
, "utility exits 0 on success, and >0 if an error occurs.");
1131 termp_nd_pre(DECL_ARGS
)
1134 term_word(p
, "\\-");
1141 termp_bl_post(DECL_ARGS
)
1144 if (MDOC_BLOCK
== node
->type
)
1151 termp_op_post(DECL_ARGS
)
1154 if (MDOC_BODY
!= node
->type
)
1156 p
->flags
|= TERMP_NOSPACE
;
1157 term_word(p
, "\\(rB");
1163 termp_xr_pre(DECL_ARGS
)
1165 const struct mdoc_node
*n
;
1167 assert(node
->child
&& MDOC_TEXT
== node
->child
->type
);
1170 term_word(p
, n
->string
);
1171 if (NULL
== (n
= n
->next
))
1173 p
->flags
|= TERMP_NOSPACE
;
1175 p
->flags
|= TERMP_NOSPACE
;
1176 term_word(p
, n
->string
);
1177 p
->flags
|= TERMP_NOSPACE
;
1185 termp_vt_pre(DECL_ARGS
)
1188 /* FIXME: this can be "type name". */
1189 TERMPAIR_SETFLAG(p
, pair
, ttypes
[TTYPE_VAR_DECL
]);
1196 termp_vt_post(DECL_ARGS
)
1199 if (node
->sec
== SEC_SYNOPSIS
)
1206 termp_fd_pre(DECL_ARGS
)
1210 * FIXME: this naming is bad. This value is used, in general,
1211 * for the #include header or other preprocessor statement.
1213 TERMPAIR_SETFLAG(p
, pair
, ttypes
[TTYPE_FUNC_DECL
]);
1220 termp_fd_post(DECL_ARGS
)
1223 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 TERMPAIR_SETFLAG(p
, pair
, 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 if ((lb
= mdoc_a2lib(node
->child
->string
))) {
1308 term_word(p
, "library");
1315 termp_lb_post(DECL_ARGS
)
1324 termp_ud_pre(DECL_ARGS
)
1327 term_word(p
, "currently under development.");
1334 termp_d1_pre(DECL_ARGS
)
1337 if (MDOC_BLOCK
!= node
->type
)
1340 p
->offset
+= (pair
->offset
= INDENT
);
1347 termp_d1_post(DECL_ARGS
)
1350 if (MDOC_BLOCK
!= node
->type
)
1353 p
->offset
-= pair
->offset
;
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 TERMPAIR_SETFLAG(p
, pair
, 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
)
1452 termp_sx_pre(DECL_ARGS
)
1455 TERMPAIR_SETFLAG(p
, pair
, ttypes
[TTYPE_LINK
]);
1462 termp_fa_pre(DECL_ARGS
)
1464 struct mdoc_node
*n
;
1466 if (node
->parent
->tok
!= MDOC_Fo
) {
1467 TERMPAIR_SETFLAG(p
, pair
, ttypes
[TTYPE_FUNC_ARG
]);
1471 for (n
= node
->child
; n
; n
= n
->next
) {
1472 p
->flags
|= ttypes
[TTYPE_FUNC_ARG
];
1473 term_word(p
, n
->string
);
1474 p
->flags
&= ~ttypes
[TTYPE_FUNC_ARG
];
1479 if (node
->child
&& node
->next
&& node
->next
->tok
== MDOC_Fa
)
1488 termp_va_pre(DECL_ARGS
)
1491 TERMPAIR_SETFLAG(p
, pair
, ttypes
[TTYPE_VAR_DECL
]);
1498 termp_bd_pre(DECL_ARGS
)
1503 * This is fairly tricky due primarily to crappy documentation.
1504 * If -ragged or -filled are specified, the block does nothing
1505 * but change the indentation.
1507 * If, on the other hand, -unfilled or -literal are specified,
1508 * then the game changes. Text is printed exactly as entered in
1509 * the display: if a macro line, a newline is appended to the
1510 * line. Blank lines are allowed.
1513 if (MDOC_BLOCK
== node
->type
)
1514 return(fmt_block_vspace(p
, node
, node
));
1515 else if (MDOC_BODY
!= node
->type
)
1518 /* FIXME: display type should be mandated by parser. */
1520 if (NULL
== node
->parent
->args
)
1521 errx(1, "missing display type");
1523 pair
->offset
= p
->offset
;
1525 for (type
= -1, i
= 0;
1526 i
< (int)node
->parent
->args
->argc
; i
++) {
1527 switch (node
->parent
->args
->argv
[i
].arg
) {
1532 case (MDOC_Unfilled
):
1534 case (MDOC_Literal
):
1535 type
= node
->parent
->args
->argv
[i
].arg
;
1536 i
= (int)node
->parent
->args
->argc
;
1543 if (NULL
== node
->parent
->args
)
1544 errx(1, "missing display type");
1546 i
= arg_getattr(MDOC_Offset
, node
->parent
);
1548 if (1 != node
->parent
->args
->argv
[i
].sz
)
1549 errx(1, "expected single value");
1550 p
->offset
+= arg_offset(&node
->parent
->args
->argv
[i
]);
1554 case (MDOC_Literal
):
1556 case (MDOC_Unfilled
):
1563 * Tricky. Iterate through all children. If we're on a
1564 * different parse line, append a newline and then the contents.
1568 p
->flags
|= TERMP_LITERAL
;
1569 ln
= node
->child
? node
->child
->line
: 0;
1571 for (node
= node
->child
; node
; node
= node
->next
) {
1572 if (ln
< node
->line
) {
1574 p
->flags
|= TERMP_NOSPACE
;
1577 print_node(p
, pair
, meta
, node
);
1586 termp_bd_post(DECL_ARGS
)
1589 if (MDOC_BODY
!= node
->type
)
1593 p
->flags
&= ~TERMP_LITERAL
;
1594 p
->offset
= pair
->offset
;
1595 p
->flags
|= TERMP_NOSPACE
;
1601 termp_qq_pre(DECL_ARGS
)
1604 if (MDOC_BODY
!= node
->type
)
1607 p
->flags
|= TERMP_NOSPACE
;
1614 termp_qq_post(DECL_ARGS
)
1617 if (MDOC_BODY
!= node
->type
)
1619 p
->flags
|= TERMP_NOSPACE
;
1626 termp_bsx_pre(DECL_ARGS
)
1629 term_word(p
, "BSDI BSD/OS");
1636 termp_bx_post(DECL_ARGS
)
1640 p
->flags
|= TERMP_NOSPACE
;
1641 term_word(p
, "BSD");
1647 termp_ox_pre(DECL_ARGS
)
1650 term_word(p
, "OpenBSD");
1657 termp_dx_pre(DECL_ARGS
)
1660 term_word(p
, "DragonFly");
1667 termp_ux_pre(DECL_ARGS
)
1670 term_word(p
, "UNIX");
1677 termp_fx_pre(DECL_ARGS
)
1680 term_word(p
, "FreeBSD");
1687 termp_nx_pre(DECL_ARGS
)
1690 term_word(p
, "NetBSD");
1697 termp_sq_pre(DECL_ARGS
)
1700 if (MDOC_BODY
!= node
->type
)
1702 term_word(p
, "\\(oq");
1703 p
->flags
|= TERMP_NOSPACE
;
1710 termp_sq_post(DECL_ARGS
)
1713 if (MDOC_BODY
!= node
->type
)
1715 p
->flags
|= TERMP_NOSPACE
;
1716 term_word(p
, "\\(aq");
1722 termp_pf_pre(DECL_ARGS
)
1725 p
->flags
|= TERMP_IGNDELIM
;
1732 termp_pf_post(DECL_ARGS
)
1735 p
->flags
&= ~TERMP_IGNDELIM
;
1736 p
->flags
|= TERMP_NOSPACE
;
1742 termp_ss_pre(DECL_ARGS
)
1745 switch (node
->type
) {
1752 TERMPAIR_SETFLAG(p
, pair
, ttypes
[TTYPE_SSECTION
]);
1753 p
->offset
= HALFINDENT
;
1765 termp_ss_post(DECL_ARGS
)
1768 switch (node
->type
) {
1781 termp_pa_pre(DECL_ARGS
)
1784 TERMPAIR_SETFLAG(p
, pair
, ttypes
[TTYPE_FILE
]);
1791 termp_em_pre(DECL_ARGS
)
1794 TERMPAIR_SETFLAG(p
, pair
, ttypes
[TTYPE_EMPH
]);
1801 termp_cd_pre(DECL_ARGS
)
1804 TERMPAIR_SETFLAG(p
, pair
, ttypes
[TTYPE_CONFIG
]);
1812 termp_cm_pre(DECL_ARGS
)
1815 TERMPAIR_SETFLAG(p
, pair
, ttypes
[TTYPE_CMD_FLAG
]);
1822 termp_ic_pre(DECL_ARGS
)
1825 TERMPAIR_SETFLAG(p
, pair
, ttypes
[TTYPE_CMD
]);
1832 termp_in_pre(DECL_ARGS
)
1835 TERMPAIR_SETFLAG(p
, pair
, ttypes
[TTYPE_INCLUDE
]);
1836 term_word(p
, "#include");
1838 p
->flags
|= TERMP_NOSPACE
;
1845 termp_in_post(DECL_ARGS
)
1848 p
->flags
|= TERMP_NOSPACE
;
1852 if (SEC_SYNOPSIS
!= node
->sec
)
1854 if (node
->next
&& MDOC_In
!= node
->next
->tok
)
1861 termp_at_pre(DECL_ARGS
)
1868 att
= mdoc_a2att(node
->child
->string
);
1879 termp_brq_pre(DECL_ARGS
)
1882 if (MDOC_BODY
!= node
->type
)
1884 term_word(p
, "\\(lC");
1885 p
->flags
|= TERMP_NOSPACE
;
1892 termp_brq_post(DECL_ARGS
)
1895 if (MDOC_BODY
!= node
->type
)
1897 p
->flags
|= TERMP_NOSPACE
;
1898 term_word(p
, "\\(rC");
1904 termp_bq_pre(DECL_ARGS
)
1907 if (MDOC_BODY
!= node
->type
)
1909 term_word(p
, "\\(lB");
1910 p
->flags
|= TERMP_NOSPACE
;
1917 termp_bq_post(DECL_ARGS
)
1920 if (MDOC_BODY
!= node
->type
)
1922 p
->flags
|= TERMP_NOSPACE
;
1923 term_word(p
, "\\(rB");
1929 termp_pq_pre(DECL_ARGS
)
1932 if (MDOC_BODY
!= node
->type
)
1934 term_word(p
, "\\&(");
1935 p
->flags
|= TERMP_NOSPACE
;
1942 termp_pq_post(DECL_ARGS
)
1945 if (MDOC_BODY
!= node
->type
)
1953 termp_fo_pre(DECL_ARGS
)
1955 const struct mdoc_node
*n
;
1957 if (MDOC_BODY
== node
->type
) {
1959 p
->flags
|= TERMP_NOSPACE
;
1961 } else if (MDOC_HEAD
!= node
->type
)
1964 /* XXX - groff shows only first parameter */
1966 p
->flags
|= ttypes
[TTYPE_FUNC_NAME
];
1967 for (n
= node
->child
; n
; n
= n
->next
) {
1968 assert(MDOC_TEXT
== n
->type
);
1969 term_word(p
, n
->string
);
1971 p
->flags
&= ~ttypes
[TTYPE_FUNC_NAME
];
1979 termp_fo_post(DECL_ARGS
)
1982 if (MDOC_BODY
!= node
->type
)
1984 p
->flags
|= TERMP_NOSPACE
;
1986 p
->flags
|= TERMP_NOSPACE
;
1994 termp_bf_pre(DECL_ARGS
)
1996 const struct mdoc_node
*n
;
1998 if (MDOC_HEAD
== node
->type
) {
2000 } else if (MDOC_BLOCK
!= node
->type
)
2003 if (NULL
== (n
= node
->head
->child
)) {
2004 if (arg_hasattr(MDOC_Emphasis
, node
))
2005 TERMPAIR_SETFLAG(p
, pair
, ttypes
[TTYPE_EMPH
]);
2006 else if (arg_hasattr(MDOC_Symbolic
, node
))
2007 TERMPAIR_SETFLAG(p
, pair
, ttypes
[TTYPE_SYMB
]);
2012 assert(MDOC_TEXT
== n
->type
);
2013 if (0 == strcmp("Em", n
->string
))
2014 TERMPAIR_SETFLAG(p
, pair
, ttypes
[TTYPE_EMPH
]);
2015 else if (0 == strcmp("Sy", n
->string
))
2016 TERMPAIR_SETFLAG(p
, pair
, ttypes
[TTYPE_EMPH
]);
2024 termp_sy_pre(DECL_ARGS
)
2027 TERMPAIR_SETFLAG(p
, pair
, ttypes
[TTYPE_SYMB
]);
2034 termp_ms_pre(DECL_ARGS
)
2037 TERMPAIR_SETFLAG(p
, pair
, ttypes
[TTYPE_SYMBOL
]);
2045 termp_sm_pre(DECL_ARGS
)
2048 assert(node
->child
&& MDOC_TEXT
== node
->child
->type
);
2049 if (0 == strcmp("on", node
->child
->string
)) {
2050 p
->flags
&= ~TERMP_NONOSPACE
;
2051 p
->flags
&= ~TERMP_NOSPACE
;
2053 p
->flags
|= TERMP_NONOSPACE
;
2061 termp_ap_pre(DECL_ARGS
)
2064 p
->flags
|= TERMP_NOSPACE
;
2065 term_word(p
, "\\(aq");
2066 p
->flags
|= TERMP_NOSPACE
;
2073 termp__j_pre(DECL_ARGS
)
2076 TERMPAIR_SETFLAG(p
, pair
, ttypes
[TTYPE_REF_JOURNAL
]);
2083 termp__t_pre(DECL_ARGS
)
2087 p
->flags
|= TERMP_NOSPACE
;
2094 termp__t_post(DECL_ARGS
)
2097 p
->flags
|= TERMP_NOSPACE
;
2099 termp____post(p
, pair
, meta
, node
);
2105 termp____post(DECL_ARGS
)
2108 p
->flags
|= TERMP_NOSPACE
;
2109 term_word(p
, node
->next
? "," : ".");
2115 termp_lk_pre(DECL_ARGS
)
2117 const struct mdoc_node
*n
;
2119 assert(node
->child
);
2122 if (NULL
== n
->next
) {
2123 TERMPAIR_SETFLAG(p
, pair
, ttypes
[TTYPE_LINK_ANCHOR
]);
2127 p
->flags
|= ttypes
[TTYPE_LINK_ANCHOR
];
2128 term_word(p
, n
->string
);
2129 p
->flags
|= TERMP_NOSPACE
;
2131 p
->flags
&= ~ttypes
[TTYPE_LINK_ANCHOR
];
2133 p
->flags
|= ttypes
[TTYPE_LINK_TEXT
];
2134 for (n
= n
->next
; n
; n
= n
->next
)
2135 term_word(p
, n
->string
);
2137 p
->flags
&= ~ttypes
[TTYPE_LINK_TEXT
];
2144 termp_mt_pre(DECL_ARGS
)
2147 TERMPAIR_SETFLAG(p
, pair
, ttypes
[TTYPE_LINK_ANCHOR
]);