]>
git.cameronkatri.com Git - mandoc.git/blob - mdoc_term.c
1 /* $Id: mdoc_term.c,v 1.157 2010/06/25 18:53:14 kristaps Exp $ */
3 * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@bsd.lv>
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 #include <sys/types.h>
41 struct termpair
*ppair
;
45 #define DECL_ARGS struct termp *p, \
46 struct termpair *pair, \
47 const struct mdoc_meta *m, \
48 const struct mdoc_node *n
51 int (*pre
)(DECL_ARGS
);
52 void (*post
)(DECL_ARGS
);
55 static size_t a2width(const struct termp
*, const char *);
56 static size_t a2height(const struct termp
*, const char *);
57 static size_t a2offs(const struct termp
*, const char *);
59 static int arg_hasattr(int, const struct mdoc_node
*);
60 static int arg_getattr(int, const struct mdoc_node
*);
61 static void print_bvspace(struct termp
*,
62 const struct mdoc_node
*,
63 const struct mdoc_node
*);
64 static void print_mdoc_node(DECL_ARGS
);
65 static void print_mdoc_nodelist(DECL_ARGS
);
66 static void print_mdoc_head(struct termp
*, const void *);
67 static void print_mdoc_foot(struct termp
*, const void *);
68 static void synopsis_pre(struct termp
*,
69 const struct mdoc_node
*);
71 static void termp____post(DECL_ARGS
);
72 static void termp_an_post(DECL_ARGS
);
73 static void termp_aq_post(DECL_ARGS
);
74 static void termp_bd_post(DECL_ARGS
);
75 static void termp_bl_post(DECL_ARGS
);
76 static void termp_bq_post(DECL_ARGS
);
77 static void termp_brq_post(DECL_ARGS
);
78 static void termp_bx_post(DECL_ARGS
);
79 static void termp_d1_post(DECL_ARGS
);
80 static void termp_dq_post(DECL_ARGS
);
81 static int termp_fd_pre(DECL_ARGS
);
82 static void termp_fo_post(DECL_ARGS
);
83 static void termp_in_post(DECL_ARGS
);
84 static void termp_it_post(DECL_ARGS
);
85 static void termp_lb_post(DECL_ARGS
);
86 static void termp_op_post(DECL_ARGS
);
87 static void termp_pf_post(DECL_ARGS
);
88 static void termp_pq_post(DECL_ARGS
);
89 static void termp_qq_post(DECL_ARGS
);
90 static void termp_sh_post(DECL_ARGS
);
91 static void termp_sq_post(DECL_ARGS
);
92 static void termp_ss_post(DECL_ARGS
);
94 static int termp_an_pre(DECL_ARGS
);
95 static int termp_ap_pre(DECL_ARGS
);
96 static int termp_aq_pre(DECL_ARGS
);
97 static int termp_bd_pre(DECL_ARGS
);
98 static int termp_bf_pre(DECL_ARGS
);
99 static int termp_bl_pre(DECL_ARGS
);
100 static int termp_bold_pre(DECL_ARGS
);
101 static int termp_bq_pre(DECL_ARGS
);
102 static int termp_brq_pre(DECL_ARGS
);
103 static int termp_bt_pre(DECL_ARGS
);
104 static int termp_cd_pre(DECL_ARGS
);
105 static int termp_d1_pre(DECL_ARGS
);
106 static int termp_dq_pre(DECL_ARGS
);
107 static int termp_ex_pre(DECL_ARGS
);
108 static int termp_fa_pre(DECL_ARGS
);
109 static int termp_fl_pre(DECL_ARGS
);
110 static int termp_fn_pre(DECL_ARGS
);
111 static int termp_fo_pre(DECL_ARGS
);
112 static int termp_ft_pre(DECL_ARGS
);
113 static int termp_in_pre(DECL_ARGS
);
114 static int termp_it_pre(DECL_ARGS
);
115 static int termp_li_pre(DECL_ARGS
);
116 static int termp_lk_pre(DECL_ARGS
);
117 static int termp_nd_pre(DECL_ARGS
);
118 static int termp_nm_pre(DECL_ARGS
);
119 static int termp_ns_pre(DECL_ARGS
);
120 static int termp_op_pre(DECL_ARGS
);
121 static int termp_pf_pre(DECL_ARGS
);
122 static int termp_pq_pre(DECL_ARGS
);
123 static int termp_qq_pre(DECL_ARGS
);
124 static int termp_rs_pre(DECL_ARGS
);
125 static int termp_rv_pre(DECL_ARGS
);
126 static int termp_sh_pre(DECL_ARGS
);
127 static int termp_sm_pre(DECL_ARGS
);
128 static int termp_sp_pre(DECL_ARGS
);
129 static int termp_sq_pre(DECL_ARGS
);
130 static int termp_ss_pre(DECL_ARGS
);
131 static int termp_under_pre(DECL_ARGS
);
132 static int termp_ud_pre(DECL_ARGS
);
133 static int termp_vt_pre(DECL_ARGS
);
134 static int termp_xr_pre(DECL_ARGS
);
135 static int termp_xx_pre(DECL_ARGS
);
137 static const struct termact termacts
[MDOC_MAX
] = {
138 { termp_ap_pre
, NULL
}, /* Ap */
139 { NULL
, NULL
}, /* Dd */
140 { NULL
, NULL
}, /* Dt */
141 { NULL
, NULL
}, /* Os */
142 { termp_sh_pre
, termp_sh_post
}, /* Sh */
143 { termp_ss_pre
, termp_ss_post
}, /* Ss */
144 { termp_sp_pre
, NULL
}, /* Pp */
145 { termp_d1_pre
, termp_d1_post
}, /* D1 */
146 { termp_d1_pre
, termp_d1_post
}, /* Dl */
147 { termp_bd_pre
, termp_bd_post
}, /* Bd */
148 { NULL
, NULL
}, /* Ed */
149 { termp_bl_pre
, termp_bl_post
}, /* Bl */
150 { NULL
, NULL
}, /* El */
151 { termp_it_pre
, termp_it_post
}, /* It */
152 { NULL
, NULL
}, /* Ad */
153 { termp_an_pre
, termp_an_post
}, /* An */
154 { termp_under_pre
, NULL
}, /* Ar */
155 { termp_cd_pre
, NULL
}, /* Cd */
156 { termp_bold_pre
, NULL
}, /* Cm */
157 { NULL
, NULL
}, /* Dv */
158 { NULL
, NULL
}, /* Er */
159 { NULL
, NULL
}, /* Ev */
160 { termp_ex_pre
, NULL
}, /* Ex */
161 { termp_fa_pre
, NULL
}, /* Fa */
162 { termp_fd_pre
, NULL
}, /* Fd */
163 { termp_fl_pre
, NULL
}, /* Fl */
164 { termp_fn_pre
, NULL
}, /* Fn */
165 { termp_ft_pre
, NULL
}, /* Ft */
166 { termp_bold_pre
, NULL
}, /* Ic */
167 { termp_in_pre
, termp_in_post
}, /* In */
168 { termp_li_pre
, NULL
}, /* Li */
169 { termp_nd_pre
, NULL
}, /* Nd */
170 { termp_nm_pre
, NULL
}, /* Nm */
171 { termp_op_pre
, termp_op_post
}, /* Op */
172 { NULL
, NULL
}, /* Ot */
173 { termp_under_pre
, NULL
}, /* Pa */
174 { termp_rv_pre
, NULL
}, /* Rv */
175 { NULL
, NULL
}, /* St */
176 { termp_under_pre
, NULL
}, /* Va */
177 { termp_vt_pre
, NULL
}, /* Vt */
178 { termp_xr_pre
, NULL
}, /* Xr */
179 { NULL
, termp____post
}, /* %A */
180 { termp_under_pre
, termp____post
}, /* %B */
181 { NULL
, termp____post
}, /* %D */
182 { termp_under_pre
, termp____post
}, /* %I */
183 { termp_under_pre
, termp____post
}, /* %J */
184 { NULL
, termp____post
}, /* %N */
185 { NULL
, termp____post
}, /* %O */
186 { NULL
, termp____post
}, /* %P */
187 { NULL
, termp____post
}, /* %R */
188 { termp_under_pre
, termp____post
}, /* %T */
189 { NULL
, termp____post
}, /* %V */
190 { NULL
, NULL
}, /* Ac */
191 { termp_aq_pre
, termp_aq_post
}, /* Ao */
192 { termp_aq_pre
, termp_aq_post
}, /* Aq */
193 { NULL
, NULL
}, /* At */
194 { NULL
, NULL
}, /* Bc */
195 { termp_bf_pre
, NULL
}, /* Bf */
196 { termp_bq_pre
, termp_bq_post
}, /* Bo */
197 { termp_bq_pre
, termp_bq_post
}, /* Bq */
198 { termp_xx_pre
, NULL
}, /* Bsx */
199 { NULL
, termp_bx_post
}, /* Bx */
200 { NULL
, NULL
}, /* Db */
201 { NULL
, NULL
}, /* Dc */
202 { termp_dq_pre
, termp_dq_post
}, /* Do */
203 { termp_dq_pre
, termp_dq_post
}, /* Dq */
204 { NULL
, NULL
}, /* Ec */ /* FIXME: no space */
205 { NULL
, NULL
}, /* Ef */
206 { termp_under_pre
, NULL
}, /* Em */
207 { NULL
, NULL
}, /* Eo */
208 { termp_xx_pre
, NULL
}, /* Fx */
209 { termp_bold_pre
, NULL
}, /* Ms */ /* FIXME: convert to symbol? */
210 { NULL
, NULL
}, /* No */
211 { termp_ns_pre
, NULL
}, /* Ns */
212 { termp_xx_pre
, NULL
}, /* Nx */
213 { termp_xx_pre
, NULL
}, /* Ox */
214 { NULL
, NULL
}, /* Pc */
215 { termp_pf_pre
, termp_pf_post
}, /* Pf */
216 { termp_pq_pre
, termp_pq_post
}, /* Po */
217 { termp_pq_pre
, termp_pq_post
}, /* Pq */
218 { NULL
, NULL
}, /* Qc */
219 { termp_sq_pre
, termp_sq_post
}, /* Ql */
220 { termp_qq_pre
, termp_qq_post
}, /* Qo */
221 { termp_qq_pre
, termp_qq_post
}, /* Qq */
222 { NULL
, NULL
}, /* Re */
223 { termp_rs_pre
, NULL
}, /* Rs */
224 { NULL
, NULL
}, /* Sc */
225 { termp_sq_pre
, termp_sq_post
}, /* So */
226 { termp_sq_pre
, termp_sq_post
}, /* Sq */
227 { termp_sm_pre
, NULL
}, /* Sm */
228 { termp_under_pre
, NULL
}, /* Sx */
229 { termp_bold_pre
, NULL
}, /* Sy */
230 { NULL
, NULL
}, /* Tn */
231 { termp_xx_pre
, NULL
}, /* Ux */
232 { NULL
, NULL
}, /* Xc */
233 { NULL
, NULL
}, /* Xo */
234 { termp_fo_pre
, termp_fo_post
}, /* Fo */
235 { NULL
, NULL
}, /* Fc */
236 { termp_op_pre
, termp_op_post
}, /* Oo */
237 { NULL
, NULL
}, /* Oc */
238 { NULL
, NULL
}, /* Bk */
239 { NULL
, NULL
}, /* Ek */
240 { termp_bt_pre
, NULL
}, /* Bt */
241 { NULL
, NULL
}, /* Hf */
242 { NULL
, NULL
}, /* Fr */
243 { termp_ud_pre
, NULL
}, /* Ud */
244 { NULL
, termp_lb_post
}, /* Lb */
245 { termp_sp_pre
, NULL
}, /* Lp */
246 { termp_lk_pre
, NULL
}, /* Lk */
247 { termp_under_pre
, NULL
}, /* Mt */
248 { termp_brq_pre
, termp_brq_post
}, /* Brq */
249 { termp_brq_pre
, termp_brq_post
}, /* Bro */
250 { NULL
, NULL
}, /* Brc */
251 { NULL
, termp____post
}, /* %C */
252 { NULL
, NULL
}, /* Es */ /* TODO */
253 { NULL
, NULL
}, /* En */ /* TODO */
254 { termp_xx_pre
, NULL
}, /* Dx */
255 { NULL
, termp____post
}, /* %Q */
256 { termp_sp_pre
, NULL
}, /* br */
257 { termp_sp_pre
, NULL
}, /* sp */
258 { termp_under_pre
, termp____post
}, /* %U */
259 { NULL
, NULL
}, /* Ta */
264 terminal_mdoc(void *arg
, const struct mdoc
*mdoc
)
266 const struct mdoc_node
*n
;
267 const struct mdoc_meta
*m
;
270 p
= (struct termp
*)arg
;
273 p
->maxrmargin
= p
->defrmargin
;
274 p
->tabwidth
= term_len(p
, 5);
276 if (NULL
== p
->symtab
)
278 case (TERMENC_ASCII
):
279 p
->symtab
= chars_init(CHARS_ASCII
);
289 term_begin(p
, print_mdoc_head
, print_mdoc_foot
, m
);
292 print_mdoc_nodelist(p
, NULL
, m
, n
->child
);
299 print_mdoc_nodelist(DECL_ARGS
)
302 print_mdoc_node(p
, pair
, m
, n
);
304 print_mdoc_nodelist(p
, pair
, m
, n
->next
);
310 print_mdoc_node(DECL_ARGS
)
314 struct termpair npair
;
315 size_t offset
, rmargin
;
319 rmargin
= p
->rmargin
;
320 font
= term_fontq(p
);
322 memset(&npair
, 0, sizeof(struct termpair
));
325 if (MDOC_TEXT
!= n
->type
) {
326 if (termacts
[n
->tok
].pre
)
327 chld
= (*termacts
[n
->tok
].pre
)(p
, &npair
, m
, n
);
329 term_word(p
, n
->string
);
331 if (chld
&& n
->child
)
332 print_mdoc_nodelist(p
, &npair
, m
, n
->child
);
334 term_fontpopq(p
, font
);
336 if (MDOC_TEXT
!= n
->type
)
337 if (termacts
[n
->tok
].post
)
338 (*termacts
[n
->tok
].post
)(p
, &npair
, m
, n
);
340 if (MDOC_EOS
& n
->flags
)
341 p
->flags
|= TERMP_SENTENCE
;
344 p
->rmargin
= rmargin
;
349 print_mdoc_foot(struct termp
*p
, const void *arg
)
351 char buf
[DATESIZ
], os
[BUFSIZ
];
352 const struct mdoc_meta
*m
;
354 m
= (const struct mdoc_meta
*)arg
;
356 term_fontrepl(p
, TERMFONT_NONE
);
359 * Output the footer in new-groff style, that is, three columns
360 * with the middle being the manual date and flanking columns
361 * being the operating system:
366 time2a(m
->date
, buf
, DATESIZ
);
367 strlcpy(os
, m
->os
, BUFSIZ
);
372 p
->rmargin
= (p
->maxrmargin
-
373 term_strlen(p
, buf
) + term_len(p
, 1)) / 2;
374 p
->flags
|= TERMP_NOSPACE
| TERMP_NOBREAK
;
379 p
->offset
= p
->rmargin
;
380 p
->rmargin
= p
->maxrmargin
- term_strlen(p
, os
);
381 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
386 p
->offset
= p
->rmargin
;
387 p
->rmargin
= p
->maxrmargin
;
388 p
->flags
&= ~TERMP_NOBREAK
;
389 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
395 p
->rmargin
= p
->maxrmargin
;
401 print_mdoc_head(struct termp
*p
, const void *arg
)
403 char buf
[BUFSIZ
], title
[BUFSIZ
];
404 const struct mdoc_meta
*m
;
406 m
= (const struct mdoc_meta
*)arg
;
408 p
->rmargin
= p
->maxrmargin
;
412 * The header is strange. It has three components, which are
413 * really two with the first duplicated. It goes like this:
415 * IDENTIFIER TITLE IDENTIFIER
417 * The IDENTIFIER is NAME(SECTION), which is the command-name
418 * (if given, or "unknown" if not) followed by the manual page
419 * section. These are given in `Dt'. The TITLE is a free-form
420 * string depending on the manual volume. If not specified, it
421 * switches on the manual section.
425 strlcpy(buf
, m
->vol
, BUFSIZ
);
428 strlcat(buf
, " (", BUFSIZ
);
429 strlcat(buf
, m
->arch
, BUFSIZ
);
430 strlcat(buf
, ")", BUFSIZ
);
433 snprintf(title
, BUFSIZ
, "%s(%s)", m
->title
, m
->msec
);
436 p
->rmargin
= (p
->maxrmargin
-
437 term_strlen(p
, buf
) + term_len(p
, 1)) / 2;
438 p
->flags
|= TERMP_NOBREAK
| TERMP_NOSPACE
;
443 p
->offset
= p
->rmargin
;
444 p
->rmargin
= p
->maxrmargin
- term_strlen(p
, title
);
445 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
450 p
->offset
= p
->rmargin
;
451 p
->rmargin
= p
->maxrmargin
;
452 p
->flags
&= ~TERMP_NOBREAK
;
453 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
459 p
->rmargin
= p
->maxrmargin
;
460 p
->flags
&= ~TERMP_NOSPACE
;
465 a2height(const struct termp
*p
, const char *v
)
470 if ( ! a2roffsu(v
, &su
, SCALE_VS
))
471 SCALE_VS_INIT(&su
, term_len(p
, 1));
473 return(term_vspan(p
, &su
));
478 a2width(const struct termp
*p
, const char *v
)
483 if ( ! a2roffsu(v
, &su
, SCALE_MAX
))
484 SCALE_HS_INIT(&su
, term_strlen(p
, v
));
486 return(term_hspan(p
, &su
));
491 a2offs(const struct termp
*p
, const char *v
)
497 else if (0 == strcmp(v
, "left"))
499 else if (0 == strcmp(v
, "indent"))
500 return(term_len(p
, INDENT
+ 1));
501 else if (0 == strcmp(v
, "indent-two"))
502 return(term_len(p
, (INDENT
+ 1) * 2));
503 else if ( ! a2roffsu(v
, &su
, SCALE_MAX
))
504 SCALE_HS_INIT(&su
, term_strlen(p
, v
));
506 return(term_hspan(p
, &su
));
511 * Return 1 if an argument has a particular argument value or 0 if it
512 * does not. See arg_getattr().
515 arg_hasattr(int arg
, const struct mdoc_node
*n
)
518 return(-1 != arg_getattr(arg
, n
));
523 * Get the index of an argument in a node's argument list or -1 if it
527 arg_getattr(int v
, const struct mdoc_node
*n
)
534 for (i
= 0; i
< (int)n
->args
->argc
; i
++)
535 if (n
->args
->argv
[i
].arg
== v
)
543 * Determine how much space to print out before block elements of `It'
544 * (and thus `Bl') and `Bd'. And then go ahead and print that space,
548 print_bvspace(struct termp
*p
,
549 const struct mdoc_node
*bl
,
550 const struct mdoc_node
*n
)
552 const struct mdoc_node
*nn
;
556 if (MDOC_Bd
== bl
->tok
&& bl
->data
.Bd
.comp
)
558 if (MDOC_Bl
== bl
->tok
&& bl
->data
.Bl
.comp
)
561 /* Do not vspace directly after Ss/Sh. */
563 for (nn
= n
; nn
; nn
= nn
->parent
) {
564 if (MDOC_BLOCK
!= nn
->type
)
566 if (MDOC_Ss
== nn
->tok
)
568 if (MDOC_Sh
== nn
->tok
)
570 if (NULL
== nn
->prev
)
575 /* A `-column' does not assert vspace within the list. */
577 if (MDOC_Bl
== bl
->tok
&& LIST_column
== bl
->data
.Bl
.type
)
578 if (n
->prev
&& MDOC_It
== n
->prev
->tok
)
581 /* A `-diag' without body does not vspace. */
583 if (MDOC_Bl
== bl
->tok
&& LIST_diag
== bl
->data
.Bl
.type
)
584 if (n
->prev
&& MDOC_It
== n
->prev
->tok
) {
585 assert(n
->prev
->body
);
586 if (NULL
== n
->prev
->body
->child
)
596 termp_dq_pre(DECL_ARGS
)
599 if (MDOC_BODY
!= n
->type
)
602 term_word(p
, "\\(lq");
603 p
->flags
|= TERMP_NOSPACE
;
610 termp_dq_post(DECL_ARGS
)
613 if (MDOC_BODY
!= n
->type
)
616 p
->flags
|= TERMP_NOSPACE
;
617 term_word(p
, "\\(rq");
623 termp_it_pre(DECL_ARGS
)
625 const struct mdoc_node
*bl
, *nn
;
628 size_t width
, offset
, ncols
, dcol
;
631 if (MDOC_BLOCK
== n
->type
) {
632 print_bvspace(p
, n
->parent
->parent
, n
);
636 bl
= n
->parent
->parent
->parent
;
637 type
= bl
->data
.Bl
.type
;
640 * First calculate width and offset. This is pretty easy unless
641 * we're a -column list, in which case all prior columns must
647 if (bl
->data
.Bl
.offs
)
648 offset
= a2offs(p
, bl
->data
.Bl
.offs
);
652 if (MDOC_HEAD
== n
->type
)
655 col
= arg_getattr(MDOC_Column
, bl
);
658 * Imitate groff's column handling:
659 * - For each earlier column, add its width.
660 * - For less than 5 columns, add four more blanks per
662 * - For exactly 5 columns, add three more blank per
664 * - For more than 5 columns, add only one column.
666 ncols
= bl
->args
->argv
[col
].sz
;
668 dcol
= ncols
< 5 ? term_len(p
, 4) :
669 ncols
== 5 ? term_len(p
, 3) : term_len(p
, 1);
672 * Calculate the offset by applying all prior MDOC_BODY,
673 * so we stop at the MDOC_HEAD (NULL == nn->prev).
676 for (i
= 0, nn
= n
->prev
;
677 nn
->prev
&& i
< (int)ncols
;
679 offset
+= dcol
+ a2width
680 (p
, bl
->args
->argv
[col
].value
[i
]);
683 * When exceeding the declared number of columns, leave
684 * the remaining widths at 0. This will later be
685 * adjusted to the default width of 10, or, for the last
686 * column, stretched to the right margin.
692 * Use the declared column widths, extended as explained
693 * in the preceding paragraph.
695 width
= a2width(p
, bl
->args
->argv
[col
].value
[i
]) + dcol
;
698 if (NULL
== bl
->data
.Bl
.width
)
702 * Note: buffer the width by 2, which is groff's magic
703 * number for buffering single arguments. See the above
704 * handling for column for how this changes.
706 assert(bl
->data
.Bl
.width
);
707 width
= a2width(p
, bl
->data
.Bl
.width
) + term_len(p
, 2);
712 * List-type can override the width in the case of fixed-head
713 * values (bullet, dash/hyphen, enum). Tags need a non-zero
723 if (width
< term_len(p
, 4))
724 width
= term_len(p
, 4);
727 if (width
< term_len(p
, 5))
728 width
= term_len(p
, 5);
732 width
= term_len(p
, 8);
738 width
= term_len(p
, 10);
745 * Whitespace control. Inset bodies need an initial space,
746 * while diagonal bodies need two.
749 p
->flags
|= TERMP_NOSPACE
;
753 if (MDOC_BODY
== n
->type
)
754 term_word(p
, "\\ \\ ");
757 if (MDOC_BODY
== n
->type
)
764 p
->flags
|= TERMP_NOSPACE
;
768 if (MDOC_HEAD
== n
->type
)
769 term_fontpush(p
, TERMFONT_BOLD
);
776 * Pad and break control. This is the tricky part. These flags
777 * are documented in term_flushln() in term.c. Note that we're
778 * going to unset all of these flags in termp_it_post() when we
790 if (MDOC_HEAD
== n
->type
)
791 p
->flags
|= TERMP_NOBREAK
;
793 p
->flags
|= TERMP_NOLPAD
;
796 if (MDOC_HEAD
== n
->type
)
797 p
->flags
|= TERMP_NOBREAK
;
799 p
->flags
|= TERMP_NOLPAD
;
801 if (MDOC_HEAD
!= n
->type
)
805 * This is ugly. If `-hang' is specified and the body
806 * is a `Bl' or `Bd', then we want basically to nullify
807 * the "overstep" effect in term_flushln() and treat
808 * this as a `-ohang' list instead.
810 if (n
->next
->child
&&
811 (MDOC_Bl
== n
->next
->child
->tok
||
812 MDOC_Bd
== n
->next
->child
->tok
)) {
813 p
->flags
&= ~TERMP_NOBREAK
;
814 p
->flags
&= ~TERMP_NOLPAD
;
816 p
->flags
|= TERMP_HANG
;
819 if (MDOC_HEAD
== n
->type
)
820 p
->flags
|= TERMP_NOBREAK
| TERMP_TWOSPACE
;
822 p
->flags
|= TERMP_NOLPAD
;
824 if (MDOC_HEAD
!= n
->type
)
826 if (NULL
== n
->next
|| NULL
== n
->next
->child
)
827 p
->flags
|= TERMP_DANGLE
;
830 if (MDOC_HEAD
== n
->type
)
834 p
->flags
&= ~TERMP_NOBREAK
;
836 p
->flags
|= TERMP_NOBREAK
;
839 if (MDOC_BODY
== n
->prev
->type
)
840 p
->flags
|= TERMP_NOLPAD
;
844 if (MDOC_HEAD
== n
->type
)
845 p
->flags
|= TERMP_NOBREAK
;
852 * Margin control. Set-head-width lists have their right
853 * margins shortened. The body for these lists has the offset
854 * necessarily lengthened. Everybody gets the offset.
862 * Same stipulation as above, regarding `-hang'. We
863 * don't want to recalculate rmargin and offsets when
864 * using `Bd' or `Bl' within `-hang' overstep lists.
866 if (MDOC_HEAD
== n
->type
&& n
->next
->child
&&
867 (MDOC_Bl
== n
->next
->child
->tok
||
868 MDOC_Bd
== n
->next
->child
->tok
))
881 if (MDOC_HEAD
== n
->type
)
882 p
->rmargin
= p
->offset
+ width
;
888 p
->rmargin
= p
->offset
+ width
;
890 * XXX - this behaviour is not documented: the
891 * right-most column is filled to the right margin.
893 if (MDOC_HEAD
== n
->type
)
895 if (NULL
== n
->next
&& p
->rmargin
< p
->maxrmargin
)
896 p
->rmargin
= p
->maxrmargin
;
903 * The dash, hyphen, bullet and enum lists all have a special
904 * HEAD character (temporarily bold, in some cases).
907 if (MDOC_HEAD
== n
->type
)
910 term_fontpush(p
, TERMFONT_BOLD
);
911 term_word(p
, "\\[bu]");
917 term_fontpush(p
, TERMFONT_BOLD
);
918 term_word(p
, "\\(hy");
922 (pair
->ppair
->ppair
->count
)++;
923 snprintf(buf
, sizeof(buf
), "%d.",
924 pair
->ppair
->ppair
->count
);
932 * If we're not going to process our children, indicate so here.
945 if (MDOC_HEAD
== n
->type
)
949 if (MDOC_HEAD
== n
->type
)
962 termp_it_post(DECL_ARGS
)
966 if (MDOC_BLOCK
== n
->type
)
969 type
= n
->parent
->parent
->parent
->data
.Bl
.type
;
977 if (MDOC_BODY
== n
->type
)
981 if (MDOC_BODY
== n
->type
)
990 * Now that our output is flushed, we can reset our tags. Since
991 * only `It' sets these flags, we're free to assume that nobody
992 * has munged them in the meanwhile.
995 p
->flags
&= ~TERMP_DANGLE
;
996 p
->flags
&= ~TERMP_NOBREAK
;
997 p
->flags
&= ~TERMP_TWOSPACE
;
998 p
->flags
&= ~TERMP_NOLPAD
;
999 p
->flags
&= ~TERMP_HANG
;
1005 termp_nm_pre(DECL_ARGS
)
1008 if (NULL
== n
->child
&& NULL
== m
->name
)
1013 term_fontpush(p
, TERMFONT_BOLD
);
1014 if (NULL
== n
->child
)
1015 term_word(p
, m
->name
);
1022 termp_fl_pre(DECL_ARGS
)
1025 term_fontpush(p
, TERMFONT_BOLD
);
1026 term_word(p
, "\\-");
1029 p
->flags
|= TERMP_NOSPACE
;
1030 else if (n
->next
&& n
->next
->line
== n
->line
)
1031 p
->flags
|= TERMP_NOSPACE
;
1039 termp_an_pre(DECL_ARGS
)
1042 if (NULL
== n
->child
)
1046 * If not in the AUTHORS section, `An -split' will cause
1047 * newlines to occur before the author name. If in the AUTHORS
1048 * section, by default, the first `An' invocation is nosplit,
1049 * then all subsequent ones, regardless of whether interspersed
1050 * with other macros/text, are split. -split, in this case,
1051 * will override the condition of the implied first -nosplit.
1054 if (n
->sec
== SEC_AUTHORS
) {
1055 if ( ! (TERMP_ANPREC
& p
->flags
)) {
1056 if (TERMP_SPLIT
& p
->flags
)
1060 if (TERMP_NOSPLIT
& p
->flags
)
1066 if (TERMP_SPLIT
& p
->flags
)
1075 termp_an_post(DECL_ARGS
)
1079 if (SEC_AUTHORS
== n
->sec
)
1080 p
->flags
|= TERMP_ANPREC
;
1084 if (arg_hasattr(MDOC_Split
, n
)) {
1085 p
->flags
&= ~TERMP_NOSPLIT
;
1086 p
->flags
|= TERMP_SPLIT
;
1088 p
->flags
&= ~TERMP_SPLIT
;
1089 p
->flags
|= TERMP_NOSPLIT
;
1097 termp_ns_pre(DECL_ARGS
)
1100 p
->flags
|= TERMP_NOSPACE
;
1107 termp_rs_pre(DECL_ARGS
)
1110 if (SEC_SEE_ALSO
!= n
->sec
)
1112 if (MDOC_BLOCK
== n
->type
&& n
->prev
)
1120 termp_rv_pre(DECL_ARGS
)
1122 const struct mdoc_node
*nn
;
1125 term_word(p
, "The");
1127 for (nn
= n
->child
; nn
; nn
= nn
->next
) {
1128 term_fontpush(p
, TERMFONT_BOLD
);
1129 term_word(p
, nn
->string
);
1131 p
->flags
|= TERMP_NOSPACE
;
1132 if (nn
->next
&& NULL
== nn
->next
->next
)
1133 term_word(p
, "(), and");
1135 term_word(p
, "(),");
1140 if (n
->child
&& n
->child
->next
)
1141 term_word(p
, "functions return");
1143 term_word(p
, "function returns");
1145 term_word(p
, "the value 0 if successful; otherwise the value "
1146 "-1 is returned and the global variable");
1148 term_fontpush(p
, TERMFONT_UNDER
);
1149 term_word(p
, "errno");
1152 term_word(p
, "is set to indicate the error.");
1153 p
->flags
|= TERMP_SENTENCE
;
1161 termp_ex_pre(DECL_ARGS
)
1163 const struct mdoc_node
*nn
;
1165 term_word(p
, "The");
1167 for (nn
= n
->child
; nn
; nn
= nn
->next
) {
1168 term_fontpush(p
, TERMFONT_BOLD
);
1169 term_word(p
, nn
->string
);
1171 p
->flags
|= TERMP_NOSPACE
;
1172 if (nn
->next
&& NULL
== nn
->next
->next
)
1173 term_word(p
, ", and");
1177 p
->flags
&= ~TERMP_NOSPACE
;
1180 if (n
->child
&& n
->child
->next
)
1181 term_word(p
, "utilities exit");
1183 term_word(p
, "utility exits");
1185 term_word(p
, "0 on success, and >0 if an error occurs.");
1186 p
->flags
|= TERMP_SENTENCE
;
1194 termp_nd_pre(DECL_ARGS
)
1197 if (MDOC_BODY
!= n
->type
)
1200 #if defined(__OpenBSD__) || defined(__linux__)
1201 term_word(p
, "\\(en");
1203 term_word(p
, "\\(em");
1211 termp_bl_pre(DECL_ARGS
)
1214 return(MDOC_HEAD
!= n
->type
);
1220 termp_bl_post(DECL_ARGS
)
1223 if (MDOC_BLOCK
== n
->type
)
1230 termp_op_post(DECL_ARGS
)
1233 if (MDOC_BODY
!= n
->type
)
1235 p
->flags
|= TERMP_NOSPACE
;
1236 term_word(p
, "\\(rB");
1242 termp_xr_pre(DECL_ARGS
)
1244 const struct mdoc_node
*nn
;
1246 if (NULL
== n
->child
)
1249 assert(MDOC_TEXT
== n
->child
->type
);
1252 term_word(p
, nn
->string
);
1253 if (NULL
== (nn
= nn
->next
))
1255 p
->flags
|= TERMP_NOSPACE
;
1257 p
->flags
|= TERMP_NOSPACE
;
1258 term_word(p
, nn
->string
);
1259 p
->flags
|= TERMP_NOSPACE
;
1267 * This decides how to assert whitespace before any of the SYNOPSIS set
1268 * of macros (which, as in the case of Ft/Fo and Ft/Fn, may contain
1272 synopsis_pre(struct termp
*p
, const struct mdoc_node
*n
)
1275 * Obviously, if we're not in a SYNOPSIS or no prior macros
1276 * exist, do nothing.
1278 if (NULL
== n
->prev
|| SEC_SYNOPSIS
!= n
->sec
)
1282 * If we're the second in a pair of like elements, emit our
1283 * newline and return. UNLESS we're `Fo', `Fn', `Fn', in which
1284 * case we soldier on.
1286 if (n
->prev
->tok
== n
->tok
&&
1287 MDOC_Ft
!= n
->tok
&&
1288 MDOC_Fo
!= n
->tok
&&
1289 MDOC_Fn
!= n
->tok
) {
1295 * If we're one of the SYNOPSIS set and non-like pair-wise after
1296 * another (or Fn/Fo, which we've let slip through) then assert
1297 * vertical space, else only newline and move on.
1299 switch (n
->prev
->tok
) {
1312 if (MDOC_Fn
!= n
->tok
&& MDOC_Fo
!= n
->tok
) {
1325 termp_vt_pre(DECL_ARGS
)
1328 if (MDOC_ELEM
== n
->type
) {
1330 return(termp_under_pre(p
, pair
, m
, n
));
1331 } else if (MDOC_BLOCK
== n
->type
) {
1334 } else if (MDOC_HEAD
== n
->type
)
1337 return(termp_under_pre(p
, pair
, m
, n
));
1343 termp_bold_pre(DECL_ARGS
)
1346 term_fontpush(p
, TERMFONT_BOLD
);
1353 termp_fd_pre(DECL_ARGS
)
1357 return(termp_bold_pre(p
, pair
, m
, n
));
1363 termp_sh_pre(DECL_ARGS
)
1366 /* No vspace between consecutive `Sh' calls. */
1370 if (n
->prev
&& MDOC_Sh
== n
->prev
->tok
)
1371 if (NULL
== n
->prev
->body
->child
)
1376 term_fontpush(p
, TERMFONT_BOLD
);
1379 p
->offset
= term_len(p
, INDENT
);
1390 termp_sh_post(DECL_ARGS
)
1409 termp_op_pre(DECL_ARGS
)
1414 term_word(p
, "\\(lB");
1415 p
->flags
|= TERMP_NOSPACE
;
1426 termp_bt_pre(DECL_ARGS
)
1429 term_word(p
, "is currently in beta test.");
1430 p
->flags
|= TERMP_SENTENCE
;
1437 termp_lb_post(DECL_ARGS
)
1440 if (SEC_LIBRARY
== n
->sec
&& MDOC_LINE
& n
->flags
)
1447 termp_ud_pre(DECL_ARGS
)
1450 term_word(p
, "currently under development.");
1451 p
->flags
|= TERMP_SENTENCE
;
1458 termp_d1_pre(DECL_ARGS
)
1461 if (MDOC_BLOCK
!= n
->type
)
1464 p
->offset
+= term_len(p
, (INDENT
+ 1));
1471 termp_d1_post(DECL_ARGS
)
1474 if (MDOC_BLOCK
!= n
->type
)
1482 termp_aq_pre(DECL_ARGS
)
1485 if (MDOC_BODY
!= n
->type
)
1487 term_word(p
, "\\(la");
1488 p
->flags
|= TERMP_NOSPACE
;
1495 termp_aq_post(DECL_ARGS
)
1498 if (MDOC_BODY
!= n
->type
)
1500 p
->flags
|= TERMP_NOSPACE
;
1501 term_word(p
, "\\(ra");
1507 termp_ft_pre(DECL_ARGS
)
1510 /* NB: MDOC_LINE does not effect this! */
1512 term_fontpush(p
, TERMFONT_UNDER
);
1519 termp_fn_pre(DECL_ARGS
)
1521 const struct mdoc_node
*nn
;
1525 term_fontpush(p
, TERMFONT_BOLD
);
1526 term_word(p
, n
->child
->string
);
1529 p
->flags
|= TERMP_NOSPACE
;
1532 for (nn
= n
->child
->next
; nn
; nn
= nn
->next
) {
1533 term_fontpush(p
, TERMFONT_UNDER
);
1534 term_word(p
, nn
->string
);
1543 if (SEC_SYNOPSIS
== n
->sec
)
1552 termp_fa_pre(DECL_ARGS
)
1554 const struct mdoc_node
*nn
;
1556 if (n
->parent
->tok
!= MDOC_Fo
) {
1557 term_fontpush(p
, TERMFONT_UNDER
);
1561 for (nn
= n
->child
; nn
; nn
= nn
->next
) {
1562 term_fontpush(p
, TERMFONT_UNDER
);
1563 term_word(p
, nn
->string
);
1570 if (n
->child
&& n
->next
&& n
->next
->tok
== MDOC_Fa
)
1579 termp_bd_pre(DECL_ARGS
)
1583 const struct mdoc_node
*nn
;
1585 if (MDOC_BLOCK
== n
->type
) {
1586 print_bvspace(p
, n
, n
);
1588 } else if (MDOC_HEAD
== n
->type
)
1591 if (n
->data
.Bd
.offs
)
1592 p
->offset
+= a2offs(p
, n
->data
.Bd
.offs
);
1595 * If -ragged or -filled are specified, the block does nothing
1596 * but change the indentation. If -unfilled or -literal are
1597 * specified, text is printed exactly as entered in the display:
1598 * for macro lines, a newline is appended to the line. Blank
1599 * lines are allowed.
1602 if (DISP_literal
!= n
->data
.Bd
.type
&&
1603 DISP_unfilled
!= n
->data
.Bd
.type
)
1606 tabwidth
= p
->tabwidth
;
1607 p
->tabwidth
= term_len(p
, 8);
1609 rmax
= p
->maxrmargin
;
1610 p
->rmargin
= p
->maxrmargin
= TERM_MAXMARGIN
;
1612 for (nn
= n
->child
; nn
; nn
= nn
->next
) {
1613 p
->flags
|= TERMP_NOSPACE
;
1614 print_mdoc_node(p
, pair
, m
, nn
);
1615 if (NULL
== nn
->prev
||
1616 nn
->prev
->line
< nn
->line
||
1621 p
->tabwidth
= tabwidth
;
1623 p
->maxrmargin
= rmax
;
1630 termp_bd_post(DECL_ARGS
)
1634 if (MDOC_BODY
!= n
->type
)
1638 rmax
= p
->maxrmargin
;
1640 if (DISP_literal
== n
->data
.Bd
.type
||
1641 DISP_unfilled
== n
->data
.Bd
.type
)
1642 p
->rmargin
= p
->maxrmargin
= TERM_MAXMARGIN
;
1644 p
->flags
|= TERMP_NOSPACE
;
1648 p
->maxrmargin
= rmax
;
1654 termp_qq_pre(DECL_ARGS
)
1657 if (MDOC_BODY
!= n
->type
)
1660 p
->flags
|= TERMP_NOSPACE
;
1667 termp_qq_post(DECL_ARGS
)
1670 if (MDOC_BODY
!= n
->type
)
1672 p
->flags
|= TERMP_NOSPACE
;
1679 termp_bx_post(DECL_ARGS
)
1683 p
->flags
|= TERMP_NOSPACE
;
1684 term_word(p
, "BSD");
1690 termp_xx_pre(DECL_ARGS
)
1726 termp_sq_pre(DECL_ARGS
)
1729 if (MDOC_BODY
!= n
->type
)
1731 term_word(p
, "\\(oq");
1732 p
->flags
|= TERMP_NOSPACE
;
1739 termp_sq_post(DECL_ARGS
)
1742 if (MDOC_BODY
!= n
->type
)
1744 p
->flags
|= TERMP_NOSPACE
;
1745 term_word(p
, "\\(aq");
1751 termp_pf_pre(DECL_ARGS
)
1754 p
->flags
|= TERMP_IGNDELIM
;
1761 termp_pf_post(DECL_ARGS
)
1764 p
->flags
&= ~TERMP_IGNDELIM
;
1765 p
->flags
|= TERMP_NOSPACE
;
1771 termp_ss_pre(DECL_ARGS
)
1781 term_fontpush(p
, TERMFONT_BOLD
);
1782 p
->offset
= term_len(p
, HALFINDENT
);
1794 termp_ss_post(DECL_ARGS
)
1797 if (MDOC_HEAD
== n
->type
)
1804 termp_cd_pre(DECL_ARGS
)
1808 term_fontpush(p
, TERMFONT_BOLD
);
1815 termp_in_pre(DECL_ARGS
)
1820 if (SEC_SYNOPSIS
== n
->sec
&& MDOC_LINE
& n
->flags
) {
1821 term_fontpush(p
, TERMFONT_BOLD
);
1822 term_word(p
, "#include");
1826 term_fontpush(p
, TERMFONT_UNDER
);
1829 p
->flags
|= TERMP_NOSPACE
;
1836 termp_in_post(DECL_ARGS
)
1839 if (SEC_SYNOPSIS
== n
->sec
)
1840 term_fontpush(p
, TERMFONT_BOLD
);
1842 p
->flags
|= TERMP_NOSPACE
;
1845 if (SEC_SYNOPSIS
== n
->sec
)
1852 termp_sp_pre(DECL_ARGS
)
1858 len
= n
->child
? a2height(p
, n
->child
->string
) : 1;
1870 for (i
= 0; i
< len
; i
++)
1879 termp_brq_pre(DECL_ARGS
)
1882 if (MDOC_BODY
!= n
->type
)
1884 term_word(p
, "\\(lC");
1885 p
->flags
|= TERMP_NOSPACE
;
1892 termp_brq_post(DECL_ARGS
)
1895 if (MDOC_BODY
!= n
->type
)
1897 p
->flags
|= TERMP_NOSPACE
;
1898 term_word(p
, "\\(rC");
1904 termp_bq_pre(DECL_ARGS
)
1907 if (MDOC_BODY
!= n
->type
)
1909 term_word(p
, "\\(lB");
1910 p
->flags
|= TERMP_NOSPACE
;
1917 termp_bq_post(DECL_ARGS
)
1920 if (MDOC_BODY
!= n
->type
)
1922 p
->flags
|= TERMP_NOSPACE
;
1923 term_word(p
, "\\(rB");
1929 termp_pq_pre(DECL_ARGS
)
1932 if (MDOC_BODY
!= n
->type
)
1934 term_word(p
, "\\&(");
1935 p
->flags
|= TERMP_NOSPACE
;
1942 termp_pq_post(DECL_ARGS
)
1945 if (MDOC_BODY
!= n
->type
)
1953 termp_fo_pre(DECL_ARGS
)
1956 if (MDOC_BLOCK
== n
->type
) {
1959 } else if (MDOC_BODY
== n
->type
) {
1960 p
->flags
|= TERMP_NOSPACE
;
1962 p
->flags
|= TERMP_NOSPACE
;
1966 /* 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
)
1984 p
->flags
|= TERMP_NOSPACE
;
1987 if (SEC_SYNOPSIS
== n
->sec
) {
1988 p
->flags
|= TERMP_NOSPACE
;
1996 termp_bf_pre(DECL_ARGS
)
1998 const struct mdoc_node
*nn
;
2000 if (MDOC_HEAD
== n
->type
)
2002 else if (MDOC_BLOCK
!= n
->type
)
2005 if (NULL
== (nn
= n
->head
->child
)) {
2006 if (arg_hasattr(MDOC_Emphasis
, n
))
2007 term_fontpush(p
, TERMFONT_UNDER
);
2008 else if (arg_hasattr(MDOC_Symbolic
, n
))
2009 term_fontpush(p
, TERMFONT_BOLD
);
2011 term_fontpush(p
, TERMFONT_NONE
);
2016 assert(MDOC_TEXT
== nn
->type
);
2017 if (0 == strcmp("Em", nn
->string
))
2018 term_fontpush(p
, TERMFONT_UNDER
);
2019 else if (0 == strcmp("Sy", nn
->string
))
2020 term_fontpush(p
, TERMFONT_BOLD
);
2022 term_fontpush(p
, TERMFONT_NONE
);
2030 termp_sm_pre(DECL_ARGS
)
2033 assert(n
->child
&& MDOC_TEXT
== n
->child
->type
);
2034 if (0 == strcmp("on", n
->child
->string
))
2035 p
->flags
&= ~TERMP_NONOSPACE
;
2037 p
->flags
|= TERMP_NONOSPACE
;
2045 termp_ap_pre(DECL_ARGS
)
2048 p
->flags
|= TERMP_NOSPACE
;
2049 term_word(p
, "\\(aq");
2050 p
->flags
|= TERMP_NOSPACE
;
2057 termp____post(DECL_ARGS
)
2062 p
->flags
|= TERMP_NOSPACE
;
2063 term_word(p
, n
->next
? "," : ".");
2069 termp_li_pre(DECL_ARGS
)
2072 term_fontpush(p
, TERMFONT_NONE
);
2079 termp_lk_pre(DECL_ARGS
)
2081 const struct mdoc_node
*nn
;
2083 term_fontpush(p
, TERMFONT_UNDER
);
2086 if (NULL
== nn
->next
)
2089 term_word(p
, nn
->string
);
2092 p
->flags
|= TERMP_NOSPACE
;
2095 term_fontpush(p
, TERMFONT_BOLD
);
2096 for (nn
= nn
->next
; nn
; nn
= nn
->next
)
2097 term_word(p
, nn
->string
);
2106 termp_under_pre(DECL_ARGS
)
2109 term_fontpush(p
, TERMFONT_UNDER
);