1 /* $Vendor-Id: mdoc_term.c,v 1.102 2009/11/12 05:50:12 kristaps Exp $ */
3 * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@kth.se>
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.
17 #include <sys/types.h>
35 struct termpair
*ppair
;
40 #define DECL_ARGS struct termp *p, \
41 struct termpair *pair, \
42 const struct mdoc_meta *m, \
43 const struct mdoc_node *n
46 int (*pre
)(DECL_ARGS
);
47 void (*post
)(DECL_ARGS
);
50 static size_t a2width(const struct mdoc_argv
*, int);
51 static size_t a2height(const struct mdoc_node
*);
52 static size_t a2offs(const struct mdoc_argv
*);
54 static int arg_hasattr(int, const struct mdoc_node
*);
55 static int arg_getattrs(const int *, int *, size_t,
56 const struct mdoc_node
*);
57 static int arg_getattr(int, const struct mdoc_node
*);
58 static int arg_listtype(const struct mdoc_node
*);
59 static void print_bvspace(struct termp
*,
60 const struct mdoc_node
*,
61 const struct mdoc_node
*);
62 static void print_mdoc_node(DECL_ARGS
);
63 static void print_mdoc_head(DECL_ARGS
);
64 static void print_mdoc_nodelist(DECL_ARGS
);
65 static void print_foot(DECL_ARGS
);
68 extern size_t strlcpy(char *, const char *, size_t);
69 extern size_t strlcat(char *, const char *, size_t);
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_bl_post(DECL_ARGS
);
77 static void termp_bq_post(DECL_ARGS
);
78 static void termp_brq_post(DECL_ARGS
);
79 static void termp_bx_post(DECL_ARGS
);
80 static void termp_d1_post(DECL_ARGS
);
81 static void termp_dq_post(DECL_ARGS
);
82 static void termp_fd_post(DECL_ARGS
);
83 static void termp_fn_post(DECL_ARGS
);
84 static void termp_fo_post(DECL_ARGS
);
85 static void termp_ft_post(DECL_ARGS
);
86 static void termp_in_post(DECL_ARGS
);
87 static void termp_it_post(DECL_ARGS
);
88 static void termp_lb_post(DECL_ARGS
);
89 static void termp_op_post(DECL_ARGS
);
90 static void termp_pf_post(DECL_ARGS
);
91 static void termp_pq_post(DECL_ARGS
);
92 static void termp_qq_post(DECL_ARGS
);
93 static void termp_sh_post(DECL_ARGS
);
94 static void termp_sq_post(DECL_ARGS
);
95 static void termp_ss_post(DECL_ARGS
);
96 static void termp_vt_post(DECL_ARGS
);
98 static int termp__t_pre(DECL_ARGS
);
99 static int termp_an_pre(DECL_ARGS
);
100 static int termp_ap_pre(DECL_ARGS
);
101 static int termp_aq_pre(DECL_ARGS
);
102 static int termp_bd_pre(DECL_ARGS
);
103 static int termp_bf_pre(DECL_ARGS
);
104 static int termp_bold_pre(DECL_ARGS
);
105 static int termp_bq_pre(DECL_ARGS
);
106 static int termp_brq_pre(DECL_ARGS
);
107 static int termp_bt_pre(DECL_ARGS
);
108 static int termp_cd_pre(DECL_ARGS
);
109 static int termp_d1_pre(DECL_ARGS
);
110 static int termp_dq_pre(DECL_ARGS
);
111 static int termp_ex_pre(DECL_ARGS
);
112 static int termp_fa_pre(DECL_ARGS
);
113 static int termp_fl_pre(DECL_ARGS
);
114 static int termp_fn_pre(DECL_ARGS
);
115 static int termp_fo_pre(DECL_ARGS
);
116 static int termp_ft_pre(DECL_ARGS
);
117 static int termp_in_pre(DECL_ARGS
);
118 static int termp_it_pre(DECL_ARGS
);
119 static int termp_li_pre(DECL_ARGS
);
120 static int termp_lk_pre(DECL_ARGS
);
121 static int termp_nd_pre(DECL_ARGS
);
122 static int termp_nm_pre(DECL_ARGS
);
123 static int termp_ns_pre(DECL_ARGS
);
124 static int termp_op_pre(DECL_ARGS
);
125 static int termp_pf_pre(DECL_ARGS
);
126 static int termp_pq_pre(DECL_ARGS
);
127 static int termp_qq_pre(DECL_ARGS
);
128 static int termp_rs_pre(DECL_ARGS
);
129 static int termp_rv_pre(DECL_ARGS
);
130 static int termp_sh_pre(DECL_ARGS
);
131 static int termp_sm_pre(DECL_ARGS
);
132 static int termp_sp_pre(DECL_ARGS
);
133 static int termp_sq_pre(DECL_ARGS
);
134 static int termp_ss_pre(DECL_ARGS
);
135 static int termp_under_pre(DECL_ARGS
);
136 static int termp_ud_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 { NULL
, 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_bold_pre
, termp_fd_post
}, /* Fd */
166 { termp_fl_pre
, NULL
}, /* Fl */
167 { termp_fn_pre
, termp_fn_post
}, /* Fn */
168 { termp_ft_pre
, termp_ft_post
}, /* 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_under_pre
, termp_vt_post
}, /* 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__t_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 */
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 { NULL
, NULL
}, /* 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 */
266 terminal_mdoc(void *arg
, const struct mdoc
*mdoc
)
268 const struct mdoc_node
*n
;
269 const struct mdoc_meta
*m
;
272 p
= (struct termp
*)arg
;
274 if (NULL
== p
->symtab
)
276 case (TERMENC_ASCII
):
277 p
->symtab
= chars_init(CHARS_ASCII
);
287 print_mdoc_head(p
, NULL
, m
, n
);
289 print_mdoc_nodelist(p
, NULL
, m
, n
->child
);
290 print_foot(p
, NULL
, m
, n
);
295 print_mdoc_nodelist(DECL_ARGS
)
298 print_mdoc_node(p
, pair
, m
, n
);
300 print_mdoc_nodelist(p
, pair
, m
, n
->next
);
306 print_mdoc_node(DECL_ARGS
)
310 struct termpair npair
;
311 size_t offset
, rmargin
;
315 rmargin
= p
->rmargin
;
316 font
= term_fontq(p
);
318 memset(&npair
, 0, sizeof(struct termpair
));
321 if (MDOC_TEXT
!= n
->type
) {
322 if (termacts
[n
->tok
].pre
)
323 chld
= (*termacts
[n
->tok
].pre
)(p
, &npair
, m
, n
);
325 term_word(p
, n
->string
);
327 if (chld
&& n
->child
)
328 print_mdoc_nodelist(p
, &npair
, m
, n
->child
);
330 term_fontpopq(p
, font
);
332 if (MDOC_TEXT
!= n
->type
)
333 if (termacts
[n
->tok
].post
)
334 (*termacts
[n
->tok
].post
)(p
, &npair
, m
, n
);
337 p
->rmargin
= rmargin
;
343 print_foot(DECL_ARGS
)
345 char buf
[DATESIZ
], os
[BUFSIZ
];
347 term_fontrepl(p
, TERMFONT_NONE
);
350 * Output the footer in new-groff style, that is, three columns
351 * with the middle being the manual date and flanking columns
352 * being the operating system:
357 time2a(m
->date
, buf
, DATESIZ
);
358 strlcpy(os
, m
->os
, BUFSIZ
);
363 p
->rmargin
= (p
->maxrmargin
- strlen(buf
) + 1) / 2;
364 p
->flags
|= TERMP_NOSPACE
| TERMP_NOBREAK
;
369 p
->offset
= p
->rmargin
;
370 p
->rmargin
= p
->maxrmargin
- strlen(os
);
371 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
376 p
->offset
= p
->rmargin
;
377 p
->rmargin
= p
->maxrmargin
;
378 p
->flags
&= ~TERMP_NOBREAK
;
379 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
385 p
->rmargin
= p
->maxrmargin
;
392 print_mdoc_head(DECL_ARGS
)
394 char buf
[BUFSIZ
], title
[BUFSIZ
];
396 p
->rmargin
= p
->maxrmargin
;
400 * The header is strange. It has three components, which are
401 * really two with the first duplicated. It goes like this:
403 * IDENTIFIER TITLE IDENTIFIER
405 * The IDENTIFIER is NAME(SECTION), which is the command-name
406 * (if given, or "unknown" if not) followed by the manual page
407 * section. These are given in `Dt'. The TITLE is a free-form
408 * string depending on the manual volume. If not specified, it
409 * switches on the manual section.
413 strlcpy(buf
, m
->vol
, BUFSIZ
);
416 strlcat(buf
, " (", BUFSIZ
);
417 strlcat(buf
, m
->arch
, BUFSIZ
);
418 strlcat(buf
, ")", BUFSIZ
);
421 snprintf(title
, BUFSIZ
, "%s(%d)", m
->title
, m
->msec
);
424 p
->rmargin
= (p
->maxrmargin
- strlen(buf
) + 1) / 2;
425 p
->flags
|= TERMP_NOBREAK
| TERMP_NOSPACE
;
430 p
->offset
= p
->rmargin
;
431 p
->rmargin
= p
->maxrmargin
- strlen(title
);
432 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
437 p
->offset
= p
->rmargin
;
438 p
->rmargin
= p
->maxrmargin
;
439 p
->flags
&= ~TERMP_NOBREAK
;
440 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
446 p
->rmargin
= p
->maxrmargin
;
447 p
->flags
&= ~TERMP_NOSPACE
;
452 a2height(const struct mdoc_node
*n
)
456 assert(MDOC_TEXT
== n
->type
);
458 if ( ! a2roffsu(n
->string
, &su
, SCALE_VS
))
459 SCALE_VS_INIT(&su
, strlen(n
->string
));
461 return(term_vspan(&su
));
466 a2width(const struct mdoc_argv
*arg
, int pos
)
470 assert(arg
->value
[pos
]);
471 if ( ! a2roffsu(arg
->value
[pos
], &su
, SCALE_MAX
))
472 SCALE_HS_INIT(&su
, strlen(arg
->value
[pos
]));
475 return(term_hspan(&su
) + 2);
480 arg_listtype(const struct mdoc_node
*n
)
484 assert(MDOC_BLOCK
== n
->type
);
486 len
= (int)(n
->args
? n
->args
->argc
: 0);
488 for (i
= 0; i
< len
; i
++)
489 switch (n
->args
->argv
[i
].arg
) {
511 return(n
->args
->argv
[i
].arg
);
521 a2offs(const struct mdoc_argv
*arg
)
525 if ('\0' == arg
->value
[0][0])
527 else if (0 == strcmp(arg
->value
[0], "left"))
529 else if (0 == strcmp(arg
->value
[0], "indent"))
531 else if (0 == strcmp(arg
->value
[0], "indent-two"))
532 return((INDENT
+ 1) * 2);
533 else if ( ! a2roffsu(arg
->value
[0], &su
, SCALE_MAX
))
534 SCALE_HS_INIT(&su
, strlen(arg
->value
[0]));
536 return(term_hspan(&su
));
541 arg_hasattr(int arg
, const struct mdoc_node
*n
)
544 return(-1 != arg_getattr(arg
, n
));
549 arg_getattr(int v
, const struct mdoc_node
*n
)
553 return(arg_getattrs(&v
, &val
, 1, n
) ? val
: -1);
558 arg_getattrs(const int *keys
, int *vals
,
559 size_t sz
, const struct mdoc_node
*n
)
566 for (k
= i
= 0; i
< (int)n
->args
->argc
; i
++)
567 for (j
= 0; j
< (int)sz
; j
++)
568 if (n
->args
->argv
[i
].arg
== keys
[j
]) {
577 print_bvspace(struct termp
*p
,
578 const struct mdoc_node
*bl
,
579 const struct mdoc_node
*n
)
581 const struct mdoc_node
*nn
;
584 if (arg_hasattr(MDOC_Compact
, bl
))
587 /* Do not vspace directly after Ss/Sh. */
589 for (nn
= n
; nn
; nn
= nn
->parent
) {
590 if (MDOC_BLOCK
!= nn
->type
)
592 if (MDOC_Ss
== nn
->tok
)
594 if (MDOC_Sh
== nn
->tok
)
596 if (NULL
== nn
->prev
)
601 /* A `-column' does not assert vspace within the list. */
603 if (MDOC_Bl
== bl
->tok
&& arg_hasattr(MDOC_Column
, bl
))
604 if (n
->prev
&& MDOC_It
== n
->prev
->tok
)
607 /* A `-diag' without body does not vspace. */
609 if (MDOC_Bl
== bl
->tok
&& arg_hasattr(MDOC_Diag
, bl
))
610 if (n
->prev
&& MDOC_It
== n
->prev
->tok
) {
611 assert(n
->prev
->body
);
612 if (NULL
== n
->prev
->body
->child
)
622 termp_dq_pre(DECL_ARGS
)
625 if (MDOC_BODY
!= n
->type
)
628 term_word(p
, "\\(lq");
629 p
->flags
|= TERMP_NOSPACE
;
636 termp_dq_post(DECL_ARGS
)
639 if (MDOC_BODY
!= n
->type
)
642 p
->flags
|= TERMP_NOSPACE
;
643 term_word(p
, "\\(rq");
649 termp_it_pre(DECL_ARGS
)
651 const struct mdoc_node
*bl
, *nn
;
653 int i
, type
, keys
[3], vals
[3];
654 size_t width
, offset
;
656 if (MDOC_BLOCK
== n
->type
) {
657 print_bvspace(p
, n
->parent
->parent
, n
);
661 bl
= n
->parent
->parent
->parent
;
663 /* Save parent attributes. */
665 pair
->flag
= p
->flags
;
667 /* Get list width and offset. */
669 keys
[0] = MDOC_Width
;
670 keys
[1] = MDOC_Offset
;
671 keys
[2] = MDOC_Column
;
673 vals
[0] = vals
[1] = vals
[2] = -1;
677 (void)arg_getattrs(keys
, vals
, 3, bl
);
679 type
= arg_listtype(bl
);
682 /* Calculate real width and offset. */
686 if (MDOC_BODY
== n
->type
)
689 * Work around groff's column handling. The offset is
690 * equal to the sum of all widths leading to the current
691 * column (plus the -offset value). If this column
692 * exceeds the stated number of columns, the width is
693 * set as 0, else it's the stated column width (later
694 * the 0 will be adjusted to default 10 or, if in the
695 * last column case, set to stretch to the margin).
697 for (i
= 0, nn
= n
->prev
; nn
&&
698 i
< (int)bl
->args
->argv
[vals
[2]].sz
;
701 (&bl
->args
->argv
[vals
[2]], i
);
703 /* Whether exceeds maximum column. */
704 if (i
< (int)bl
->args
->argv
[vals
[2]].sz
)
705 width
= a2width(&bl
->args
->argv
[vals
[2]], i
);
710 offset
+= a2offs(&bl
->args
->argv
[vals
[1]]);
714 width
= a2width(&bl
->args
->argv
[vals
[0]], 0);
716 offset
+= a2offs(&bl
->args
->argv
[vals
[1]]);
721 * List-type can override the width in the case of fixed-head
722 * values (bullet, dash/hyphen, enum). Tags need a non-zero
754 * Whitespace control. Inset bodies need an initial space,
755 * while diagonal bodies need two.
758 p
->flags
|= TERMP_NOSPACE
;
762 if (MDOC_BODY
== n
->type
)
763 term_word(p
, "\\ \\ ");
766 if (MDOC_BODY
== n
->type
)
773 p
->flags
|= TERMP_NOSPACE
;
777 if (MDOC_HEAD
== n
->type
)
778 term_fontpush(p
, TERMFONT_BOLD
);
785 * Pad and break control. This is the tricker part. Lists with
786 * set right-margins for the head get TERMP_NOBREAK because, if
787 * they overrun the margin, they wrap to the new margin.
788 * Correspondingly, the body for these types don't left-pad, as
789 * the head will pad out to to the right.
800 if (MDOC_HEAD
== n
->type
)
801 p
->flags
|= TERMP_NOBREAK
;
803 p
->flags
|= TERMP_NOLPAD
;
806 if (MDOC_HEAD
== n
->type
)
807 p
->flags
|= TERMP_NOBREAK
;
809 p
->flags
|= TERMP_NOLPAD
;
811 if (MDOC_HEAD
!= n
->type
)
815 * This is ugly. If `-hang' is specified and the body
816 * is a `Bl' or `Bd', then we want basically to nullify
817 * the "overstep" effect in term_flushln() and treat
818 * this as a `-ohang' list instead.
820 if (n
->next
->child
&&
821 (MDOC_Bl
== n
->next
->child
->tok
||
822 MDOC_Bd
== n
->next
->child
->tok
)) {
823 p
->flags
&= ~TERMP_NOBREAK
;
824 p
->flags
&= ~TERMP_NOLPAD
;
826 p
->flags
|= TERMP_HANG
;
829 if (MDOC_HEAD
== n
->type
)
830 p
->flags
|= TERMP_NOBREAK
| TERMP_TWOSPACE
;
832 p
->flags
|= TERMP_NOLPAD
;
834 if (MDOC_HEAD
!= n
->type
)
836 if (NULL
== n
->next
|| NULL
== n
->next
->child
)
837 p
->flags
|= TERMP_DANGLE
;
840 if (MDOC_HEAD
== n
->type
) {
842 if (MDOC_BODY
== n
->next
->type
)
843 p
->flags
&= ~TERMP_NOBREAK
;
845 p
->flags
|= TERMP_NOBREAK
;
847 p
->flags
|= TERMP_NOLPAD
;
851 if (MDOC_HEAD
== n
->type
)
852 p
->flags
|= TERMP_NOBREAK
;
859 * Margin control. Set-head-width lists have their right
860 * margins shortened. The body for these lists has the offset
861 * necessarily lengthened. Everybody gets the offset.
869 * Same stipulation as above, regarding `-hang'. We
870 * don't want to recalculate rmargin and offsets when
871 * using `Bd' or `Bl' within `-hang' overstep lists.
873 if (MDOC_HEAD
== n
->type
&& n
->next
->child
&&
874 (MDOC_Bl
== n
->next
->child
->tok
||
875 MDOC_Bd
== n
->next
->child
->tok
))
888 if (MDOC_HEAD
== n
->type
)
889 p
->rmargin
= p
->offset
+ width
;
895 p
->rmargin
= p
->offset
+ width
;
897 * XXX - this behaviour is not documented: the
898 * right-most column is filled to the right margin.
900 if (MDOC_HEAD
== n
->type
&&
901 MDOC_BODY
== n
->next
->type
)
902 p
->rmargin
= p
->maxrmargin
;
909 * The dash, hyphen, bullet and enum lists all have a special
910 * HEAD character (temporarily bold, in some cases).
913 if (MDOC_HEAD
== n
->type
)
916 term_fontpush(p
, TERMFONT_BOLD
);
917 term_word(p
, "\\[bu]");
923 term_fontpush(p
, TERMFONT_BOLD
);
924 term_word(p
, "\\(hy");
928 (pair
->ppair
->ppair
->count
)++;
929 (void)snprintf(buf
, sizeof(buf
), "%d.",
930 pair
->ppair
->ppair
->count
);
938 * If we're not going to process our children, indicate so here.
951 if (MDOC_HEAD
== n
->type
)
955 if (MDOC_BODY
== n
->type
)
968 termp_it_post(DECL_ARGS
)
972 if (MDOC_BODY
!= n
->type
&& MDOC_HEAD
!= n
->type
)
975 type
= arg_listtype(n
->parent
->parent
->parent
);
984 if (MDOC_BODY
== n
->type
)
988 if (MDOC_HEAD
== n
->type
)
996 p
->flags
= pair
->flag
;
1002 termp_nm_pre(DECL_ARGS
)
1005 if (SEC_SYNOPSIS
== n
->sec
)
1008 term_fontpush(p
, TERMFONT_BOLD
);
1010 if (NULL
== n
->child
)
1011 term_word(p
, m
->name
);
1018 termp_fl_pre(DECL_ARGS
)
1021 term_fontpush(p
, TERMFONT_BOLD
);
1022 term_word(p
, "\\-");
1023 p
->flags
|= TERMP_NOSPACE
;
1030 termp_an_pre(DECL_ARGS
)
1033 if (NULL
== n
->child
)
1037 * If not in the AUTHORS section, `An -split' will cause
1038 * newlines to occur before the author name. If in the AUTHORS
1039 * section, by default, the first `An' invocation is nosplit,
1040 * then all subsequent ones, regardless of whether interspersed
1041 * with other macros/text, are split. -split, in this case,
1042 * will override the condition of the implied first -nosplit.
1045 if (n
->sec
== SEC_AUTHORS
) {
1046 if ( ! (TERMP_ANPREC
& p
->flags
)) {
1047 if (TERMP_SPLIT
& p
->flags
)
1051 if (TERMP_NOSPLIT
& p
->flags
)
1057 if (TERMP_SPLIT
& p
->flags
)
1066 termp_an_post(DECL_ARGS
)
1070 if (SEC_AUTHORS
== n
->sec
)
1071 p
->flags
|= TERMP_ANPREC
;
1075 if (arg_getattr(MDOC_Split
, n
) > -1) {
1076 p
->flags
&= ~TERMP_NOSPLIT
;
1077 p
->flags
|= TERMP_SPLIT
;
1079 p
->flags
&= ~TERMP_SPLIT
;
1080 p
->flags
|= TERMP_NOSPLIT
;
1088 termp_ns_pre(DECL_ARGS
)
1091 p
->flags
|= TERMP_NOSPACE
;
1098 termp_rs_pre(DECL_ARGS
)
1101 if (SEC_SEE_ALSO
!= n
->sec
)
1103 if (MDOC_BLOCK
== n
->type
&& n
->prev
)
1111 termp_rv_pre(DECL_ARGS
)
1113 const struct mdoc_node
*nn
;
1116 term_word(p
, "The");
1118 for (nn
= n
->child
; nn
; nn
= nn
->next
) {
1119 term_fontpush(p
, TERMFONT_BOLD
);
1120 term_word(p
, nn
->string
);
1122 p
->flags
|= TERMP_NOSPACE
;
1123 if (nn
->next
&& NULL
== nn
->next
->next
)
1124 term_word(p
, "(), and");
1126 term_word(p
, "(),");
1132 term_word(p
, "functions return");
1134 term_word(p
, "function returns");
1136 term_word(p
, "the value 0 if successful; otherwise the value "
1137 "-1 is returned and the global variable");
1139 term_fontpush(p
, TERMFONT_UNDER
);
1140 term_word(p
, "errno");
1143 term_word(p
, "is set to indicate the error.");
1151 termp_ex_pre(DECL_ARGS
)
1153 const struct mdoc_node
*nn
;
1155 term_word(p
, "The");
1157 for (nn
= n
->child
; nn
; nn
= nn
->next
) {
1158 term_fontpush(p
, TERMFONT_BOLD
);
1159 term_word(p
, nn
->string
);
1161 p
->flags
|= TERMP_NOSPACE
;
1162 if (nn
->next
&& NULL
== nn
->next
->next
)
1163 term_word(p
, ", and");
1167 p
->flags
&= ~TERMP_NOSPACE
;
1171 term_word(p
, "utilities exit");
1173 term_word(p
, "utility exits");
1175 term_word(p
, "0 on success, and >0 if an error occurs.");
1183 termp_nd_pre(DECL_ARGS
)
1186 if (MDOC_BODY
!= n
->type
)
1189 #if defined(__OpenBSD__) || defined(__linux__)
1190 term_word(p
, "\\(en");
1192 term_word(p
, "\\(em");
1200 termp_bl_post(DECL_ARGS
)
1203 if (MDOC_BLOCK
== n
->type
)
1210 termp_op_post(DECL_ARGS
)
1213 if (MDOC_BODY
!= n
->type
)
1215 p
->flags
|= TERMP_NOSPACE
;
1216 term_word(p
, "\\(rB");
1222 termp_xr_pre(DECL_ARGS
)
1224 const struct mdoc_node
*nn
;
1226 assert(n
->child
&& MDOC_TEXT
== n
->child
->type
);
1229 term_word(p
, nn
->string
);
1230 if (NULL
== (nn
= nn
->next
))
1232 p
->flags
|= TERMP_NOSPACE
;
1234 p
->flags
|= TERMP_NOSPACE
;
1235 term_word(p
, nn
->string
);
1236 p
->flags
|= TERMP_NOSPACE
;
1245 termp_vt_post(DECL_ARGS
)
1248 if (n
->sec
!= SEC_SYNOPSIS
)
1250 if (n
->next
&& MDOC_Vt
== n
->next
->tok
)
1259 termp_bold_pre(DECL_ARGS
)
1262 term_fontpush(p
, TERMFONT_BOLD
);
1269 termp_fd_post(DECL_ARGS
)
1272 if (n
->sec
!= SEC_SYNOPSIS
)
1276 if (n
->next
&& MDOC_Fd
!= n
->next
->tok
)
1283 termp_sh_pre(DECL_ARGS
)
1286 /* No vspace between consecutive `Sh' calls. */
1290 if (n
->prev
&& MDOC_Sh
== n
->prev
->tok
)
1291 if (NULL
== n
->prev
->body
->child
)
1296 term_fontpush(p
, TERMFONT_BOLD
);
1310 termp_sh_post(DECL_ARGS
)
1329 termp_op_pre(DECL_ARGS
)
1334 term_word(p
, "\\(lB");
1335 p
->flags
|= TERMP_NOSPACE
;
1346 termp_bt_pre(DECL_ARGS
)
1349 term_word(p
, "is currently in beta test.");
1356 termp_lb_post(DECL_ARGS
)
1359 if (SEC_LIBRARY
== n
->sec
)
1366 termp_ud_pre(DECL_ARGS
)
1369 term_word(p
, "currently under development.");
1376 termp_d1_pre(DECL_ARGS
)
1379 if (MDOC_BLOCK
!= n
->type
)
1382 p
->offset
+= (INDENT
+ 1);
1389 termp_d1_post(DECL_ARGS
)
1392 if (MDOC_BLOCK
!= n
->type
)
1400 termp_aq_pre(DECL_ARGS
)
1403 if (MDOC_BODY
!= n
->type
)
1405 term_word(p
, "\\(la");
1406 p
->flags
|= TERMP_NOSPACE
;
1413 termp_aq_post(DECL_ARGS
)
1416 if (MDOC_BODY
!= n
->type
)
1418 p
->flags
|= TERMP_NOSPACE
;
1419 term_word(p
, "\\(ra");
1425 termp_ft_pre(DECL_ARGS
)
1428 if (SEC_SYNOPSIS
== n
->sec
)
1429 if (n
->prev
&& MDOC_Fo
== n
->prev
->tok
)
1432 term_fontpush(p
, TERMFONT_UNDER
);
1439 termp_ft_post(DECL_ARGS
)
1442 if (SEC_SYNOPSIS
== n
->sec
)
1449 termp_fn_pre(DECL_ARGS
)
1451 const struct mdoc_node
*nn
;
1453 term_fontpush(p
, TERMFONT_BOLD
);
1454 term_word(p
, n
->child
->string
);
1457 p
->flags
|= TERMP_NOSPACE
;
1460 for (nn
= n
->child
->next
; nn
; nn
= nn
->next
) {
1461 term_fontpush(p
, TERMFONT_UNDER
);
1462 term_word(p
, nn
->string
);
1471 if (SEC_SYNOPSIS
== n
->sec
)
1480 termp_fn_post(DECL_ARGS
)
1483 if (n
->sec
== SEC_SYNOPSIS
&& n
->next
)
1490 termp_fa_pre(DECL_ARGS
)
1492 const struct mdoc_node
*nn
;
1494 if (n
->parent
->tok
!= MDOC_Fo
) {
1495 term_fontpush(p
, TERMFONT_UNDER
);
1499 for (nn
= n
->child
; nn
; nn
= nn
->next
) {
1500 term_fontpush(p
, TERMFONT_UNDER
);
1501 term_word(p
, nn
->string
);
1508 if (n
->child
&& n
->next
&& n
->next
->tok
== MDOC_Fa
)
1517 termp_bd_pre(DECL_ARGS
)
1520 const struct mdoc_node
*nn
;
1522 if (MDOC_BLOCK
== n
->type
) {
1523 print_bvspace(p
, n
, n
);
1525 } else if (MDOC_BODY
!= n
->type
)
1530 for (type
= -1, i
= 0; i
< (int)nn
->args
->argc
; i
++) {
1531 switch (nn
->args
->argv
[i
].arg
) {
1532 case (MDOC_Centred
):
1538 case (MDOC_Unfilled
):
1540 case (MDOC_Literal
):
1541 type
= nn
->args
->argv
[i
].arg
;
1544 p
->offset
+= a2offs(&nn
->args
->argv
[i
]);
1552 * If -ragged or -filled are specified, the block does nothing
1553 * but change the indentation. If -unfilled or -literal are
1554 * specified, text is printed exactly as entered in the display:
1555 * for macro lines, a newline is appended to the line. Blank
1556 * lines are allowed.
1560 if (MDOC_Literal
!= type
&& MDOC_Unfilled
!= type
)
1563 for (nn
= n
->child
; nn
; nn
= nn
->next
) {
1564 p
->flags
|= TERMP_NOSPACE
;
1565 print_mdoc_node(p
, pair
, m
, nn
);
1566 if (NULL
== nn
->next
)
1568 if (nn
->prev
&& nn
->prev
->line
< nn
->line
)
1570 else if (NULL
== nn
->prev
)
1580 termp_bd_post(DECL_ARGS
)
1583 if (MDOC_BODY
!= n
->type
)
1585 p
->flags
|= TERMP_NOSPACE
;
1592 termp_qq_pre(DECL_ARGS
)
1595 if (MDOC_BODY
!= n
->type
)
1598 p
->flags
|= TERMP_NOSPACE
;
1605 termp_qq_post(DECL_ARGS
)
1608 if (MDOC_BODY
!= n
->type
)
1610 p
->flags
|= TERMP_NOSPACE
;
1617 termp_bx_post(DECL_ARGS
)
1621 p
->flags
|= TERMP_NOSPACE
;
1622 term_word(p
, "BSD");
1628 termp_xx_pre(DECL_ARGS
)
1664 termp_sq_pre(DECL_ARGS
)
1667 if (MDOC_BODY
!= n
->type
)
1669 term_word(p
, "\\(oq");
1670 p
->flags
|= TERMP_NOSPACE
;
1677 termp_sq_post(DECL_ARGS
)
1680 if (MDOC_BODY
!= n
->type
)
1682 p
->flags
|= TERMP_NOSPACE
;
1683 term_word(p
, "\\(aq");
1689 termp_pf_pre(DECL_ARGS
)
1692 p
->flags
|= TERMP_IGNDELIM
;
1699 termp_pf_post(DECL_ARGS
)
1702 p
->flags
&= ~TERMP_IGNDELIM
;
1703 p
->flags
|= TERMP_NOSPACE
;
1709 termp_ss_pre(DECL_ARGS
)
1719 term_fontpush(p
, TERMFONT_BOLD
);
1720 p
->offset
= HALFINDENT
;
1732 termp_ss_post(DECL_ARGS
)
1735 if (MDOC_HEAD
== n
->type
)
1742 termp_cd_pre(DECL_ARGS
)
1745 term_fontpush(p
, TERMFONT_BOLD
);
1753 termp_in_pre(DECL_ARGS
)
1756 term_fontpush(p
, TERMFONT_BOLD
);
1757 if (SEC_SYNOPSIS
== n
->sec
)
1758 term_word(p
, "#include");
1761 p
->flags
|= TERMP_NOSPACE
;
1768 termp_in_post(DECL_ARGS
)
1771 term_fontpush(p
, TERMFONT_BOLD
);
1772 p
->flags
|= TERMP_NOSPACE
;
1776 if (SEC_SYNOPSIS
!= n
->sec
)
1781 * XXX Not entirely correct. If `.In foo bar' is specified in
1782 * the SYNOPSIS section, then it produces a single break after
1783 * the <foo>; mandoc asserts a vertical space. Since this
1784 * construction is rarely used, I think it's fine.
1786 if (n
->next
&& MDOC_In
!= n
->next
->tok
)
1793 termp_sp_pre(DECL_ARGS
)
1799 len
= n
->child
? a2height(n
->child
) : 1;
1811 for (i
= 0; i
< len
; i
++)
1820 termp_brq_pre(DECL_ARGS
)
1823 if (MDOC_BODY
!= n
->type
)
1825 term_word(p
, "\\(lC");
1826 p
->flags
|= TERMP_NOSPACE
;
1833 termp_brq_post(DECL_ARGS
)
1836 if (MDOC_BODY
!= n
->type
)
1838 p
->flags
|= TERMP_NOSPACE
;
1839 term_word(p
, "\\(rC");
1845 termp_bq_pre(DECL_ARGS
)
1848 if (MDOC_BODY
!= n
->type
)
1850 term_word(p
, "\\(lB");
1851 p
->flags
|= TERMP_NOSPACE
;
1858 termp_bq_post(DECL_ARGS
)
1861 if (MDOC_BODY
!= n
->type
)
1863 p
->flags
|= TERMP_NOSPACE
;
1864 term_word(p
, "\\(rB");
1870 termp_pq_pre(DECL_ARGS
)
1873 if (MDOC_BODY
!= n
->type
)
1875 term_word(p
, "\\&(");
1876 p
->flags
|= TERMP_NOSPACE
;
1883 termp_pq_post(DECL_ARGS
)
1886 if (MDOC_BODY
!= n
->type
)
1894 termp_fo_pre(DECL_ARGS
)
1896 const struct mdoc_node
*nn
;
1898 if (MDOC_BODY
== n
->type
) {
1899 p
->flags
|= TERMP_NOSPACE
;
1901 p
->flags
|= TERMP_NOSPACE
;
1903 } else if (MDOC_HEAD
!= n
->type
)
1906 term_fontpush(p
, TERMFONT_BOLD
);
1907 for (nn
= n
->child
; nn
; nn
= nn
->next
) {
1908 assert(MDOC_TEXT
== nn
->type
);
1909 term_word(p
, nn
->string
);
1919 termp_fo_post(DECL_ARGS
)
1922 if (MDOC_BODY
!= n
->type
)
1924 p
->flags
|= TERMP_NOSPACE
;
1926 p
->flags
|= TERMP_NOSPACE
;
1934 termp_bf_pre(DECL_ARGS
)
1936 const struct mdoc_node
*nn
;
1938 if (MDOC_HEAD
== n
->type
)
1940 else if (MDOC_BLOCK
!= n
->type
)
1943 if (NULL
== (nn
= n
->head
->child
)) {
1944 if (arg_hasattr(MDOC_Emphasis
, n
))
1945 term_fontpush(p
, TERMFONT_UNDER
);
1946 else if (arg_hasattr(MDOC_Symbolic
, n
))
1947 term_fontpush(p
, TERMFONT_BOLD
);
1949 term_fontpush(p
, TERMFONT_NONE
);
1954 assert(MDOC_TEXT
== nn
->type
);
1955 if (0 == strcmp("Em", nn
->string
))
1956 term_fontpush(p
, TERMFONT_UNDER
);
1957 else if (0 == strcmp("Sy", nn
->string
))
1958 term_fontpush(p
, TERMFONT_BOLD
);
1960 term_fontpush(p
, TERMFONT_NONE
);
1968 termp_sm_pre(DECL_ARGS
)
1971 assert(n
->child
&& MDOC_TEXT
== n
->child
->type
);
1972 if (0 == strcmp("on", n
->child
->string
)) {
1973 p
->flags
&= ~TERMP_NONOSPACE
;
1974 p
->flags
&= ~TERMP_NOSPACE
;
1976 p
->flags
|= TERMP_NONOSPACE
;
1984 termp_ap_pre(DECL_ARGS
)
1987 p
->flags
|= TERMP_NOSPACE
;
1988 term_word(p
, "\\(aq");
1989 p
->flags
|= TERMP_NOSPACE
;
1996 termp____post(DECL_ARGS
)
2001 p
->flags
|= TERMP_NOSPACE
;
2004 term_word(p
, "\\(rq");
2005 p
->flags
|= TERMP_NOSPACE
;
2010 term_word(p
, n
->next
? "," : ".");
2016 termp_li_pre(DECL_ARGS
)
2019 term_fontpush(p
, TERMFONT_NONE
);
2026 termp_lk_pre(DECL_ARGS
)
2028 const struct mdoc_node
*nn
;
2030 term_fontpush(p
, TERMFONT_UNDER
);
2033 if (NULL
== nn
->next
)
2036 term_word(p
, nn
->string
);
2039 p
->flags
|= TERMP_NOSPACE
;
2042 term_fontpush(p
, TERMFONT_BOLD
);
2043 for (nn
= nn
->next
; nn
; nn
= nn
->next
)
2044 term_word(p
, nn
->string
);
2053 termp_under_pre(DECL_ARGS
)
2056 term_fontpush(p
, TERMFONT_UNDER
);
2063 termp__t_pre(DECL_ARGS
)
2066 term_word(p
, "\\(lq");
2067 p
->flags
|= TERMP_NOSPACE
;