]>
git.cameronkatri.com Git - mandoc.git/blob - mdoc_term.c
1 /* $Id: mdoc_term.c,v 1.196 2010/12/05 15:37:30 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.
324 if (TERMP_KEEP
& p
->flags
) {
325 if (n
->prev
&& n
->prev
->line
!= n
->line
) {
326 p
->flags
&= ~TERMP_KEEP
;
327 p
->flags
|= TERMP_PREKEEP
;
328 } else if (NULL
== n
->prev
) {
329 if (n
->parent
&& n
->parent
->line
!= n
->line
) {
330 p
->flags
&= ~TERMP_KEEP
;
331 p
->flags
|= TERMP_PREKEEP
;
336 if (chld
&& n
->child
)
337 print_mdoc_nodelist(p
, &npair
, m
, n
->child
);
339 term_fontpopq(p
, font
);
341 if (MDOC_TEXT
!= n
->type
&& termacts
[n
->tok
].post
&&
342 ! (MDOC_ENDED
& n
->flags
)) {
343 (void)(*termacts
[n
->tok
].post
)(p
, &npair
, m
, n
);
346 * Explicit end tokens not only call the post
347 * handler, but also tell the respective block
348 * that it must not call the post handler again.
350 if (ENDBODY_NOT
!= n
->end
)
351 n
->pending
->flags
|= MDOC_ENDED
;
354 * End of line terminating an implicit block
355 * while an explicit block is still open.
356 * Continue the explicit block without spacing.
358 if (ENDBODY_NOSPACE
== n
->end
)
359 p
->flags
|= TERMP_NOSPACE
;
362 if (MDOC_EOS
& n
->flags
)
363 p
->flags
|= TERMP_SENTENCE
;
366 p
->rmargin
= rmargin
;
371 print_mdoc_foot(struct termp
*p
, const void *arg
)
373 char buf
[DATESIZ
], os
[BUFSIZ
];
374 const struct mdoc_meta
*m
;
376 m
= (const struct mdoc_meta
*)arg
;
378 term_fontrepl(p
, TERMFONT_NONE
);
381 * Output the footer in new-groff style, that is, three columns
382 * with the middle being the manual date and flanking columns
383 * being the operating system:
388 time2a(m
->date
, buf
, DATESIZ
);
389 strlcpy(os
, m
->os
, BUFSIZ
);
394 p
->rmargin
= (p
->maxrmargin
-
395 term_strlen(p
, buf
) + term_len(p
, 1)) / 2;
396 p
->flags
|= TERMP_NOSPACE
| TERMP_NOBREAK
;
401 p
->offset
= p
->rmargin
;
402 p
->rmargin
= p
->maxrmargin
- term_strlen(p
, os
);
403 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
408 p
->offset
= p
->rmargin
;
409 p
->rmargin
= p
->maxrmargin
;
410 p
->flags
&= ~TERMP_NOBREAK
;
411 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
417 p
->rmargin
= p
->maxrmargin
;
423 print_mdoc_head(struct termp
*p
, const void *arg
)
425 char buf
[BUFSIZ
], title
[BUFSIZ
];
426 const struct mdoc_meta
*m
;
428 m
= (const struct mdoc_meta
*)arg
;
430 p
->rmargin
= p
->maxrmargin
;
434 * The header is strange. It has three components, which are
435 * really two with the first duplicated. It goes like this:
437 * IDENTIFIER TITLE IDENTIFIER
439 * The IDENTIFIER is NAME(SECTION), which is the command-name
440 * (if given, or "unknown" if not) followed by the manual page
441 * section. These are given in `Dt'. The TITLE is a free-form
442 * string depending on the manual volume. If not specified, it
443 * switches on the manual section.
447 strlcpy(buf
, m
->vol
, BUFSIZ
);
450 strlcat(buf
, " (", BUFSIZ
);
451 strlcat(buf
, m
->arch
, BUFSIZ
);
452 strlcat(buf
, ")", BUFSIZ
);
455 snprintf(title
, BUFSIZ
, "%s(%s)", m
->title
, m
->msec
);
458 p
->rmargin
= (p
->maxrmargin
-
459 term_strlen(p
, buf
) + term_len(p
, 1)) / 2;
460 p
->flags
|= TERMP_NOBREAK
| TERMP_NOSPACE
;
465 p
->offset
= p
->rmargin
;
466 p
->rmargin
= p
->maxrmargin
- term_strlen(p
, title
);
467 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
472 p
->offset
= p
->rmargin
;
473 p
->rmargin
= p
->maxrmargin
;
474 p
->flags
&= ~TERMP_NOBREAK
;
475 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
481 p
->rmargin
= p
->maxrmargin
;
482 p
->flags
&= ~TERMP_NOSPACE
;
487 a2height(const struct termp
*p
, const char *v
)
492 if ( ! a2roffsu(v
, &su
, SCALE_VS
))
493 SCALE_VS_INIT(&su
, term_len(p
, 1));
495 return(term_vspan(p
, &su
));
500 a2width(const struct termp
*p
, const char *v
)
505 if ( ! a2roffsu(v
, &su
, SCALE_MAX
))
506 SCALE_HS_INIT(&su
, term_strlen(p
, v
));
508 return(term_hspan(p
, &su
));
513 a2offs(const struct termp
*p
, const char *v
)
519 else if (0 == strcmp(v
, "left"))
521 else if (0 == strcmp(v
, "indent"))
522 return(term_len(p
, INDENT
+ 1));
523 else if (0 == strcmp(v
, "indent-two"))
524 return(term_len(p
, (INDENT
+ 1) * 2));
525 else if ( ! a2roffsu(v
, &su
, SCALE_MAX
))
526 SCALE_HS_INIT(&su
, term_strlen(p
, v
));
528 return(term_hspan(p
, &su
));
533 * Determine how much space to print out before block elements of `It'
534 * (and thus `Bl') and `Bd'. And then go ahead and print that space,
538 print_bvspace(struct termp
*p
,
539 const struct mdoc_node
*bl
,
540 const struct mdoc_node
*n
)
542 const struct mdoc_node
*nn
;
546 if (MDOC_Bd
== bl
->tok
&& bl
->data
.Bd
->comp
)
548 if (MDOC_Bl
== bl
->tok
&& bl
->data
.Bl
->comp
)
551 /* Do not vspace directly after Ss/Sh. */
553 for (nn
= n
; nn
; nn
= nn
->parent
) {
554 if (MDOC_BLOCK
!= nn
->type
)
556 if (MDOC_Ss
== nn
->tok
)
558 if (MDOC_Sh
== nn
->tok
)
560 if (NULL
== nn
->prev
)
565 /* A `-column' does not assert vspace within the list. */
567 if (MDOC_Bl
== bl
->tok
&& LIST_column
== bl
->data
.Bl
->type
)
568 if (n
->prev
&& MDOC_It
== n
->prev
->tok
)
571 /* A `-diag' without body does not vspace. */
573 if (MDOC_Bl
== bl
->tok
&& LIST_diag
== bl
->data
.Bl
->type
)
574 if (n
->prev
&& MDOC_It
== n
->prev
->tok
) {
575 assert(n
->prev
->body
);
576 if (NULL
== n
->prev
->body
->child
)
586 termp_it_pre(DECL_ARGS
)
588 const struct mdoc_node
*bl
, *nn
;
591 size_t width
, offset
, ncols
, dcol
;
594 if (MDOC_BLOCK
== n
->type
) {
595 print_bvspace(p
, n
->parent
->parent
, n
);
599 bl
= n
->parent
->parent
->parent
;
601 type
= bl
->data
.Bl
->type
;
604 * First calculate width and offset. This is pretty easy unless
605 * we're a -column list, in which case all prior columns must
611 if (bl
->data
.Bl
->offs
)
612 offset
= a2offs(p
, bl
->data
.Bl
->offs
);
616 if (MDOC_HEAD
== n
->type
)
620 * Imitate groff's column handling:
621 * - For each earlier column, add its width.
622 * - For less than 5 columns, add four more blanks per
624 * - For exactly 5 columns, add three more blank per
626 * - For more than 5 columns, add only one column.
628 ncols
= bl
->data
.Bl
->ncols
;
631 dcol
= ncols
< 5 ? term_len(p
, 4) :
632 ncols
== 5 ? term_len(p
, 3) : term_len(p
, 1);
635 * Calculate the offset by applying all prior MDOC_BODY,
636 * so we stop at the MDOC_HEAD (NULL == nn->prev).
639 for (i
= 0, nn
= n
->prev
;
640 nn
->prev
&& i
< (int)ncols
;
642 offset
+= dcol
+ a2width
643 (p
, bl
->data
.Bl
->cols
[i
]);
646 * When exceeding the declared number of columns, leave
647 * the remaining widths at 0. This will later be
648 * adjusted to the default width of 10, or, for the last
649 * column, stretched to the right margin.
655 * Use the declared column widths, extended as explained
656 * in the preceding paragraph.
658 width
= a2width(p
, bl
->data
.Bl
->cols
[i
]) + dcol
;
661 if (NULL
== bl
->data
.Bl
->width
)
665 * Note: buffer the width by 2, which is groff's magic
666 * number for buffering single arguments. See the above
667 * handling for column for how this changes.
669 assert(bl
->data
.Bl
->width
);
670 width
= a2width(p
, bl
->data
.Bl
->width
) + term_len(p
, 2);
675 * List-type can override the width in the case of fixed-head
676 * values (bullet, dash/hyphen, enum). Tags need a non-zero
686 if (width
< term_len(p
, 4))
687 width
= term_len(p
, 4);
690 if (width
< term_len(p
, 5))
691 width
= term_len(p
, 5);
695 width
= term_len(p
, 8);
701 width
= term_len(p
, 10);
708 * Whitespace control. Inset bodies need an initial space,
709 * while diagonal bodies need two.
712 p
->flags
|= TERMP_NOSPACE
;
716 if (MDOC_BODY
== n
->type
)
717 term_word(p
, "\\ \\ ");
720 if (MDOC_BODY
== n
->type
)
727 p
->flags
|= TERMP_NOSPACE
;
731 if (MDOC_HEAD
== n
->type
)
732 term_fontpush(p
, TERMFONT_BOLD
);
739 * Pad and break control. This is the tricky part. These flags
740 * are documented in term_flushln() in term.c. Note that we're
741 * going to unset all of these flags in termp_it_post() when we
753 if (MDOC_HEAD
== n
->type
)
754 p
->flags
|= TERMP_NOBREAK
;
756 p
->flags
|= TERMP_NOLPAD
;
759 if (MDOC_HEAD
== n
->type
)
760 p
->flags
|= TERMP_NOBREAK
;
762 p
->flags
|= TERMP_NOLPAD
;
764 if (MDOC_HEAD
!= n
->type
)
768 * This is ugly. If `-hang' is specified and the body
769 * is a `Bl' or `Bd', then we want basically to nullify
770 * the "overstep" effect in term_flushln() and treat
771 * this as a `-ohang' list instead.
773 if (n
->next
->child
&&
774 (MDOC_Bl
== n
->next
->child
->tok
||
775 MDOC_Bd
== n
->next
->child
->tok
)) {
776 p
->flags
&= ~TERMP_NOBREAK
;
777 p
->flags
&= ~TERMP_NOLPAD
;
779 p
->flags
|= TERMP_HANG
;
782 if (MDOC_HEAD
== n
->type
)
783 p
->flags
|= TERMP_NOBREAK
| TERMP_TWOSPACE
;
785 p
->flags
|= TERMP_NOLPAD
;
787 if (MDOC_HEAD
!= n
->type
)
789 if (NULL
== n
->next
|| NULL
== n
->next
->child
)
790 p
->flags
|= TERMP_DANGLE
;
793 if (MDOC_HEAD
== n
->type
)
797 p
->flags
&= ~TERMP_NOBREAK
;
799 p
->flags
|= TERMP_NOBREAK
;
802 if (MDOC_BODY
== n
->prev
->type
)
803 p
->flags
|= TERMP_NOLPAD
;
807 if (MDOC_HEAD
== n
->type
)
808 p
->flags
|= TERMP_NOBREAK
;
815 * Margin control. Set-head-width lists have their right
816 * margins shortened. The body for these lists has the offset
817 * necessarily lengthened. Everybody gets the offset.
825 * Same stipulation as above, regarding `-hang'. We
826 * don't want to recalculate rmargin and offsets when
827 * using `Bd' or `Bl' within `-hang' overstep lists.
829 if (MDOC_HEAD
== n
->type
&& n
->next
->child
&&
830 (MDOC_Bl
== n
->next
->child
->tok
||
831 MDOC_Bd
== n
->next
->child
->tok
))
844 if (MDOC_HEAD
== n
->type
)
845 p
->rmargin
= p
->offset
+ width
;
851 p
->rmargin
= p
->offset
+ width
;
853 * XXX - this behaviour is not documented: the
854 * right-most column is filled to the right margin.
856 if (MDOC_HEAD
== n
->type
)
858 if (NULL
== n
->next
&& p
->rmargin
< p
->maxrmargin
)
859 p
->rmargin
= p
->maxrmargin
;
866 * The dash, hyphen, bullet and enum lists all have a special
867 * HEAD character (temporarily bold, in some cases).
870 if (MDOC_HEAD
== n
->type
)
873 term_fontpush(p
, TERMFONT_BOLD
);
874 term_word(p
, "\\[bu]");
880 term_fontpush(p
, TERMFONT_BOLD
);
881 term_word(p
, "\\(hy");
885 (pair
->ppair
->ppair
->count
)++;
886 snprintf(buf
, sizeof(buf
), "%d.",
887 pair
->ppair
->ppair
->count
);
895 * If we're not going to process our children, indicate so here.
908 if (MDOC_HEAD
== n
->type
)
912 if (MDOC_HEAD
== n
->type
)
925 termp_it_post(DECL_ARGS
)
929 if (MDOC_BLOCK
== n
->type
)
932 type
= n
->parent
->parent
->parent
->data
.Bl
->type
;
940 if (MDOC_BODY
== n
->type
)
944 if (MDOC_BODY
== n
->type
)
953 * Now that our output is flushed, we can reset our tags. Since
954 * only `It' sets these flags, we're free to assume that nobody
955 * has munged them in the meanwhile.
958 p
->flags
&= ~TERMP_DANGLE
;
959 p
->flags
&= ~TERMP_NOBREAK
;
960 p
->flags
&= ~TERMP_TWOSPACE
;
961 p
->flags
&= ~TERMP_NOLPAD
;
962 p
->flags
&= ~TERMP_HANG
;
968 termp_nm_pre(DECL_ARGS
)
971 if (MDOC_BLOCK
== n
->type
)
974 if (MDOC_BODY
== n
->type
) {
975 if (NULL
== n
->child
)
977 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
978 p
->offset
+= term_len(p
, 1) +
979 (NULL
== n
->prev
->child
? term_strlen(p
, m
->name
) :
980 MDOC_TEXT
== n
->prev
->child
->type
?
981 term_strlen(p
, n
->prev
->child
->string
) :
986 if (NULL
== n
->child
&& NULL
== m
->name
)
989 if (MDOC_HEAD
== n
->type
)
990 synopsis_pre(p
, n
->parent
);
992 if (MDOC_HEAD
== n
->type
&& n
->next
->child
) {
993 p
->flags
|= TERMP_NOSPACE
| TERMP_NOBREAK
;
994 p
->rmargin
= p
->offset
+ term_len(p
, 1);
995 if (NULL
== n
->child
) {
996 p
->rmargin
+= term_strlen(p
, m
->name
);
997 } else if (MDOC_TEXT
== n
->child
->type
) {
998 p
->rmargin
+= term_strlen(p
, n
->child
->string
);
1000 p
->flags
|= TERMP_HANG
;
1002 p
->rmargin
+= term_len(p
, 5);
1003 p
->flags
|= TERMP_HANG
;
1007 term_fontpush(p
, TERMFONT_BOLD
);
1008 if (NULL
== n
->child
)
1009 term_word(p
, m
->name
);
1016 termp_nm_post(DECL_ARGS
)
1019 if (MDOC_HEAD
== n
->type
&& n
->next
->child
) {
1021 p
->flags
&= ~(TERMP_NOBREAK
| TERMP_HANG
);
1022 } else if (MDOC_BODY
== n
->type
&& n
->child
) {
1024 p
->flags
&= ~TERMP_NOLPAD
;
1031 termp_fl_pre(DECL_ARGS
)
1034 term_fontpush(p
, TERMFONT_BOLD
);
1035 term_word(p
, "\\-");
1038 p
->flags
|= TERMP_NOSPACE
;
1039 else if (n
->next
&& n
->next
->line
== n
->line
)
1040 p
->flags
|= TERMP_NOSPACE
;
1048 termp__a_pre(DECL_ARGS
)
1051 if (n
->prev
&& MDOC__A
== n
->prev
->tok
)
1052 if (NULL
== n
->next
|| MDOC__A
!= n
->next
->tok
)
1053 term_word(p
, "and");
1061 termp_an_pre(DECL_ARGS
)
1064 if (NULL
== n
->child
)
1068 * If not in the AUTHORS section, `An -split' will cause
1069 * newlines to occur before the author name. If in the AUTHORS
1070 * section, by default, the first `An' invocation is nosplit,
1071 * then all subsequent ones, regardless of whether interspersed
1072 * with other macros/text, are split. -split, in this case,
1073 * will override the condition of the implied first -nosplit.
1076 if (n
->sec
== SEC_AUTHORS
) {
1077 if ( ! (TERMP_ANPREC
& p
->flags
)) {
1078 if (TERMP_SPLIT
& p
->flags
)
1082 if (TERMP_NOSPLIT
& p
->flags
)
1088 if (TERMP_SPLIT
& p
->flags
)
1097 termp_an_post(DECL_ARGS
)
1101 if (SEC_AUTHORS
== n
->sec
)
1102 p
->flags
|= TERMP_ANPREC
;
1106 if (AUTH_split
== n
->data
.An
.auth
) {
1107 p
->flags
&= ~TERMP_NOSPLIT
;
1108 p
->flags
|= TERMP_SPLIT
;
1109 } else if (AUTH_nosplit
== n
->data
.An
.auth
) {
1110 p
->flags
&= ~TERMP_SPLIT
;
1111 p
->flags
|= TERMP_NOSPLIT
;
1119 termp_ns_pre(DECL_ARGS
)
1122 p
->flags
|= TERMP_NOSPACE
;
1129 termp_rs_pre(DECL_ARGS
)
1132 if (SEC_SEE_ALSO
!= n
->sec
)
1134 if (MDOC_BLOCK
== n
->type
&& n
->prev
)
1142 termp_rv_pre(DECL_ARGS
)
1144 const struct mdoc_node
*nn
;
1147 term_word(p
, "The");
1149 for (nn
= n
->child
; nn
; nn
= nn
->next
) {
1150 term_fontpush(p
, TERMFONT_BOLD
);
1151 term_word(p
, nn
->string
);
1153 p
->flags
|= TERMP_NOSPACE
;
1154 if (nn
->next
&& NULL
== nn
->next
->next
)
1155 term_word(p
, "(), and");
1157 term_word(p
, "(),");
1162 if (n
->child
&& n
->child
->next
)
1163 term_word(p
, "functions return");
1165 term_word(p
, "function returns");
1167 term_word(p
, "the value 0 if successful; otherwise the value "
1168 "-1 is returned and the global variable");
1170 term_fontpush(p
, TERMFONT_UNDER
);
1171 term_word(p
, "errno");
1174 term_word(p
, "is set to indicate the error.");
1175 p
->flags
|= TERMP_SENTENCE
;
1183 termp_ex_pre(DECL_ARGS
)
1185 const struct mdoc_node
*nn
;
1187 term_word(p
, "The");
1189 for (nn
= n
->child
; nn
; nn
= nn
->next
) {
1190 term_fontpush(p
, TERMFONT_BOLD
);
1191 term_word(p
, nn
->string
);
1193 p
->flags
|= TERMP_NOSPACE
;
1194 if (nn
->next
&& NULL
== nn
->next
->next
)
1195 term_word(p
, ", and");
1199 p
->flags
&= ~TERMP_NOSPACE
;
1202 if (n
->child
&& n
->child
->next
)
1203 term_word(p
, "utilities exit");
1205 term_word(p
, "utility exits");
1207 term_word(p
, "0 on success, and >0 if an error occurs.");
1208 p
->flags
|= TERMP_SENTENCE
;
1216 termp_nd_pre(DECL_ARGS
)
1219 if (MDOC_BODY
!= n
->type
)
1222 #if defined(__OpenBSD__) || defined(__linux__)
1223 term_word(p
, "\\(en");
1225 term_word(p
, "\\(em");
1233 termp_bl_pre(DECL_ARGS
)
1236 return(MDOC_HEAD
!= n
->type
);
1242 termp_bl_post(DECL_ARGS
)
1245 if (MDOC_BLOCK
== n
->type
)
1252 termp_xr_pre(DECL_ARGS
)
1254 const struct mdoc_node
*nn
;
1256 if (NULL
== n
->child
)
1259 assert(MDOC_TEXT
== n
->child
->type
);
1262 term_word(p
, nn
->string
);
1263 if (NULL
== (nn
= nn
->next
))
1265 p
->flags
|= TERMP_NOSPACE
;
1267 term_word(p
, nn
->string
);
1275 * This decides how to assert whitespace before any of the SYNOPSIS set
1276 * of macros (which, as in the case of Ft/Fo and Ft/Fn, may contain
1280 synopsis_pre(struct termp
*p
, const struct mdoc_node
*n
)
1283 * Obviously, if we're not in a SYNOPSIS or no prior macros
1284 * exist, do nothing.
1286 if (NULL
== n
->prev
|| ! (MDOC_SYNPRETTY
& n
->flags
))
1290 * If we're the second in a pair of like elements, emit our
1291 * newline and return. UNLESS we're `Fo', `Fn', `Fn', in which
1292 * case we soldier on.
1294 if (n
->prev
->tok
== n
->tok
&&
1295 MDOC_Ft
!= n
->tok
&&
1296 MDOC_Fo
!= n
->tok
&&
1297 MDOC_Fn
!= n
->tok
) {
1303 * If we're one of the SYNOPSIS set and non-like pair-wise after
1304 * another (or Fn/Fo, which we've let slip through) then assert
1305 * vertical space, else only newline and move on.
1307 switch (n
->prev
->tok
) {
1320 if (MDOC_Fn
!= n
->tok
&& MDOC_Fo
!= n
->tok
) {
1333 termp_vt_pre(DECL_ARGS
)
1336 if (MDOC_ELEM
== n
->type
) {
1338 return(termp_under_pre(p
, pair
, m
, n
));
1339 } else if (MDOC_BLOCK
== n
->type
) {
1342 } else if (MDOC_HEAD
== n
->type
)
1345 return(termp_under_pre(p
, pair
, m
, n
));
1351 termp_bold_pre(DECL_ARGS
)
1354 term_fontpush(p
, TERMFONT_BOLD
);
1361 termp_fd_pre(DECL_ARGS
)
1365 return(termp_bold_pre(p
, pair
, m
, n
));
1371 termp_sh_pre(DECL_ARGS
)
1374 /* No vspace between consecutive `Sh' calls. */
1378 if (n
->prev
&& MDOC_Sh
== n
->prev
->tok
)
1379 if (NULL
== n
->prev
->body
->child
)
1384 term_fontpush(p
, TERMFONT_BOLD
);
1387 p
->offset
= term_len(p
, INDENT
);
1398 termp_sh_post(DECL_ARGS
)
1417 termp_bt_pre(DECL_ARGS
)
1420 term_word(p
, "is currently in beta test.");
1421 p
->flags
|= TERMP_SENTENCE
;
1428 termp_lb_post(DECL_ARGS
)
1431 if (SEC_LIBRARY
== n
->sec
&& MDOC_LINE
& n
->flags
)
1438 termp_ud_pre(DECL_ARGS
)
1441 term_word(p
, "currently under development.");
1442 p
->flags
|= TERMP_SENTENCE
;
1449 termp_d1_pre(DECL_ARGS
)
1452 if (MDOC_BLOCK
!= n
->type
)
1455 p
->offset
+= term_len(p
, (INDENT
+ 1));
1462 termp_d1_post(DECL_ARGS
)
1465 if (MDOC_BLOCK
!= n
->type
)
1473 termp_ft_pre(DECL_ARGS
)
1476 /* NB: MDOC_LINE does not effect this! */
1478 term_fontpush(p
, TERMFONT_UNDER
);
1485 termp_fn_pre(DECL_ARGS
)
1487 const struct mdoc_node
*nn
;
1491 term_fontpush(p
, TERMFONT_BOLD
);
1492 term_word(p
, n
->child
->string
);
1495 p
->flags
|= TERMP_NOSPACE
;
1498 for (nn
= n
->child
->next
; nn
; nn
= nn
->next
) {
1499 term_fontpush(p
, TERMFONT_UNDER
);
1500 term_word(p
, nn
->string
);
1509 if (MDOC_SYNPRETTY
& n
->flags
)
1518 termp_fa_pre(DECL_ARGS
)
1520 const struct mdoc_node
*nn
;
1522 if (n
->parent
->tok
!= MDOC_Fo
) {
1523 term_fontpush(p
, TERMFONT_UNDER
);
1527 for (nn
= n
->child
; nn
; nn
= nn
->next
) {
1528 term_fontpush(p
, TERMFONT_UNDER
);
1529 term_word(p
, nn
->string
);
1536 if (n
->child
&& n
->next
&& n
->next
->tok
== MDOC_Fa
)
1545 termp_bd_pre(DECL_ARGS
)
1547 size_t tabwidth
, rm
, rmax
;
1548 const struct mdoc_node
*nn
;
1550 if (MDOC_BLOCK
== n
->type
) {
1551 print_bvspace(p
, n
, n
);
1553 } else if (MDOC_HEAD
== n
->type
)
1557 if (n
->data
.Bd
->offs
)
1558 p
->offset
+= a2offs(p
, n
->data
.Bd
->offs
);
1561 * If -ragged or -filled are specified, the block does nothing
1562 * but change the indentation. If -unfilled or -literal are
1563 * specified, text is printed exactly as entered in the display:
1564 * for macro lines, a newline is appended to the line. Blank
1565 * lines are allowed.
1568 if (DISP_literal
!= n
->data
.Bd
->type
&&
1569 DISP_unfilled
!= n
->data
.Bd
->type
)
1572 tabwidth
= p
->tabwidth
;
1573 p
->tabwidth
= term_len(p
, 8);
1575 rmax
= p
->maxrmargin
;
1576 p
->rmargin
= p
->maxrmargin
= TERM_MAXMARGIN
;
1578 for (nn
= n
->child
; nn
; nn
= nn
->next
) {
1579 print_mdoc_node(p
, pair
, m
, nn
);
1581 * If the printed node flushes its own line, then we
1582 * needn't do it here as well. This is hacky, but the
1583 * notion of selective eoln whitespace is pretty dumb
1584 * anyway, so don't sweat it.
1606 if (nn
->next
&& nn
->next
->line
== nn
->line
)
1609 p
->flags
|= TERMP_NOSPACE
;
1612 p
->tabwidth
= tabwidth
;
1614 p
->maxrmargin
= rmax
;
1621 termp_bd_post(DECL_ARGS
)
1625 if (MDOC_BODY
!= n
->type
)
1629 rmax
= p
->maxrmargin
;
1632 if (DISP_literal
== n
->data
.Bd
->type
||
1633 DISP_unfilled
== n
->data
.Bd
->type
)
1634 p
->rmargin
= p
->maxrmargin
= TERM_MAXMARGIN
;
1636 p
->flags
|= TERMP_NOSPACE
;
1640 p
->maxrmargin
= rmax
;
1646 termp_bx_post(DECL_ARGS
)
1650 p
->flags
|= TERMP_NOSPACE
;
1651 term_word(p
, "BSD");
1657 termp_xx_pre(DECL_ARGS
)
1693 termp_igndelim_pre(DECL_ARGS
)
1696 p
->flags
|= TERMP_IGNDELIM
;
1703 termp_pf_post(DECL_ARGS
)
1706 p
->flags
|= TERMP_NOSPACE
;
1712 termp_ss_pre(DECL_ARGS
)
1722 term_fontpush(p
, TERMFONT_BOLD
);
1723 p
->offset
= term_len(p
, HALFINDENT
);
1735 termp_ss_post(DECL_ARGS
)
1738 if (MDOC_HEAD
== n
->type
)
1745 termp_cd_pre(DECL_ARGS
)
1749 term_fontpush(p
, TERMFONT_BOLD
);
1756 termp_in_pre(DECL_ARGS
)
1761 if (MDOC_SYNPRETTY
& n
->flags
&& MDOC_LINE
& n
->flags
) {
1762 term_fontpush(p
, TERMFONT_BOLD
);
1763 term_word(p
, "#include");
1767 term_fontpush(p
, TERMFONT_UNDER
);
1770 p
->flags
|= TERMP_NOSPACE
;
1777 termp_in_post(DECL_ARGS
)
1780 if (MDOC_SYNPRETTY
& n
->flags
)
1781 term_fontpush(p
, TERMFONT_BOLD
);
1783 p
->flags
|= TERMP_NOSPACE
;
1786 if (MDOC_SYNPRETTY
& n
->flags
)
1793 termp_sp_pre(DECL_ARGS
)
1799 len
= n
->child
? a2height(p
, n
->child
->string
) : 1;
1806 if ((NULL
== n
->next
|| NULL
== n
->prev
) &&
1807 (MDOC_Ss
== n
->parent
->tok
||
1808 MDOC_Sh
== n
->parent
->tok
))
1816 for (i
= 0; i
< len
; i
++)
1825 termp_quote_pre(DECL_ARGS
)
1828 if (MDOC_BODY
!= n
->type
)
1878 p
->flags
|= TERMP_NOSPACE
;
1885 termp_quote_post(DECL_ARGS
)
1888 if (MDOC_BODY
!= n
->type
)
1891 p
->flags
|= TERMP_NOSPACE
;
1944 termp_fo_pre(DECL_ARGS
)
1947 if (MDOC_BLOCK
== n
->type
) {
1950 } else if (MDOC_BODY
== n
->type
) {
1951 p
->flags
|= TERMP_NOSPACE
;
1956 if (NULL
== n
->child
)
1959 /* XXX: we drop non-initial arguments as per groff. */
1961 assert(n
->child
->string
);
1962 term_fontpush(p
, TERMFONT_BOLD
);
1963 term_word(p
, n
->child
->string
);
1970 termp_fo_post(DECL_ARGS
)
1973 if (MDOC_BODY
!= n
->type
)
1978 if (MDOC_SYNPRETTY
& n
->flags
)
1985 termp_bf_pre(DECL_ARGS
)
1988 if (MDOC_HEAD
== n
->type
)
1990 else if (MDOC_BLOCK
!= n
->type
)
1995 if (FONT_Em
== n
->data
.Bf
->font
)
1996 term_fontpush(p
, TERMFONT_UNDER
);
1997 else if (FONT_Sy
== n
->data
.Bf
->font
)
1998 term_fontpush(p
, TERMFONT_BOLD
);
2000 term_fontpush(p
, TERMFONT_NONE
);
2008 termp_sm_pre(DECL_ARGS
)
2011 assert(n
->child
&& MDOC_TEXT
== n
->child
->type
);
2012 if (0 == strcmp("on", n
->child
->string
)) {
2014 p
->flags
&= ~TERMP_NOSPACE
;
2015 p
->flags
&= ~TERMP_NONOSPACE
;
2017 p
->flags
|= TERMP_NONOSPACE
;
2025 termp_ap_pre(DECL_ARGS
)
2028 p
->flags
|= TERMP_NOSPACE
;
2030 p
->flags
|= TERMP_NOSPACE
;
2037 termp____post(DECL_ARGS
)
2041 * Handle lists of authors. In general, print each followed by
2042 * a comma. Don't print the comma if there are only two
2045 if (MDOC__A
== n
->tok
&& n
->next
&& MDOC__A
== n
->next
->tok
)
2046 if (NULL
== n
->next
->next
|| MDOC__A
!= n
->next
->next
->tok
)
2047 if (NULL
== n
->prev
|| MDOC__A
!= n
->prev
->tok
)
2052 if (NULL
== n
->parent
|| MDOC_Rs
!= n
->parent
->tok
)
2055 if (NULL
== n
->next
) {
2057 p
->flags
|= TERMP_SENTENCE
;
2065 termp_li_pre(DECL_ARGS
)
2068 term_fontpush(p
, TERMFONT_NONE
);
2075 termp_lk_pre(DECL_ARGS
)
2077 const struct mdoc_node
*nn
, *sv
;
2079 term_fontpush(p
, TERMFONT_UNDER
);
2083 if (NULL
== nn
|| NULL
== nn
->next
)
2086 for (nn
= nn
->next
; nn
; nn
= nn
->next
)
2087 term_word(p
, nn
->string
);
2093 term_fontpush(p
, TERMFONT_BOLD
);
2094 term_word(p
, sv
->string
);
2103 termp_bk_pre(DECL_ARGS
)
2112 p
->flags
|= TERMP_PREKEEP
;
2125 termp_bk_post(DECL_ARGS
)
2128 if (MDOC_BODY
== n
->type
)
2129 p
->flags
&= ~(TERMP_KEEP
| TERMP_PREKEEP
);
2134 termp_under_pre(DECL_ARGS
)
2137 term_fontpush(p
, TERMFONT_UNDER
);