]>
git.cameronkatri.com Git - mandoc.git/blob - mdoc_term.c
1 /* $Id: mdoc_term.c,v 1.205 2010/12/25 23:27:50 kristaps Exp $ */
3 * Copyright (c) 2008, 2009, 2010 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2010 Ingo Schwarze <schwarze@openbsd.org>
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22 #include <sys/types.h>
42 struct termpair
*ppair
;
46 #define DECL_ARGS struct termp *p, \
47 struct termpair *pair, \
48 const struct mdoc_meta *m, \
49 const struct mdoc_node *n
52 int (*pre
)(DECL_ARGS
);
53 void (*post
)(DECL_ARGS
);
56 static size_t a2width(const struct termp
*, const char *);
57 static size_t a2height(const struct termp
*, const char *);
58 static size_t a2offs(const struct termp
*, const char *);
60 static void print_bvspace(struct termp
*,
61 const struct mdoc_node
*,
62 const struct mdoc_node
*);
63 static void print_mdoc_node(DECL_ARGS
);
64 static void print_mdoc_nodelist(DECL_ARGS
);
65 static void print_mdoc_head(struct termp
*, const void *);
66 static void print_mdoc_foot(struct termp
*, const void *);
67 static void synopsis_pre(struct termp
*,
68 const struct mdoc_node
*);
70 static void termp____post(DECL_ARGS
);
71 static void termp__t_post(DECL_ARGS
);
72 static void termp_an_post(DECL_ARGS
);
73 static void termp_bd_post(DECL_ARGS
);
74 static void termp_bk_post(DECL_ARGS
);
75 static void termp_bl_post(DECL_ARGS
);
76 static void termp_bx_post(DECL_ARGS
);
77 static void termp_d1_post(DECL_ARGS
);
78 static void termp_fo_post(DECL_ARGS
);
79 static void termp_in_post(DECL_ARGS
);
80 static void termp_it_post(DECL_ARGS
);
81 static void termp_lb_post(DECL_ARGS
);
82 static void termp_nm_post(DECL_ARGS
);
83 static void termp_pf_post(DECL_ARGS
);
84 static void termp_quote_post(DECL_ARGS
);
85 static void termp_sh_post(DECL_ARGS
);
86 static void termp_ss_post(DECL_ARGS
);
88 static int termp__a_pre(DECL_ARGS
);
89 static int termp__t_pre(DECL_ARGS
);
90 static int termp_an_pre(DECL_ARGS
);
91 static int termp_ap_pre(DECL_ARGS
);
92 static int termp_bd_pre(DECL_ARGS
);
93 static int termp_bf_pre(DECL_ARGS
);
94 static int termp_bk_pre(DECL_ARGS
);
95 static int termp_bl_pre(DECL_ARGS
);
96 static int termp_bold_pre(DECL_ARGS
);
97 static int termp_bt_pre(DECL_ARGS
);
98 static int termp_cd_pre(DECL_ARGS
);
99 static int termp_d1_pre(DECL_ARGS
);
100 static int termp_ex_pre(DECL_ARGS
);
101 static int termp_fa_pre(DECL_ARGS
);
102 static int termp_fd_pre(DECL_ARGS
);
103 static int termp_fl_pre(DECL_ARGS
);
104 static int termp_fn_pre(DECL_ARGS
);
105 static int termp_fo_pre(DECL_ARGS
);
106 static int termp_ft_pre(DECL_ARGS
);
107 static int termp_igndelim_pre(DECL_ARGS
);
108 static int termp_in_pre(DECL_ARGS
);
109 static int termp_it_pre(DECL_ARGS
);
110 static int termp_li_pre(DECL_ARGS
);
111 static int termp_lk_pre(DECL_ARGS
);
112 static int termp_nd_pre(DECL_ARGS
);
113 static int termp_nm_pre(DECL_ARGS
);
114 static int termp_ns_pre(DECL_ARGS
);
115 static int termp_quote_pre(DECL_ARGS
);
116 static int termp_rs_pre(DECL_ARGS
);
117 static int termp_rv_pre(DECL_ARGS
);
118 static int termp_sh_pre(DECL_ARGS
);
119 static int termp_sm_pre(DECL_ARGS
);
120 static int termp_sp_pre(DECL_ARGS
);
121 static int termp_ss_pre(DECL_ARGS
);
122 static int termp_under_pre(DECL_ARGS
);
123 static int termp_ud_pre(DECL_ARGS
);
124 static int termp_vt_pre(DECL_ARGS
);
125 static int termp_xr_pre(DECL_ARGS
);
126 static int termp_xx_pre(DECL_ARGS
);
128 static const struct termact termacts
[MDOC_MAX
] = {
129 { termp_ap_pre
, NULL
}, /* Ap */
130 { NULL
, NULL
}, /* Dd */
131 { NULL
, NULL
}, /* Dt */
132 { NULL
, NULL
}, /* Os */
133 { termp_sh_pre
, termp_sh_post
}, /* Sh */
134 { termp_ss_pre
, termp_ss_post
}, /* Ss */
135 { termp_sp_pre
, NULL
}, /* Pp */
136 { termp_d1_pre
, termp_d1_post
}, /* D1 */
137 { termp_d1_pre
, termp_d1_post
}, /* Dl */
138 { termp_bd_pre
, termp_bd_post
}, /* Bd */
139 { NULL
, NULL
}, /* Ed */
140 { termp_bl_pre
, termp_bl_post
}, /* Bl */
141 { NULL
, NULL
}, /* El */
142 { termp_it_pre
, termp_it_post
}, /* It */
143 { termp_under_pre
, NULL
}, /* Ad */
144 { termp_an_pre
, termp_an_post
}, /* An */
145 { termp_under_pre
, NULL
}, /* Ar */
146 { termp_cd_pre
, NULL
}, /* Cd */
147 { termp_bold_pre
, NULL
}, /* Cm */
148 { NULL
, NULL
}, /* Dv */
149 { NULL
, NULL
}, /* Er */
150 { NULL
, NULL
}, /* Ev */
151 { termp_ex_pre
, NULL
}, /* Ex */
152 { termp_fa_pre
, NULL
}, /* Fa */
153 { termp_fd_pre
, NULL
}, /* Fd */
154 { termp_fl_pre
, NULL
}, /* Fl */
155 { termp_fn_pre
, NULL
}, /* Fn */
156 { termp_ft_pre
, NULL
}, /* Ft */
157 { termp_bold_pre
, NULL
}, /* Ic */
158 { termp_in_pre
, termp_in_post
}, /* In */
159 { termp_li_pre
, NULL
}, /* Li */
160 { termp_nd_pre
, NULL
}, /* Nd */
161 { termp_nm_pre
, termp_nm_post
}, /* Nm */
162 { termp_quote_pre
, termp_quote_post
}, /* Op */
163 { NULL
, NULL
}, /* Ot */
164 { termp_under_pre
, NULL
}, /* Pa */
165 { termp_rv_pre
, NULL
}, /* Rv */
166 { NULL
, NULL
}, /* St */
167 { termp_under_pre
, NULL
}, /* Va */
168 { termp_vt_pre
, NULL
}, /* Vt */
169 { termp_xr_pre
, NULL
}, /* Xr */
170 { termp__a_pre
, termp____post
}, /* %A */
171 { termp_under_pre
, termp____post
}, /* %B */
172 { NULL
, termp____post
}, /* %D */
173 { termp_under_pre
, termp____post
}, /* %I */
174 { termp_under_pre
, termp____post
}, /* %J */
175 { NULL
, termp____post
}, /* %N */
176 { NULL
, termp____post
}, /* %O */
177 { NULL
, termp____post
}, /* %P */
178 { NULL
, termp____post
}, /* %R */
179 { termp__t_pre
, termp__t_post
}, /* %T */
180 { NULL
, termp____post
}, /* %V */
181 { NULL
, NULL
}, /* Ac */
182 { termp_quote_pre
, termp_quote_post
}, /* Ao */
183 { termp_quote_pre
, termp_quote_post
}, /* Aq */
184 { NULL
, NULL
}, /* At */
185 { NULL
, NULL
}, /* Bc */
186 { termp_bf_pre
, NULL
}, /* Bf */
187 { termp_quote_pre
, termp_quote_post
}, /* Bo */
188 { termp_quote_pre
, termp_quote_post
}, /* Bq */
189 { termp_xx_pre
, NULL
}, /* Bsx */
190 { NULL
, termp_bx_post
}, /* Bx */
191 { NULL
, NULL
}, /* Db */
192 { NULL
, NULL
}, /* Dc */
193 { termp_quote_pre
, termp_quote_post
}, /* Do */
194 { termp_quote_pre
, termp_quote_post
}, /* Dq */
195 { NULL
, NULL
}, /* Ec */ /* FIXME: no space */
196 { NULL
, NULL
}, /* Ef */
197 { termp_under_pre
, NULL
}, /* Em */
198 { NULL
, NULL
}, /* Eo */
199 { termp_xx_pre
, NULL
}, /* Fx */
200 { termp_bold_pre
, NULL
}, /* Ms */
201 { termp_igndelim_pre
, NULL
}, /* No */
202 { termp_ns_pre
, NULL
}, /* Ns */
203 { termp_xx_pre
, NULL
}, /* Nx */
204 { termp_xx_pre
, NULL
}, /* Ox */
205 { NULL
, NULL
}, /* Pc */
206 { termp_igndelim_pre
, termp_pf_post
}, /* Pf */
207 { termp_quote_pre
, termp_quote_post
}, /* Po */
208 { termp_quote_pre
, termp_quote_post
}, /* Pq */
209 { NULL
, NULL
}, /* Qc */
210 { termp_quote_pre
, termp_quote_post
}, /* Ql */
211 { termp_quote_pre
, termp_quote_post
}, /* Qo */
212 { termp_quote_pre
, termp_quote_post
}, /* Qq */
213 { NULL
, NULL
}, /* Re */
214 { termp_rs_pre
, NULL
}, /* Rs */
215 { NULL
, NULL
}, /* Sc */
216 { termp_quote_pre
, termp_quote_post
}, /* So */
217 { termp_quote_pre
, termp_quote_post
}, /* Sq */
218 { termp_sm_pre
, NULL
}, /* Sm */
219 { termp_under_pre
, NULL
}, /* Sx */
220 { termp_bold_pre
, NULL
}, /* Sy */
221 { NULL
, NULL
}, /* Tn */
222 { termp_xx_pre
, NULL
}, /* Ux */
223 { NULL
, NULL
}, /* Xc */
224 { NULL
, NULL
}, /* Xo */
225 { termp_fo_pre
, termp_fo_post
}, /* Fo */
226 { NULL
, NULL
}, /* Fc */
227 { termp_quote_pre
, termp_quote_post
}, /* Oo */
228 { NULL
, NULL
}, /* Oc */
229 { termp_bk_pre
, termp_bk_post
}, /* Bk */
230 { NULL
, NULL
}, /* Ek */
231 { termp_bt_pre
, NULL
}, /* Bt */
232 { NULL
, NULL
}, /* Hf */
233 { NULL
, NULL
}, /* Fr */
234 { termp_ud_pre
, NULL
}, /* Ud */
235 { NULL
, termp_lb_post
}, /* Lb */
236 { termp_sp_pre
, NULL
}, /* Lp */
237 { termp_lk_pre
, NULL
}, /* Lk */
238 { termp_under_pre
, NULL
}, /* Mt */
239 { termp_quote_pre
, termp_quote_post
}, /* Brq */
240 { termp_quote_pre
, termp_quote_post
}, /* Bro */
241 { NULL
, NULL
}, /* Brc */
242 { NULL
, termp____post
}, /* %C */
243 { NULL
, NULL
}, /* Es */ /* TODO */
244 { NULL
, NULL
}, /* En */ /* TODO */
245 { termp_xx_pre
, NULL
}, /* Dx */
246 { NULL
, termp____post
}, /* %Q */
247 { termp_sp_pre
, NULL
}, /* br */
248 { termp_sp_pre
, NULL
}, /* sp */
249 { termp_under_pre
, termp____post
}, /* %U */
250 { NULL
, NULL
}, /* Ta */
255 terminal_mdoc(void *arg
, const struct mdoc
*mdoc
)
257 const struct mdoc_node
*n
;
258 const struct mdoc_meta
*m
;
261 p
= (struct termp
*)arg
;
264 p
->maxrmargin
= p
->defrmargin
;
265 p
->tabwidth
= term_len(p
, 5);
267 if (NULL
== p
->symtab
)
269 case (TERMENC_ASCII
):
270 p
->symtab
= chars_init(CHARS_ASCII
);
280 term_begin(p
, print_mdoc_head
, print_mdoc_foot
, m
);
283 print_mdoc_nodelist(p
, NULL
, m
, n
->child
);
290 print_mdoc_nodelist(DECL_ARGS
)
293 print_mdoc_node(p
, pair
, m
, n
);
295 print_mdoc_nodelist(p
, pair
, m
, n
->next
);
301 print_mdoc_node(DECL_ARGS
)
305 struct termpair npair
;
306 size_t offset
, rmargin
;
310 rmargin
= p
->rmargin
;
311 font
= term_fontq(p
);
313 memset(&npair
, 0, sizeof(struct termpair
));
316 if (MDOC_TEXT
== n
->type
)
317 term_word(p
, n
->string
);
318 else if (termacts
[n
->tok
].pre
&& ENDBODY_NOT
== n
->end
)
319 chld
= (*termacts
[n
->tok
].pre
)(p
, &npair
, m
, n
);
322 * Keeps only work until the end of a line. If a keep was
323 * invoked in a prior line, revert it to PREKEEP.
325 * Also let SYNPRETTY sections behave as if they were wrapped
329 if (TERMP_KEEP
& p
->flags
|| MDOC_SYNPRETTY
& n
->flags
) {
330 if (n
->prev
&& n
->prev
->line
!= n
->line
) {
331 p
->flags
&= ~TERMP_KEEP
;
332 p
->flags
|= TERMP_PREKEEP
;
333 } else if (NULL
== n
->prev
) {
334 if (n
->parent
&& n
->parent
->line
!= n
->line
) {
335 p
->flags
&= ~TERMP_KEEP
;
336 p
->flags
|= TERMP_PREKEEP
;
342 * Since SYNPRETTY sections aren't "turned off" with `Ek',
343 * we have to intuit whether we should disable formatting.
346 if ( ! (MDOC_SYNPRETTY
& n
->flags
) &&
347 ((n
->prev
&& MDOC_SYNPRETTY
& n
->prev
->flags
) ||
348 (n
->parent
&& MDOC_SYNPRETTY
& n
->parent
->flags
)))
349 p
->flags
&= ~(TERMP_KEEP
| TERMP_PREKEEP
);
351 if (chld
&& n
->child
)
352 print_mdoc_nodelist(p
, &npair
, m
, n
->child
);
354 term_fontpopq(p
, font
);
356 if (MDOC_TEXT
!= n
->type
&& termacts
[n
->tok
].post
&&
357 ! (MDOC_ENDED
& n
->flags
)) {
358 (void)(*termacts
[n
->tok
].post
)(p
, &npair
, m
, n
);
361 * Explicit end tokens not only call the post
362 * handler, but also tell the respective block
363 * that it must not call the post handler again.
365 if (ENDBODY_NOT
!= n
->end
)
366 n
->pending
->flags
|= MDOC_ENDED
;
369 * End of line terminating an implicit block
370 * while an explicit block is still open.
371 * Continue the explicit block without spacing.
373 if (ENDBODY_NOSPACE
== n
->end
)
374 p
->flags
|= TERMP_NOSPACE
;
377 if (MDOC_EOS
& n
->flags
)
378 p
->flags
|= TERMP_SENTENCE
;
381 p
->rmargin
= rmargin
;
386 print_mdoc_foot(struct termp
*p
, const void *arg
)
388 char buf
[DATESIZ
], os
[BUFSIZ
];
389 const struct mdoc_meta
*m
;
391 m
= (const struct mdoc_meta
*)arg
;
393 term_fontrepl(p
, TERMFONT_NONE
);
396 * Output the footer in new-groff style, that is, three columns
397 * with the middle being the manual date and flanking columns
398 * being the operating system:
403 time2a(m
->date
, buf
, DATESIZ
);
404 strlcpy(os
, m
->os
, BUFSIZ
);
409 p
->rmargin
= (p
->maxrmargin
-
410 term_strlen(p
, buf
) + term_len(p
, 1)) / 2;
411 p
->flags
|= TERMP_NOSPACE
| TERMP_NOBREAK
;
416 p
->offset
= p
->rmargin
;
417 p
->rmargin
= p
->maxrmargin
- term_strlen(p
, os
);
418 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
423 p
->offset
= p
->rmargin
;
424 p
->rmargin
= p
->maxrmargin
;
425 p
->flags
&= ~TERMP_NOBREAK
;
426 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
432 p
->rmargin
= p
->maxrmargin
;
438 print_mdoc_head(struct termp
*p
, const void *arg
)
440 char buf
[BUFSIZ
], title
[BUFSIZ
];
441 const struct mdoc_meta
*m
;
443 m
= (const struct mdoc_meta
*)arg
;
445 p
->rmargin
= p
->maxrmargin
;
449 * The header is strange. It has three components, which are
450 * really two with the first duplicated. It goes like this:
452 * IDENTIFIER TITLE IDENTIFIER
454 * The IDENTIFIER is NAME(SECTION), which is the command-name
455 * (if given, or "unknown" if not) followed by the manual page
456 * section. These are given in `Dt'. The TITLE is a free-form
457 * string depending on the manual volume. If not specified, it
458 * switches on the manual section.
462 strlcpy(buf
, m
->vol
, BUFSIZ
);
465 strlcat(buf
, " (", BUFSIZ
);
466 strlcat(buf
, m
->arch
, BUFSIZ
);
467 strlcat(buf
, ")", BUFSIZ
);
470 snprintf(title
, BUFSIZ
, "%s(%s)", m
->title
, m
->msec
);
473 p
->rmargin
= (p
->maxrmargin
-
474 term_strlen(p
, buf
) + term_len(p
, 1)) / 2;
475 p
->flags
|= TERMP_NOBREAK
| TERMP_NOSPACE
;
480 p
->offset
= p
->rmargin
;
481 p
->rmargin
= p
->maxrmargin
- term_strlen(p
, title
);
482 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
487 p
->offset
= p
->rmargin
;
488 p
->rmargin
= p
->maxrmargin
;
489 p
->flags
&= ~TERMP_NOBREAK
;
490 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
496 p
->rmargin
= p
->maxrmargin
;
497 p
->flags
&= ~TERMP_NOSPACE
;
502 a2height(const struct termp
*p
, const char *v
)
507 if ( ! a2roffsu(v
, &su
, SCALE_VS
))
508 SCALE_VS_INIT(&su
, term_len(p
, 1));
510 return(term_vspan(p
, &su
));
515 a2width(const struct termp
*p
, const char *v
)
520 if ( ! a2roffsu(v
, &su
, SCALE_MAX
))
521 SCALE_HS_INIT(&su
, term_strlen(p
, v
));
523 return(term_hspan(p
, &su
));
528 a2offs(const struct termp
*p
, const char *v
)
534 else if (0 == strcmp(v
, "left"))
536 else if (0 == strcmp(v
, "indent"))
537 return(term_len(p
, INDENT
+ 1));
538 else if (0 == strcmp(v
, "indent-two"))
539 return(term_len(p
, (INDENT
+ 1) * 2));
540 else if ( ! a2roffsu(v
, &su
, SCALE_MAX
))
541 SCALE_HS_INIT(&su
, term_strlen(p
, v
));
543 return(term_hspan(p
, &su
));
548 * Determine how much space to print out before block elements of `It'
549 * (and thus `Bl') and `Bd'. And then go ahead and print that space,
553 print_bvspace(struct termp
*p
,
554 const struct mdoc_node
*bl
,
555 const struct mdoc_node
*n
)
557 const struct mdoc_node
*nn
;
561 if (MDOC_Bd
== bl
->tok
&& bl
->norm
->Bd
.comp
)
563 if (MDOC_Bl
== bl
->tok
&& bl
->norm
->Bl
.comp
)
566 /* Do not vspace directly after Ss/Sh. */
568 for (nn
= n
; nn
; nn
= nn
->parent
) {
569 if (MDOC_BLOCK
!= nn
->type
)
571 if (MDOC_Ss
== nn
->tok
)
573 if (MDOC_Sh
== nn
->tok
)
575 if (NULL
== nn
->prev
)
580 /* A `-column' does not assert vspace within the list. */
582 if (MDOC_Bl
== bl
->tok
&& LIST_column
== bl
->norm
->Bl
.type
)
583 if (n
->prev
&& MDOC_It
== n
->prev
->tok
)
586 /* A `-diag' without body does not vspace. */
588 if (MDOC_Bl
== bl
->tok
&& LIST_diag
== bl
->norm
->Bl
.type
)
589 if (n
->prev
&& MDOC_It
== n
->prev
->tok
) {
590 assert(n
->prev
->body
);
591 if (NULL
== n
->prev
->body
->child
)
601 termp_it_pre(DECL_ARGS
)
603 const struct mdoc_node
*bl
, *nn
;
606 size_t width
, offset
, ncols
, dcol
;
609 if (MDOC_BLOCK
== n
->type
) {
610 print_bvspace(p
, n
->parent
->parent
, n
);
614 bl
= n
->parent
->parent
->parent
;
615 type
= bl
->norm
->Bl
.type
;
618 * First calculate width and offset. This is pretty easy unless
619 * we're a -column list, in which case all prior columns must
625 if (bl
->norm
->Bl
.offs
)
626 offset
= a2offs(p
, bl
->norm
->Bl
.offs
);
630 if (MDOC_HEAD
== n
->type
)
634 * Imitate groff's column handling:
635 * - For each earlier column, add its width.
636 * - For less than 5 columns, add four more blanks per
638 * - For exactly 5 columns, add three more blank per
640 * - For more than 5 columns, add only one column.
642 ncols
= bl
->norm
->Bl
.ncols
;
645 dcol
= ncols
< 5 ? term_len(p
, 4) :
646 ncols
== 5 ? term_len(p
, 3) : term_len(p
, 1);
649 * Calculate the offset by applying all prior MDOC_BODY,
650 * so we stop at the MDOC_HEAD (NULL == nn->prev).
653 for (i
= 0, nn
= n
->prev
;
654 nn
->prev
&& i
< (int)ncols
;
656 offset
+= dcol
+ a2width
657 (p
, bl
->norm
->Bl
.cols
[i
]);
660 * When exceeding the declared number of columns, leave
661 * the remaining widths at 0. This will later be
662 * adjusted to the default width of 10, or, for the last
663 * column, stretched to the right margin.
669 * Use the declared column widths, extended as explained
670 * in the preceding paragraph.
672 width
= a2width(p
, bl
->norm
->Bl
.cols
[i
]) + dcol
;
675 if (NULL
== bl
->norm
->Bl
.width
)
679 * Note: buffer the width by 2, which is groff's magic
680 * number for buffering single arguments. See the above
681 * handling for column for how this changes.
683 assert(bl
->norm
->Bl
.width
);
684 width
= a2width(p
, bl
->norm
->Bl
.width
) + term_len(p
, 2);
689 * List-type can override the width in the case of fixed-head
690 * values (bullet, dash/hyphen, enum). Tags need a non-zero
700 if (width
< term_len(p
, 4))
701 width
= term_len(p
, 4);
704 if (width
< term_len(p
, 5))
705 width
= term_len(p
, 5);
709 width
= term_len(p
, 8);
715 width
= term_len(p
, 10);
722 * Whitespace control. Inset bodies need an initial space,
723 * while diagonal bodies need two.
726 p
->flags
|= TERMP_NOSPACE
;
730 if (MDOC_BODY
== n
->type
)
731 term_word(p
, "\\ \\ ");
734 if (MDOC_BODY
== n
->type
)
741 p
->flags
|= TERMP_NOSPACE
;
745 if (MDOC_HEAD
== n
->type
)
746 term_fontpush(p
, TERMFONT_BOLD
);
753 * Pad and break control. This is the tricky part. These flags
754 * are documented in term_flushln() in term.c. Note that we're
755 * going to unset all of these flags in termp_it_post() when we
767 if (MDOC_HEAD
== n
->type
)
768 p
->flags
|= TERMP_NOBREAK
;
770 p
->flags
|= TERMP_NOLPAD
;
773 if (MDOC_HEAD
== n
->type
)
774 p
->flags
|= TERMP_NOBREAK
;
776 p
->flags
|= TERMP_NOLPAD
;
778 if (MDOC_HEAD
!= n
->type
)
782 * This is ugly. If `-hang' is specified and the body
783 * is a `Bl' or `Bd', then we want basically to nullify
784 * the "overstep" effect in term_flushln() and treat
785 * this as a `-ohang' list instead.
787 if (n
->next
->child
&&
788 (MDOC_Bl
== n
->next
->child
->tok
||
789 MDOC_Bd
== n
->next
->child
->tok
)) {
790 p
->flags
&= ~TERMP_NOBREAK
;
791 p
->flags
&= ~TERMP_NOLPAD
;
793 p
->flags
|= TERMP_HANG
;
796 if (MDOC_HEAD
== n
->type
)
797 p
->flags
|= TERMP_NOBREAK
| TERMP_TWOSPACE
;
799 p
->flags
|= TERMP_NOLPAD
;
801 if (MDOC_HEAD
!= n
->type
)
803 if (NULL
== n
->next
|| NULL
== n
->next
->child
)
804 p
->flags
|= TERMP_DANGLE
;
807 if (MDOC_HEAD
== n
->type
)
811 p
->flags
&= ~TERMP_NOBREAK
;
813 p
->flags
|= TERMP_NOBREAK
;
816 if (MDOC_BODY
== n
->prev
->type
)
817 p
->flags
|= TERMP_NOLPAD
;
821 if (MDOC_HEAD
== n
->type
)
822 p
->flags
|= TERMP_NOBREAK
;
829 * Margin control. Set-head-width lists have their right
830 * margins shortened. The body for these lists has the offset
831 * necessarily lengthened. Everybody gets the offset.
839 * Same stipulation as above, regarding `-hang'. We
840 * don't want to recalculate rmargin and offsets when
841 * using `Bd' or `Bl' within `-hang' overstep lists.
843 if (MDOC_HEAD
== n
->type
&& n
->next
->child
&&
844 (MDOC_Bl
== n
->next
->child
->tok
||
845 MDOC_Bd
== n
->next
->child
->tok
))
858 if (MDOC_HEAD
== n
->type
)
859 p
->rmargin
= p
->offset
+ width
;
865 p
->rmargin
= p
->offset
+ width
;
867 * XXX - this behaviour is not documented: the
868 * right-most column is filled to the right margin.
870 if (MDOC_HEAD
== n
->type
)
872 if (NULL
== n
->next
&& p
->rmargin
< p
->maxrmargin
)
873 p
->rmargin
= p
->maxrmargin
;
880 * The dash, hyphen, bullet and enum lists all have a special
881 * HEAD character (temporarily bold, in some cases).
884 if (MDOC_HEAD
== n
->type
)
887 term_fontpush(p
, TERMFONT_BOLD
);
888 term_word(p
, "\\[bu]");
894 term_fontpush(p
, TERMFONT_BOLD
);
895 term_word(p
, "\\(hy");
899 (pair
->ppair
->ppair
->count
)++;
900 snprintf(buf
, sizeof(buf
), "%d.",
901 pair
->ppair
->ppair
->count
);
909 * If we're not going to process our children, indicate so here.
922 if (MDOC_HEAD
== n
->type
)
926 if (MDOC_HEAD
== n
->type
)
939 termp_it_post(DECL_ARGS
)
943 if (MDOC_BLOCK
== n
->type
)
946 type
= n
->parent
->parent
->parent
->norm
->Bl
.type
;
954 if (MDOC_BODY
== n
->type
)
958 if (MDOC_BODY
== n
->type
)
967 * Now that our output is flushed, we can reset our tags. Since
968 * only `It' sets these flags, we're free to assume that nobody
969 * has munged them in the meanwhile.
972 p
->flags
&= ~TERMP_DANGLE
;
973 p
->flags
&= ~TERMP_NOBREAK
;
974 p
->flags
&= ~TERMP_TWOSPACE
;
975 p
->flags
&= ~TERMP_NOLPAD
;
976 p
->flags
&= ~TERMP_HANG
;
982 termp_nm_pre(DECL_ARGS
)
985 if (MDOC_BLOCK
== n
->type
)
988 if (MDOC_BODY
== n
->type
) {
989 if (NULL
== n
->child
)
991 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
992 p
->offset
+= term_len(p
, 1) +
993 (NULL
== n
->prev
->child
? term_strlen(p
, m
->name
) :
994 MDOC_TEXT
== n
->prev
->child
->type
?
995 term_strlen(p
, n
->prev
->child
->string
) :
1000 if (NULL
== n
->child
&& NULL
== m
->name
)
1003 if (MDOC_HEAD
== n
->type
)
1004 synopsis_pre(p
, n
->parent
);
1006 if (MDOC_HEAD
== n
->type
&& n
->next
->child
) {
1007 p
->flags
|= TERMP_NOSPACE
| TERMP_NOBREAK
;
1008 p
->rmargin
= p
->offset
+ term_len(p
, 1);
1009 if (NULL
== n
->child
) {
1010 p
->rmargin
+= term_strlen(p
, m
->name
);
1011 } else if (MDOC_TEXT
== n
->child
->type
) {
1012 p
->rmargin
+= term_strlen(p
, n
->child
->string
);
1014 p
->flags
|= TERMP_HANG
;
1016 p
->rmargin
+= term_len(p
, 5);
1017 p
->flags
|= TERMP_HANG
;
1021 term_fontpush(p
, TERMFONT_BOLD
);
1022 if (NULL
== n
->child
)
1023 term_word(p
, m
->name
);
1030 termp_nm_post(DECL_ARGS
)
1033 if (MDOC_HEAD
== n
->type
&& n
->next
->child
) {
1035 p
->flags
&= ~(TERMP_NOBREAK
| TERMP_HANG
);
1036 } else if (MDOC_BODY
== n
->type
&& n
->child
) {
1038 p
->flags
&= ~TERMP_NOLPAD
;
1045 termp_fl_pre(DECL_ARGS
)
1048 term_fontpush(p
, TERMFONT_BOLD
);
1049 term_word(p
, "\\-");
1052 p
->flags
|= TERMP_NOSPACE
;
1053 else if (n
->next
&& n
->next
->line
== n
->line
)
1054 p
->flags
|= TERMP_NOSPACE
;
1062 termp__a_pre(DECL_ARGS
)
1065 if (n
->prev
&& MDOC__A
== n
->prev
->tok
)
1066 if (NULL
== n
->next
|| MDOC__A
!= n
->next
->tok
)
1067 term_word(p
, "and");
1075 termp_an_pre(DECL_ARGS
)
1078 if (NULL
== n
->child
)
1082 * If not in the AUTHORS section, `An -split' will cause
1083 * newlines to occur before the author name. If in the AUTHORS
1084 * section, by default, the first `An' invocation is nosplit,
1085 * then all subsequent ones, regardless of whether interspersed
1086 * with other macros/text, are split. -split, in this case,
1087 * will override the condition of the implied first -nosplit.
1090 if (n
->sec
== SEC_AUTHORS
) {
1091 if ( ! (TERMP_ANPREC
& p
->flags
)) {
1092 if (TERMP_SPLIT
& p
->flags
)
1096 if (TERMP_NOSPLIT
& p
->flags
)
1102 if (TERMP_SPLIT
& p
->flags
)
1111 termp_an_post(DECL_ARGS
)
1115 if (SEC_AUTHORS
== n
->sec
)
1116 p
->flags
|= TERMP_ANPREC
;
1120 if (AUTH_split
== n
->norm
->An
.auth
) {
1121 p
->flags
&= ~TERMP_NOSPLIT
;
1122 p
->flags
|= TERMP_SPLIT
;
1123 } else if (AUTH_nosplit
== n
->norm
->An
.auth
) {
1124 p
->flags
&= ~TERMP_SPLIT
;
1125 p
->flags
|= TERMP_NOSPLIT
;
1133 termp_ns_pre(DECL_ARGS
)
1136 p
->flags
|= TERMP_NOSPACE
;
1143 termp_rs_pre(DECL_ARGS
)
1146 if (SEC_SEE_ALSO
!= n
->sec
)
1148 if (MDOC_BLOCK
== n
->type
&& n
->prev
)
1156 termp_rv_pre(DECL_ARGS
)
1158 const struct mdoc_node
*nn
;
1161 term_word(p
, "The");
1163 for (nn
= n
->child
; nn
; nn
= nn
->next
) {
1164 term_fontpush(p
, TERMFONT_BOLD
);
1165 term_word(p
, nn
->string
);
1167 p
->flags
|= TERMP_NOSPACE
;
1168 if (nn
->next
&& NULL
== nn
->next
->next
)
1169 term_word(p
, "(), and");
1171 term_word(p
, "(),");
1176 if (n
->child
&& n
->child
->next
)
1177 term_word(p
, "functions return");
1179 term_word(p
, "function returns");
1181 term_word(p
, "the value 0 if successful; otherwise the value "
1182 "-1 is returned and the global variable");
1184 term_fontpush(p
, TERMFONT_UNDER
);
1185 term_word(p
, "errno");
1188 term_word(p
, "is set to indicate the error.");
1189 p
->flags
|= TERMP_SENTENCE
;
1197 termp_ex_pre(DECL_ARGS
)
1199 const struct mdoc_node
*nn
;
1201 term_word(p
, "The");
1203 for (nn
= n
->child
; nn
; nn
= nn
->next
) {
1204 term_fontpush(p
, TERMFONT_BOLD
);
1205 term_word(p
, nn
->string
);
1207 p
->flags
|= TERMP_NOSPACE
;
1208 if (nn
->next
&& NULL
== nn
->next
->next
)
1209 term_word(p
, ", and");
1213 p
->flags
&= ~TERMP_NOSPACE
;
1216 if (n
->child
&& n
->child
->next
)
1217 term_word(p
, "utilities exit");
1219 term_word(p
, "utility exits");
1221 term_word(p
, "0 on success, and >0 if an error occurs.");
1222 p
->flags
|= TERMP_SENTENCE
;
1230 termp_nd_pre(DECL_ARGS
)
1233 if (MDOC_BODY
!= n
->type
)
1236 #if defined(__OpenBSD__) || defined(__linux__)
1237 term_word(p
, "\\(en");
1239 term_word(p
, "\\(em");
1247 termp_bl_pre(DECL_ARGS
)
1250 return(MDOC_HEAD
!= n
->type
);
1256 termp_bl_post(DECL_ARGS
)
1259 if (MDOC_BLOCK
== n
->type
)
1266 termp_xr_pre(DECL_ARGS
)
1268 const struct mdoc_node
*nn
;
1270 if (NULL
== n
->child
)
1273 assert(MDOC_TEXT
== n
->child
->type
);
1276 term_word(p
, nn
->string
);
1277 if (NULL
== (nn
= nn
->next
))
1279 p
->flags
|= TERMP_NOSPACE
;
1281 term_word(p
, nn
->string
);
1289 * This decides how to assert whitespace before any of the SYNOPSIS set
1290 * of macros (which, as in the case of Ft/Fo and Ft/Fn, may contain
1294 synopsis_pre(struct termp
*p
, const struct mdoc_node
*n
)
1297 * Obviously, if we're not in a SYNOPSIS or no prior macros
1298 * exist, do nothing.
1300 if (NULL
== n
->prev
|| ! (MDOC_SYNPRETTY
& n
->flags
))
1304 * If we're the second in a pair of like elements, emit our
1305 * newline and return. UNLESS we're `Fo', `Fn', `Fn', in which
1306 * case we soldier on.
1308 if (n
->prev
->tok
== n
->tok
&&
1309 MDOC_Ft
!= n
->tok
&&
1310 MDOC_Fo
!= n
->tok
&&
1311 MDOC_Fn
!= n
->tok
) {
1317 * If we're one of the SYNOPSIS set and non-like pair-wise after
1318 * another (or Fn/Fo, which we've let slip through) then assert
1319 * vertical space, else only newline and move on.
1321 switch (n
->prev
->tok
) {
1334 if (MDOC_Fn
!= n
->tok
&& MDOC_Fo
!= n
->tok
) {
1347 termp_vt_pre(DECL_ARGS
)
1350 if (MDOC_ELEM
== n
->type
) {
1352 return(termp_under_pre(p
, pair
, m
, n
));
1353 } else if (MDOC_BLOCK
== n
->type
) {
1356 } else if (MDOC_HEAD
== n
->type
)
1359 return(termp_under_pre(p
, pair
, m
, n
));
1365 termp_bold_pre(DECL_ARGS
)
1368 term_fontpush(p
, TERMFONT_BOLD
);
1375 termp_fd_pre(DECL_ARGS
)
1379 return(termp_bold_pre(p
, pair
, m
, n
));
1385 termp_sh_pre(DECL_ARGS
)
1388 /* No vspace between consecutive `Sh' calls. */
1392 if (n
->prev
&& MDOC_Sh
== n
->prev
->tok
)
1393 if (NULL
== n
->prev
->body
->child
)
1398 term_fontpush(p
, TERMFONT_BOLD
);
1401 p
->offset
= term_len(p
, INDENT
);
1412 termp_sh_post(DECL_ARGS
)
1431 termp_bt_pre(DECL_ARGS
)
1434 term_word(p
, "is currently in beta test.");
1435 p
->flags
|= TERMP_SENTENCE
;
1442 termp_lb_post(DECL_ARGS
)
1445 if (SEC_LIBRARY
== n
->sec
&& MDOC_LINE
& n
->flags
)
1452 termp_ud_pre(DECL_ARGS
)
1455 term_word(p
, "currently under development.");
1456 p
->flags
|= TERMP_SENTENCE
;
1463 termp_d1_pre(DECL_ARGS
)
1466 if (MDOC_BLOCK
!= n
->type
)
1469 p
->offset
+= term_len(p
, (INDENT
+ 1));
1476 termp_d1_post(DECL_ARGS
)
1479 if (MDOC_BLOCK
!= n
->type
)
1487 termp_ft_pre(DECL_ARGS
)
1490 /* NB: MDOC_LINE does not effect this! */
1492 term_fontpush(p
, TERMFONT_UNDER
);
1499 termp_fn_pre(DECL_ARGS
)
1501 const struct mdoc_node
*nn
;
1505 term_fontpush(p
, TERMFONT_BOLD
);
1506 term_word(p
, n
->child
->string
);
1509 p
->flags
|= TERMP_NOSPACE
;
1512 for (nn
= n
->child
->next
; nn
; nn
= nn
->next
) {
1513 term_fontpush(p
, TERMFONT_UNDER
);
1514 term_word(p
, nn
->string
);
1523 if (MDOC_SYNPRETTY
& n
->flags
)
1532 termp_fa_pre(DECL_ARGS
)
1534 const struct mdoc_node
*nn
;
1536 if (n
->parent
->tok
!= MDOC_Fo
) {
1537 term_fontpush(p
, TERMFONT_UNDER
);
1541 for (nn
= n
->child
; nn
; nn
= nn
->next
) {
1542 term_fontpush(p
, TERMFONT_UNDER
);
1543 term_word(p
, nn
->string
);
1550 if (n
->child
&& n
->next
&& n
->next
->tok
== MDOC_Fa
)
1559 termp_bd_pre(DECL_ARGS
)
1561 size_t tabwidth
, rm
, rmax
;
1562 const struct mdoc_node
*nn
;
1564 if (MDOC_BLOCK
== n
->type
) {
1565 print_bvspace(p
, n
, n
);
1567 } else if (MDOC_HEAD
== n
->type
)
1570 if (n
->norm
->Bd
.offs
)
1571 p
->offset
+= a2offs(p
, n
->norm
->Bd
.offs
);
1574 * If -ragged or -filled are specified, the block does nothing
1575 * but change the indentation. If -unfilled or -literal are
1576 * specified, text is printed exactly as entered in the display:
1577 * for macro lines, a newline is appended to the line. Blank
1578 * lines are allowed.
1581 if (DISP_literal
!= n
->norm
->Bd
.type
&&
1582 DISP_unfilled
!= n
->norm
->Bd
.type
)
1585 tabwidth
= p
->tabwidth
;
1586 p
->tabwidth
= term_len(p
, 8);
1588 rmax
= p
->maxrmargin
;
1589 p
->rmargin
= p
->maxrmargin
= TERM_MAXMARGIN
;
1591 for (nn
= n
->child
; nn
; nn
= nn
->next
) {
1592 print_mdoc_node(p
, pair
, m
, nn
);
1594 * If the printed node flushes its own line, then we
1595 * needn't do it here as well. This is hacky, but the
1596 * notion of selective eoln whitespace is pretty dumb
1597 * anyway, so don't sweat it.
1619 if (nn
->next
&& nn
->next
->line
== nn
->line
)
1622 p
->flags
|= TERMP_NOSPACE
;
1625 p
->tabwidth
= tabwidth
;
1627 p
->maxrmargin
= rmax
;
1634 termp_bd_post(DECL_ARGS
)
1638 if (MDOC_BODY
!= n
->type
)
1642 rmax
= p
->maxrmargin
;
1644 if (DISP_literal
== n
->norm
->Bd
.type
||
1645 DISP_unfilled
== n
->norm
->Bd
.type
)
1646 p
->rmargin
= p
->maxrmargin
= TERM_MAXMARGIN
;
1648 p
->flags
|= TERMP_NOSPACE
;
1652 p
->maxrmargin
= rmax
;
1658 termp_bx_post(DECL_ARGS
)
1662 p
->flags
|= TERMP_NOSPACE
;
1663 term_word(p
, "BSD");
1669 termp_xx_pre(DECL_ARGS
)
1705 termp_igndelim_pre(DECL_ARGS
)
1708 p
->flags
|= TERMP_IGNDELIM
;
1715 termp_pf_post(DECL_ARGS
)
1718 p
->flags
|= TERMP_NOSPACE
;
1724 termp_ss_pre(DECL_ARGS
)
1734 term_fontpush(p
, TERMFONT_BOLD
);
1735 p
->offset
= term_len(p
, HALFINDENT
);
1747 termp_ss_post(DECL_ARGS
)
1750 if (MDOC_HEAD
== n
->type
)
1757 termp_cd_pre(DECL_ARGS
)
1761 term_fontpush(p
, TERMFONT_BOLD
);
1768 termp_in_pre(DECL_ARGS
)
1773 if (MDOC_SYNPRETTY
& n
->flags
&& MDOC_LINE
& n
->flags
) {
1774 term_fontpush(p
, TERMFONT_BOLD
);
1775 term_word(p
, "#include");
1779 term_fontpush(p
, TERMFONT_UNDER
);
1782 p
->flags
|= TERMP_NOSPACE
;
1789 termp_in_post(DECL_ARGS
)
1792 if (MDOC_SYNPRETTY
& n
->flags
)
1793 term_fontpush(p
, TERMFONT_BOLD
);
1795 p
->flags
|= TERMP_NOSPACE
;
1798 if (MDOC_SYNPRETTY
& n
->flags
)
1805 termp_sp_pre(DECL_ARGS
)
1811 len
= n
->child
? a2height(p
, n
->child
->string
) : 1;
1823 for (i
= 0; i
< len
; i
++)
1832 termp_quote_pre(DECL_ARGS
)
1835 if (MDOC_BODY
!= n
->type
&& MDOC_ELEM
!= n
->type
)
1887 p
->flags
|= TERMP_NOSPACE
;
1894 termp_quote_post(DECL_ARGS
)
1897 if (MDOC_BODY
!= n
->type
&& MDOC_ELEM
!= n
->type
)
1900 p
->flags
|= TERMP_NOSPACE
;
1955 termp_fo_pre(DECL_ARGS
)
1958 if (MDOC_BLOCK
== n
->type
) {
1961 } else if (MDOC_BODY
== n
->type
) {
1962 p
->flags
|= TERMP_NOSPACE
;
1967 if (NULL
== n
->child
)
1970 /* XXX: we drop non-initial arguments as per groff. */
1972 assert(n
->child
->string
);
1973 term_fontpush(p
, TERMFONT_BOLD
);
1974 term_word(p
, n
->child
->string
);
1981 termp_fo_post(DECL_ARGS
)
1984 if (MDOC_BODY
!= n
->type
)
1989 if (MDOC_SYNPRETTY
& n
->flags
)
1996 termp_bf_pre(DECL_ARGS
)
1999 if (MDOC_HEAD
== n
->type
)
2001 else if (MDOC_BLOCK
!= n
->type
)
2004 if (FONT_Em
== n
->norm
->Bf
.font
)
2005 term_fontpush(p
, TERMFONT_UNDER
);
2006 else if (FONT_Sy
== n
->norm
->Bf
.font
)
2007 term_fontpush(p
, TERMFONT_BOLD
);
2009 term_fontpush(p
, TERMFONT_NONE
);
2017 termp_sm_pre(DECL_ARGS
)
2020 assert(n
->child
&& MDOC_TEXT
== n
->child
->type
);
2021 if (0 == strcmp("on", n
->child
->string
)) {
2023 p
->flags
&= ~TERMP_NOSPACE
;
2024 p
->flags
&= ~TERMP_NONOSPACE
;
2026 p
->flags
|= TERMP_NONOSPACE
;
2034 termp_ap_pre(DECL_ARGS
)
2037 p
->flags
|= TERMP_NOSPACE
;
2039 p
->flags
|= TERMP_NOSPACE
;
2046 termp____post(DECL_ARGS
)
2050 * Handle lists of authors. In general, print each followed by
2051 * a comma. Don't print the comma if there are only two
2054 if (MDOC__A
== n
->tok
&& n
->next
&& MDOC__A
== n
->next
->tok
)
2055 if (NULL
== n
->next
->next
|| MDOC__A
!= n
->next
->next
->tok
)
2056 if (NULL
== n
->prev
|| MDOC__A
!= n
->prev
->tok
)
2061 if (NULL
== n
->parent
|| MDOC_Rs
!= n
->parent
->tok
)
2064 if (NULL
== n
->next
) {
2066 p
->flags
|= TERMP_SENTENCE
;
2074 termp_li_pre(DECL_ARGS
)
2077 term_fontpush(p
, TERMFONT_NONE
);
2084 termp_lk_pre(DECL_ARGS
)
2086 const struct mdoc_node
*nn
, *sv
;
2088 term_fontpush(p
, TERMFONT_UNDER
);
2092 if (NULL
== nn
|| NULL
== nn
->next
)
2095 for (nn
= nn
->next
; nn
; nn
= nn
->next
)
2096 term_word(p
, nn
->string
);
2102 term_fontpush(p
, TERMFONT_BOLD
);
2103 term_word(p
, sv
->string
);
2112 termp_bk_pre(DECL_ARGS
)
2121 if (n
->parent
->args
|| 0 == n
->prev
->nchild
)
2122 p
->flags
|= TERMP_PREKEEP
;
2135 termp_bk_post(DECL_ARGS
)
2138 if (MDOC_BODY
== n
->type
)
2139 p
->flags
&= ~(TERMP_KEEP
| TERMP_PREKEEP
);
2144 termp__t_post(DECL_ARGS
)
2148 * If we're in an `Rs' and there's a journal present, then quote
2149 * us instead of underlining us (for disambiguation).
2151 if (n
->parent
&& MDOC_Rs
== n
->parent
->tok
&&
2152 n
->parent
->norm
->Rs
.child_J
)
2153 termp_quote_post(p
, pair
, m
, n
);
2155 termp____post(p
, pair
, m
, n
);
2160 termp__t_pre(DECL_ARGS
)
2164 * If we're in an `Rs' and there's a journal present, then quote
2165 * us instead of underlining us (for disambiguation).
2167 if (n
->parent
&& MDOC_Rs
== n
->parent
->tok
&&
2168 n
->parent
->norm
->Rs
.child_J
)
2169 return(termp_quote_pre(p
, pair
, m
, n
));
2171 term_fontpush(p
, TERMFONT_UNDER
);
2177 termp_under_pre(DECL_ARGS
)
2180 term_fontpush(p
, TERMFONT_UNDER
);