]>
git.cameronkatri.com Git - mandoc.git/blob - mdoc_term.c
1 /* $Id: mdoc_term.c,v 1.161 2010/06/27 17:53:27 schwarze 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>
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 int arg_hasattr(int, const struct mdoc_node
*);
61 static int arg_getattr(int, const struct mdoc_node
*);
62 static void print_bvspace(struct termp
*,
63 const struct mdoc_node
*,
64 const struct mdoc_node
*);
65 static void print_mdoc_node(DECL_ARGS
);
66 static void print_mdoc_nodelist(DECL_ARGS
);
67 static void print_mdoc_head(struct termp
*, const void *);
68 static void print_mdoc_foot(struct termp
*, const void *);
69 static void synopsis_pre(struct termp
*,
70 const struct mdoc_node
*);
72 static void termp____post(DECL_ARGS
);
73 static void termp_an_post(DECL_ARGS
);
74 static void termp_aq_post(DECL_ARGS
);
75 static void termp_bd_post(DECL_ARGS
);
76 static void termp_bk_post(DECL_ARGS
);
77 static void termp_bl_post(DECL_ARGS
);
78 static void termp_bq_post(DECL_ARGS
);
79 static void termp_brq_post(DECL_ARGS
);
80 static void termp_bx_post(DECL_ARGS
);
81 static void termp_d1_post(DECL_ARGS
);
82 static void termp_dq_post(DECL_ARGS
);
83 static int termp_fd_pre(DECL_ARGS
);
84 static void termp_fo_post(DECL_ARGS
);
85 static void termp_in_post(DECL_ARGS
);
86 static void termp_it_post(DECL_ARGS
);
87 static void termp_lb_post(DECL_ARGS
);
88 static void termp_op_post(DECL_ARGS
);
89 static void termp_pf_post(DECL_ARGS
);
90 static void termp_pq_post(DECL_ARGS
);
91 static void termp_qq_post(DECL_ARGS
);
92 static void termp_sh_post(DECL_ARGS
);
93 static void termp_sq_post(DECL_ARGS
);
94 static void termp_ss_post(DECL_ARGS
);
96 static int termp_an_pre(DECL_ARGS
);
97 static int termp_ap_pre(DECL_ARGS
);
98 static int termp_aq_pre(DECL_ARGS
);
99 static int termp_bd_pre(DECL_ARGS
);
100 static int termp_bf_pre(DECL_ARGS
);
101 static int termp_bk_pre(DECL_ARGS
);
102 static int termp_bl_pre(DECL_ARGS
);
103 static int termp_bold_pre(DECL_ARGS
);
104 static int termp_bq_pre(DECL_ARGS
);
105 static int termp_brq_pre(DECL_ARGS
);
106 static int termp_bt_pre(DECL_ARGS
);
107 static int termp_cd_pre(DECL_ARGS
);
108 static int termp_d1_pre(DECL_ARGS
);
109 static int termp_dq_pre(DECL_ARGS
);
110 static int termp_ex_pre(DECL_ARGS
);
111 static int termp_fa_pre(DECL_ARGS
);
112 static int termp_fl_pre(DECL_ARGS
);
113 static int termp_fn_pre(DECL_ARGS
);
114 static int termp_fo_pre(DECL_ARGS
);
115 static int termp_ft_pre(DECL_ARGS
);
116 static int termp_in_pre(DECL_ARGS
);
117 static int termp_it_pre(DECL_ARGS
);
118 static int termp_li_pre(DECL_ARGS
);
119 static int termp_lk_pre(DECL_ARGS
);
120 static int termp_nd_pre(DECL_ARGS
);
121 static int termp_nm_pre(DECL_ARGS
);
122 static int termp_ns_pre(DECL_ARGS
);
123 static int termp_op_pre(DECL_ARGS
);
124 static int termp_pf_pre(DECL_ARGS
);
125 static int termp_pq_pre(DECL_ARGS
);
126 static int termp_qq_pre(DECL_ARGS
);
127 static int termp_rs_pre(DECL_ARGS
);
128 static int termp_rv_pre(DECL_ARGS
);
129 static int termp_sh_pre(DECL_ARGS
);
130 static int termp_sm_pre(DECL_ARGS
);
131 static int termp_sp_pre(DECL_ARGS
);
132 static int termp_sq_pre(DECL_ARGS
);
133 static int termp_ss_pre(DECL_ARGS
);
134 static int termp_under_pre(DECL_ARGS
);
135 static int termp_ud_pre(DECL_ARGS
);
136 static int termp_vt_pre(DECL_ARGS
);
137 static int termp_xr_pre(DECL_ARGS
);
138 static int termp_xx_pre(DECL_ARGS
);
140 static const struct termact termacts
[MDOC_MAX
] = {
141 { termp_ap_pre
, NULL
}, /* Ap */
142 { NULL
, NULL
}, /* Dd */
143 { NULL
, NULL
}, /* Dt */
144 { NULL
, NULL
}, /* Os */
145 { termp_sh_pre
, termp_sh_post
}, /* Sh */
146 { termp_ss_pre
, termp_ss_post
}, /* Ss */
147 { termp_sp_pre
, NULL
}, /* Pp */
148 { termp_d1_pre
, termp_d1_post
}, /* D1 */
149 { termp_d1_pre
, termp_d1_post
}, /* Dl */
150 { termp_bd_pre
, termp_bd_post
}, /* Bd */
151 { NULL
, NULL
}, /* Ed */
152 { termp_bl_pre
, termp_bl_post
}, /* Bl */
153 { NULL
, NULL
}, /* El */
154 { termp_it_pre
, termp_it_post
}, /* It */
155 { NULL
, NULL
}, /* Ad */
156 { termp_an_pre
, termp_an_post
}, /* An */
157 { termp_under_pre
, NULL
}, /* Ar */
158 { termp_cd_pre
, NULL
}, /* Cd */
159 { termp_bold_pre
, NULL
}, /* Cm */
160 { NULL
, NULL
}, /* Dv */
161 { NULL
, NULL
}, /* Er */
162 { NULL
, NULL
}, /* Ev */
163 { termp_ex_pre
, NULL
}, /* Ex */
164 { termp_fa_pre
, NULL
}, /* Fa */
165 { termp_fd_pre
, NULL
}, /* Fd */
166 { termp_fl_pre
, NULL
}, /* Fl */
167 { termp_fn_pre
, NULL
}, /* Fn */
168 { termp_ft_pre
, NULL
}, /* Ft */
169 { termp_bold_pre
, NULL
}, /* Ic */
170 { termp_in_pre
, termp_in_post
}, /* In */
171 { termp_li_pre
, NULL
}, /* Li */
172 { termp_nd_pre
, NULL
}, /* Nd */
173 { termp_nm_pre
, NULL
}, /* Nm */
174 { termp_op_pre
, termp_op_post
}, /* Op */
175 { NULL
, NULL
}, /* Ot */
176 { termp_under_pre
, NULL
}, /* Pa */
177 { termp_rv_pre
, NULL
}, /* Rv */
178 { NULL
, NULL
}, /* St */
179 { termp_under_pre
, NULL
}, /* Va */
180 { termp_vt_pre
, NULL
}, /* Vt */
181 { termp_xr_pre
, NULL
}, /* Xr */
182 { NULL
, termp____post
}, /* %A */
183 { termp_under_pre
, termp____post
}, /* %B */
184 { NULL
, termp____post
}, /* %D */
185 { termp_under_pre
, termp____post
}, /* %I */
186 { termp_under_pre
, termp____post
}, /* %J */
187 { NULL
, termp____post
}, /* %N */
188 { NULL
, termp____post
}, /* %O */
189 { NULL
, termp____post
}, /* %P */
190 { NULL
, termp____post
}, /* %R */
191 { termp_under_pre
, termp____post
}, /* %T */
192 { NULL
, termp____post
}, /* %V */
193 { NULL
, NULL
}, /* Ac */
194 { termp_aq_pre
, termp_aq_post
}, /* Ao */
195 { termp_aq_pre
, termp_aq_post
}, /* Aq */
196 { NULL
, NULL
}, /* At */
197 { NULL
, NULL
}, /* Bc */
198 { termp_bf_pre
, NULL
}, /* Bf */
199 { termp_bq_pre
, termp_bq_post
}, /* Bo */
200 { termp_bq_pre
, termp_bq_post
}, /* Bq */
201 { termp_xx_pre
, NULL
}, /* Bsx */
202 { NULL
, termp_bx_post
}, /* Bx */
203 { NULL
, NULL
}, /* Db */
204 { NULL
, NULL
}, /* Dc */
205 { termp_dq_pre
, termp_dq_post
}, /* Do */
206 { termp_dq_pre
, termp_dq_post
}, /* Dq */
207 { NULL
, NULL
}, /* Ec */ /* FIXME: no space */
208 { NULL
, NULL
}, /* Ef */
209 { termp_under_pre
, NULL
}, /* Em */
210 { NULL
, NULL
}, /* Eo */
211 { termp_xx_pre
, NULL
}, /* Fx */
212 { termp_bold_pre
, NULL
}, /* Ms */ /* FIXME: convert to symbol? */
213 { NULL
, NULL
}, /* No */
214 { termp_ns_pre
, NULL
}, /* Ns */
215 { termp_xx_pre
, NULL
}, /* Nx */
216 { termp_xx_pre
, NULL
}, /* Ox */
217 { NULL
, NULL
}, /* Pc */
218 { termp_pf_pre
, termp_pf_post
}, /* Pf */
219 { termp_pq_pre
, termp_pq_post
}, /* Po */
220 { termp_pq_pre
, termp_pq_post
}, /* Pq */
221 { NULL
, NULL
}, /* Qc */
222 { termp_sq_pre
, termp_sq_post
}, /* Ql */
223 { termp_qq_pre
, termp_qq_post
}, /* Qo */
224 { termp_qq_pre
, termp_qq_post
}, /* Qq */
225 { NULL
, NULL
}, /* Re */
226 { termp_rs_pre
, NULL
}, /* Rs */
227 { NULL
, NULL
}, /* Sc */
228 { termp_sq_pre
, termp_sq_post
}, /* So */
229 { termp_sq_pre
, termp_sq_post
}, /* Sq */
230 { termp_sm_pre
, NULL
}, /* Sm */
231 { termp_under_pre
, NULL
}, /* Sx */
232 { termp_bold_pre
, NULL
}, /* Sy */
233 { NULL
, NULL
}, /* Tn */
234 { termp_xx_pre
, NULL
}, /* Ux */
235 { NULL
, NULL
}, /* Xc */
236 { NULL
, NULL
}, /* Xo */
237 { termp_fo_pre
, termp_fo_post
}, /* Fo */
238 { NULL
, NULL
}, /* Fc */
239 { termp_op_pre
, termp_op_post
}, /* Oo */
240 { NULL
, NULL
}, /* Oc */
241 { termp_bk_pre
, termp_bk_post
}, /* Bk */
242 { NULL
, NULL
}, /* Ek */
243 { termp_bt_pre
, NULL
}, /* Bt */
244 { NULL
, NULL
}, /* Hf */
245 { NULL
, NULL
}, /* Fr */
246 { termp_ud_pre
, NULL
}, /* Ud */
247 { NULL
, termp_lb_post
}, /* Lb */
248 { termp_sp_pre
, NULL
}, /* Lp */
249 { termp_lk_pre
, NULL
}, /* Lk */
250 { termp_under_pre
, NULL
}, /* Mt */
251 { termp_brq_pre
, termp_brq_post
}, /* Brq */
252 { termp_brq_pre
, termp_brq_post
}, /* Bro */
253 { NULL
, NULL
}, /* Brc */
254 { NULL
, termp____post
}, /* %C */
255 { NULL
, NULL
}, /* Es */ /* TODO */
256 { NULL
, NULL
}, /* En */ /* TODO */
257 { termp_xx_pre
, NULL
}, /* Dx */
258 { NULL
, termp____post
}, /* %Q */
259 { termp_sp_pre
, NULL
}, /* br */
260 { termp_sp_pre
, NULL
}, /* sp */
261 { termp_under_pre
, termp____post
}, /* %U */
262 { NULL
, NULL
}, /* Ta */
267 terminal_mdoc(void *arg
, const struct mdoc
*mdoc
)
269 const struct mdoc_node
*n
;
270 const struct mdoc_meta
*m
;
273 p
= (struct termp
*)arg
;
276 p
->maxrmargin
= p
->defrmargin
;
277 p
->tabwidth
= term_len(p
, 5);
279 if (NULL
== p
->symtab
)
281 case (TERMENC_ASCII
):
282 p
->symtab
= chars_init(CHARS_ASCII
);
292 term_begin(p
, print_mdoc_head
, print_mdoc_foot
, m
);
295 print_mdoc_nodelist(p
, NULL
, m
, n
->child
);
302 print_mdoc_nodelist(DECL_ARGS
)
305 print_mdoc_node(p
, pair
, m
, n
);
307 print_mdoc_nodelist(p
, pair
, m
, n
->next
);
313 print_mdoc_node(DECL_ARGS
)
317 struct termpair npair
;
318 size_t offset
, rmargin
;
322 rmargin
= p
->rmargin
;
323 font
= term_fontq(p
);
325 memset(&npair
, 0, sizeof(struct termpair
));
328 if (MDOC_TEXT
!= n
->type
) {
329 if (termacts
[n
->tok
].pre
)
330 chld
= (*termacts
[n
->tok
].pre
)(p
, &npair
, m
, n
);
332 term_word(p
, n
->string
);
334 if (chld
&& n
->child
)
335 print_mdoc_nodelist(p
, &npair
, m
, n
->child
);
337 term_fontpopq(p
, font
);
339 if (MDOC_TEXT
!= n
->type
)
340 if (termacts
[n
->tok
].post
)
341 (*termacts
[n
->tok
].post
)(p
, &npair
, m
, n
);
343 if (MDOC_EOS
& n
->flags
)
344 p
->flags
|= TERMP_SENTENCE
;
347 p
->rmargin
= rmargin
;
352 print_mdoc_foot(struct termp
*p
, const void *arg
)
354 char buf
[DATESIZ
], os
[BUFSIZ
];
355 const struct mdoc_meta
*m
;
357 m
= (const struct mdoc_meta
*)arg
;
359 term_fontrepl(p
, TERMFONT_NONE
);
362 * Output the footer in new-groff style, that is, three columns
363 * with the middle being the manual date and flanking columns
364 * being the operating system:
369 time2a(m
->date
, buf
, DATESIZ
);
370 strlcpy(os
, m
->os
, BUFSIZ
);
375 p
->rmargin
= (p
->maxrmargin
-
376 term_strlen(p
, buf
) + term_len(p
, 1)) / 2;
377 p
->flags
|= TERMP_NOSPACE
| TERMP_NOBREAK
;
382 p
->offset
= p
->rmargin
;
383 p
->rmargin
= p
->maxrmargin
- term_strlen(p
, os
);
384 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
389 p
->offset
= p
->rmargin
;
390 p
->rmargin
= p
->maxrmargin
;
391 p
->flags
&= ~TERMP_NOBREAK
;
392 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
398 p
->rmargin
= p
->maxrmargin
;
404 print_mdoc_head(struct termp
*p
, const void *arg
)
406 char buf
[BUFSIZ
], title
[BUFSIZ
];
407 const struct mdoc_meta
*m
;
409 m
= (const struct mdoc_meta
*)arg
;
411 p
->rmargin
= p
->maxrmargin
;
415 * The header is strange. It has three components, which are
416 * really two with the first duplicated. It goes like this:
418 * IDENTIFIER TITLE IDENTIFIER
420 * The IDENTIFIER is NAME(SECTION), which is the command-name
421 * (if given, or "unknown" if not) followed by the manual page
422 * section. These are given in `Dt'. The TITLE is a free-form
423 * string depending on the manual volume. If not specified, it
424 * switches on the manual section.
428 strlcpy(buf
, m
->vol
, BUFSIZ
);
431 strlcat(buf
, " (", BUFSIZ
);
432 strlcat(buf
, m
->arch
, BUFSIZ
);
433 strlcat(buf
, ")", BUFSIZ
);
436 snprintf(title
, BUFSIZ
, "%s(%s)", m
->title
, m
->msec
);
439 p
->rmargin
= (p
->maxrmargin
-
440 term_strlen(p
, buf
) + term_len(p
, 1)) / 2;
441 p
->flags
|= TERMP_NOBREAK
| TERMP_NOSPACE
;
446 p
->offset
= p
->rmargin
;
447 p
->rmargin
= p
->maxrmargin
- term_strlen(p
, title
);
448 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
453 p
->offset
= p
->rmargin
;
454 p
->rmargin
= p
->maxrmargin
;
455 p
->flags
&= ~TERMP_NOBREAK
;
456 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
462 p
->rmargin
= p
->maxrmargin
;
463 p
->flags
&= ~TERMP_NOSPACE
;
468 a2height(const struct termp
*p
, const char *v
)
473 if ( ! a2roffsu(v
, &su
, SCALE_VS
))
474 SCALE_VS_INIT(&su
, term_len(p
, 1));
476 return(term_vspan(p
, &su
));
481 a2width(const struct termp
*p
, const char *v
)
486 if ( ! a2roffsu(v
, &su
, SCALE_MAX
))
487 SCALE_HS_INIT(&su
, term_strlen(p
, v
));
489 return(term_hspan(p
, &su
));
494 a2offs(const struct termp
*p
, const char *v
)
500 else if (0 == strcmp(v
, "left"))
502 else if (0 == strcmp(v
, "indent"))
503 return(term_len(p
, INDENT
+ 1));
504 else if (0 == strcmp(v
, "indent-two"))
505 return(term_len(p
, (INDENT
+ 1) * 2));
506 else if ( ! a2roffsu(v
, &su
, SCALE_MAX
))
507 SCALE_HS_INIT(&su
, term_strlen(p
, v
));
509 return(term_hspan(p
, &su
));
514 * Return 1 if an argument has a particular argument value or 0 if it
515 * does not. See arg_getattr().
518 arg_hasattr(int arg
, const struct mdoc_node
*n
)
521 return(-1 != arg_getattr(arg
, n
));
526 * Get the index of an argument in a node's argument list or -1 if it
530 arg_getattr(int v
, const struct mdoc_node
*n
)
537 for (i
= 0; i
< (int)n
->args
->argc
; i
++)
538 if (n
->args
->argv
[i
].arg
== v
)
546 * Determine how much space to print out before block elements of `It'
547 * (and thus `Bl') and `Bd'. And then go ahead and print that space,
551 print_bvspace(struct termp
*p
,
552 const struct mdoc_node
*bl
,
553 const struct mdoc_node
*n
)
555 const struct mdoc_node
*nn
;
559 if (MDOC_Bd
== bl
->tok
&& bl
->data
.Bd
.comp
)
561 if (MDOC_Bl
== bl
->tok
&& bl
->data
.Bl
.comp
)
564 /* Do not vspace directly after Ss/Sh. */
566 for (nn
= n
; nn
; nn
= nn
->parent
) {
567 if (MDOC_BLOCK
!= nn
->type
)
569 if (MDOC_Ss
== nn
->tok
)
571 if (MDOC_Sh
== nn
->tok
)
573 if (NULL
== nn
->prev
)
578 /* A `-column' does not assert vspace within the list. */
580 if (MDOC_Bl
== bl
->tok
&& LIST_column
== bl
->data
.Bl
.type
)
581 if (n
->prev
&& MDOC_It
== n
->prev
->tok
)
584 /* A `-diag' without body does not vspace. */
586 if (MDOC_Bl
== bl
->tok
&& LIST_diag
== bl
->data
.Bl
.type
)
587 if (n
->prev
&& MDOC_It
== n
->prev
->tok
) {
588 assert(n
->prev
->body
);
589 if (NULL
== n
->prev
->body
->child
)
599 termp_dq_pre(DECL_ARGS
)
602 if (MDOC_BODY
!= n
->type
)
605 term_word(p
, "\\(lq");
606 p
->flags
|= TERMP_NOSPACE
;
613 termp_dq_post(DECL_ARGS
)
616 if (MDOC_BODY
!= n
->type
)
619 p
->flags
|= TERMP_NOSPACE
;
620 term_word(p
, "\\(rq");
626 termp_it_pre(DECL_ARGS
)
628 const struct mdoc_node
*bl
, *nn
;
631 size_t width
, offset
, ncols
, dcol
;
634 if (MDOC_BLOCK
== n
->type
) {
635 print_bvspace(p
, n
->parent
->parent
, n
);
639 bl
= n
->parent
->parent
->parent
;
640 type
= bl
->data
.Bl
.type
;
643 * First calculate width and offset. This is pretty easy unless
644 * we're a -column list, in which case all prior columns must
650 if (bl
->data
.Bl
.offs
)
651 offset
= a2offs(p
, bl
->data
.Bl
.offs
);
655 if (MDOC_HEAD
== n
->type
)
658 col
= arg_getattr(MDOC_Column
, bl
);
661 * Imitate groff's column handling:
662 * - For each earlier column, add its width.
663 * - For less than 5 columns, add four more blanks per
665 * - For exactly 5 columns, add three more blank per
667 * - For more than 5 columns, add only one column.
669 ncols
= bl
->args
->argv
[col
].sz
;
671 dcol
= ncols
< 5 ? term_len(p
, 4) :
672 ncols
== 5 ? term_len(p
, 3) : term_len(p
, 1);
675 * Calculate the offset by applying all prior MDOC_BODY,
676 * so we stop at the MDOC_HEAD (NULL == nn->prev).
679 for (i
= 0, nn
= n
->prev
;
680 nn
->prev
&& i
< (int)ncols
;
682 offset
+= dcol
+ a2width
683 (p
, bl
->args
->argv
[col
].value
[i
]);
686 * When exceeding the declared number of columns, leave
687 * the remaining widths at 0. This will later be
688 * adjusted to the default width of 10, or, for the last
689 * column, stretched to the right margin.
695 * Use the declared column widths, extended as explained
696 * in the preceding paragraph.
698 width
= a2width(p
, bl
->args
->argv
[col
].value
[i
]) + dcol
;
701 if (NULL
== bl
->data
.Bl
.width
)
705 * Note: buffer the width by 2, which is groff's magic
706 * number for buffering single arguments. See the above
707 * handling for column for how this changes.
709 assert(bl
->data
.Bl
.width
);
710 width
= a2width(p
, bl
->data
.Bl
.width
) + term_len(p
, 2);
715 * List-type can override the width in the case of fixed-head
716 * values (bullet, dash/hyphen, enum). Tags need a non-zero
726 if (width
< term_len(p
, 4))
727 width
= term_len(p
, 4);
730 if (width
< term_len(p
, 5))
731 width
= term_len(p
, 5);
735 width
= term_len(p
, 8);
741 width
= term_len(p
, 10);
748 * Whitespace control. Inset bodies need an initial space,
749 * while diagonal bodies need two.
752 p
->flags
|= TERMP_NOSPACE
;
756 if (MDOC_BODY
== n
->type
)
757 term_word(p
, "\\ \\ ");
760 if (MDOC_BODY
== n
->type
)
767 p
->flags
|= TERMP_NOSPACE
;
771 if (MDOC_HEAD
== n
->type
)
772 term_fontpush(p
, TERMFONT_BOLD
);
779 * Pad and break control. This is the tricky part. These flags
780 * are documented in term_flushln() in term.c. Note that we're
781 * going to unset all of these flags in termp_it_post() when we
793 if (MDOC_HEAD
== n
->type
)
794 p
->flags
|= TERMP_NOBREAK
;
796 p
->flags
|= TERMP_NOLPAD
;
799 if (MDOC_HEAD
== n
->type
)
800 p
->flags
|= TERMP_NOBREAK
;
802 p
->flags
|= TERMP_NOLPAD
;
804 if (MDOC_HEAD
!= n
->type
)
808 * This is ugly. If `-hang' is specified and the body
809 * is a `Bl' or `Bd', then we want basically to nullify
810 * the "overstep" effect in term_flushln() and treat
811 * this as a `-ohang' list instead.
813 if (n
->next
->child
&&
814 (MDOC_Bl
== n
->next
->child
->tok
||
815 MDOC_Bd
== n
->next
->child
->tok
)) {
816 p
->flags
&= ~TERMP_NOBREAK
;
817 p
->flags
&= ~TERMP_NOLPAD
;
819 p
->flags
|= TERMP_HANG
;
822 if (MDOC_HEAD
== n
->type
)
823 p
->flags
|= TERMP_NOBREAK
| TERMP_TWOSPACE
;
825 p
->flags
|= TERMP_NOLPAD
;
827 if (MDOC_HEAD
!= n
->type
)
829 if (NULL
== n
->next
|| NULL
== n
->next
->child
)
830 p
->flags
|= TERMP_DANGLE
;
833 if (MDOC_HEAD
== n
->type
)
837 p
->flags
&= ~TERMP_NOBREAK
;
839 p
->flags
|= TERMP_NOBREAK
;
842 if (MDOC_BODY
== n
->prev
->type
)
843 p
->flags
|= TERMP_NOLPAD
;
847 if (MDOC_HEAD
== n
->type
)
848 p
->flags
|= TERMP_NOBREAK
;
855 * Margin control. Set-head-width lists have their right
856 * margins shortened. The body for these lists has the offset
857 * necessarily lengthened. Everybody gets the offset.
865 * Same stipulation as above, regarding `-hang'. We
866 * don't want to recalculate rmargin and offsets when
867 * using `Bd' or `Bl' within `-hang' overstep lists.
869 if (MDOC_HEAD
== n
->type
&& n
->next
->child
&&
870 (MDOC_Bl
== n
->next
->child
->tok
||
871 MDOC_Bd
== n
->next
->child
->tok
))
884 if (MDOC_HEAD
== n
->type
)
885 p
->rmargin
= p
->offset
+ width
;
891 p
->rmargin
= p
->offset
+ width
;
893 * XXX - this behaviour is not documented: the
894 * right-most column is filled to the right margin.
896 if (MDOC_HEAD
== n
->type
)
898 if (NULL
== n
->next
&& p
->rmargin
< p
->maxrmargin
)
899 p
->rmargin
= p
->maxrmargin
;
906 * The dash, hyphen, bullet and enum lists all have a special
907 * HEAD character (temporarily bold, in some cases).
910 if (MDOC_HEAD
== n
->type
)
913 term_fontpush(p
, TERMFONT_BOLD
);
914 term_word(p
, "\\[bu]");
920 term_fontpush(p
, TERMFONT_BOLD
);
921 term_word(p
, "\\(hy");
925 (pair
->ppair
->ppair
->count
)++;
926 snprintf(buf
, sizeof(buf
), "%d.",
927 pair
->ppair
->ppair
->count
);
935 * If we're not going to process our children, indicate so here.
948 if (MDOC_HEAD
== n
->type
)
952 if (MDOC_HEAD
== n
->type
)
965 termp_it_post(DECL_ARGS
)
969 if (MDOC_BLOCK
== n
->type
)
972 type
= n
->parent
->parent
->parent
->data
.Bl
.type
;
980 if (MDOC_BODY
== n
->type
)
984 if (MDOC_BODY
== n
->type
)
993 * Now that our output is flushed, we can reset our tags. Since
994 * only `It' sets these flags, we're free to assume that nobody
995 * has munged them in the meanwhile.
998 p
->flags
&= ~TERMP_DANGLE
;
999 p
->flags
&= ~TERMP_NOBREAK
;
1000 p
->flags
&= ~TERMP_TWOSPACE
;
1001 p
->flags
&= ~TERMP_NOLPAD
;
1002 p
->flags
&= ~TERMP_HANG
;
1008 termp_nm_pre(DECL_ARGS
)
1011 if (NULL
== n
->child
&& NULL
== m
->name
)
1016 term_fontpush(p
, TERMFONT_BOLD
);
1017 if (NULL
== n
->child
)
1018 term_word(p
, m
->name
);
1025 termp_fl_pre(DECL_ARGS
)
1028 term_fontpush(p
, TERMFONT_BOLD
);
1029 term_word(p
, "\\-");
1032 p
->flags
|= TERMP_NOSPACE
;
1033 else if (n
->next
&& n
->next
->line
== n
->line
)
1034 p
->flags
|= TERMP_NOSPACE
;
1042 termp_an_pre(DECL_ARGS
)
1045 if (NULL
== n
->child
)
1049 * If not in the AUTHORS section, `An -split' will cause
1050 * newlines to occur before the author name. If in the AUTHORS
1051 * section, by default, the first `An' invocation is nosplit,
1052 * then all subsequent ones, regardless of whether interspersed
1053 * with other macros/text, are split. -split, in this case,
1054 * will override the condition of the implied first -nosplit.
1057 if (n
->sec
== SEC_AUTHORS
) {
1058 if ( ! (TERMP_ANPREC
& p
->flags
)) {
1059 if (TERMP_SPLIT
& p
->flags
)
1063 if (TERMP_NOSPLIT
& p
->flags
)
1069 if (TERMP_SPLIT
& p
->flags
)
1078 termp_an_post(DECL_ARGS
)
1082 if (SEC_AUTHORS
== n
->sec
)
1083 p
->flags
|= TERMP_ANPREC
;
1087 if (arg_hasattr(MDOC_Split
, n
)) {
1088 p
->flags
&= ~TERMP_NOSPLIT
;
1089 p
->flags
|= TERMP_SPLIT
;
1091 p
->flags
&= ~TERMP_SPLIT
;
1092 p
->flags
|= TERMP_NOSPLIT
;
1100 termp_ns_pre(DECL_ARGS
)
1103 p
->flags
|= TERMP_NOSPACE
;
1110 termp_rs_pre(DECL_ARGS
)
1113 if (SEC_SEE_ALSO
!= n
->sec
)
1115 if (MDOC_BLOCK
== n
->type
&& n
->prev
)
1123 termp_rv_pre(DECL_ARGS
)
1125 const struct mdoc_node
*nn
;
1128 term_word(p
, "The");
1130 for (nn
= n
->child
; nn
; nn
= nn
->next
) {
1131 term_fontpush(p
, TERMFONT_BOLD
);
1132 term_word(p
, nn
->string
);
1134 p
->flags
|= TERMP_NOSPACE
;
1135 if (nn
->next
&& NULL
== nn
->next
->next
)
1136 term_word(p
, "(), and");
1138 term_word(p
, "(),");
1143 if (n
->child
&& n
->child
->next
)
1144 term_word(p
, "functions return");
1146 term_word(p
, "function returns");
1148 term_word(p
, "the value 0 if successful; otherwise the value "
1149 "-1 is returned and the global variable");
1151 term_fontpush(p
, TERMFONT_UNDER
);
1152 term_word(p
, "errno");
1155 term_word(p
, "is set to indicate the error.");
1156 p
->flags
|= TERMP_SENTENCE
;
1164 termp_ex_pre(DECL_ARGS
)
1166 const struct mdoc_node
*nn
;
1168 term_word(p
, "The");
1170 for (nn
= n
->child
; nn
; nn
= nn
->next
) {
1171 term_fontpush(p
, TERMFONT_BOLD
);
1172 term_word(p
, nn
->string
);
1174 p
->flags
|= TERMP_NOSPACE
;
1175 if (nn
->next
&& NULL
== nn
->next
->next
)
1176 term_word(p
, ", and");
1180 p
->flags
&= ~TERMP_NOSPACE
;
1183 if (n
->child
&& n
->child
->next
)
1184 term_word(p
, "utilities exit");
1186 term_word(p
, "utility exits");
1188 term_word(p
, "0 on success, and >0 if an error occurs.");
1189 p
->flags
|= TERMP_SENTENCE
;
1197 termp_nd_pre(DECL_ARGS
)
1200 if (MDOC_BODY
!= n
->type
)
1203 #if defined(__OpenBSD__) || defined(__linux__)
1204 term_word(p
, "\\(en");
1206 term_word(p
, "\\(em");
1214 termp_bl_pre(DECL_ARGS
)
1217 return(MDOC_HEAD
!= n
->type
);
1223 termp_bl_post(DECL_ARGS
)
1226 if (MDOC_BLOCK
== n
->type
)
1233 termp_op_post(DECL_ARGS
)
1236 if (MDOC_BODY
!= n
->type
)
1238 p
->flags
|= TERMP_NOSPACE
;
1239 term_word(p
, "\\(rB");
1245 termp_xr_pre(DECL_ARGS
)
1247 const struct mdoc_node
*nn
;
1249 if (NULL
== n
->child
)
1252 assert(MDOC_TEXT
== n
->child
->type
);
1255 term_word(p
, nn
->string
);
1256 if (NULL
== (nn
= nn
->next
))
1258 p
->flags
|= TERMP_NOSPACE
;
1260 p
->flags
|= TERMP_NOSPACE
;
1261 term_word(p
, nn
->string
);
1262 p
->flags
|= TERMP_NOSPACE
;
1270 * This decides how to assert whitespace before any of the SYNOPSIS set
1271 * of macros (which, as in the case of Ft/Fo and Ft/Fn, may contain
1275 synopsis_pre(struct termp
*p
, const struct mdoc_node
*n
)
1278 * Obviously, if we're not in a SYNOPSIS or no prior macros
1279 * exist, do nothing.
1281 if (NULL
== n
->prev
|| ! (MDOC_SYNPRETTY
& n
->flags
))
1285 * If we're the second in a pair of like elements, emit our
1286 * newline and return. UNLESS we're `Fo', `Fn', `Fn', in which
1287 * case we soldier on.
1289 if (n
->prev
->tok
== n
->tok
&&
1290 MDOC_Ft
!= n
->tok
&&
1291 MDOC_Fo
!= n
->tok
&&
1292 MDOC_Fn
!= n
->tok
) {
1298 * If we're one of the SYNOPSIS set and non-like pair-wise after
1299 * another (or Fn/Fo, which we've let slip through) then assert
1300 * vertical space, else only newline and move on.
1302 switch (n
->prev
->tok
) {
1315 if (MDOC_Fn
!= n
->tok
&& MDOC_Fo
!= n
->tok
) {
1328 termp_vt_pre(DECL_ARGS
)
1331 if (MDOC_ELEM
== n
->type
) {
1333 return(termp_under_pre(p
, pair
, m
, n
));
1334 } else if (MDOC_BLOCK
== n
->type
) {
1337 } else if (MDOC_HEAD
== n
->type
)
1340 return(termp_under_pre(p
, pair
, m
, n
));
1346 termp_bold_pre(DECL_ARGS
)
1349 term_fontpush(p
, TERMFONT_BOLD
);
1356 termp_fd_pre(DECL_ARGS
)
1360 return(termp_bold_pre(p
, pair
, m
, n
));
1366 termp_sh_pre(DECL_ARGS
)
1369 /* No vspace between consecutive `Sh' calls. */
1373 if (n
->prev
&& MDOC_Sh
== n
->prev
->tok
)
1374 if (NULL
== n
->prev
->body
->child
)
1379 term_fontpush(p
, TERMFONT_BOLD
);
1382 p
->offset
= term_len(p
, INDENT
);
1393 termp_sh_post(DECL_ARGS
)
1412 termp_op_pre(DECL_ARGS
)
1417 term_word(p
, "\\(lB");
1418 p
->flags
|= TERMP_NOSPACE
;
1429 termp_bt_pre(DECL_ARGS
)
1432 term_word(p
, "is currently in beta test.");
1433 p
->flags
|= TERMP_SENTENCE
;
1440 termp_lb_post(DECL_ARGS
)
1443 if (SEC_LIBRARY
== n
->sec
&& MDOC_LINE
& n
->flags
)
1450 termp_ud_pre(DECL_ARGS
)
1453 term_word(p
, "currently under development.");
1454 p
->flags
|= TERMP_SENTENCE
;
1461 termp_d1_pre(DECL_ARGS
)
1464 if (MDOC_BLOCK
!= n
->type
)
1467 p
->offset
+= term_len(p
, (INDENT
+ 1));
1474 termp_d1_post(DECL_ARGS
)
1477 if (MDOC_BLOCK
!= n
->type
)
1485 termp_aq_pre(DECL_ARGS
)
1488 if (MDOC_BODY
!= n
->type
)
1490 term_word(p
, "\\(la");
1491 p
->flags
|= TERMP_NOSPACE
;
1498 termp_aq_post(DECL_ARGS
)
1501 if (MDOC_BODY
!= n
->type
)
1503 p
->flags
|= TERMP_NOSPACE
;
1504 term_word(p
, "\\(ra");
1510 termp_ft_pre(DECL_ARGS
)
1513 /* NB: MDOC_LINE does not effect this! */
1515 term_fontpush(p
, TERMFONT_UNDER
);
1522 termp_fn_pre(DECL_ARGS
)
1524 const struct mdoc_node
*nn
;
1528 term_fontpush(p
, TERMFONT_BOLD
);
1529 term_word(p
, n
->child
->string
);
1532 p
->flags
|= TERMP_NOSPACE
;
1535 for (nn
= n
->child
->next
; nn
; nn
= nn
->next
) {
1536 term_fontpush(p
, TERMFONT_UNDER
);
1537 term_word(p
, nn
->string
);
1546 if (MDOC_SYNPRETTY
& n
->flags
)
1555 termp_fa_pre(DECL_ARGS
)
1557 const struct mdoc_node
*nn
;
1559 if (n
->parent
->tok
!= MDOC_Fo
) {
1560 term_fontpush(p
, TERMFONT_UNDER
);
1564 for (nn
= n
->child
; nn
; nn
= nn
->next
) {
1565 term_fontpush(p
, TERMFONT_UNDER
);
1566 term_word(p
, nn
->string
);
1573 if (n
->child
&& n
->next
&& n
->next
->tok
== MDOC_Fa
)
1582 termp_bd_pre(DECL_ARGS
)
1586 const struct mdoc_node
*nn
;
1588 if (MDOC_BLOCK
== n
->type
) {
1589 print_bvspace(p
, n
, n
);
1591 } else if (MDOC_HEAD
== n
->type
)
1594 if (n
->data
.Bd
.offs
)
1595 p
->offset
+= a2offs(p
, n
->data
.Bd
.offs
);
1598 * If -ragged or -filled are specified, the block does nothing
1599 * but change the indentation. If -unfilled or -literal are
1600 * specified, text is printed exactly as entered in the display:
1601 * for macro lines, a newline is appended to the line. Blank
1602 * lines are allowed.
1605 if (DISP_literal
!= n
->data
.Bd
.type
&&
1606 DISP_unfilled
!= n
->data
.Bd
.type
)
1609 tabwidth
= p
->tabwidth
;
1610 p
->tabwidth
= term_len(p
, 8);
1612 rmax
= p
->maxrmargin
;
1613 p
->rmargin
= p
->maxrmargin
= TERM_MAXMARGIN
;
1615 for (nn
= n
->child
; nn
; nn
= nn
->next
) {
1616 p
->flags
|= TERMP_NOSPACE
;
1617 print_mdoc_node(p
, pair
, m
, nn
);
1618 if (NULL
== nn
->prev
||
1619 nn
->prev
->line
< nn
->line
||
1624 p
->tabwidth
= tabwidth
;
1626 p
->maxrmargin
= rmax
;
1633 termp_bd_post(DECL_ARGS
)
1637 if (MDOC_BODY
!= n
->type
)
1641 rmax
= p
->maxrmargin
;
1643 if (DISP_literal
== n
->data
.Bd
.type
||
1644 DISP_unfilled
== n
->data
.Bd
.type
)
1645 p
->rmargin
= p
->maxrmargin
= TERM_MAXMARGIN
;
1647 p
->flags
|= TERMP_NOSPACE
;
1651 p
->maxrmargin
= rmax
;
1657 termp_qq_pre(DECL_ARGS
)
1660 if (MDOC_BODY
!= n
->type
)
1663 p
->flags
|= TERMP_NOSPACE
;
1670 termp_qq_post(DECL_ARGS
)
1673 if (MDOC_BODY
!= n
->type
)
1675 p
->flags
|= TERMP_NOSPACE
;
1682 termp_bx_post(DECL_ARGS
)
1686 p
->flags
|= TERMP_NOSPACE
;
1687 term_word(p
, "BSD");
1693 termp_xx_pre(DECL_ARGS
)
1729 termp_sq_pre(DECL_ARGS
)
1732 if (MDOC_BODY
!= n
->type
)
1734 term_word(p
, "\\(oq");
1735 p
->flags
|= TERMP_NOSPACE
;
1742 termp_sq_post(DECL_ARGS
)
1745 if (MDOC_BODY
!= n
->type
)
1747 p
->flags
|= TERMP_NOSPACE
;
1748 term_word(p
, "\\(aq");
1754 termp_pf_pre(DECL_ARGS
)
1757 p
->flags
|= TERMP_IGNDELIM
;
1764 termp_pf_post(DECL_ARGS
)
1767 p
->flags
&= ~TERMP_IGNDELIM
;
1768 p
->flags
|= TERMP_NOSPACE
;
1774 termp_ss_pre(DECL_ARGS
)
1784 term_fontpush(p
, TERMFONT_BOLD
);
1785 p
->offset
= term_len(p
, HALFINDENT
);
1797 termp_ss_post(DECL_ARGS
)
1800 if (MDOC_HEAD
== n
->type
)
1807 termp_cd_pre(DECL_ARGS
)
1811 term_fontpush(p
, TERMFONT_BOLD
);
1818 termp_in_pre(DECL_ARGS
)
1823 if (MDOC_SYNPRETTY
& n
->flags
&& MDOC_LINE
& n
->flags
) {
1824 term_fontpush(p
, TERMFONT_BOLD
);
1825 term_word(p
, "#include");
1829 term_fontpush(p
, TERMFONT_UNDER
);
1832 p
->flags
|= TERMP_NOSPACE
;
1839 termp_in_post(DECL_ARGS
)
1842 if (MDOC_SYNPRETTY
& n
->flags
)
1843 term_fontpush(p
, TERMFONT_BOLD
);
1845 p
->flags
|= TERMP_NOSPACE
;
1848 if (MDOC_SYNPRETTY
& n
->flags
)
1855 termp_sp_pre(DECL_ARGS
)
1861 len
= n
->child
? a2height(p
, n
->child
->string
) : 1;
1873 for (i
= 0; i
< len
; i
++)
1882 termp_brq_pre(DECL_ARGS
)
1885 if (MDOC_BODY
!= n
->type
)
1887 term_word(p
, "\\(lC");
1888 p
->flags
|= TERMP_NOSPACE
;
1895 termp_brq_post(DECL_ARGS
)
1898 if (MDOC_BODY
!= n
->type
)
1900 p
->flags
|= TERMP_NOSPACE
;
1901 term_word(p
, "\\(rC");
1907 termp_bq_pre(DECL_ARGS
)
1910 if (MDOC_BODY
!= n
->type
)
1912 term_word(p
, "\\(lB");
1913 p
->flags
|= TERMP_NOSPACE
;
1920 termp_bq_post(DECL_ARGS
)
1923 if (MDOC_BODY
!= n
->type
)
1925 p
->flags
|= TERMP_NOSPACE
;
1926 term_word(p
, "\\(rB");
1932 termp_pq_pre(DECL_ARGS
)
1935 if (MDOC_BODY
!= n
->type
)
1937 term_word(p
, "\\&(");
1938 p
->flags
|= TERMP_NOSPACE
;
1945 termp_pq_post(DECL_ARGS
)
1948 if (MDOC_BODY
!= n
->type
)
1956 termp_fo_pre(DECL_ARGS
)
1959 if (MDOC_BLOCK
== n
->type
) {
1962 } else if (MDOC_BODY
== n
->type
) {
1963 p
->flags
|= TERMP_NOSPACE
;
1965 p
->flags
|= TERMP_NOSPACE
;
1969 /* XXX: we drop non-initial arguments as per groff. */
1972 assert(n
->child
->string
);
1973 term_fontpush(p
, TERMFONT_BOLD
);
1974 term_word(p
, n
->child
->string
);
1981 termp_fo_post(DECL_ARGS
)
1984 if (MDOC_BODY
!= n
->type
)
1987 p
->flags
|= TERMP_NOSPACE
;
1990 if (MDOC_SYNPRETTY
& n
->flags
) {
1991 p
->flags
|= TERMP_NOSPACE
;
1999 termp_bf_pre(DECL_ARGS
)
2001 const struct mdoc_node
*nn
;
2003 if (MDOC_HEAD
== n
->type
)
2005 else if (MDOC_BLOCK
!= n
->type
)
2008 if (NULL
== (nn
= n
->head
->child
)) {
2009 if (arg_hasattr(MDOC_Emphasis
, n
))
2010 term_fontpush(p
, TERMFONT_UNDER
);
2011 else if (arg_hasattr(MDOC_Symbolic
, n
))
2012 term_fontpush(p
, TERMFONT_BOLD
);
2014 term_fontpush(p
, TERMFONT_NONE
);
2019 assert(MDOC_TEXT
== nn
->type
);
2020 if (0 == strcmp("Em", nn
->string
))
2021 term_fontpush(p
, TERMFONT_UNDER
);
2022 else if (0 == strcmp("Sy", nn
->string
))
2023 term_fontpush(p
, TERMFONT_BOLD
);
2025 term_fontpush(p
, TERMFONT_NONE
);
2033 termp_sm_pre(DECL_ARGS
)
2036 assert(n
->child
&& MDOC_TEXT
== n
->child
->type
);
2037 if (0 == strcmp("on", n
->child
->string
))
2038 p
->flags
&= ~TERMP_NONOSPACE
;
2040 p
->flags
|= TERMP_NONOSPACE
;
2048 termp_ap_pre(DECL_ARGS
)
2051 p
->flags
|= TERMP_NOSPACE
;
2052 term_word(p
, "\\(aq");
2053 p
->flags
|= TERMP_NOSPACE
;
2060 termp____post(DECL_ARGS
)
2065 p
->flags
|= TERMP_NOSPACE
;
2066 term_word(p
, n
->next
? "," : ".");
2072 termp_li_pre(DECL_ARGS
)
2075 term_fontpush(p
, TERMFONT_NONE
);
2082 termp_lk_pre(DECL_ARGS
)
2084 const struct mdoc_node
*nn
;
2086 term_fontpush(p
, TERMFONT_UNDER
);
2089 if (NULL
== nn
->next
)
2092 term_word(p
, nn
->string
);
2095 p
->flags
|= TERMP_NOSPACE
;
2098 term_fontpush(p
, TERMFONT_BOLD
);
2099 for (nn
= nn
->next
; nn
; nn
= nn
->next
)
2100 term_word(p
, nn
->string
);
2109 termp_bk_pre(DECL_ARGS
)
2118 p
->flags
|= TERMP_PREKEEP
;
2128 termp_bk_post(DECL_ARGS
)
2131 if (MDOC_BODY
== n
->type
)
2132 p
->flags
&= ~(TERMP_KEEP
| TERMP_PREKEEP
);
2137 termp_under_pre(DECL_ARGS
)
2140 term_fontpush(p
, TERMFONT_UNDER
);