]>
git.cameronkatri.com Git - mandoc.git/blob - mdoc_term.c
1 /* $Id: mdoc_term.c,v 1.206 2011/01/01 12:18:37 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
));
318 term_word(p
, n
->string
);
323 if (termacts
[n
->tok
].pre
&& ENDBODY_NOT
== n
->end
)
324 chld
= (*termacts
[n
->tok
].pre
)
330 * Keeps only work until the end of a line. If a keep was
331 * invoked in a prior line, revert it to PREKEEP.
333 * Also let SYNPRETTY sections behave as if they were wrapped
337 if (TERMP_KEEP
& p
->flags
|| MDOC_SYNPRETTY
& n
->flags
) {
338 if (n
->prev
&& n
->prev
->line
!= n
->line
) {
339 p
->flags
&= ~TERMP_KEEP
;
340 p
->flags
|= TERMP_PREKEEP
;
341 } else if (NULL
== n
->prev
) {
342 if (n
->parent
&& n
->parent
->line
!= n
->line
) {
343 p
->flags
&= ~TERMP_KEEP
;
344 p
->flags
|= TERMP_PREKEEP
;
350 * Since SYNPRETTY sections aren't "turned off" with `Ek',
351 * we have to intuit whether we should disable formatting.
354 if ( ! (MDOC_SYNPRETTY
& n
->flags
) &&
355 ((n
->prev
&& MDOC_SYNPRETTY
& n
->prev
->flags
) ||
356 (n
->parent
&& MDOC_SYNPRETTY
& n
->parent
->flags
)))
357 p
->flags
&= ~(TERMP_KEEP
| TERMP_PREKEEP
);
359 if (chld
&& n
->child
)
360 print_mdoc_nodelist(p
, &npair
, m
, n
->child
);
362 term_fontpopq(p
, font
);
370 if ( ! termacts
[n
->tok
].post
|| MDOC_ENDED
& n
->flags
)
372 (void)(*termacts
[n
->tok
].post
)(p
, &npair
, m
, n
);
375 * Explicit end tokens not only call the post
376 * handler, but also tell the respective block
377 * that it must not call the post handler again.
379 if (ENDBODY_NOT
!= n
->end
)
380 n
->pending
->flags
|= MDOC_ENDED
;
383 * End of line terminating an implicit block
384 * while an explicit block is still open.
385 * Continue the explicit block without spacing.
387 if (ENDBODY_NOSPACE
== n
->end
)
388 p
->flags
|= TERMP_NOSPACE
;
392 if (MDOC_EOS
& n
->flags
)
393 p
->flags
|= TERMP_SENTENCE
;
396 p
->rmargin
= rmargin
;
401 print_mdoc_foot(struct termp
*p
, const void *arg
)
403 char buf
[DATESIZ
], os
[BUFSIZ
];
404 const struct mdoc_meta
*m
;
406 m
= (const struct mdoc_meta
*)arg
;
408 term_fontrepl(p
, TERMFONT_NONE
);
411 * Output the footer in new-groff style, that is, three columns
412 * with the middle being the manual date and flanking columns
413 * being the operating system:
418 time2a(m
->date
, buf
, DATESIZ
);
419 strlcpy(os
, m
->os
, BUFSIZ
);
424 p
->rmargin
= (p
->maxrmargin
-
425 term_strlen(p
, buf
) + term_len(p
, 1)) / 2;
426 p
->flags
|= TERMP_NOSPACE
| TERMP_NOBREAK
;
431 p
->offset
= p
->rmargin
;
432 p
->rmargin
= p
->maxrmargin
- term_strlen(p
, os
);
433 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
438 p
->offset
= p
->rmargin
;
439 p
->rmargin
= p
->maxrmargin
;
440 p
->flags
&= ~TERMP_NOBREAK
;
441 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
447 p
->rmargin
= p
->maxrmargin
;
453 print_mdoc_head(struct termp
*p
, const void *arg
)
455 char buf
[BUFSIZ
], title
[BUFSIZ
];
456 const struct mdoc_meta
*m
;
458 m
= (const struct mdoc_meta
*)arg
;
460 p
->rmargin
= p
->maxrmargin
;
464 * The header is strange. It has three components, which are
465 * really two with the first duplicated. It goes like this:
467 * IDENTIFIER TITLE IDENTIFIER
469 * The IDENTIFIER is NAME(SECTION), which is the command-name
470 * (if given, or "unknown" if not) followed by the manual page
471 * section. These are given in `Dt'. The TITLE is a free-form
472 * string depending on the manual volume. If not specified, it
473 * switches on the manual section.
477 strlcpy(buf
, m
->vol
, BUFSIZ
);
480 strlcat(buf
, " (", BUFSIZ
);
481 strlcat(buf
, m
->arch
, BUFSIZ
);
482 strlcat(buf
, ")", BUFSIZ
);
485 snprintf(title
, BUFSIZ
, "%s(%s)", m
->title
, m
->msec
);
488 p
->rmargin
= (p
->maxrmargin
-
489 term_strlen(p
, buf
) + term_len(p
, 1)) / 2;
490 p
->flags
|= TERMP_NOBREAK
| TERMP_NOSPACE
;
495 p
->offset
= p
->rmargin
;
496 p
->rmargin
= p
->maxrmargin
- term_strlen(p
, title
);
497 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
502 p
->offset
= p
->rmargin
;
503 p
->rmargin
= p
->maxrmargin
;
504 p
->flags
&= ~TERMP_NOBREAK
;
505 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
511 p
->rmargin
= p
->maxrmargin
;
512 p
->flags
&= ~TERMP_NOSPACE
;
517 a2height(const struct termp
*p
, const char *v
)
522 if ( ! a2roffsu(v
, &su
, SCALE_VS
))
523 SCALE_VS_INIT(&su
, term_len(p
, 1));
525 return(term_vspan(p
, &su
));
530 a2width(const struct termp
*p
, const char *v
)
535 if ( ! a2roffsu(v
, &su
, SCALE_MAX
))
536 SCALE_HS_INIT(&su
, term_strlen(p
, v
));
538 return(term_hspan(p
, &su
));
543 a2offs(const struct termp
*p
, const char *v
)
549 else if (0 == strcmp(v
, "left"))
551 else if (0 == strcmp(v
, "indent"))
552 return(term_len(p
, INDENT
+ 1));
553 else if (0 == strcmp(v
, "indent-two"))
554 return(term_len(p
, (INDENT
+ 1) * 2));
555 else if ( ! a2roffsu(v
, &su
, SCALE_MAX
))
556 SCALE_HS_INIT(&su
, term_strlen(p
, v
));
558 return(term_hspan(p
, &su
));
563 * Determine how much space to print out before block elements of `It'
564 * (and thus `Bl') and `Bd'. And then go ahead and print that space,
568 print_bvspace(struct termp
*p
,
569 const struct mdoc_node
*bl
,
570 const struct mdoc_node
*n
)
572 const struct mdoc_node
*nn
;
576 if (MDOC_Bd
== bl
->tok
&& bl
->norm
->Bd
.comp
)
578 if (MDOC_Bl
== bl
->tok
&& bl
->norm
->Bl
.comp
)
581 /* Do not vspace directly after Ss/Sh. */
583 for (nn
= n
; nn
; nn
= nn
->parent
) {
584 if (MDOC_BLOCK
!= nn
->type
)
586 if (MDOC_Ss
== nn
->tok
)
588 if (MDOC_Sh
== nn
->tok
)
590 if (NULL
== nn
->prev
)
595 /* A `-column' does not assert vspace within the list. */
597 if (MDOC_Bl
== bl
->tok
&& LIST_column
== bl
->norm
->Bl
.type
)
598 if (n
->prev
&& MDOC_It
== n
->prev
->tok
)
601 /* A `-diag' without body does not vspace. */
603 if (MDOC_Bl
== bl
->tok
&& LIST_diag
== bl
->norm
->Bl
.type
)
604 if (n
->prev
&& MDOC_It
== n
->prev
->tok
) {
605 assert(n
->prev
->body
);
606 if (NULL
== n
->prev
->body
->child
)
616 termp_it_pre(DECL_ARGS
)
618 const struct mdoc_node
*bl
, *nn
;
621 size_t width
, offset
, ncols
, dcol
;
624 if (MDOC_BLOCK
== n
->type
) {
625 print_bvspace(p
, n
->parent
->parent
, n
);
629 bl
= n
->parent
->parent
->parent
;
630 type
= bl
->norm
->Bl
.type
;
633 * First calculate width and offset. This is pretty easy unless
634 * we're a -column list, in which case all prior columns must
640 if (bl
->norm
->Bl
.offs
)
641 offset
= a2offs(p
, bl
->norm
->Bl
.offs
);
645 if (MDOC_HEAD
== n
->type
)
649 * Imitate groff's column handling:
650 * - For each earlier column, add its width.
651 * - For less than 5 columns, add four more blanks per
653 * - For exactly 5 columns, add three more blank per
655 * - For more than 5 columns, add only one column.
657 ncols
= bl
->norm
->Bl
.ncols
;
660 dcol
= ncols
< 5 ? term_len(p
, 4) :
661 ncols
== 5 ? term_len(p
, 3) : term_len(p
, 1);
664 * Calculate the offset by applying all prior MDOC_BODY,
665 * so we stop at the MDOC_HEAD (NULL == nn->prev).
668 for (i
= 0, nn
= n
->prev
;
669 nn
->prev
&& i
< (int)ncols
;
671 offset
+= dcol
+ a2width
672 (p
, bl
->norm
->Bl
.cols
[i
]);
675 * When exceeding the declared number of columns, leave
676 * the remaining widths at 0. This will later be
677 * adjusted to the default width of 10, or, for the last
678 * column, stretched to the right margin.
684 * Use the declared column widths, extended as explained
685 * in the preceding paragraph.
687 width
= a2width(p
, bl
->norm
->Bl
.cols
[i
]) + dcol
;
690 if (NULL
== bl
->norm
->Bl
.width
)
694 * Note: buffer the width by 2, which is groff's magic
695 * number for buffering single arguments. See the above
696 * handling for column for how this changes.
698 assert(bl
->norm
->Bl
.width
);
699 width
= a2width(p
, bl
->norm
->Bl
.width
) + term_len(p
, 2);
704 * List-type can override the width in the case of fixed-head
705 * values (bullet, dash/hyphen, enum). Tags need a non-zero
715 if (width
< term_len(p
, 4))
716 width
= term_len(p
, 4);
719 if (width
< term_len(p
, 5))
720 width
= term_len(p
, 5);
724 width
= term_len(p
, 8);
730 width
= term_len(p
, 10);
737 * Whitespace control. Inset bodies need an initial space,
738 * while diagonal bodies need two.
741 p
->flags
|= TERMP_NOSPACE
;
745 if (MDOC_BODY
== n
->type
)
746 term_word(p
, "\\ \\ ");
749 if (MDOC_BODY
== n
->type
)
756 p
->flags
|= TERMP_NOSPACE
;
760 if (MDOC_HEAD
== n
->type
)
761 term_fontpush(p
, TERMFONT_BOLD
);
768 * Pad and break control. This is the tricky part. These flags
769 * are documented in term_flushln() in term.c. Note that we're
770 * going to unset all of these flags in termp_it_post() when we
782 if (MDOC_HEAD
== n
->type
)
783 p
->flags
|= TERMP_NOBREAK
;
785 p
->flags
|= TERMP_NOLPAD
;
788 if (MDOC_HEAD
== n
->type
)
789 p
->flags
|= TERMP_NOBREAK
;
791 p
->flags
|= TERMP_NOLPAD
;
793 if (MDOC_HEAD
!= n
->type
)
797 * This is ugly. If `-hang' is specified and the body
798 * is a `Bl' or `Bd', then we want basically to nullify
799 * the "overstep" effect in term_flushln() and treat
800 * this as a `-ohang' list instead.
802 if (n
->next
->child
&&
803 (MDOC_Bl
== n
->next
->child
->tok
||
804 MDOC_Bd
== n
->next
->child
->tok
)) {
805 p
->flags
&= ~TERMP_NOBREAK
;
806 p
->flags
&= ~TERMP_NOLPAD
;
808 p
->flags
|= TERMP_HANG
;
811 if (MDOC_HEAD
== n
->type
)
812 p
->flags
|= TERMP_NOBREAK
| TERMP_TWOSPACE
;
814 p
->flags
|= TERMP_NOLPAD
;
816 if (MDOC_HEAD
!= n
->type
)
818 if (NULL
== n
->next
|| NULL
== n
->next
->child
)
819 p
->flags
|= TERMP_DANGLE
;
822 if (MDOC_HEAD
== n
->type
)
826 p
->flags
&= ~TERMP_NOBREAK
;
828 p
->flags
|= TERMP_NOBREAK
;
831 if (MDOC_BODY
== n
->prev
->type
)
832 p
->flags
|= TERMP_NOLPAD
;
836 if (MDOC_HEAD
== n
->type
)
837 p
->flags
|= TERMP_NOBREAK
;
844 * Margin control. Set-head-width lists have their right
845 * margins shortened. The body for these lists has the offset
846 * necessarily lengthened. Everybody gets the offset.
854 * Same stipulation as above, regarding `-hang'. We
855 * don't want to recalculate rmargin and offsets when
856 * using `Bd' or `Bl' within `-hang' overstep lists.
858 if (MDOC_HEAD
== n
->type
&& n
->next
->child
&&
859 (MDOC_Bl
== n
->next
->child
->tok
||
860 MDOC_Bd
== n
->next
->child
->tok
))
873 if (MDOC_HEAD
== n
->type
)
874 p
->rmargin
= p
->offset
+ width
;
880 p
->rmargin
= p
->offset
+ width
;
882 * XXX - this behaviour is not documented: the
883 * right-most column is filled to the right margin.
885 if (MDOC_HEAD
== n
->type
)
887 if (NULL
== n
->next
&& p
->rmargin
< p
->maxrmargin
)
888 p
->rmargin
= p
->maxrmargin
;
895 * The dash, hyphen, bullet and enum lists all have a special
896 * HEAD character (temporarily bold, in some cases).
899 if (MDOC_HEAD
== n
->type
)
902 term_fontpush(p
, TERMFONT_BOLD
);
903 term_word(p
, "\\[bu]");
909 term_fontpush(p
, TERMFONT_BOLD
);
910 term_word(p
, "\\(hy");
914 (pair
->ppair
->ppair
->count
)++;
915 snprintf(buf
, sizeof(buf
), "%d.",
916 pair
->ppair
->ppair
->count
);
924 * If we're not going to process our children, indicate so here.
937 if (MDOC_HEAD
== n
->type
)
941 if (MDOC_HEAD
== n
->type
)
954 termp_it_post(DECL_ARGS
)
958 if (MDOC_BLOCK
== n
->type
)
961 type
= n
->parent
->parent
->parent
->norm
->Bl
.type
;
969 if (MDOC_BODY
== n
->type
)
973 if (MDOC_BODY
== n
->type
)
982 * Now that our output is flushed, we can reset our tags. Since
983 * only `It' sets these flags, we're free to assume that nobody
984 * has munged them in the meanwhile.
987 p
->flags
&= ~TERMP_DANGLE
;
988 p
->flags
&= ~TERMP_NOBREAK
;
989 p
->flags
&= ~TERMP_TWOSPACE
;
990 p
->flags
&= ~TERMP_NOLPAD
;
991 p
->flags
&= ~TERMP_HANG
;
997 termp_nm_pre(DECL_ARGS
)
1000 if (MDOC_BLOCK
== n
->type
)
1003 if (MDOC_BODY
== n
->type
) {
1004 if (NULL
== n
->child
)
1006 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
1007 p
->offset
+= term_len(p
, 1) +
1008 (NULL
== n
->prev
->child
? term_strlen(p
, m
->name
) :
1009 MDOC_TEXT
== n
->prev
->child
->type
?
1010 term_strlen(p
, n
->prev
->child
->string
) :
1015 if (NULL
== n
->child
&& NULL
== m
->name
)
1018 if (MDOC_HEAD
== n
->type
)
1019 synopsis_pre(p
, n
->parent
);
1021 if (MDOC_HEAD
== n
->type
&& n
->next
->child
) {
1022 p
->flags
|= TERMP_NOSPACE
| TERMP_NOBREAK
;
1023 p
->rmargin
= p
->offset
+ term_len(p
, 1);
1024 if (NULL
== n
->child
) {
1025 p
->rmargin
+= term_strlen(p
, m
->name
);
1026 } else if (MDOC_TEXT
== n
->child
->type
) {
1027 p
->rmargin
+= term_strlen(p
, n
->child
->string
);
1029 p
->flags
|= TERMP_HANG
;
1031 p
->rmargin
+= term_len(p
, 5);
1032 p
->flags
|= TERMP_HANG
;
1036 term_fontpush(p
, TERMFONT_BOLD
);
1037 if (NULL
== n
->child
)
1038 term_word(p
, m
->name
);
1045 termp_nm_post(DECL_ARGS
)
1048 if (MDOC_HEAD
== n
->type
&& n
->next
->child
) {
1050 p
->flags
&= ~(TERMP_NOBREAK
| TERMP_HANG
);
1051 } else if (MDOC_BODY
== n
->type
&& n
->child
) {
1053 p
->flags
&= ~TERMP_NOLPAD
;
1060 termp_fl_pre(DECL_ARGS
)
1063 term_fontpush(p
, TERMFONT_BOLD
);
1064 term_word(p
, "\\-");
1067 p
->flags
|= TERMP_NOSPACE
;
1068 else if (n
->next
&& n
->next
->line
== n
->line
)
1069 p
->flags
|= TERMP_NOSPACE
;
1077 termp__a_pre(DECL_ARGS
)
1080 if (n
->prev
&& MDOC__A
== n
->prev
->tok
)
1081 if (NULL
== n
->next
|| MDOC__A
!= n
->next
->tok
)
1082 term_word(p
, "and");
1090 termp_an_pre(DECL_ARGS
)
1093 if (NULL
== n
->child
)
1097 * If not in the AUTHORS section, `An -split' will cause
1098 * newlines to occur before the author name. If in the AUTHORS
1099 * section, by default, the first `An' invocation is nosplit,
1100 * then all subsequent ones, regardless of whether interspersed
1101 * with other macros/text, are split. -split, in this case,
1102 * will override the condition of the implied first -nosplit.
1105 if (n
->sec
== SEC_AUTHORS
) {
1106 if ( ! (TERMP_ANPREC
& p
->flags
)) {
1107 if (TERMP_SPLIT
& p
->flags
)
1111 if (TERMP_NOSPLIT
& p
->flags
)
1117 if (TERMP_SPLIT
& p
->flags
)
1126 termp_an_post(DECL_ARGS
)
1130 if (SEC_AUTHORS
== n
->sec
)
1131 p
->flags
|= TERMP_ANPREC
;
1135 if (AUTH_split
== n
->norm
->An
.auth
) {
1136 p
->flags
&= ~TERMP_NOSPLIT
;
1137 p
->flags
|= TERMP_SPLIT
;
1138 } else if (AUTH_nosplit
== n
->norm
->An
.auth
) {
1139 p
->flags
&= ~TERMP_SPLIT
;
1140 p
->flags
|= TERMP_NOSPLIT
;
1148 termp_ns_pre(DECL_ARGS
)
1151 p
->flags
|= TERMP_NOSPACE
;
1158 termp_rs_pre(DECL_ARGS
)
1161 if (SEC_SEE_ALSO
!= n
->sec
)
1163 if (MDOC_BLOCK
== n
->type
&& n
->prev
)
1171 termp_rv_pre(DECL_ARGS
)
1173 const struct mdoc_node
*nn
;
1176 term_word(p
, "The");
1178 for (nn
= n
->child
; nn
; nn
= nn
->next
) {
1179 term_fontpush(p
, TERMFONT_BOLD
);
1180 term_word(p
, nn
->string
);
1182 p
->flags
|= TERMP_NOSPACE
;
1183 if (nn
->next
&& NULL
== nn
->next
->next
)
1184 term_word(p
, "(), and");
1186 term_word(p
, "(),");
1191 if (n
->child
&& n
->child
->next
)
1192 term_word(p
, "functions return");
1194 term_word(p
, "function returns");
1196 term_word(p
, "the value 0 if successful; otherwise the value "
1197 "-1 is returned and the global variable");
1199 term_fontpush(p
, TERMFONT_UNDER
);
1200 term_word(p
, "errno");
1203 term_word(p
, "is set to indicate the error.");
1204 p
->flags
|= TERMP_SENTENCE
;
1212 termp_ex_pre(DECL_ARGS
)
1214 const struct mdoc_node
*nn
;
1216 term_word(p
, "The");
1218 for (nn
= n
->child
; nn
; nn
= nn
->next
) {
1219 term_fontpush(p
, TERMFONT_BOLD
);
1220 term_word(p
, nn
->string
);
1222 p
->flags
|= TERMP_NOSPACE
;
1223 if (nn
->next
&& NULL
== nn
->next
->next
)
1224 term_word(p
, ", and");
1228 p
->flags
&= ~TERMP_NOSPACE
;
1231 if (n
->child
&& n
->child
->next
)
1232 term_word(p
, "utilities exit");
1234 term_word(p
, "utility exits");
1236 term_word(p
, "0 on success, and >0 if an error occurs.");
1237 p
->flags
|= TERMP_SENTENCE
;
1245 termp_nd_pre(DECL_ARGS
)
1248 if (MDOC_BODY
!= n
->type
)
1251 #if defined(__OpenBSD__) || defined(__linux__)
1252 term_word(p
, "\\(en");
1254 term_word(p
, "\\(em");
1262 termp_bl_pre(DECL_ARGS
)
1265 return(MDOC_HEAD
!= n
->type
);
1271 termp_bl_post(DECL_ARGS
)
1274 if (MDOC_BLOCK
== n
->type
)
1281 termp_xr_pre(DECL_ARGS
)
1283 const struct mdoc_node
*nn
;
1285 if (NULL
== n
->child
)
1288 assert(MDOC_TEXT
== n
->child
->type
);
1291 term_word(p
, nn
->string
);
1292 if (NULL
== (nn
= nn
->next
))
1294 p
->flags
|= TERMP_NOSPACE
;
1296 term_word(p
, nn
->string
);
1304 * This decides how to assert whitespace before any of the SYNOPSIS set
1305 * of macros (which, as in the case of Ft/Fo and Ft/Fn, may contain
1309 synopsis_pre(struct termp
*p
, const struct mdoc_node
*n
)
1312 * Obviously, if we're not in a SYNOPSIS or no prior macros
1313 * exist, do nothing.
1315 if (NULL
== n
->prev
|| ! (MDOC_SYNPRETTY
& n
->flags
))
1319 * If we're the second in a pair of like elements, emit our
1320 * newline and return. UNLESS we're `Fo', `Fn', `Fn', in which
1321 * case we soldier on.
1323 if (n
->prev
->tok
== n
->tok
&&
1324 MDOC_Ft
!= n
->tok
&&
1325 MDOC_Fo
!= n
->tok
&&
1326 MDOC_Fn
!= n
->tok
) {
1332 * If we're one of the SYNOPSIS set and non-like pair-wise after
1333 * another (or Fn/Fo, which we've let slip through) then assert
1334 * vertical space, else only newline and move on.
1336 switch (n
->prev
->tok
) {
1349 if (MDOC_Fn
!= n
->tok
&& MDOC_Fo
!= n
->tok
) {
1362 termp_vt_pre(DECL_ARGS
)
1365 if (MDOC_ELEM
== n
->type
) {
1367 return(termp_under_pre(p
, pair
, m
, n
));
1368 } else if (MDOC_BLOCK
== n
->type
) {
1371 } else if (MDOC_HEAD
== n
->type
)
1374 return(termp_under_pre(p
, pair
, m
, n
));
1380 termp_bold_pre(DECL_ARGS
)
1383 term_fontpush(p
, TERMFONT_BOLD
);
1390 termp_fd_pre(DECL_ARGS
)
1394 return(termp_bold_pre(p
, pair
, m
, n
));
1400 termp_sh_pre(DECL_ARGS
)
1403 /* No vspace between consecutive `Sh' calls. */
1407 if (n
->prev
&& MDOC_Sh
== n
->prev
->tok
)
1408 if (NULL
== n
->prev
->body
->child
)
1413 term_fontpush(p
, TERMFONT_BOLD
);
1416 p
->offset
= term_len(p
, INDENT
);
1427 termp_sh_post(DECL_ARGS
)
1446 termp_bt_pre(DECL_ARGS
)
1449 term_word(p
, "is currently in beta test.");
1450 p
->flags
|= TERMP_SENTENCE
;
1457 termp_lb_post(DECL_ARGS
)
1460 if (SEC_LIBRARY
== n
->sec
&& MDOC_LINE
& n
->flags
)
1467 termp_ud_pre(DECL_ARGS
)
1470 term_word(p
, "currently under development.");
1471 p
->flags
|= TERMP_SENTENCE
;
1478 termp_d1_pre(DECL_ARGS
)
1481 if (MDOC_BLOCK
!= n
->type
)
1484 p
->offset
+= term_len(p
, (INDENT
+ 1));
1491 termp_d1_post(DECL_ARGS
)
1494 if (MDOC_BLOCK
!= n
->type
)
1502 termp_ft_pre(DECL_ARGS
)
1505 /* NB: MDOC_LINE does not effect this! */
1507 term_fontpush(p
, TERMFONT_UNDER
);
1514 termp_fn_pre(DECL_ARGS
)
1516 const struct mdoc_node
*nn
;
1520 term_fontpush(p
, TERMFONT_BOLD
);
1521 term_word(p
, n
->child
->string
);
1524 p
->flags
|= TERMP_NOSPACE
;
1527 for (nn
= n
->child
->next
; nn
; nn
= nn
->next
) {
1528 term_fontpush(p
, TERMFONT_UNDER
);
1529 term_word(p
, nn
->string
);
1538 if (MDOC_SYNPRETTY
& n
->flags
)
1547 termp_fa_pre(DECL_ARGS
)
1549 const struct mdoc_node
*nn
;
1551 if (n
->parent
->tok
!= MDOC_Fo
) {
1552 term_fontpush(p
, TERMFONT_UNDER
);
1556 for (nn
= n
->child
; nn
; nn
= nn
->next
) {
1557 term_fontpush(p
, TERMFONT_UNDER
);
1558 term_word(p
, nn
->string
);
1565 if (n
->child
&& n
->next
&& n
->next
->tok
== MDOC_Fa
)
1574 termp_bd_pre(DECL_ARGS
)
1576 size_t tabwidth
, rm
, rmax
;
1577 const struct mdoc_node
*nn
;
1579 if (MDOC_BLOCK
== n
->type
) {
1580 print_bvspace(p
, n
, n
);
1582 } else if (MDOC_HEAD
== n
->type
)
1585 if (n
->norm
->Bd
.offs
)
1586 p
->offset
+= a2offs(p
, n
->norm
->Bd
.offs
);
1589 * If -ragged or -filled are specified, the block does nothing
1590 * but change the indentation. If -unfilled or -literal are
1591 * specified, text is printed exactly as entered in the display:
1592 * for macro lines, a newline is appended to the line. Blank
1593 * lines are allowed.
1596 if (DISP_literal
!= n
->norm
->Bd
.type
&&
1597 DISP_unfilled
!= n
->norm
->Bd
.type
)
1600 tabwidth
= p
->tabwidth
;
1601 p
->tabwidth
= term_len(p
, 8);
1603 rmax
= p
->maxrmargin
;
1604 p
->rmargin
= p
->maxrmargin
= TERM_MAXMARGIN
;
1606 for (nn
= n
->child
; nn
; nn
= nn
->next
) {
1607 print_mdoc_node(p
, pair
, m
, nn
);
1609 * If the printed node flushes its own line, then we
1610 * needn't do it here as well. This is hacky, but the
1611 * notion of selective eoln whitespace is pretty dumb
1612 * anyway, so don't sweat it.
1634 if (nn
->next
&& nn
->next
->line
== nn
->line
)
1637 p
->flags
|= TERMP_NOSPACE
;
1640 p
->tabwidth
= tabwidth
;
1642 p
->maxrmargin
= rmax
;
1649 termp_bd_post(DECL_ARGS
)
1653 if (MDOC_BODY
!= n
->type
)
1657 rmax
= p
->maxrmargin
;
1659 if (DISP_literal
== n
->norm
->Bd
.type
||
1660 DISP_unfilled
== n
->norm
->Bd
.type
)
1661 p
->rmargin
= p
->maxrmargin
= TERM_MAXMARGIN
;
1663 p
->flags
|= TERMP_NOSPACE
;
1667 p
->maxrmargin
= rmax
;
1673 termp_bx_post(DECL_ARGS
)
1677 p
->flags
|= TERMP_NOSPACE
;
1678 term_word(p
, "BSD");
1684 termp_xx_pre(DECL_ARGS
)
1720 termp_igndelim_pre(DECL_ARGS
)
1723 p
->flags
|= TERMP_IGNDELIM
;
1730 termp_pf_post(DECL_ARGS
)
1733 p
->flags
|= TERMP_NOSPACE
;
1739 termp_ss_pre(DECL_ARGS
)
1749 term_fontpush(p
, TERMFONT_BOLD
);
1750 p
->offset
= term_len(p
, HALFINDENT
);
1762 termp_ss_post(DECL_ARGS
)
1765 if (MDOC_HEAD
== n
->type
)
1772 termp_cd_pre(DECL_ARGS
)
1776 term_fontpush(p
, TERMFONT_BOLD
);
1783 termp_in_pre(DECL_ARGS
)
1788 if (MDOC_SYNPRETTY
& n
->flags
&& MDOC_LINE
& n
->flags
) {
1789 term_fontpush(p
, TERMFONT_BOLD
);
1790 term_word(p
, "#include");
1794 term_fontpush(p
, TERMFONT_UNDER
);
1797 p
->flags
|= TERMP_NOSPACE
;
1804 termp_in_post(DECL_ARGS
)
1807 if (MDOC_SYNPRETTY
& n
->flags
)
1808 term_fontpush(p
, TERMFONT_BOLD
);
1810 p
->flags
|= TERMP_NOSPACE
;
1813 if (MDOC_SYNPRETTY
& n
->flags
)
1820 termp_sp_pre(DECL_ARGS
)
1826 len
= n
->child
? a2height(p
, n
->child
->string
) : 1;
1838 for (i
= 0; i
< len
; i
++)
1847 termp_quote_pre(DECL_ARGS
)
1850 if (MDOC_BODY
!= n
->type
&& MDOC_ELEM
!= n
->type
)
1902 p
->flags
|= TERMP_NOSPACE
;
1909 termp_quote_post(DECL_ARGS
)
1912 if (MDOC_BODY
!= n
->type
&& MDOC_ELEM
!= n
->type
)
1915 p
->flags
|= TERMP_NOSPACE
;
1970 termp_fo_pre(DECL_ARGS
)
1973 if (MDOC_BLOCK
== n
->type
) {
1976 } else if (MDOC_BODY
== n
->type
) {
1977 p
->flags
|= TERMP_NOSPACE
;
1982 if (NULL
== n
->child
)
1985 /* XXX: we drop non-initial arguments as per groff. */
1987 assert(n
->child
->string
);
1988 term_fontpush(p
, TERMFONT_BOLD
);
1989 term_word(p
, n
->child
->string
);
1996 termp_fo_post(DECL_ARGS
)
1999 if (MDOC_BODY
!= n
->type
)
2004 if (MDOC_SYNPRETTY
& n
->flags
)
2011 termp_bf_pre(DECL_ARGS
)
2014 if (MDOC_HEAD
== n
->type
)
2016 else if (MDOC_BLOCK
!= n
->type
)
2019 if (FONT_Em
== n
->norm
->Bf
.font
)
2020 term_fontpush(p
, TERMFONT_UNDER
);
2021 else if (FONT_Sy
== n
->norm
->Bf
.font
)
2022 term_fontpush(p
, TERMFONT_BOLD
);
2024 term_fontpush(p
, TERMFONT_NONE
);
2032 termp_sm_pre(DECL_ARGS
)
2035 assert(n
->child
&& MDOC_TEXT
== n
->child
->type
);
2036 if (0 == strcmp("on", n
->child
->string
)) {
2038 p
->flags
&= ~TERMP_NOSPACE
;
2039 p
->flags
&= ~TERMP_NONOSPACE
;
2041 p
->flags
|= TERMP_NONOSPACE
;
2049 termp_ap_pre(DECL_ARGS
)
2052 p
->flags
|= TERMP_NOSPACE
;
2054 p
->flags
|= TERMP_NOSPACE
;
2061 termp____post(DECL_ARGS
)
2065 * Handle lists of authors. In general, print each followed by
2066 * a comma. Don't print the comma if there are only two
2069 if (MDOC__A
== n
->tok
&& n
->next
&& MDOC__A
== n
->next
->tok
)
2070 if (NULL
== n
->next
->next
|| MDOC__A
!= n
->next
->next
->tok
)
2071 if (NULL
== n
->prev
|| MDOC__A
!= n
->prev
->tok
)
2076 if (NULL
== n
->parent
|| MDOC_Rs
!= n
->parent
->tok
)
2079 if (NULL
== n
->next
) {
2081 p
->flags
|= TERMP_SENTENCE
;
2089 termp_li_pre(DECL_ARGS
)
2092 term_fontpush(p
, TERMFONT_NONE
);
2099 termp_lk_pre(DECL_ARGS
)
2101 const struct mdoc_node
*nn
, *sv
;
2103 term_fontpush(p
, TERMFONT_UNDER
);
2107 if (NULL
== nn
|| NULL
== nn
->next
)
2110 for (nn
= nn
->next
; nn
; nn
= nn
->next
)
2111 term_word(p
, nn
->string
);
2117 term_fontpush(p
, TERMFONT_BOLD
);
2118 term_word(p
, sv
->string
);
2127 termp_bk_pre(DECL_ARGS
)
2136 if (n
->parent
->args
|| 0 == n
->prev
->nchild
)
2137 p
->flags
|= TERMP_PREKEEP
;
2150 termp_bk_post(DECL_ARGS
)
2153 if (MDOC_BODY
== n
->type
)
2154 p
->flags
&= ~(TERMP_KEEP
| TERMP_PREKEEP
);
2159 termp__t_post(DECL_ARGS
)
2163 * If we're in an `Rs' and there's a journal present, then quote
2164 * us instead of underlining us (for disambiguation).
2166 if (n
->parent
&& MDOC_Rs
== n
->parent
->tok
&&
2167 n
->parent
->norm
->Rs
.child_J
)
2168 termp_quote_post(p
, pair
, m
, n
);
2170 termp____post(p
, pair
, m
, n
);
2175 termp__t_pre(DECL_ARGS
)
2179 * If we're in an `Rs' and there's a journal present, then quote
2180 * us instead of underlining us (for disambiguation).
2182 if (n
->parent
&& MDOC_Rs
== n
->parent
->tok
&&
2183 n
->parent
->norm
->Rs
.child_J
)
2184 return(termp_quote_pre(p
, pair
, m
, n
));
2186 term_fontpush(p
, TERMFONT_UNDER
);
2192 termp_under_pre(DECL_ARGS
)
2195 term_fontpush(p
, TERMFONT_UNDER
);