]>
git.cameronkatri.com Git - mandoc.git/blob - mdoc_term.c
1 /* $Id: mdoc_term.c,v 1.199 2010/12/16 17:14:48 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_an_post(DECL_ARGS
);
72 static void termp_bd_post(DECL_ARGS
);
73 static void termp_bk_post(DECL_ARGS
);
74 static void termp_bl_post(DECL_ARGS
);
75 static void termp_bx_post(DECL_ARGS
);
76 static void termp_d1_post(DECL_ARGS
);
77 static void termp_fo_post(DECL_ARGS
);
78 static void termp_in_post(DECL_ARGS
);
79 static void termp_it_post(DECL_ARGS
);
80 static void termp_lb_post(DECL_ARGS
);
81 static void termp_nm_post(DECL_ARGS
);
82 static void termp_pf_post(DECL_ARGS
);
83 static void termp_quote_post(DECL_ARGS
);
84 static void termp_sh_post(DECL_ARGS
);
85 static void termp_ss_post(DECL_ARGS
);
87 static int termp__a_pre(DECL_ARGS
);
88 static int termp_an_pre(DECL_ARGS
);
89 static int termp_ap_pre(DECL_ARGS
);
90 static int termp_bd_pre(DECL_ARGS
);
91 static int termp_bf_pre(DECL_ARGS
);
92 static int termp_bk_pre(DECL_ARGS
);
93 static int termp_bl_pre(DECL_ARGS
);
94 static int termp_bold_pre(DECL_ARGS
);
95 static int termp_bt_pre(DECL_ARGS
);
96 static int termp_cd_pre(DECL_ARGS
);
97 static int termp_d1_pre(DECL_ARGS
);
98 static int termp_ex_pre(DECL_ARGS
);
99 static int termp_fa_pre(DECL_ARGS
);
100 static int termp_fd_pre(DECL_ARGS
);
101 static int termp_fl_pre(DECL_ARGS
);
102 static int termp_fn_pre(DECL_ARGS
);
103 static int termp_fo_pre(DECL_ARGS
);
104 static int termp_ft_pre(DECL_ARGS
);
105 static int termp_igndelim_pre(DECL_ARGS
);
106 static int termp_in_pre(DECL_ARGS
);
107 static int termp_it_pre(DECL_ARGS
);
108 static int termp_li_pre(DECL_ARGS
);
109 static int termp_lk_pre(DECL_ARGS
);
110 static int termp_nd_pre(DECL_ARGS
);
111 static int termp_nm_pre(DECL_ARGS
);
112 static int termp_ns_pre(DECL_ARGS
);
113 static int termp_quote_pre(DECL_ARGS
);
114 static int termp_rs_pre(DECL_ARGS
);
115 static int termp_rv_pre(DECL_ARGS
);
116 static int termp_sh_pre(DECL_ARGS
);
117 static int termp_sm_pre(DECL_ARGS
);
118 static int termp_sp_pre(DECL_ARGS
);
119 static int termp_ss_pre(DECL_ARGS
);
120 static int termp_under_pre(DECL_ARGS
);
121 static int termp_ud_pre(DECL_ARGS
);
122 static int termp_vt_pre(DECL_ARGS
);
123 static int termp_xr_pre(DECL_ARGS
);
124 static int termp_xx_pre(DECL_ARGS
);
126 static const struct termact termacts
[MDOC_MAX
] = {
127 { termp_ap_pre
, NULL
}, /* Ap */
128 { NULL
, NULL
}, /* Dd */
129 { NULL
, NULL
}, /* Dt */
130 { NULL
, NULL
}, /* Os */
131 { termp_sh_pre
, termp_sh_post
}, /* Sh */
132 { termp_ss_pre
, termp_ss_post
}, /* Ss */
133 { termp_sp_pre
, NULL
}, /* Pp */
134 { termp_d1_pre
, termp_d1_post
}, /* D1 */
135 { termp_d1_pre
, termp_d1_post
}, /* Dl */
136 { termp_bd_pre
, termp_bd_post
}, /* Bd */
137 { NULL
, NULL
}, /* Ed */
138 { termp_bl_pre
, termp_bl_post
}, /* Bl */
139 { NULL
, NULL
}, /* El */
140 { termp_it_pre
, termp_it_post
}, /* It */
141 { termp_under_pre
, NULL
}, /* Ad */
142 { termp_an_pre
, termp_an_post
}, /* An */
143 { termp_under_pre
, NULL
}, /* Ar */
144 { termp_cd_pre
, NULL
}, /* Cd */
145 { termp_bold_pre
, NULL
}, /* Cm */
146 { NULL
, NULL
}, /* Dv */
147 { NULL
, NULL
}, /* Er */
148 { NULL
, NULL
}, /* Ev */
149 { termp_ex_pre
, NULL
}, /* Ex */
150 { termp_fa_pre
, NULL
}, /* Fa */
151 { termp_fd_pre
, NULL
}, /* Fd */
152 { termp_fl_pre
, NULL
}, /* Fl */
153 { termp_fn_pre
, NULL
}, /* Fn */
154 { termp_ft_pre
, NULL
}, /* Ft */
155 { termp_bold_pre
, NULL
}, /* Ic */
156 { termp_in_pre
, termp_in_post
}, /* In */
157 { termp_li_pre
, NULL
}, /* Li */
158 { termp_nd_pre
, NULL
}, /* Nd */
159 { termp_nm_pre
, termp_nm_post
}, /* Nm */
160 { termp_quote_pre
, termp_quote_post
}, /* Op */
161 { NULL
, NULL
}, /* Ot */
162 { termp_under_pre
, NULL
}, /* Pa */
163 { termp_rv_pre
, NULL
}, /* Rv */
164 { NULL
, NULL
}, /* St */
165 { termp_under_pre
, NULL
}, /* Va */
166 { termp_vt_pre
, NULL
}, /* Vt */
167 { termp_xr_pre
, NULL
}, /* Xr */
168 { termp__a_pre
, termp____post
}, /* %A */
169 { termp_under_pre
, termp____post
}, /* %B */
170 { NULL
, termp____post
}, /* %D */
171 { termp_under_pre
, termp____post
}, /* %I */
172 { termp_under_pre
, termp____post
}, /* %J */
173 { NULL
, termp____post
}, /* %N */
174 { NULL
, termp____post
}, /* %O */
175 { NULL
, termp____post
}, /* %P */
176 { NULL
, termp____post
}, /* %R */
177 { termp_under_pre
, termp____post
}, /* %T */
178 { NULL
, termp____post
}, /* %V */
179 { NULL
, NULL
}, /* Ac */
180 { termp_quote_pre
, termp_quote_post
}, /* Ao */
181 { termp_quote_pre
, termp_quote_post
}, /* Aq */
182 { NULL
, NULL
}, /* At */
183 { NULL
, NULL
}, /* Bc */
184 { termp_bf_pre
, NULL
}, /* Bf */
185 { termp_quote_pre
, termp_quote_post
}, /* Bo */
186 { termp_quote_pre
, termp_quote_post
}, /* Bq */
187 { termp_xx_pre
, NULL
}, /* Bsx */
188 { NULL
, termp_bx_post
}, /* Bx */
189 { NULL
, NULL
}, /* Db */
190 { NULL
, NULL
}, /* Dc */
191 { termp_quote_pre
, termp_quote_post
}, /* Do */
192 { termp_quote_pre
, termp_quote_post
}, /* Dq */
193 { NULL
, NULL
}, /* Ec */ /* FIXME: no space */
194 { NULL
, NULL
}, /* Ef */
195 { termp_under_pre
, NULL
}, /* Em */
196 { NULL
, NULL
}, /* Eo */
197 { termp_xx_pre
, NULL
}, /* Fx */
198 { termp_bold_pre
, NULL
}, /* Ms */
199 { termp_igndelim_pre
, NULL
}, /* No */
200 { termp_ns_pre
, NULL
}, /* Ns */
201 { termp_xx_pre
, NULL
}, /* Nx */
202 { termp_xx_pre
, NULL
}, /* Ox */
203 { NULL
, NULL
}, /* Pc */
204 { termp_igndelim_pre
, termp_pf_post
}, /* Pf */
205 { termp_quote_pre
, termp_quote_post
}, /* Po */
206 { termp_quote_pre
, termp_quote_post
}, /* Pq */
207 { NULL
, NULL
}, /* Qc */
208 { termp_quote_pre
, termp_quote_post
}, /* Ql */
209 { termp_quote_pre
, termp_quote_post
}, /* Qo */
210 { termp_quote_pre
, termp_quote_post
}, /* Qq */
211 { NULL
, NULL
}, /* Re */
212 { termp_rs_pre
, NULL
}, /* Rs */
213 { NULL
, NULL
}, /* Sc */
214 { termp_quote_pre
, termp_quote_post
}, /* So */
215 { termp_quote_pre
, termp_quote_post
}, /* Sq */
216 { termp_sm_pre
, NULL
}, /* Sm */
217 { termp_under_pre
, NULL
}, /* Sx */
218 { termp_bold_pre
, NULL
}, /* Sy */
219 { NULL
, NULL
}, /* Tn */
220 { termp_xx_pre
, NULL
}, /* Ux */
221 { NULL
, NULL
}, /* Xc */
222 { NULL
, NULL
}, /* Xo */
223 { termp_fo_pre
, termp_fo_post
}, /* Fo */
224 { NULL
, NULL
}, /* Fc */
225 { termp_quote_pre
, termp_quote_post
}, /* Oo */
226 { NULL
, NULL
}, /* Oc */
227 { termp_bk_pre
, termp_bk_post
}, /* Bk */
228 { NULL
, NULL
}, /* Ek */
229 { termp_bt_pre
, NULL
}, /* Bt */
230 { NULL
, NULL
}, /* Hf */
231 { NULL
, NULL
}, /* Fr */
232 { termp_ud_pre
, NULL
}, /* Ud */
233 { NULL
, termp_lb_post
}, /* Lb */
234 { termp_sp_pre
, NULL
}, /* Lp */
235 { termp_lk_pre
, NULL
}, /* Lk */
236 { termp_under_pre
, NULL
}, /* Mt */
237 { termp_quote_pre
, termp_quote_post
}, /* Brq */
238 { termp_quote_pre
, termp_quote_post
}, /* Bro */
239 { NULL
, NULL
}, /* Brc */
240 { NULL
, termp____post
}, /* %C */
241 { NULL
, NULL
}, /* Es */ /* TODO */
242 { NULL
, NULL
}, /* En */ /* TODO */
243 { termp_xx_pre
, NULL
}, /* Dx */
244 { NULL
, termp____post
}, /* %Q */
245 { termp_sp_pre
, NULL
}, /* br */
246 { termp_sp_pre
, NULL
}, /* sp */
247 { termp_under_pre
, termp____post
}, /* %U */
248 { NULL
, NULL
}, /* Ta */
253 terminal_mdoc(void *arg
, const struct mdoc
*mdoc
)
255 const struct mdoc_node
*n
;
256 const struct mdoc_meta
*m
;
259 p
= (struct termp
*)arg
;
262 p
->maxrmargin
= p
->defrmargin
;
263 p
->tabwidth
= term_len(p
, 5);
265 if (NULL
== p
->symtab
)
267 case (TERMENC_ASCII
):
268 p
->symtab
= chars_init(CHARS_ASCII
);
278 term_begin(p
, print_mdoc_head
, print_mdoc_foot
, m
);
281 print_mdoc_nodelist(p
, NULL
, m
, n
->child
);
288 print_mdoc_nodelist(DECL_ARGS
)
291 print_mdoc_node(p
, pair
, m
, n
);
293 print_mdoc_nodelist(p
, pair
, m
, n
->next
);
299 print_mdoc_node(DECL_ARGS
)
303 struct termpair npair
;
304 size_t offset
, rmargin
;
308 rmargin
= p
->rmargin
;
309 font
= term_fontq(p
);
311 memset(&npair
, 0, sizeof(struct termpair
));
314 if (MDOC_TEXT
== n
->type
)
315 term_word(p
, n
->string
);
316 else if (termacts
[n
->tok
].pre
&& ENDBODY_NOT
== n
->end
)
317 chld
= (*termacts
[n
->tok
].pre
)(p
, &npair
, m
, n
);
320 * Keeps only work until the end of a line. If a keep was
321 * invoked in a prior line, revert it to PREKEEP.
323 * Also let SYNPRETTY sections behave as if they were wrapped
327 if (TERMP_KEEP
& p
->flags
|| MDOC_SYNPRETTY
& n
->flags
) {
328 if (n
->prev
&& n
->prev
->line
!= n
->line
) {
329 p
->flags
&= ~TERMP_KEEP
;
330 p
->flags
|= TERMP_PREKEEP
;
331 } else if (NULL
== n
->prev
) {
332 if (n
->parent
&& n
->parent
->line
!= n
->line
) {
333 p
->flags
&= ~TERMP_KEEP
;
334 p
->flags
|= TERMP_PREKEEP
;
340 * Since SYNPRETTY sections aren't "turned off" with `Ek',
341 * we have to intuit whether we should disable formatting.
344 if ( ! (MDOC_SYNPRETTY
& n
->flags
) &&
345 ((n
->prev
&& MDOC_SYNPRETTY
& n
->prev
->flags
) ||
346 (n
->parent
&& MDOC_SYNPRETTY
& n
->parent
->flags
)))
347 p
->flags
&= ~(TERMP_KEEP
| TERMP_PREKEEP
);
349 if (chld
&& n
->child
)
350 print_mdoc_nodelist(p
, &npair
, m
, n
->child
);
352 term_fontpopq(p
, font
);
354 if (MDOC_TEXT
!= n
->type
&& termacts
[n
->tok
].post
&&
355 ! (MDOC_ENDED
& n
->flags
)) {
356 (void)(*termacts
[n
->tok
].post
)(p
, &npair
, m
, n
);
359 * Explicit end tokens not only call the post
360 * handler, but also tell the respective block
361 * that it must not call the post handler again.
363 if (ENDBODY_NOT
!= n
->end
)
364 n
->pending
->flags
|= MDOC_ENDED
;
367 * End of line terminating an implicit block
368 * while an explicit block is still open.
369 * Continue the explicit block without spacing.
371 if (ENDBODY_NOSPACE
== n
->end
)
372 p
->flags
|= TERMP_NOSPACE
;
375 if (MDOC_EOS
& n
->flags
)
376 p
->flags
|= TERMP_SENTENCE
;
379 p
->rmargin
= rmargin
;
384 print_mdoc_foot(struct termp
*p
, const void *arg
)
386 char buf
[DATESIZ
], os
[BUFSIZ
];
387 const struct mdoc_meta
*m
;
389 m
= (const struct mdoc_meta
*)arg
;
391 term_fontrepl(p
, TERMFONT_NONE
);
394 * Output the footer in new-groff style, that is, three columns
395 * with the middle being the manual date and flanking columns
396 * being the operating system:
401 time2a(m
->date
, buf
, DATESIZ
);
402 strlcpy(os
, m
->os
, BUFSIZ
);
407 p
->rmargin
= (p
->maxrmargin
-
408 term_strlen(p
, buf
) + term_len(p
, 1)) / 2;
409 p
->flags
|= TERMP_NOSPACE
| TERMP_NOBREAK
;
414 p
->offset
= p
->rmargin
;
415 p
->rmargin
= p
->maxrmargin
- term_strlen(p
, os
);
416 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
421 p
->offset
= p
->rmargin
;
422 p
->rmargin
= p
->maxrmargin
;
423 p
->flags
&= ~TERMP_NOBREAK
;
424 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
430 p
->rmargin
= p
->maxrmargin
;
436 print_mdoc_head(struct termp
*p
, const void *arg
)
438 char buf
[BUFSIZ
], title
[BUFSIZ
];
439 const struct mdoc_meta
*m
;
441 m
= (const struct mdoc_meta
*)arg
;
443 p
->rmargin
= p
->maxrmargin
;
447 * The header is strange. It has three components, which are
448 * really two with the first duplicated. It goes like this:
450 * IDENTIFIER TITLE IDENTIFIER
452 * The IDENTIFIER is NAME(SECTION), which is the command-name
453 * (if given, or "unknown" if not) followed by the manual page
454 * section. These are given in `Dt'. The TITLE is a free-form
455 * string depending on the manual volume. If not specified, it
456 * switches on the manual section.
460 strlcpy(buf
, m
->vol
, BUFSIZ
);
463 strlcat(buf
, " (", BUFSIZ
);
464 strlcat(buf
, m
->arch
, BUFSIZ
);
465 strlcat(buf
, ")", BUFSIZ
);
468 snprintf(title
, BUFSIZ
, "%s(%s)", m
->title
, m
->msec
);
471 p
->rmargin
= (p
->maxrmargin
-
472 term_strlen(p
, buf
) + term_len(p
, 1)) / 2;
473 p
->flags
|= TERMP_NOBREAK
| TERMP_NOSPACE
;
478 p
->offset
= p
->rmargin
;
479 p
->rmargin
= p
->maxrmargin
- term_strlen(p
, title
);
480 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
485 p
->offset
= p
->rmargin
;
486 p
->rmargin
= p
->maxrmargin
;
487 p
->flags
&= ~TERMP_NOBREAK
;
488 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
494 p
->rmargin
= p
->maxrmargin
;
495 p
->flags
&= ~TERMP_NOSPACE
;
500 a2height(const struct termp
*p
, const char *v
)
505 if ( ! a2roffsu(v
, &su
, SCALE_VS
))
506 SCALE_VS_INIT(&su
, term_len(p
, 1));
508 return(term_vspan(p
, &su
));
513 a2width(const struct termp
*p
, const char *v
)
518 if ( ! a2roffsu(v
, &su
, SCALE_MAX
))
519 SCALE_HS_INIT(&su
, term_strlen(p
, v
));
521 return(term_hspan(p
, &su
));
526 a2offs(const struct termp
*p
, const char *v
)
532 else if (0 == strcmp(v
, "left"))
534 else if (0 == strcmp(v
, "indent"))
535 return(term_len(p
, INDENT
+ 1));
536 else if (0 == strcmp(v
, "indent-two"))
537 return(term_len(p
, (INDENT
+ 1) * 2));
538 else if ( ! a2roffsu(v
, &su
, SCALE_MAX
))
539 SCALE_HS_INIT(&su
, term_strlen(p
, v
));
541 return(term_hspan(p
, &su
));
546 * Determine how much space to print out before block elements of `It'
547 * (and thus `Bl') and `Bd'. And then go ahead and print that space,
551 print_bvspace(struct termp
*p
,
552 const struct mdoc_node
*bl
,
553 const struct mdoc_node
*n
)
555 const struct mdoc_node
*nn
;
559 if (MDOC_Bd
== bl
->tok
&& bl
->data
.Bd
->comp
)
561 if (MDOC_Bl
== bl
->tok
&& bl
->data
.Bl
->comp
)
564 /* Do not vspace directly after Ss/Sh. */
566 for (nn
= n
; nn
; nn
= nn
->parent
) {
567 if (MDOC_BLOCK
!= nn
->type
)
569 if (MDOC_Ss
== nn
->tok
)
571 if (MDOC_Sh
== nn
->tok
)
573 if (NULL
== nn
->prev
)
578 /* A `-column' does not assert vspace within the list. */
580 if (MDOC_Bl
== bl
->tok
&& LIST_column
== bl
->data
.Bl
->type
)
581 if (n
->prev
&& MDOC_It
== n
->prev
->tok
)
584 /* A `-diag' without body does not vspace. */
586 if (MDOC_Bl
== bl
->tok
&& LIST_diag
== bl
->data
.Bl
->type
)
587 if (n
->prev
&& MDOC_It
== n
->prev
->tok
) {
588 assert(n
->prev
->body
);
589 if (NULL
== n
->prev
->body
->child
)
599 termp_it_pre(DECL_ARGS
)
601 const struct mdoc_node
*bl
, *nn
;
604 size_t width
, offset
, ncols
, dcol
;
607 if (MDOC_BLOCK
== n
->type
) {
608 print_bvspace(p
, n
->parent
->parent
, n
);
612 bl
= n
->parent
->parent
->parent
;
614 type
= bl
->data
.Bl
->type
;
617 * First calculate width and offset. This is pretty easy unless
618 * we're a -column list, in which case all prior columns must
624 if (bl
->data
.Bl
->offs
)
625 offset
= a2offs(p
, bl
->data
.Bl
->offs
);
629 if (MDOC_HEAD
== n
->type
)
633 * Imitate groff's column handling:
634 * - For each earlier column, add its width.
635 * - For less than 5 columns, add four more blanks per
637 * - For exactly 5 columns, add three more blank per
639 * - For more than 5 columns, add only one column.
641 ncols
= bl
->data
.Bl
->ncols
;
644 dcol
= ncols
< 5 ? term_len(p
, 4) :
645 ncols
== 5 ? term_len(p
, 3) : term_len(p
, 1);
648 * Calculate the offset by applying all prior MDOC_BODY,
649 * so we stop at the MDOC_HEAD (NULL == nn->prev).
652 for (i
= 0, nn
= n
->prev
;
653 nn
->prev
&& i
< (int)ncols
;
655 offset
+= dcol
+ a2width
656 (p
, bl
->data
.Bl
->cols
[i
]);
659 * When exceeding the declared number of columns, leave
660 * the remaining widths at 0. This will later be
661 * adjusted to the default width of 10, or, for the last
662 * column, stretched to the right margin.
668 * Use the declared column widths, extended as explained
669 * in the preceding paragraph.
671 width
= a2width(p
, bl
->data
.Bl
->cols
[i
]) + dcol
;
674 if (NULL
== bl
->data
.Bl
->width
)
678 * Note: buffer the width by 2, which is groff's magic
679 * number for buffering single arguments. See the above
680 * handling for column for how this changes.
682 assert(bl
->data
.Bl
->width
);
683 width
= a2width(p
, bl
->data
.Bl
->width
) + term_len(p
, 2);
688 * List-type can override the width in the case of fixed-head
689 * values (bullet, dash/hyphen, enum). Tags need a non-zero
699 if (width
< term_len(p
, 4))
700 width
= term_len(p
, 4);
703 if (width
< term_len(p
, 5))
704 width
= term_len(p
, 5);
708 width
= term_len(p
, 8);
714 width
= term_len(p
, 10);
721 * Whitespace control. Inset bodies need an initial space,
722 * while diagonal bodies need two.
725 p
->flags
|= TERMP_NOSPACE
;
729 if (MDOC_BODY
== n
->type
)
730 term_word(p
, "\\ \\ ");
733 if (MDOC_BODY
== n
->type
)
740 p
->flags
|= TERMP_NOSPACE
;
744 if (MDOC_HEAD
== n
->type
)
745 term_fontpush(p
, TERMFONT_BOLD
);
752 * Pad and break control. This is the tricky part. These flags
753 * are documented in term_flushln() in term.c. Note that we're
754 * going to unset all of these flags in termp_it_post() when we
766 if (MDOC_HEAD
== n
->type
)
767 p
->flags
|= TERMP_NOBREAK
;
769 p
->flags
|= TERMP_NOLPAD
;
772 if (MDOC_HEAD
== n
->type
)
773 p
->flags
|= TERMP_NOBREAK
;
775 p
->flags
|= TERMP_NOLPAD
;
777 if (MDOC_HEAD
!= n
->type
)
781 * This is ugly. If `-hang' is specified and the body
782 * is a `Bl' or `Bd', then we want basically to nullify
783 * the "overstep" effect in term_flushln() and treat
784 * this as a `-ohang' list instead.
786 if (n
->next
->child
&&
787 (MDOC_Bl
== n
->next
->child
->tok
||
788 MDOC_Bd
== n
->next
->child
->tok
)) {
789 p
->flags
&= ~TERMP_NOBREAK
;
790 p
->flags
&= ~TERMP_NOLPAD
;
792 p
->flags
|= TERMP_HANG
;
795 if (MDOC_HEAD
== n
->type
)
796 p
->flags
|= TERMP_NOBREAK
| TERMP_TWOSPACE
;
798 p
->flags
|= TERMP_NOLPAD
;
800 if (MDOC_HEAD
!= n
->type
)
802 if (NULL
== n
->next
|| NULL
== n
->next
->child
)
803 p
->flags
|= TERMP_DANGLE
;
806 if (MDOC_HEAD
== n
->type
)
810 p
->flags
&= ~TERMP_NOBREAK
;
812 p
->flags
|= TERMP_NOBREAK
;
815 if (MDOC_BODY
== n
->prev
->type
)
816 p
->flags
|= TERMP_NOLPAD
;
820 if (MDOC_HEAD
== n
->type
)
821 p
->flags
|= TERMP_NOBREAK
;
828 * Margin control. Set-head-width lists have their right
829 * margins shortened. The body for these lists has the offset
830 * necessarily lengthened. Everybody gets the offset.
838 * Same stipulation as above, regarding `-hang'. We
839 * don't want to recalculate rmargin and offsets when
840 * using `Bd' or `Bl' within `-hang' overstep lists.
842 if (MDOC_HEAD
== n
->type
&& n
->next
->child
&&
843 (MDOC_Bl
== n
->next
->child
->tok
||
844 MDOC_Bd
== n
->next
->child
->tok
))
857 if (MDOC_HEAD
== n
->type
)
858 p
->rmargin
= p
->offset
+ width
;
864 p
->rmargin
= p
->offset
+ width
;
866 * XXX - this behaviour is not documented: the
867 * right-most column is filled to the right margin.
869 if (MDOC_HEAD
== n
->type
)
871 if (NULL
== n
->next
&& p
->rmargin
< p
->maxrmargin
)
872 p
->rmargin
= p
->maxrmargin
;
879 * The dash, hyphen, bullet and enum lists all have a special
880 * HEAD character (temporarily bold, in some cases).
883 if (MDOC_HEAD
== n
->type
)
886 term_fontpush(p
, TERMFONT_BOLD
);
887 term_word(p
, "\\[bu]");
893 term_fontpush(p
, TERMFONT_BOLD
);
894 term_word(p
, "\\(hy");
898 (pair
->ppair
->ppair
->count
)++;
899 snprintf(buf
, sizeof(buf
), "%d.",
900 pair
->ppair
->ppair
->count
);
908 * If we're not going to process our children, indicate so here.
921 if (MDOC_HEAD
== n
->type
)
925 if (MDOC_HEAD
== n
->type
)
938 termp_it_post(DECL_ARGS
)
942 if (MDOC_BLOCK
== n
->type
)
945 type
= n
->parent
->parent
->parent
->data
.Bl
->type
;
953 if (MDOC_BODY
== n
->type
)
957 if (MDOC_BODY
== n
->type
)
966 * Now that our output is flushed, we can reset our tags. Since
967 * only `It' sets these flags, we're free to assume that nobody
968 * has munged them in the meanwhile.
971 p
->flags
&= ~TERMP_DANGLE
;
972 p
->flags
&= ~TERMP_NOBREAK
;
973 p
->flags
&= ~TERMP_TWOSPACE
;
974 p
->flags
&= ~TERMP_NOLPAD
;
975 p
->flags
&= ~TERMP_HANG
;
981 termp_nm_pre(DECL_ARGS
)
984 if (MDOC_BLOCK
== n
->type
)
987 if (MDOC_BODY
== n
->type
) {
988 if (NULL
== n
->child
)
990 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
991 p
->offset
+= term_len(p
, 1) +
992 (NULL
== n
->prev
->child
? term_strlen(p
, m
->name
) :
993 MDOC_TEXT
== n
->prev
->child
->type
?
994 term_strlen(p
, n
->prev
->child
->string
) :
999 if (NULL
== n
->child
&& NULL
== m
->name
)
1002 if (MDOC_HEAD
== n
->type
)
1003 synopsis_pre(p
, n
->parent
);
1005 if (MDOC_HEAD
== n
->type
&& n
->next
->child
) {
1006 p
->flags
|= TERMP_NOSPACE
| TERMP_NOBREAK
;
1007 p
->rmargin
= p
->offset
+ term_len(p
, 1);
1008 if (NULL
== n
->child
) {
1009 p
->rmargin
+= term_strlen(p
, m
->name
);
1010 } else if (MDOC_TEXT
== n
->child
->type
) {
1011 p
->rmargin
+= term_strlen(p
, n
->child
->string
);
1013 p
->flags
|= TERMP_HANG
;
1015 p
->rmargin
+= term_len(p
, 5);
1016 p
->flags
|= TERMP_HANG
;
1020 term_fontpush(p
, TERMFONT_BOLD
);
1021 if (NULL
== n
->child
)
1022 term_word(p
, m
->name
);
1029 termp_nm_post(DECL_ARGS
)
1032 if (MDOC_HEAD
== n
->type
&& n
->next
->child
) {
1034 p
->flags
&= ~(TERMP_NOBREAK
| TERMP_HANG
);
1035 } else if (MDOC_BODY
== n
->type
&& n
->child
) {
1037 p
->flags
&= ~TERMP_NOLPAD
;
1044 termp_fl_pre(DECL_ARGS
)
1047 term_fontpush(p
, TERMFONT_BOLD
);
1048 term_word(p
, "\\-");
1051 p
->flags
|= TERMP_NOSPACE
;
1052 else if (n
->next
&& n
->next
->line
== n
->line
)
1053 p
->flags
|= TERMP_NOSPACE
;
1061 termp__a_pre(DECL_ARGS
)
1064 if (n
->prev
&& MDOC__A
== n
->prev
->tok
)
1065 if (NULL
== n
->next
|| MDOC__A
!= n
->next
->tok
)
1066 term_word(p
, "and");
1074 termp_an_pre(DECL_ARGS
)
1077 if (NULL
== n
->child
)
1081 * If not in the AUTHORS section, `An -split' will cause
1082 * newlines to occur before the author name. If in the AUTHORS
1083 * section, by default, the first `An' invocation is nosplit,
1084 * then all subsequent ones, regardless of whether interspersed
1085 * with other macros/text, are split. -split, in this case,
1086 * will override the condition of the implied first -nosplit.
1089 if (n
->sec
== SEC_AUTHORS
) {
1090 if ( ! (TERMP_ANPREC
& p
->flags
)) {
1091 if (TERMP_SPLIT
& p
->flags
)
1095 if (TERMP_NOSPLIT
& p
->flags
)
1101 if (TERMP_SPLIT
& p
->flags
)
1110 termp_an_post(DECL_ARGS
)
1114 if (SEC_AUTHORS
== n
->sec
)
1115 p
->flags
|= TERMP_ANPREC
;
1119 if (AUTH_split
== n
->data
.An
->auth
) {
1120 p
->flags
&= ~TERMP_NOSPLIT
;
1121 p
->flags
|= TERMP_SPLIT
;
1122 } else if (AUTH_nosplit
== n
->data
.An
->auth
) {
1123 p
->flags
&= ~TERMP_SPLIT
;
1124 p
->flags
|= TERMP_NOSPLIT
;
1132 termp_ns_pre(DECL_ARGS
)
1135 p
->flags
|= TERMP_NOSPACE
;
1142 termp_rs_pre(DECL_ARGS
)
1145 if (SEC_SEE_ALSO
!= n
->sec
)
1147 if (MDOC_BLOCK
== n
->type
&& n
->prev
)
1155 termp_rv_pre(DECL_ARGS
)
1157 const struct mdoc_node
*nn
;
1160 term_word(p
, "The");
1162 for (nn
= n
->child
; nn
; nn
= nn
->next
) {
1163 term_fontpush(p
, TERMFONT_BOLD
);
1164 term_word(p
, nn
->string
);
1166 p
->flags
|= TERMP_NOSPACE
;
1167 if (nn
->next
&& NULL
== nn
->next
->next
)
1168 term_word(p
, "(), and");
1170 term_word(p
, "(),");
1175 if (n
->child
&& n
->child
->next
)
1176 term_word(p
, "functions return");
1178 term_word(p
, "function returns");
1180 term_word(p
, "the value 0 if successful; otherwise the value "
1181 "-1 is returned and the global variable");
1183 term_fontpush(p
, TERMFONT_UNDER
);
1184 term_word(p
, "errno");
1187 term_word(p
, "is set to indicate the error.");
1188 p
->flags
|= TERMP_SENTENCE
;
1196 termp_ex_pre(DECL_ARGS
)
1198 const struct mdoc_node
*nn
;
1200 term_word(p
, "The");
1202 for (nn
= n
->child
; nn
; nn
= nn
->next
) {
1203 term_fontpush(p
, TERMFONT_BOLD
);
1204 term_word(p
, nn
->string
);
1206 p
->flags
|= TERMP_NOSPACE
;
1207 if (nn
->next
&& NULL
== nn
->next
->next
)
1208 term_word(p
, ", and");
1212 p
->flags
&= ~TERMP_NOSPACE
;
1215 if (n
->child
&& n
->child
->next
)
1216 term_word(p
, "utilities exit");
1218 term_word(p
, "utility exits");
1220 term_word(p
, "0 on success, and >0 if an error occurs.");
1221 p
->flags
|= TERMP_SENTENCE
;
1229 termp_nd_pre(DECL_ARGS
)
1232 if (MDOC_BODY
!= n
->type
)
1235 #if defined(__OpenBSD__) || defined(__linux__)
1236 term_word(p
, "\\(en");
1238 term_word(p
, "\\(em");
1246 termp_bl_pre(DECL_ARGS
)
1249 return(MDOC_HEAD
!= n
->type
);
1255 termp_bl_post(DECL_ARGS
)
1258 if (MDOC_BLOCK
== n
->type
)
1265 termp_xr_pre(DECL_ARGS
)
1267 const struct mdoc_node
*nn
;
1269 if (NULL
== n
->child
)
1272 assert(MDOC_TEXT
== n
->child
->type
);
1275 term_word(p
, nn
->string
);
1276 if (NULL
== (nn
= nn
->next
))
1278 p
->flags
|= TERMP_NOSPACE
;
1280 term_word(p
, nn
->string
);
1288 * This decides how to assert whitespace before any of the SYNOPSIS set
1289 * of macros (which, as in the case of Ft/Fo and Ft/Fn, may contain
1293 synopsis_pre(struct termp
*p
, const struct mdoc_node
*n
)
1296 * Obviously, if we're not in a SYNOPSIS or no prior macros
1297 * exist, do nothing.
1299 if (NULL
== n
->prev
|| ! (MDOC_SYNPRETTY
& n
->flags
))
1303 * If we're the second in a pair of like elements, emit our
1304 * newline and return. UNLESS we're `Fo', `Fn', `Fn', in which
1305 * case we soldier on.
1307 if (n
->prev
->tok
== n
->tok
&&
1308 MDOC_Ft
!= n
->tok
&&
1309 MDOC_Fo
!= n
->tok
&&
1310 MDOC_Fn
!= n
->tok
) {
1316 * If we're one of the SYNOPSIS set and non-like pair-wise after
1317 * another (or Fn/Fo, which we've let slip through) then assert
1318 * vertical space, else only newline and move on.
1320 switch (n
->prev
->tok
) {
1333 if (MDOC_Fn
!= n
->tok
&& MDOC_Fo
!= n
->tok
) {
1346 termp_vt_pre(DECL_ARGS
)
1349 if (MDOC_ELEM
== n
->type
) {
1351 return(termp_under_pre(p
, pair
, m
, n
));
1352 } else if (MDOC_BLOCK
== n
->type
) {
1355 } else if (MDOC_HEAD
== n
->type
)
1358 return(termp_under_pre(p
, pair
, m
, n
));
1364 termp_bold_pre(DECL_ARGS
)
1367 term_fontpush(p
, TERMFONT_BOLD
);
1374 termp_fd_pre(DECL_ARGS
)
1378 return(termp_bold_pre(p
, pair
, m
, n
));
1384 termp_sh_pre(DECL_ARGS
)
1387 /* No vspace between consecutive `Sh' calls. */
1391 if (n
->prev
&& MDOC_Sh
== n
->prev
->tok
)
1392 if (NULL
== n
->prev
->body
->child
)
1397 term_fontpush(p
, TERMFONT_BOLD
);
1400 p
->offset
= term_len(p
, INDENT
);
1411 termp_sh_post(DECL_ARGS
)
1430 termp_bt_pre(DECL_ARGS
)
1433 term_word(p
, "is currently in beta test.");
1434 p
->flags
|= TERMP_SENTENCE
;
1441 termp_lb_post(DECL_ARGS
)
1444 if (SEC_LIBRARY
== n
->sec
&& MDOC_LINE
& n
->flags
)
1451 termp_ud_pre(DECL_ARGS
)
1454 term_word(p
, "currently under development.");
1455 p
->flags
|= TERMP_SENTENCE
;
1462 termp_d1_pre(DECL_ARGS
)
1465 if (MDOC_BLOCK
!= n
->type
)
1468 p
->offset
+= term_len(p
, (INDENT
+ 1));
1475 termp_d1_post(DECL_ARGS
)
1478 if (MDOC_BLOCK
!= n
->type
)
1486 termp_ft_pre(DECL_ARGS
)
1489 /* NB: MDOC_LINE does not effect this! */
1491 term_fontpush(p
, TERMFONT_UNDER
);
1498 termp_fn_pre(DECL_ARGS
)
1500 const struct mdoc_node
*nn
;
1504 term_fontpush(p
, TERMFONT_BOLD
);
1505 term_word(p
, n
->child
->string
);
1508 p
->flags
|= TERMP_NOSPACE
;
1511 for (nn
= n
->child
->next
; nn
; nn
= nn
->next
) {
1512 term_fontpush(p
, TERMFONT_UNDER
);
1513 term_word(p
, nn
->string
);
1522 if (MDOC_SYNPRETTY
& n
->flags
)
1531 termp_fa_pre(DECL_ARGS
)
1533 const struct mdoc_node
*nn
;
1535 if (n
->parent
->tok
!= MDOC_Fo
) {
1536 term_fontpush(p
, TERMFONT_UNDER
);
1540 for (nn
= n
->child
; nn
; nn
= nn
->next
) {
1541 term_fontpush(p
, TERMFONT_UNDER
);
1542 term_word(p
, nn
->string
);
1549 if (n
->child
&& n
->next
&& n
->next
->tok
== MDOC_Fa
)
1558 termp_bd_pre(DECL_ARGS
)
1560 size_t tabwidth
, rm
, rmax
;
1561 const struct mdoc_node
*nn
;
1563 if (MDOC_BLOCK
== n
->type
) {
1564 print_bvspace(p
, n
, n
);
1566 } else if (MDOC_HEAD
== n
->type
)
1570 if (n
->data
.Bd
->offs
)
1571 p
->offset
+= a2offs(p
, n
->data
.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
->data
.Bd
->type
&&
1582 DISP_unfilled
!= n
->data
.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
;
1645 if (DISP_literal
== n
->data
.Bd
->type
||
1646 DISP_unfilled
== n
->data
.Bd
->type
)
1647 p
->rmargin
= p
->maxrmargin
= TERM_MAXMARGIN
;
1649 p
->flags
|= TERMP_NOSPACE
;
1653 p
->maxrmargin
= rmax
;
1659 termp_bx_post(DECL_ARGS
)
1663 p
->flags
|= TERMP_NOSPACE
;
1664 term_word(p
, "BSD");
1670 termp_xx_pre(DECL_ARGS
)
1706 termp_igndelim_pre(DECL_ARGS
)
1709 p
->flags
|= TERMP_IGNDELIM
;
1716 termp_pf_post(DECL_ARGS
)
1719 p
->flags
|= TERMP_NOSPACE
;
1725 termp_ss_pre(DECL_ARGS
)
1735 term_fontpush(p
, TERMFONT_BOLD
);
1736 p
->offset
= term_len(p
, HALFINDENT
);
1748 termp_ss_post(DECL_ARGS
)
1751 if (MDOC_HEAD
== n
->type
)
1758 termp_cd_pre(DECL_ARGS
)
1762 term_fontpush(p
, TERMFONT_BOLD
);
1769 termp_in_pre(DECL_ARGS
)
1774 if (MDOC_SYNPRETTY
& n
->flags
&& MDOC_LINE
& n
->flags
) {
1775 term_fontpush(p
, TERMFONT_BOLD
);
1776 term_word(p
, "#include");
1780 term_fontpush(p
, TERMFONT_UNDER
);
1783 p
->flags
|= TERMP_NOSPACE
;
1790 termp_in_post(DECL_ARGS
)
1793 if (MDOC_SYNPRETTY
& n
->flags
)
1794 term_fontpush(p
, TERMFONT_BOLD
);
1796 p
->flags
|= TERMP_NOSPACE
;
1799 if (MDOC_SYNPRETTY
& n
->flags
)
1806 termp_sp_pre(DECL_ARGS
)
1812 len
= n
->child
? a2height(p
, n
->child
->string
) : 1;
1824 for (i
= 0; i
< len
; i
++)
1833 termp_quote_pre(DECL_ARGS
)
1836 if (MDOC_BODY
!= n
->type
)
1886 p
->flags
|= TERMP_NOSPACE
;
1893 termp_quote_post(DECL_ARGS
)
1896 if (MDOC_BODY
!= n
->type
)
1899 p
->flags
|= TERMP_NOSPACE
;
1952 termp_fo_pre(DECL_ARGS
)
1955 if (MDOC_BLOCK
== n
->type
) {
1958 } else if (MDOC_BODY
== n
->type
) {
1959 p
->flags
|= TERMP_NOSPACE
;
1964 if (NULL
== n
->child
)
1967 /* XXX: we drop non-initial arguments as per groff. */
1969 assert(n
->child
->string
);
1970 term_fontpush(p
, TERMFONT_BOLD
);
1971 term_word(p
, n
->child
->string
);
1978 termp_fo_post(DECL_ARGS
)
1981 if (MDOC_BODY
!= n
->type
)
1986 if (MDOC_SYNPRETTY
& n
->flags
)
1993 termp_bf_pre(DECL_ARGS
)
1996 if (MDOC_HEAD
== n
->type
)
1998 else if (MDOC_BLOCK
!= n
->type
)
2003 if (FONT_Em
== n
->data
.Bf
->font
)
2004 term_fontpush(p
, TERMFONT_UNDER
);
2005 else if (FONT_Sy
== n
->data
.Bf
->font
)
2006 term_fontpush(p
, TERMFONT_BOLD
);
2008 term_fontpush(p
, TERMFONT_NONE
);
2016 termp_sm_pre(DECL_ARGS
)
2019 assert(n
->child
&& MDOC_TEXT
== n
->child
->type
);
2020 if (0 == strcmp("on", n
->child
->string
)) {
2022 p
->flags
&= ~TERMP_NOSPACE
;
2023 p
->flags
&= ~TERMP_NONOSPACE
;
2025 p
->flags
|= TERMP_NONOSPACE
;
2033 termp_ap_pre(DECL_ARGS
)
2036 p
->flags
|= TERMP_NOSPACE
;
2038 p
->flags
|= TERMP_NOSPACE
;
2045 termp____post(DECL_ARGS
)
2049 * Handle lists of authors. In general, print each followed by
2050 * a comma. Don't print the comma if there are only two
2053 if (MDOC__A
== n
->tok
&& n
->next
&& MDOC__A
== n
->next
->tok
)
2054 if (NULL
== n
->next
->next
|| MDOC__A
!= n
->next
->next
->tok
)
2055 if (NULL
== n
->prev
|| MDOC__A
!= n
->prev
->tok
)
2060 if (NULL
== n
->parent
|| MDOC_Rs
!= n
->parent
->tok
)
2063 if (NULL
== n
->next
) {
2065 p
->flags
|= TERMP_SENTENCE
;
2073 termp_li_pre(DECL_ARGS
)
2076 term_fontpush(p
, TERMFONT_NONE
);
2083 termp_lk_pre(DECL_ARGS
)
2085 const struct mdoc_node
*nn
, *sv
;
2087 term_fontpush(p
, TERMFONT_UNDER
);
2091 if (NULL
== nn
|| NULL
== nn
->next
)
2094 for (nn
= nn
->next
; nn
; nn
= nn
->next
)
2095 term_word(p
, nn
->string
);
2101 term_fontpush(p
, TERMFONT_BOLD
);
2102 term_word(p
, sv
->string
);
2111 termp_bk_pre(DECL_ARGS
)
2120 p
->flags
|= TERMP_PREKEEP
;
2133 termp_bk_post(DECL_ARGS
)
2136 if (MDOC_BODY
== n
->type
)
2137 p
->flags
&= ~(TERMP_KEEP
| TERMP_PREKEEP
);
2142 termp_under_pre(DECL_ARGS
)
2145 term_fontpush(p
, TERMFONT_UNDER
);