]>
git.cameronkatri.com Git - mandoc.git/blob - mdoc_term.c
1 /* $Id: mdoc_term.c,v 1.90 2009/10/15 01:33: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 void termp____post(DECL_ARGS
);
51 static void termp_an_post(DECL_ARGS
);
52 static void termp_aq_post(DECL_ARGS
);
53 static void termp_bd_post(DECL_ARGS
);
54 static void termp_bl_post(DECL_ARGS
);
55 static void termp_bq_post(DECL_ARGS
);
56 static void termp_brq_post(DECL_ARGS
);
57 static void termp_bx_post(DECL_ARGS
);
58 static void termp_d1_post(DECL_ARGS
);
59 static void termp_dq_post(DECL_ARGS
);
60 static void termp_fd_post(DECL_ARGS
);
61 static void termp_fn_post(DECL_ARGS
);
62 static void termp_fo_post(DECL_ARGS
);
63 static void termp_ft_post(DECL_ARGS
);
64 static void termp_in_post(DECL_ARGS
);
65 static void termp_it_post(DECL_ARGS
);
66 static void termp_lb_post(DECL_ARGS
);
67 static void termp_op_post(DECL_ARGS
);
68 static void termp_pf_post(DECL_ARGS
);
69 static void termp_pq_post(DECL_ARGS
);
70 static void termp_qq_post(DECL_ARGS
);
71 static void termp_sh_post(DECL_ARGS
);
72 static void termp_sq_post(DECL_ARGS
);
73 static void termp_ss_post(DECL_ARGS
);
74 static void termp_vt_post(DECL_ARGS
);
76 static int termp__t_pre(DECL_ARGS
);
77 static int termp_an_pre(DECL_ARGS
);
78 static int termp_ap_pre(DECL_ARGS
);
79 static int termp_aq_pre(DECL_ARGS
);
80 static int termp_bd_pre(DECL_ARGS
);
81 static int termp_bf_pre(DECL_ARGS
);
82 static int termp_bold_pre(DECL_ARGS
);
83 static int termp_bq_pre(DECL_ARGS
);
84 static int termp_brq_pre(DECL_ARGS
);
85 static int termp_bt_pre(DECL_ARGS
);
86 static int termp_cd_pre(DECL_ARGS
);
87 static int termp_d1_pre(DECL_ARGS
);
88 static int termp_dq_pre(DECL_ARGS
);
89 static int termp_ex_pre(DECL_ARGS
);
90 static int termp_fa_pre(DECL_ARGS
);
91 static int termp_fl_pre(DECL_ARGS
);
92 static int termp_fn_pre(DECL_ARGS
);
93 static int termp_fo_pre(DECL_ARGS
);
94 static int termp_ft_pre(DECL_ARGS
);
95 static int termp_in_pre(DECL_ARGS
);
96 static int termp_it_pre(DECL_ARGS
);
97 static int termp_lk_pre(DECL_ARGS
);
98 static int termp_nd_pre(DECL_ARGS
);
99 static int termp_nm_pre(DECL_ARGS
);
100 static int termp_ns_pre(DECL_ARGS
);
101 static int termp_op_pre(DECL_ARGS
);
102 static int termp_pf_pre(DECL_ARGS
);
103 static int termp_pq_pre(DECL_ARGS
);
104 static int termp_qq_pre(DECL_ARGS
);
105 static int termp_rs_pre(DECL_ARGS
);
106 static int termp_rv_pre(DECL_ARGS
);
107 static int termp_sh_pre(DECL_ARGS
);
108 static int termp_sm_pre(DECL_ARGS
);
109 static int termp_sp_pre(DECL_ARGS
);
110 static int termp_sq_pre(DECL_ARGS
);
111 static int termp_ss_pre(DECL_ARGS
);
112 static int termp_under_pre(DECL_ARGS
);
113 static int termp_ud_pre(DECL_ARGS
);
114 static int termp_xr_pre(DECL_ARGS
);
115 static int termp_xx_pre(DECL_ARGS
);
117 static const struct termact termacts
[MDOC_MAX
] = {
118 { termp_ap_pre
, NULL
}, /* Ap */
119 { NULL
, NULL
}, /* Dd */
120 { NULL
, NULL
}, /* Dt */
121 { NULL
, NULL
}, /* Os */
122 { termp_sh_pre
, termp_sh_post
}, /* Sh */
123 { termp_ss_pre
, termp_ss_post
}, /* Ss */
124 { termp_sp_pre
, NULL
}, /* Pp */
125 { termp_d1_pre
, termp_d1_post
}, /* D1 */
126 { termp_d1_pre
, termp_d1_post
}, /* Dl */
127 { termp_bd_pre
, termp_bd_post
}, /* Bd */
128 { NULL
, NULL
}, /* Ed */
129 { NULL
, termp_bl_post
}, /* Bl */
130 { NULL
, NULL
}, /* El */
131 { termp_it_pre
, termp_it_post
}, /* It */
132 { NULL
, NULL
}, /* Ad */
133 { termp_an_pre
, termp_an_post
}, /* An */
134 { termp_under_pre
, NULL
}, /* Ar */
135 { termp_cd_pre
, NULL
}, /* Cd */
136 { termp_bold_pre
, NULL
}, /* Cm */
137 { NULL
, NULL
}, /* Dv */
138 { NULL
, NULL
}, /* Er */
139 { NULL
, NULL
}, /* Ev */
140 { termp_ex_pre
, NULL
}, /* Ex */
141 { termp_fa_pre
, NULL
}, /* Fa */
142 { termp_bold_pre
, termp_fd_post
}, /* Fd */
143 { termp_fl_pre
, NULL
}, /* Fl */
144 { termp_fn_pre
, termp_fn_post
}, /* Fn */
145 { termp_ft_pre
, termp_ft_post
}, /* Ft */
146 { termp_bold_pre
, NULL
}, /* Ic */
147 { termp_in_pre
, termp_in_post
}, /* In */
148 { NULL
, NULL
}, /* Li */
149 { termp_nd_pre
, NULL
}, /* Nd */
150 { termp_nm_pre
, NULL
}, /* Nm */
151 { termp_op_pre
, termp_op_post
}, /* Op */
152 { NULL
, NULL
}, /* Ot */
153 { termp_under_pre
, NULL
}, /* Pa */
154 { termp_rv_pre
, NULL
}, /* Rv */
155 { NULL
, NULL
}, /* St */
156 { termp_under_pre
, NULL
}, /* Va */
157 { termp_under_pre
, termp_vt_post
}, /* Vt */
158 { termp_xr_pre
, NULL
}, /* Xr */
159 { NULL
, termp____post
}, /* %A */
160 { termp_under_pre
, termp____post
}, /* %B */
161 { NULL
, termp____post
}, /* %D */
162 { termp_under_pre
, termp____post
}, /* %I */
163 { termp_under_pre
, termp____post
}, /* %J */
164 { NULL
, termp____post
}, /* %N */
165 { NULL
, termp____post
}, /* %O */
166 { NULL
, termp____post
}, /* %P */
167 { NULL
, termp____post
}, /* %R */
168 { termp__t_pre
, termp____post
}, /* %T */
169 { NULL
, termp____post
}, /* %V */
170 { NULL
, NULL
}, /* Ac */
171 { termp_aq_pre
, termp_aq_post
}, /* Ao */
172 { termp_aq_pre
, termp_aq_post
}, /* Aq */
173 { NULL
, NULL
}, /* At */
174 { NULL
, NULL
}, /* Bc */
175 { termp_bf_pre
, NULL
}, /* Bf */
176 { termp_bq_pre
, termp_bq_post
}, /* Bo */
177 { termp_bq_pre
, termp_bq_post
}, /* Bq */
178 { termp_xx_pre
, NULL
}, /* Bsx */
179 { NULL
, termp_bx_post
}, /* Bx */
180 { NULL
, NULL
}, /* Db */
181 { NULL
, NULL
}, /* Dc */
182 { termp_dq_pre
, termp_dq_post
}, /* Do */
183 { termp_dq_pre
, termp_dq_post
}, /* Dq */
184 { NULL
, NULL
}, /* Ec */
185 { NULL
, NULL
}, /* Ef */
186 { termp_under_pre
, NULL
}, /* Em */
187 { NULL
, NULL
}, /* Eo */
188 { termp_xx_pre
, NULL
}, /* Fx */
189 { termp_bold_pre
, NULL
}, /* Ms */ /* FIXME: convert to symbol? */
190 { NULL
, NULL
}, /* No */
191 { termp_ns_pre
, NULL
}, /* Ns */
192 { termp_xx_pre
, NULL
}, /* Nx */
193 { termp_xx_pre
, NULL
}, /* Ox */
194 { NULL
, NULL
}, /* Pc */
195 { termp_pf_pre
, termp_pf_post
}, /* Pf */
196 { termp_pq_pre
, termp_pq_post
}, /* Po */
197 { termp_pq_pre
, termp_pq_post
}, /* Pq */
198 { NULL
, NULL
}, /* Qc */
199 { termp_sq_pre
, termp_sq_post
}, /* Ql */
200 { termp_qq_pre
, termp_qq_post
}, /* Qo */
201 { termp_qq_pre
, termp_qq_post
}, /* Qq */
202 { NULL
, NULL
}, /* Re */
203 { termp_rs_pre
, NULL
}, /* Rs */
204 { NULL
, NULL
}, /* Sc */
205 { termp_sq_pre
, termp_sq_post
}, /* So */
206 { termp_sq_pre
, termp_sq_post
}, /* Sq */
207 { termp_sm_pre
, NULL
}, /* Sm */
208 { termp_under_pre
, NULL
}, /* Sx */
209 { termp_bold_pre
, NULL
}, /* Sy */
210 { NULL
, NULL
}, /* Tn */
211 { termp_xx_pre
, NULL
}, /* Ux */
212 { NULL
, NULL
}, /* Xc */
213 { NULL
, NULL
}, /* Xo */
214 { termp_fo_pre
, termp_fo_post
}, /* Fo */
215 { NULL
, NULL
}, /* Fc */
216 { termp_op_pre
, termp_op_post
}, /* Oo */
217 { NULL
, NULL
}, /* Oc */
218 { NULL
, NULL
}, /* Bk */
219 { NULL
, NULL
}, /* Ek */
220 { termp_bt_pre
, NULL
}, /* Bt */
221 { NULL
, NULL
}, /* Hf */
222 { NULL
, NULL
}, /* Fr */
223 { termp_ud_pre
, NULL
}, /* Ud */
224 { NULL
, termp_lb_post
}, /* Lb */
225 { termp_sp_pre
, NULL
}, /* Lp */
226 { termp_lk_pre
, NULL
}, /* Lk */
227 { termp_under_pre
, NULL
}, /* Mt */
228 { termp_brq_pre
, termp_brq_post
}, /* Brq */
229 { termp_brq_pre
, termp_brq_post
}, /* Bro */
230 { NULL
, NULL
}, /* Brc */
231 { NULL
, termp____post
}, /* %C */
232 { NULL
, NULL
}, /* Es */ /* TODO */
233 { NULL
, NULL
}, /* En */ /* TODO */
234 { termp_xx_pre
, NULL
}, /* Dx */
235 { NULL
, termp____post
}, /* %Q */
236 { termp_sp_pre
, NULL
}, /* br */
237 { termp_sp_pre
, NULL
}, /* sp */
241 extern size_t strlcpy(char *, const char *, size_t);
242 extern size_t strlcat(char *, const char *, size_t);
245 static int arg_hasattr(int, const struct mdoc_node
*);
246 static int arg_getattrs(const int *, int *, size_t,
247 const struct mdoc_node
*);
248 static int arg_getattr(int, const struct mdoc_node
*);
249 static size_t arg_offset(const struct mdoc_argv
*);
250 static size_t arg_width(const struct mdoc_argv
*, int);
251 static int arg_listtype(const struct mdoc_node
*);
252 static void fmt_block_vspace(struct termp
*,
253 const struct mdoc_node
*,
254 const struct mdoc_node
*);
255 static void print_node(DECL_ARGS
);
256 static void print_head(DECL_ARGS
);
257 static void print_body(DECL_ARGS
);
258 static void print_foot(DECL_ARGS
);
262 terminal_mdoc(void *arg
, const struct mdoc
*mdoc
)
264 const struct mdoc_node
*n
;
265 const struct mdoc_meta
*m
;
268 p
= (struct termp
*)arg
;
270 if (NULL
== p
->symtab
)
272 case (TERMENC_ASCII
):
273 p
->symtab
= chars_init(CHARS_ASCII
);
283 print_head(p
, NULL
, m
, n
);
285 print_body(p
, NULL
, m
, n
->child
);
286 print_foot(p
, NULL
, m
, n
);
291 print_body(DECL_ARGS
)
294 print_node(p
, pair
, m
, n
);
296 print_body(p
, pair
, m
, n
->next
);
302 print_node(DECL_ARGS
)
304 int chld
, bold
, under
;
305 struct termpair npair
;
306 size_t offset
, rmargin
;
310 rmargin
= p
->rmargin
;
314 bzero(&npair
, sizeof(struct termpair
));
317 if (MDOC_TEXT
!= n
->type
) {
318 if (termacts
[n
->tok
].pre
)
319 chld
= (*termacts
[n
->tok
].pre
)(p
, &npair
, m
, n
);
321 term_word(p
, n
->string
);
322 if (chld
&& n
->child
)
323 print_body(p
, &npair
, m
, n
->child
);
326 * XXX - if bold/under were to span scopes, this wouldn't be
327 * possible, but because decoration is always in-scope, we can
328 * get away with this.
334 if (MDOC_TEXT
!= n
->type
)
335 if (termacts
[n
->tok
].post
)
336 (*termacts
[n
->tok
].post
)(p
, &npair
, m
, n
);
339 p
->rmargin
= rmargin
;
345 print_foot(DECL_ARGS
)
351 * Output the footer in new-groff style, that is, three columns
352 * with the middle being the manual date and flanking columns
353 * being the operating system:
358 if (NULL
== (buf
= malloc(p
->rmargin
)))
359 err(EXIT_FAILURE
, "malloc");
360 if (NULL
== (os
= malloc(p
->rmargin
)))
361 err(EXIT_FAILURE
, "malloc");
363 tm
= localtime(&m
->date
);
365 if (0 == strftime(buf
, p
->rmargin
, "%B %e, %Y", tm
))
366 err(EXIT_FAILURE
, "strftime");
368 (void)strlcpy(os
, m
->os
, p
->rmargin
);
373 p
->rmargin
= (p
->maxrmargin
- strlen(buf
) + 1) / 2;
374 p
->flags
|= TERMP_NOSPACE
| TERMP_NOBREAK
;
379 p
->offset
= p
->rmargin
;
380 p
->rmargin
= p
->maxrmargin
- strlen(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
;
403 /* FIXME: put in utility library. */
406 print_head(DECL_ARGS
)
410 p
->rmargin
= p
->maxrmargin
;
413 if (NULL
== (buf
= malloc(p
->rmargin
)))
414 err(EXIT_FAILURE
, "malloc");
415 if (NULL
== (title
= malloc(p
->rmargin
)))
416 err(EXIT_FAILURE
, "malloc");
419 * The header is strange. It has three components, which are
420 * really two with the first duplicated. It goes like this:
422 * IDENTIFIER TITLE IDENTIFIER
424 * The IDENTIFIER is NAME(SECTION), which is the command-name
425 * (if given, or "unknown" if not) followed by the manual page
426 * section. These are given in `Dt'. The TITLE is a free-form
427 * string depending on the manual volume. If not specified, it
428 * switches on the manual section.
432 (void)strlcpy(buf
, m
->vol
, p
->rmargin
);
435 (void)strlcat(buf
, " (", p
->rmargin
);
436 (void)strlcat(buf
, m
->arch
, p
->rmargin
);
437 (void)strlcat(buf
, ")", p
->rmargin
);
440 snprintf(title
, p
->rmargin
, "%s(%d)", m
->title
, m
->msec
);
443 p
->rmargin
= (p
->maxrmargin
- strlen(buf
) + 1) / 2;
444 p
->flags
|= TERMP_NOBREAK
| TERMP_NOSPACE
;
449 p
->offset
= p
->rmargin
;
450 p
->rmargin
= p
->maxrmargin
- strlen(title
);
451 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
456 p
->offset
= p
->rmargin
;
457 p
->rmargin
= p
->maxrmargin
;
458 p
->flags
&= ~TERMP_NOBREAK
;
459 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
465 p
->rmargin
= p
->maxrmargin
;
466 p
->flags
&= ~TERMP_NOSPACE
;
473 /* FIXME: put in utility file for front-ends. */
475 arg_width(const struct mdoc_argv
*arg
, int pos
)
480 assert(pos
< (int)arg
->sz
&& pos
>= 0);
481 assert(arg
->value
[pos
]);
485 if (0 == (len
= (int)strlen(p
)))
488 for (i
= 0; i
< len
- 1; i
++)
489 if ( ! isdigit((u_char
)p
[i
]))
493 if ('n' == p
[len
- 1] || 'm' == p
[len
- 1])
494 return((size_t)atoi(p
) + 2);
496 return((size_t)len
+ 2);
500 /* FIXME: put in utility file for front-ends. */
502 arg_listtype(const struct mdoc_node
*n
)
506 assert(MDOC_BLOCK
== n
->type
);
508 len
= (int)(n
->args
? n
->args
->argc
: 0);
510 for (i
= 0; i
< len
; i
++)
511 switch (n
->args
->argv
[i
].arg
) {
533 return(n
->args
->argv
[i
].arg
);
542 /* FIXME: put in utility file for front-ends. */
544 arg_offset(const struct mdoc_argv
*arg
)
552 if (0 == strcmp(p
, "left"))
554 if (0 == strcmp(p
, "indent"))
556 if (0 == strcmp(p
, "indent-two"))
557 return((INDENT
+ 1) * 2);
559 if (0 == (len
= (int)strlen(p
)))
562 for (i
= 0; i
< len
- 1; i
++)
563 if ( ! isdigit((u_char
)p
[i
]))
567 if ('n' == p
[len
- 1] || 'm' == p
[len
- 1])
568 return((size_t)atoi(p
));
575 arg_hasattr(int arg
, const struct mdoc_node
*n
)
578 return(-1 != arg_getattr(arg
, n
));
583 arg_getattr(int v
, const struct mdoc_node
*n
)
587 return(arg_getattrs(&v
, &val
, 1, n
) ? val
: -1);
592 arg_getattrs(const int *keys
, int *vals
,
593 size_t sz
, const struct mdoc_node
*n
)
600 for (k
= i
= 0; i
< (int)n
->args
->argc
; i
++)
601 for (j
= 0; j
< (int)sz
; j
++)
602 if (n
->args
->argv
[i
].arg
== keys
[j
]) {
612 fmt_block_vspace(struct termp
*p
,
613 const struct mdoc_node
*bl
,
614 const struct mdoc_node
*n
)
616 const struct mdoc_node
*nn
;
619 if (arg_hasattr(MDOC_Compact
, bl
))
622 /* Do not vspace directly after Ss/Sh. */
624 for (nn
= n
; nn
; nn
= nn
->parent
) {
625 if (MDOC_BLOCK
!= nn
->type
)
627 if (MDOC_Ss
== nn
->tok
)
629 if (MDOC_Sh
== nn
->tok
)
631 if (NULL
== nn
->prev
)
636 /* A `-column' does not assert vspace within the list. */
638 if (MDOC_Bl
== bl
->tok
&& arg_hasattr(MDOC_Column
, bl
))
639 if (n
->prev
&& MDOC_It
== n
->prev
->tok
)
642 /* A `-diag' without body does not vspace. */
644 if (MDOC_Bl
== bl
->tok
&& arg_hasattr(MDOC_Diag
, bl
))
645 if (n
->prev
&& MDOC_It
== n
->prev
->tok
) {
646 assert(n
->prev
->body
);
647 if (NULL
== n
->prev
->body
->child
)
657 termp_dq_pre(DECL_ARGS
)
660 if (MDOC_BODY
!= n
->type
)
663 term_word(p
, "\\(lq");
664 p
->flags
|= TERMP_NOSPACE
;
671 termp_dq_post(DECL_ARGS
)
674 if (MDOC_BODY
!= n
->type
)
677 p
->flags
|= TERMP_NOSPACE
;
678 term_word(p
, "\\(rq");
684 termp_it_pre(DECL_ARGS
)
686 const struct mdoc_node
*bl
, *nn
;
688 int i
, type
, keys
[3], vals
[3];
689 size_t width
, offset
;
691 if (MDOC_BLOCK
== n
->type
) {
692 fmt_block_vspace(p
, n
->parent
->parent
, n
);
696 bl
= n
->parent
->parent
->parent
;
698 /* Save parent attributes. */
700 pair
->flag
= p
->flags
;
702 /* Get list width and offset. */
704 keys
[0] = MDOC_Width
;
705 keys
[1] = MDOC_Offset
;
706 keys
[2] = MDOC_Column
;
708 vals
[0] = vals
[1] = vals
[2] = -1;
712 (void)arg_getattrs(keys
, vals
, 3, bl
);
714 type
= arg_listtype(bl
);
717 /* Calculate real width and offset. */
721 if (MDOC_BODY
== n
->type
)
724 * Work around groff's column handling. The offset is
725 * equal to the sum of all widths leading to the current
726 * column (plus the -offset value). If this column
727 * exceeds the stated number of columns, the width is
728 * set as 0, else it's the stated column width (later
729 * the 0 will be adjusted to default 10 or, if in the
730 * last column case, set to stretch to the margin).
732 for (i
= 0, nn
= n
->prev
; nn
&&
733 i
< (int)bl
->args
->argv
[vals
[2]].sz
;
736 (&bl
->args
->argv
[vals
[2]], i
);
738 /* Whether exceeds maximum column. */
739 if (i
< (int)bl
->args
->argv
[vals
[2]].sz
)
740 width
= arg_width(&bl
->args
->argv
[vals
[2]], i
);
745 offset
+= arg_offset(&bl
->args
->argv
[vals
[1]]);
749 width
= arg_width(&bl
->args
->argv
[vals
[0]], 0);
751 offset
+= arg_offset(&bl
->args
->argv
[vals
[1]]);
756 * List-type can override the width in the case of fixed-head
757 * values (bullet, dash/hyphen, enum). Tags need a non-zero
789 * Whitespace control. Inset bodies need an initial space,
790 * while diagonal bodies need two.
793 p
->flags
|= TERMP_NOSPACE
;
797 if (MDOC_BODY
== n
->type
)
798 term_word(p
, "\\ \\ ");
801 if (MDOC_BODY
== n
->type
)
808 p
->flags
|= TERMP_NOSPACE
;
812 if (MDOC_HEAD
== n
->type
)
820 * Pad and break control. This is the tricker part. Lists with
821 * set right-margins for the head get TERMP_NOBREAK because, if
822 * they overrun the margin, they wrap to the new margin.
823 * Correspondingly, the body for these types don't left-pad, as
824 * the head will pad out to to the right.
835 if (MDOC_HEAD
== n
->type
)
836 p
->flags
|= TERMP_NOBREAK
;
838 p
->flags
|= TERMP_NOLPAD
;
841 if (MDOC_HEAD
== n
->type
)
842 p
->flags
|= TERMP_NOBREAK
;
844 p
->flags
|= TERMP_NOLPAD
;
846 if (MDOC_HEAD
!= n
->type
)
850 * This is ugly. If `-hang' is specified and the body
851 * is a `Bl' or `Bd', then we want basically to nullify
852 * the "overstep" effect in term_flushln() and treat
853 * this as a `-ohang' list instead.
855 if (n
->next
->child
&&
856 (MDOC_Bl
== n
->next
->child
->tok
||
857 MDOC_Bd
== n
->next
->child
->tok
)) {
858 p
->flags
&= ~TERMP_NOBREAK
;
859 p
->flags
&= ~TERMP_NOLPAD
;
861 p
->flags
|= TERMP_HANG
;
864 if (MDOC_HEAD
== n
->type
)
865 p
->flags
|= TERMP_NOBREAK
| TERMP_TWOSPACE
;
867 p
->flags
|= TERMP_NOLPAD
;
869 if (MDOC_HEAD
!= n
->type
)
871 if (NULL
== n
->next
|| NULL
== n
->next
->child
)
872 p
->flags
|= TERMP_DANGLE
;
875 if (MDOC_HEAD
== n
->type
) {
877 if (MDOC_BODY
== n
->next
->type
)
878 p
->flags
&= ~TERMP_NOBREAK
;
880 p
->flags
|= TERMP_NOBREAK
;
882 p
->flags
|= TERMP_NOLPAD
;
886 if (MDOC_HEAD
== n
->type
)
887 p
->flags
|= TERMP_NOBREAK
;
894 * Margin control. Set-head-width lists have their right
895 * margins shortened. The body for these lists has the offset
896 * necessarily lengthened. Everybody gets the offset.
904 * Same stipulation as above, regarding `-hang'. We
905 * don't want to recalculate rmargin and offsets when
906 * using `Bd' or `Bl' within `-hang' overstep lists.
908 if (MDOC_HEAD
== n
->type
&& n
->next
->child
&&
909 (MDOC_Bl
== n
->next
->child
->tok
||
910 MDOC_Bd
== n
->next
->child
->tok
))
923 if (MDOC_HEAD
== n
->type
)
924 p
->rmargin
= p
->offset
+ width
;
930 p
->rmargin
= p
->offset
+ width
;
932 * XXX - this behaviour is not documented: the
933 * right-most column is filled to the right margin.
935 if (MDOC_HEAD
== n
->type
&&
936 MDOC_BODY
== n
->next
->type
)
937 p
->rmargin
= p
->maxrmargin
;
944 * The dash, hyphen, bullet and enum lists all have a special
945 * HEAD character (temporarily bold, in some cases).
948 if (MDOC_HEAD
== n
->type
)
952 term_word(p
, "\\[bu]");
959 term_word(p
, "\\(hy");
963 (pair
->ppair
->ppair
->count
)++;
964 (void)snprintf(buf
, sizeof(buf
), "%d.",
965 pair
->ppair
->ppair
->count
);
973 * If we're not going to process our children, indicate so here.
986 if (MDOC_HEAD
== n
->type
)
990 if (MDOC_BODY
== n
->type
)
1003 termp_it_post(DECL_ARGS
)
1007 if (MDOC_BODY
!= n
->type
&& MDOC_HEAD
!= n
->type
)
1010 type
= arg_listtype(n
->parent
->parent
->parent
);
1019 if (MDOC_BODY
== n
->type
)
1023 if (MDOC_HEAD
== n
->type
)
1031 p
->flags
= pair
->flag
;
1037 termp_nm_pre(DECL_ARGS
)
1040 if (SEC_SYNOPSIS
== n
->sec
)
1043 if (NULL
== n
->child
)
1044 term_word(p
, m
->name
);
1051 termp_fl_pre(DECL_ARGS
)
1055 term_word(p
, "\\-");
1056 p
->flags
|= TERMP_NOSPACE
;
1063 termp_an_pre(DECL_ARGS
)
1066 if (NULL
== n
->child
)
1070 * If not in the AUTHORS section, `An -split' will cause
1071 * newlines to occur before the author name. If in the AUTHORS
1072 * section, by default, the first `An' invocation is nosplit,
1073 * then all subsequent ones, regardless of whether interspersed
1074 * with other macros/text, are split. -split, in this case,
1075 * will override the condition of the implied first -nosplit.
1078 if (n
->sec
== SEC_AUTHORS
) {
1079 if ( ! (TERMP_ANPREC
& p
->flags
)) {
1080 if (TERMP_SPLIT
& p
->flags
)
1084 if (TERMP_NOSPLIT
& p
->flags
)
1090 if (TERMP_SPLIT
& p
->flags
)
1099 termp_an_post(DECL_ARGS
)
1103 if (SEC_AUTHORS
== n
->sec
)
1104 p
->flags
|= TERMP_ANPREC
;
1108 if (arg_getattr(MDOC_Split
, n
) > -1) {
1109 p
->flags
&= ~TERMP_NOSPLIT
;
1110 p
->flags
|= TERMP_SPLIT
;
1112 p
->flags
&= ~TERMP_SPLIT
;
1113 p
->flags
|= TERMP_NOSPLIT
;
1121 termp_ns_pre(DECL_ARGS
)
1124 p
->flags
|= TERMP_NOSPACE
;
1131 termp_rs_pre(DECL_ARGS
)
1134 if (SEC_SEE_ALSO
!= n
->sec
)
1136 if (MDOC_BLOCK
== n
->type
&& n
->prev
)
1144 termp_rv_pre(DECL_ARGS
)
1146 const struct mdoc_node
*nn
;
1149 term_word(p
, "The");
1151 for (nn
= n
->child
; nn
; nn
= nn
->next
) {
1153 term_word(p
, nn
->string
);
1155 p
->flags
|= TERMP_NOSPACE
;
1156 if (nn
->next
&& NULL
== nn
->next
->next
)
1157 term_word(p
, "(), and");
1159 term_word(p
, "(),");
1165 term_word(p
, "functions return");
1167 term_word(p
, "function returns");
1169 term_word(p
, "the value 0 if successful; otherwise the value "
1170 "-1 is returned and the global variable");
1173 term_word(p
, "errno");
1176 term_word(p
, "is set to indicate the error.");
1184 termp_ex_pre(DECL_ARGS
)
1186 const struct mdoc_node
*nn
;
1188 term_word(p
, "The");
1190 for (nn
= n
->child
; nn
; nn
= nn
->next
) {
1192 term_word(p
, nn
->string
);
1194 p
->flags
|= TERMP_NOSPACE
;
1195 if (nn
->next
&& NULL
== nn
->next
->next
)
1196 term_word(p
, ", and");
1200 p
->flags
&= ~TERMP_NOSPACE
;
1204 term_word(p
, "utilities exit");
1206 term_word(p
, "utility exits");
1208 term_word(p
, "0 on success, and >0 if an error occurs.");
1216 termp_nd_pre(DECL_ARGS
)
1219 if (MDOC_BODY
!= n
->type
)
1222 #if defined(__OpenBSD__) || defined(__linux__)
1223 term_word(p
, "\\(en");
1225 term_word(p
, "\\(em");
1233 termp_bl_post(DECL_ARGS
)
1236 if (MDOC_BLOCK
== n
->type
)
1243 termp_op_post(DECL_ARGS
)
1246 if (MDOC_BODY
!= n
->type
)
1248 p
->flags
|= TERMP_NOSPACE
;
1249 term_word(p
, "\\(rB");
1255 termp_xr_pre(DECL_ARGS
)
1257 const struct mdoc_node
*nn
;
1259 assert(n
->child
&& MDOC_TEXT
== n
->child
->type
);
1262 term_word(p
, nn
->string
);
1263 if (NULL
== (nn
= nn
->next
))
1265 p
->flags
|= TERMP_NOSPACE
;
1267 p
->flags
|= TERMP_NOSPACE
;
1268 term_word(p
, nn
->string
);
1269 p
->flags
|= TERMP_NOSPACE
;
1278 termp_vt_post(DECL_ARGS
)
1281 if (n
->sec
!= SEC_SYNOPSIS
)
1283 if (n
->next
&& MDOC_Vt
== n
->next
->tok
)
1292 termp_bold_pre(DECL_ARGS
)
1302 termp_fd_post(DECL_ARGS
)
1305 if (n
->sec
!= SEC_SYNOPSIS
)
1309 if (n
->next
&& MDOC_Fd
!= n
->next
->tok
)
1316 termp_sh_pre(DECL_ARGS
)
1319 /* No vspace between consecutive `Sh' calls. */
1323 if (n
->prev
&& MDOC_Sh
== n
->prev
->tok
)
1324 if (NULL
== n
->prev
->body
->child
)
1343 termp_sh_post(DECL_ARGS
)
1362 termp_op_pre(DECL_ARGS
)
1367 term_word(p
, "\\(lB");
1368 p
->flags
|= TERMP_NOSPACE
;
1379 termp_bt_pre(DECL_ARGS
)
1382 term_word(p
, "is currently in beta test.");
1389 termp_lb_post(DECL_ARGS
)
1392 if (SEC_LIBRARY
== n
->sec
)
1399 termp_ud_pre(DECL_ARGS
)
1402 term_word(p
, "currently under development.");
1409 termp_d1_pre(DECL_ARGS
)
1412 if (MDOC_BLOCK
!= n
->type
)
1415 p
->offset
+= (INDENT
+ 1);
1422 termp_d1_post(DECL_ARGS
)
1425 if (MDOC_BLOCK
!= n
->type
)
1433 termp_aq_pre(DECL_ARGS
)
1436 if (MDOC_BODY
!= n
->type
)
1438 term_word(p
, "\\(la");
1439 p
->flags
|= TERMP_NOSPACE
;
1446 termp_aq_post(DECL_ARGS
)
1449 if (MDOC_BODY
!= n
->type
)
1451 p
->flags
|= TERMP_NOSPACE
;
1452 term_word(p
, "\\(ra");
1458 termp_ft_pre(DECL_ARGS
)
1461 if (SEC_SYNOPSIS
== n
->sec
)
1462 if (n
->prev
&& MDOC_Fo
== n
->prev
->tok
)
1471 termp_ft_post(DECL_ARGS
)
1474 if (SEC_SYNOPSIS
== n
->sec
)
1481 termp_fn_pre(DECL_ARGS
)
1483 const struct mdoc_node
*nn
;
1486 term_word(p
, n
->child
->string
);
1489 p
->flags
|= TERMP_NOSPACE
;
1492 for (nn
= n
->child
->next
; nn
; nn
= nn
->next
) {
1494 term_word(p
, nn
->string
);
1502 if (SEC_SYNOPSIS
== n
->sec
)
1511 termp_fn_post(DECL_ARGS
)
1514 if (n
->sec
== SEC_SYNOPSIS
&& n
->next
)
1521 termp_fa_pre(DECL_ARGS
)
1523 const struct mdoc_node
*nn
;
1525 if (n
->parent
->tok
!= MDOC_Fo
) {
1530 for (nn
= n
->child
; nn
; nn
= nn
->next
) {
1532 term_word(p
, nn
->string
);
1538 if (n
->child
&& n
->next
&& n
->next
->tok
== MDOC_Fa
)
1547 termp_bd_pre(DECL_ARGS
)
1550 const struct mdoc_node
*nn
;
1552 if (MDOC_BLOCK
== n
->type
) {
1553 fmt_block_vspace(p
, n
, n
);
1555 } else if (MDOC_BODY
!= n
->type
)
1560 for (type
= -1, i
= 0; i
< (int)nn
->args
->argc
; i
++) {
1561 switch (nn
->args
->argv
[i
].arg
) {
1562 case (MDOC_Centred
):
1568 case (MDOC_Unfilled
):
1570 case (MDOC_Literal
):
1571 type
= nn
->args
->argv
[i
].arg
;
1574 p
->offset
+= arg_offset(&nn
->args
->argv
[i
]);
1582 * If -ragged or -filled are specified, the block does nothing
1583 * but change the indentation. If -unfilled or -literal are
1584 * specified, text is printed exactly as entered in the display:
1585 * for macro lines, a newline is appended to the line. Blank
1586 * lines are allowed.
1590 if (MDOC_Literal
!= type
&& MDOC_Unfilled
!= type
)
1593 for (nn
= n
->child
; nn
; nn
= nn
->next
) {
1594 p
->flags
|= TERMP_NOSPACE
;
1595 print_node(p
, pair
, m
, nn
);
1596 if (NULL
== nn
->next
)
1598 if (nn
->prev
&& nn
->prev
->line
< nn
->line
)
1600 else if (NULL
== nn
->prev
)
1610 termp_bd_post(DECL_ARGS
)
1613 if (MDOC_BODY
!= n
->type
)
1615 p
->flags
|= TERMP_NOSPACE
;
1622 termp_qq_pre(DECL_ARGS
)
1625 if (MDOC_BODY
!= n
->type
)
1628 p
->flags
|= TERMP_NOSPACE
;
1635 termp_qq_post(DECL_ARGS
)
1638 if (MDOC_BODY
!= n
->type
)
1640 p
->flags
|= TERMP_NOSPACE
;
1647 termp_bx_post(DECL_ARGS
)
1651 p
->flags
|= TERMP_NOSPACE
;
1652 term_word(p
, "BSD");
1658 termp_xx_pre(DECL_ARGS
)
1668 pp
= "DragonFlyBSD";
1694 termp_sq_pre(DECL_ARGS
)
1697 if (MDOC_BODY
!= n
->type
)
1699 term_word(p
, "\\(oq");
1700 p
->flags
|= TERMP_NOSPACE
;
1707 termp_sq_post(DECL_ARGS
)
1710 if (MDOC_BODY
!= n
->type
)
1712 p
->flags
|= TERMP_NOSPACE
;
1713 term_word(p
, "\\(aq");
1719 termp_pf_pre(DECL_ARGS
)
1722 p
->flags
|= TERMP_IGNDELIM
;
1729 termp_pf_post(DECL_ARGS
)
1732 p
->flags
&= ~TERMP_IGNDELIM
;
1733 p
->flags
|= TERMP_NOSPACE
;
1739 termp_ss_pre(DECL_ARGS
)
1750 p
->offset
= HALFINDENT
;
1762 termp_ss_post(DECL_ARGS
)
1765 if (MDOC_HEAD
== n
->type
)
1772 termp_cd_pre(DECL_ARGS
)
1783 termp_in_pre(DECL_ARGS
)
1787 if (SEC_SYNOPSIS
== n
->sec
)
1788 term_word(p
, "#include");
1791 p
->flags
|= TERMP_NOSPACE
;
1798 termp_in_post(DECL_ARGS
)
1802 p
->flags
|= TERMP_NOSPACE
;
1806 if (SEC_SYNOPSIS
!= n
->sec
)
1811 * XXX Not entirely correct. If `.In foo bar' is specified in
1812 * the SYNOPSIS section, then it produces a single break after
1813 * the <foo>; mandoc asserts a vertical space. Since this
1814 * construction is rarely used, I think it's fine.
1816 if (n
->next
&& MDOC_In
!= n
->next
->tok
)
1823 termp_sp_pre(DECL_ARGS
)
1829 len
= n
->child
? atoi(n
->child
->string
) : 1;
1841 for (i
= 0; i
< len
; i
++)
1850 termp_brq_pre(DECL_ARGS
)
1853 if (MDOC_BODY
!= n
->type
)
1855 term_word(p
, "\\(lC");
1856 p
->flags
|= TERMP_NOSPACE
;
1863 termp_brq_post(DECL_ARGS
)
1866 if (MDOC_BODY
!= n
->type
)
1868 p
->flags
|= TERMP_NOSPACE
;
1869 term_word(p
, "\\(rC");
1875 termp_bq_pre(DECL_ARGS
)
1878 if (MDOC_BODY
!= n
->type
)
1880 term_word(p
, "\\(lB");
1881 p
->flags
|= TERMP_NOSPACE
;
1888 termp_bq_post(DECL_ARGS
)
1891 if (MDOC_BODY
!= n
->type
)
1893 p
->flags
|= TERMP_NOSPACE
;
1894 term_word(p
, "\\(rB");
1900 termp_pq_pre(DECL_ARGS
)
1903 if (MDOC_BODY
!= n
->type
)
1905 term_word(p
, "\\&(");
1906 p
->flags
|= TERMP_NOSPACE
;
1913 termp_pq_post(DECL_ARGS
)
1916 if (MDOC_BODY
!= n
->type
)
1924 termp_fo_pre(DECL_ARGS
)
1926 const struct mdoc_node
*nn
;
1928 if (MDOC_BODY
== n
->type
) {
1929 p
->flags
|= TERMP_NOSPACE
;
1931 p
->flags
|= TERMP_NOSPACE
;
1933 } else if (MDOC_HEAD
!= n
->type
)
1937 for (nn
= n
->child
; nn
; nn
= nn
->next
) {
1938 assert(MDOC_TEXT
== nn
->type
);
1939 term_word(p
, nn
->string
);
1949 termp_fo_post(DECL_ARGS
)
1952 if (MDOC_BODY
!= n
->type
)
1954 p
->flags
|= TERMP_NOSPACE
;
1956 p
->flags
|= TERMP_NOSPACE
;
1964 termp_bf_pre(DECL_ARGS
)
1966 const struct mdoc_node
*nn
;
1968 if (MDOC_HEAD
== n
->type
)
1970 else if (MDOC_BLOCK
!= n
->type
)
1973 if (NULL
== (nn
= n
->head
->child
)) {
1974 if (arg_hasattr(MDOC_Emphasis
, n
))
1976 else if (arg_hasattr(MDOC_Symbolic
, n
))
1982 assert(MDOC_TEXT
== nn
->type
);
1983 if (0 == strcmp("Em", nn
->string
))
1985 else if (0 == strcmp("Sy", nn
->string
))
1994 termp_sm_pre(DECL_ARGS
)
1997 assert(n
->child
&& MDOC_TEXT
== n
->child
->type
);
1998 if (0 == strcmp("on", n
->child
->string
)) {
1999 p
->flags
&= ~TERMP_NONOSPACE
;
2000 p
->flags
&= ~TERMP_NOSPACE
;
2002 p
->flags
|= TERMP_NONOSPACE
;
2010 termp_ap_pre(DECL_ARGS
)
2013 p
->flags
|= TERMP_NOSPACE
;
2014 term_word(p
, "\\(aq");
2015 p
->flags
|= TERMP_NOSPACE
;
2022 termp____post(DECL_ARGS
)
2025 p
->flags
|= TERMP_NOSPACE
;
2028 term_word(p
, "\\(rq");
2029 p
->flags
|= TERMP_NOSPACE
;
2034 term_word(p
, n
->next
? "," : ".");
2040 termp_lk_pre(DECL_ARGS
)
2042 const struct mdoc_node
*nn
;
2044 if (NULL
== (nn
= n
->child
->next
)) {
2050 term_word(p
, nn
->string
);
2051 p
->flags
|= TERMP_NOSPACE
;
2056 for (nn
= nn
->next
; nn
; nn
= nn
->next
)
2057 term_word(p
, nn
->string
);
2066 termp_under_pre(DECL_ARGS
)
2076 termp__t_pre(DECL_ARGS
)
2079 term_word(p
, "\\(lq");
2080 p
->flags
|= TERMP_NOSPACE
;