]>
git.cameronkatri.com Git - mandoc.git/blob - mdoc_term.c
1 /* $Id: mdoc_term.c,v 1.93 2009/10/19 15:18:30 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>
36 struct termpair
*ppair
;
41 #define DECL_ARGS struct termp *p, \
42 struct termpair *pair, \
43 const struct mdoc_meta *m, \
44 const struct mdoc_node *n
47 int (*pre
)(DECL_ARGS
);
48 void (*post
)(DECL_ARGS
);
51 static void termp____post(DECL_ARGS
);
52 static void termp_an_post(DECL_ARGS
);
53 static void termp_aq_post(DECL_ARGS
);
54 static void termp_bd_post(DECL_ARGS
);
55 static void termp_bl_post(DECL_ARGS
);
56 static void termp_bq_post(DECL_ARGS
);
57 static void termp_brq_post(DECL_ARGS
);
58 static void termp_bx_post(DECL_ARGS
);
59 static void termp_d1_post(DECL_ARGS
);
60 static void termp_dq_post(DECL_ARGS
);
61 static void termp_fd_post(DECL_ARGS
);
62 static void termp_fn_post(DECL_ARGS
);
63 static void termp_fo_post(DECL_ARGS
);
64 static void termp_ft_post(DECL_ARGS
);
65 static void termp_in_post(DECL_ARGS
);
66 static void termp_it_post(DECL_ARGS
);
67 static void termp_lb_post(DECL_ARGS
);
68 static void termp_op_post(DECL_ARGS
);
69 static void termp_pf_post(DECL_ARGS
);
70 static void termp_pq_post(DECL_ARGS
);
71 static void termp_qq_post(DECL_ARGS
);
72 static void termp_sh_post(DECL_ARGS
);
73 static void termp_sq_post(DECL_ARGS
);
74 static void termp_ss_post(DECL_ARGS
);
75 static void termp_vt_post(DECL_ARGS
);
77 static int termp__t_pre(DECL_ARGS
);
78 static int termp_an_pre(DECL_ARGS
);
79 static int termp_ap_pre(DECL_ARGS
);
80 static int termp_aq_pre(DECL_ARGS
);
81 static int termp_bd_pre(DECL_ARGS
);
82 static int termp_bf_pre(DECL_ARGS
);
83 static int termp_bold_pre(DECL_ARGS
);
84 static int termp_bq_pre(DECL_ARGS
);
85 static int termp_brq_pre(DECL_ARGS
);
86 static int termp_bt_pre(DECL_ARGS
);
87 static int termp_cd_pre(DECL_ARGS
);
88 static int termp_d1_pre(DECL_ARGS
);
89 static int termp_dq_pre(DECL_ARGS
);
90 static int termp_ex_pre(DECL_ARGS
);
91 static int termp_fa_pre(DECL_ARGS
);
92 static int termp_fl_pre(DECL_ARGS
);
93 static int termp_fn_pre(DECL_ARGS
);
94 static int termp_fo_pre(DECL_ARGS
);
95 static int termp_ft_pre(DECL_ARGS
);
96 static int termp_in_pre(DECL_ARGS
);
97 static int termp_it_pre(DECL_ARGS
);
98 static int termp_lk_pre(DECL_ARGS
);
99 static int termp_nd_pre(DECL_ARGS
);
100 static int termp_nm_pre(DECL_ARGS
);
101 static int termp_ns_pre(DECL_ARGS
);
102 static int termp_op_pre(DECL_ARGS
);
103 static int termp_pf_pre(DECL_ARGS
);
104 static int termp_pq_pre(DECL_ARGS
);
105 static int termp_qq_pre(DECL_ARGS
);
106 static int termp_rs_pre(DECL_ARGS
);
107 static int termp_rv_pre(DECL_ARGS
);
108 static int termp_sh_pre(DECL_ARGS
);
109 static int termp_sm_pre(DECL_ARGS
);
110 static int termp_sp_pre(DECL_ARGS
);
111 static int termp_sq_pre(DECL_ARGS
);
112 static int termp_ss_pre(DECL_ARGS
);
113 static int termp_under_pre(DECL_ARGS
);
114 static int termp_ud_pre(DECL_ARGS
);
115 static int termp_xr_pre(DECL_ARGS
);
116 static int termp_xx_pre(DECL_ARGS
);
118 static const struct termact termacts
[MDOC_MAX
] = {
119 { termp_ap_pre
, NULL
}, /* Ap */
120 { NULL
, NULL
}, /* Dd */
121 { NULL
, NULL
}, /* Dt */
122 { NULL
, NULL
}, /* Os */
123 { termp_sh_pre
, termp_sh_post
}, /* Sh */
124 { termp_ss_pre
, termp_ss_post
}, /* Ss */
125 { termp_sp_pre
, NULL
}, /* Pp */
126 { termp_d1_pre
, termp_d1_post
}, /* D1 */
127 { termp_d1_pre
, termp_d1_post
}, /* Dl */
128 { termp_bd_pre
, termp_bd_post
}, /* Bd */
129 { NULL
, NULL
}, /* Ed */
130 { NULL
, termp_bl_post
}, /* Bl */
131 { NULL
, NULL
}, /* El */
132 { termp_it_pre
, termp_it_post
}, /* It */
133 { NULL
, NULL
}, /* Ad */
134 { termp_an_pre
, termp_an_post
}, /* An */
135 { termp_under_pre
, NULL
}, /* Ar */
136 { termp_cd_pre
, NULL
}, /* Cd */
137 { termp_bold_pre
, NULL
}, /* Cm */
138 { NULL
, NULL
}, /* Dv */
139 { NULL
, NULL
}, /* Er */
140 { NULL
, NULL
}, /* Ev */
141 { termp_ex_pre
, NULL
}, /* Ex */
142 { termp_fa_pre
, NULL
}, /* Fa */
143 { termp_bold_pre
, termp_fd_post
}, /* Fd */
144 { termp_fl_pre
, NULL
}, /* Fl */
145 { termp_fn_pre
, termp_fn_post
}, /* Fn */
146 { termp_ft_pre
, termp_ft_post
}, /* Ft */
147 { termp_bold_pre
, NULL
}, /* Ic */
148 { termp_in_pre
, termp_in_post
}, /* In */
149 { NULL
, NULL
}, /* Li */
150 { termp_nd_pre
, NULL
}, /* Nd */
151 { termp_nm_pre
, NULL
}, /* Nm */
152 { termp_op_pre
, termp_op_post
}, /* Op */
153 { NULL
, NULL
}, /* Ot */
154 { termp_under_pre
, NULL
}, /* Pa */
155 { termp_rv_pre
, NULL
}, /* Rv */
156 { NULL
, NULL
}, /* St */
157 { termp_under_pre
, NULL
}, /* Va */
158 { termp_under_pre
, termp_vt_post
}, /* Vt */
159 { termp_xr_pre
, NULL
}, /* Xr */
160 { NULL
, termp____post
}, /* %A */
161 { termp_under_pre
, termp____post
}, /* %B */
162 { NULL
, termp____post
}, /* %D */
163 { termp_under_pre
, termp____post
}, /* %I */
164 { termp_under_pre
, termp____post
}, /* %J */
165 { NULL
, termp____post
}, /* %N */
166 { NULL
, termp____post
}, /* %O */
167 { NULL
, termp____post
}, /* %P */
168 { NULL
, termp____post
}, /* %R */
169 { termp__t_pre
, termp____post
}, /* %T */
170 { NULL
, termp____post
}, /* %V */
171 { NULL
, NULL
}, /* Ac */
172 { termp_aq_pre
, termp_aq_post
}, /* Ao */
173 { termp_aq_pre
, termp_aq_post
}, /* Aq */
174 { NULL
, NULL
}, /* At */
175 { NULL
, NULL
}, /* Bc */
176 { termp_bf_pre
, NULL
}, /* Bf */
177 { termp_bq_pre
, termp_bq_post
}, /* Bo */
178 { termp_bq_pre
, termp_bq_post
}, /* Bq */
179 { termp_xx_pre
, NULL
}, /* Bsx */
180 { NULL
, termp_bx_post
}, /* Bx */
181 { NULL
, NULL
}, /* Db */
182 { NULL
, NULL
}, /* Dc */
183 { termp_dq_pre
, termp_dq_post
}, /* Do */
184 { termp_dq_pre
, termp_dq_post
}, /* Dq */
185 { NULL
, NULL
}, /* Ec */
186 { NULL
, NULL
}, /* Ef */
187 { termp_under_pre
, NULL
}, /* Em */
188 { NULL
, NULL
}, /* Eo */
189 { termp_xx_pre
, NULL
}, /* Fx */
190 { termp_bold_pre
, NULL
}, /* Ms */ /* FIXME: convert to symbol? */
191 { NULL
, NULL
}, /* No */
192 { termp_ns_pre
, NULL
}, /* Ns */
193 { termp_xx_pre
, NULL
}, /* Nx */
194 { termp_xx_pre
, NULL
}, /* Ox */
195 { NULL
, NULL
}, /* Pc */
196 { termp_pf_pre
, termp_pf_post
}, /* Pf */
197 { termp_pq_pre
, termp_pq_post
}, /* Po */
198 { termp_pq_pre
, termp_pq_post
}, /* Pq */
199 { NULL
, NULL
}, /* Qc */
200 { termp_sq_pre
, termp_sq_post
}, /* Ql */
201 { termp_qq_pre
, termp_qq_post
}, /* Qo */
202 { termp_qq_pre
, termp_qq_post
}, /* Qq */
203 { NULL
, NULL
}, /* Re */
204 { termp_rs_pre
, NULL
}, /* Rs */
205 { NULL
, NULL
}, /* Sc */
206 { termp_sq_pre
, termp_sq_post
}, /* So */
207 { termp_sq_pre
, termp_sq_post
}, /* Sq */
208 { termp_sm_pre
, NULL
}, /* Sm */
209 { termp_under_pre
, NULL
}, /* Sx */
210 { termp_bold_pre
, NULL
}, /* Sy */
211 { NULL
, NULL
}, /* Tn */
212 { termp_xx_pre
, NULL
}, /* Ux */
213 { NULL
, NULL
}, /* Xc */
214 { NULL
, NULL
}, /* Xo */
215 { termp_fo_pre
, termp_fo_post
}, /* Fo */
216 { NULL
, NULL
}, /* Fc */
217 { termp_op_pre
, termp_op_post
}, /* Oo */
218 { NULL
, NULL
}, /* Oc */
219 { NULL
, NULL
}, /* Bk */
220 { NULL
, NULL
}, /* Ek */
221 { termp_bt_pre
, NULL
}, /* Bt */
222 { NULL
, NULL
}, /* Hf */
223 { NULL
, NULL
}, /* Fr */
224 { termp_ud_pre
, NULL
}, /* Ud */
225 { NULL
, termp_lb_post
}, /* Lb */
226 { termp_sp_pre
, NULL
}, /* Lp */
227 { termp_lk_pre
, NULL
}, /* Lk */
228 { termp_under_pre
, NULL
}, /* Mt */
229 { termp_brq_pre
, termp_brq_post
}, /* Brq */
230 { termp_brq_pre
, termp_brq_post
}, /* Bro */
231 { NULL
, NULL
}, /* Brc */
232 { NULL
, termp____post
}, /* %C */
233 { NULL
, NULL
}, /* Es */ /* TODO */
234 { NULL
, NULL
}, /* En */ /* TODO */
235 { termp_xx_pre
, NULL
}, /* Dx */
236 { NULL
, termp____post
}, /* %Q */
237 { termp_sp_pre
, NULL
}, /* br */
238 { termp_sp_pre
, NULL
}, /* sp */
242 extern size_t strlcpy(char *, const char *, size_t);
243 extern size_t strlcat(char *, const char *, size_t);
246 static size_t arg2width(const struct mdoc_argv
*, int);
247 static size_t arg2height(const struct mdoc_node
*);
248 static size_t arg2offs(const struct mdoc_argv
*);
250 static int arg_hasattr(int, const struct mdoc_node
*);
251 static int arg_getattrs(const int *, int *, size_t,
252 const struct mdoc_node
*);
253 static int arg_getattr(int, const struct mdoc_node
*);
254 static int arg_listtype(const struct mdoc_node
*);
255 static void print_bvspace(struct termp
*,
256 const struct mdoc_node
*,
257 const struct mdoc_node
*);
258 static void print_node(DECL_ARGS
);
259 static void print_head(DECL_ARGS
);
260 static void print_body(DECL_ARGS
);
261 static void print_foot(DECL_ARGS
);
265 terminal_mdoc(void *arg
, const struct mdoc
*mdoc
)
267 const struct mdoc_node
*n
;
268 const struct mdoc_meta
*m
;
271 p
= (struct termp
*)arg
;
273 if (NULL
== p
->symtab
)
275 case (TERMENC_ASCII
):
276 p
->symtab
= chars_init(CHARS_ASCII
);
286 print_head(p
, NULL
, m
, n
);
288 print_body(p
, NULL
, m
, n
->child
);
289 print_foot(p
, NULL
, m
, n
);
294 print_body(DECL_ARGS
)
297 print_node(p
, pair
, m
, n
);
299 print_body(p
, pair
, m
, n
->next
);
305 print_node(DECL_ARGS
)
307 int chld
, bold
, under
;
308 struct termpair npair
;
309 size_t offset
, rmargin
;
313 rmargin
= p
->rmargin
;
317 bzero(&npair
, sizeof(struct termpair
));
320 if (MDOC_TEXT
!= n
->type
) {
321 if (termacts
[n
->tok
].pre
)
322 chld
= (*termacts
[n
->tok
].pre
)(p
, &npair
, m
, n
);
324 term_word(p
, n
->string
);
325 if (chld
&& n
->child
)
326 print_body(p
, &npair
, m
, n
->child
);
329 * XXX - if bold/under were to span scopes, this wouldn't be
330 * possible, but because decoration is always in-scope, we can
331 * get away with this.
337 if (MDOC_TEXT
!= n
->type
)
338 if (termacts
[n
->tok
].post
)
339 (*termacts
[n
->tok
].post
)(p
, &npair
, m
, n
);
342 p
->rmargin
= rmargin
;
348 print_foot(DECL_ARGS
)
354 * Output the footer in new-groff style, that is, three columns
355 * with the middle being the manual date and flanking columns
356 * being the operating system:
361 if (NULL
== (buf
= malloc(p
->rmargin
)))
362 err(EXIT_FAILURE
, "malloc");
363 if (NULL
== (os
= malloc(p
->rmargin
)))
364 err(EXIT_FAILURE
, "malloc");
366 tm
= localtime(&m
->date
);
368 if (0 == strftime(buf
, p
->rmargin
, "%B %e, %Y", tm
))
369 err(EXIT_FAILURE
, "strftime");
371 (void)strlcpy(os
, m
->os
, p
->rmargin
);
376 p
->rmargin
= (p
->maxrmargin
- strlen(buf
) + 1) / 2;
377 p
->flags
|= TERMP_NOSPACE
| TERMP_NOBREAK
;
382 p
->offset
= p
->rmargin
;
383 p
->rmargin
= p
->maxrmargin
- strlen(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
;
406 /* FIXME: put in utility library. */
409 print_head(DECL_ARGS
)
413 p
->rmargin
= p
->maxrmargin
;
416 if (NULL
== (buf
= malloc(p
->rmargin
)))
417 err(EXIT_FAILURE
, "malloc");
418 if (NULL
== (title
= malloc(p
->rmargin
)))
419 err(EXIT_FAILURE
, "malloc");
422 * The header is strange. It has three components, which are
423 * really two with the first duplicated. It goes like this:
425 * IDENTIFIER TITLE IDENTIFIER
427 * The IDENTIFIER is NAME(SECTION), which is the command-name
428 * (if given, or "unknown" if not) followed by the manual page
429 * section. These are given in `Dt'. The TITLE is a free-form
430 * string depending on the manual volume. If not specified, it
431 * switches on the manual section.
435 (void)strlcpy(buf
, m
->vol
, p
->rmargin
);
438 (void)strlcat(buf
, " (", p
->rmargin
);
439 (void)strlcat(buf
, m
->arch
, p
->rmargin
);
440 (void)strlcat(buf
, ")", p
->rmargin
);
443 snprintf(title
, p
->rmargin
, "%s(%d)", m
->title
, m
->msec
);
446 p
->rmargin
= (p
->maxrmargin
- strlen(buf
) + 1) / 2;
447 p
->flags
|= TERMP_NOBREAK
| TERMP_NOSPACE
;
452 p
->offset
= p
->rmargin
;
453 p
->rmargin
= p
->maxrmargin
- strlen(title
);
454 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
459 p
->offset
= p
->rmargin
;
460 p
->rmargin
= p
->maxrmargin
;
461 p
->flags
&= ~TERMP_NOBREAK
;
462 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
468 p
->rmargin
= p
->maxrmargin
;
469 p
->flags
&= ~TERMP_NOSPACE
;
477 arg2height(const struct mdoc_node
*n
)
481 assert(MDOC_TEXT
== n
->type
);
483 if ( ! a2roffsu(n
->string
, &su
, SCALE_VS
))
484 SCALE_VS_INIT(&su
, strlen(n
->string
));
486 return(term_vspan(&su
));
491 arg2width(const struct mdoc_argv
*arg
, int pos
)
495 assert(arg
->value
[pos
]);
496 if ( ! a2roffsu(arg
->value
[pos
], &su
, SCALE_MAX
))
497 SCALE_HS_INIT(&su
, strlen(arg
->value
[pos
]));
500 return(term_hspan(&su
) + 2);
505 arg_listtype(const struct mdoc_node
*n
)
509 assert(MDOC_BLOCK
== n
->type
);
511 len
= (int)(n
->args
? n
->args
->argc
: 0);
513 for (i
= 0; i
< len
; i
++)
514 switch (n
->args
->argv
[i
].arg
) {
536 return(n
->args
->argv
[i
].arg
);
546 arg2offs(const struct mdoc_argv
*arg
)
550 if ('\0' == arg
->value
[0][0])
552 else if (0 == strcmp(arg
->value
[0], "left"))
554 else if (0 == strcmp(arg
->value
[0], "indent"))
556 else if (0 == strcmp(arg
->value
[0], "indent-two"))
557 return((INDENT
+ 1) * 2);
558 else if ( ! a2roffsu(arg
->value
[0], &su
, SCALE_MAX
))
559 SCALE_HS_INIT(&su
, strlen(arg
->value
[0]));
561 return(term_hspan(&su
));
566 arg_hasattr(int arg
, const struct mdoc_node
*n
)
569 return(-1 != arg_getattr(arg
, n
));
574 arg_getattr(int v
, const struct mdoc_node
*n
)
578 return(arg_getattrs(&v
, &val
, 1, n
) ? val
: -1);
583 arg_getattrs(const int *keys
, int *vals
,
584 size_t sz
, const struct mdoc_node
*n
)
591 for (k
= i
= 0; i
< (int)n
->args
->argc
; i
++)
592 for (j
= 0; j
< (int)sz
; j
++)
593 if (n
->args
->argv
[i
].arg
== keys
[j
]) {
602 print_bvspace(struct termp
*p
,
603 const struct mdoc_node
*bl
,
604 const struct mdoc_node
*n
)
606 const struct mdoc_node
*nn
;
609 if (arg_hasattr(MDOC_Compact
, bl
))
612 /* Do not vspace directly after Ss/Sh. */
614 for (nn
= n
; nn
; nn
= nn
->parent
) {
615 if (MDOC_BLOCK
!= nn
->type
)
617 if (MDOC_Ss
== nn
->tok
)
619 if (MDOC_Sh
== nn
->tok
)
621 if (NULL
== nn
->prev
)
626 /* A `-column' does not assert vspace within the list. */
628 if (MDOC_Bl
== bl
->tok
&& arg_hasattr(MDOC_Column
, bl
))
629 if (n
->prev
&& MDOC_It
== n
->prev
->tok
)
632 /* A `-diag' without body does not vspace. */
634 if (MDOC_Bl
== bl
->tok
&& arg_hasattr(MDOC_Diag
, bl
))
635 if (n
->prev
&& MDOC_It
== n
->prev
->tok
) {
636 assert(n
->prev
->body
);
637 if (NULL
== n
->prev
->body
->child
)
647 termp_dq_pre(DECL_ARGS
)
650 if (MDOC_BODY
!= n
->type
)
653 term_word(p
, "\\(lq");
654 p
->flags
|= TERMP_NOSPACE
;
661 termp_dq_post(DECL_ARGS
)
664 if (MDOC_BODY
!= n
->type
)
667 p
->flags
|= TERMP_NOSPACE
;
668 term_word(p
, "\\(rq");
674 termp_it_pre(DECL_ARGS
)
676 const struct mdoc_node
*bl
, *nn
;
678 int i
, type
, keys
[3], vals
[3];
679 size_t width
, offset
;
681 if (MDOC_BLOCK
== n
->type
) {
682 print_bvspace(p
, n
->parent
->parent
, n
);
686 bl
= n
->parent
->parent
->parent
;
688 /* Save parent attributes. */
690 pair
->flag
= p
->flags
;
692 /* Get list width and offset. */
694 keys
[0] = MDOC_Width
;
695 keys
[1] = MDOC_Offset
;
696 keys
[2] = MDOC_Column
;
698 vals
[0] = vals
[1] = vals
[2] = -1;
702 (void)arg_getattrs(keys
, vals
, 3, bl
);
704 type
= arg_listtype(bl
);
707 /* Calculate real width and offset. */
711 if (MDOC_BODY
== n
->type
)
714 * Work around groff's column handling. The offset is
715 * equal to the sum of all widths leading to the current
716 * column (plus the -offset value). If this column
717 * exceeds the stated number of columns, the width is
718 * set as 0, else it's the stated column width (later
719 * the 0 will be adjusted to default 10 or, if in the
720 * last column case, set to stretch to the margin).
722 for (i
= 0, nn
= n
->prev
; nn
&&
723 i
< (int)bl
->args
->argv
[vals
[2]].sz
;
726 (&bl
->args
->argv
[vals
[2]], i
);
728 /* Whether exceeds maximum column. */
729 if (i
< (int)bl
->args
->argv
[vals
[2]].sz
)
730 width
= arg2width(&bl
->args
->argv
[vals
[2]], i
);
735 offset
+= arg2offs(&bl
->args
->argv
[vals
[1]]);
739 width
= arg2width(&bl
->args
->argv
[vals
[0]], 0);
741 offset
+= arg2offs(&bl
->args
->argv
[vals
[1]]);
746 * List-type can override the width in the case of fixed-head
747 * values (bullet, dash/hyphen, enum). Tags need a non-zero
779 * Whitespace control. Inset bodies need an initial space,
780 * while diagonal bodies need two.
783 p
->flags
|= TERMP_NOSPACE
;
787 if (MDOC_BODY
== n
->type
)
788 term_word(p
, "\\ \\ ");
791 if (MDOC_BODY
== n
->type
)
798 p
->flags
|= TERMP_NOSPACE
;
802 if (MDOC_HEAD
== n
->type
)
810 * Pad and break control. This is the tricker part. Lists with
811 * set right-margins for the head get TERMP_NOBREAK because, if
812 * they overrun the margin, they wrap to the new margin.
813 * Correspondingly, the body for these types don't left-pad, as
814 * the head will pad out to to the right.
825 if (MDOC_HEAD
== n
->type
)
826 p
->flags
|= TERMP_NOBREAK
;
828 p
->flags
|= TERMP_NOLPAD
;
831 if (MDOC_HEAD
== n
->type
)
832 p
->flags
|= TERMP_NOBREAK
;
834 p
->flags
|= TERMP_NOLPAD
;
836 if (MDOC_HEAD
!= n
->type
)
840 * This is ugly. If `-hang' is specified and the body
841 * is a `Bl' or `Bd', then we want basically to nullify
842 * the "overstep" effect in term_flushln() and treat
843 * this as a `-ohang' list instead.
845 if (n
->next
->child
&&
846 (MDOC_Bl
== n
->next
->child
->tok
||
847 MDOC_Bd
== n
->next
->child
->tok
)) {
848 p
->flags
&= ~TERMP_NOBREAK
;
849 p
->flags
&= ~TERMP_NOLPAD
;
851 p
->flags
|= TERMP_HANG
;
854 if (MDOC_HEAD
== n
->type
)
855 p
->flags
|= TERMP_NOBREAK
| TERMP_TWOSPACE
;
857 p
->flags
|= TERMP_NOLPAD
;
859 if (MDOC_HEAD
!= n
->type
)
861 if (NULL
== n
->next
|| NULL
== n
->next
->child
)
862 p
->flags
|= TERMP_DANGLE
;
865 if (MDOC_HEAD
== n
->type
) {
867 if (MDOC_BODY
== n
->next
->type
)
868 p
->flags
&= ~TERMP_NOBREAK
;
870 p
->flags
|= TERMP_NOBREAK
;
872 p
->flags
|= TERMP_NOLPAD
;
876 if (MDOC_HEAD
== n
->type
)
877 p
->flags
|= TERMP_NOBREAK
;
884 * Margin control. Set-head-width lists have their right
885 * margins shortened. The body for these lists has the offset
886 * necessarily lengthened. Everybody gets the offset.
894 * Same stipulation as above, regarding `-hang'. We
895 * don't want to recalculate rmargin and offsets when
896 * using `Bd' or `Bl' within `-hang' overstep lists.
898 if (MDOC_HEAD
== n
->type
&& n
->next
->child
&&
899 (MDOC_Bl
== n
->next
->child
->tok
||
900 MDOC_Bd
== n
->next
->child
->tok
))
913 if (MDOC_HEAD
== n
->type
)
914 p
->rmargin
= p
->offset
+ width
;
920 p
->rmargin
= p
->offset
+ width
;
922 * XXX - this behaviour is not documented: the
923 * right-most column is filled to the right margin.
925 if (MDOC_HEAD
== n
->type
&&
926 MDOC_BODY
== n
->next
->type
)
927 p
->rmargin
= p
->maxrmargin
;
934 * The dash, hyphen, bullet and enum lists all have a special
935 * HEAD character (temporarily bold, in some cases).
938 if (MDOC_HEAD
== n
->type
)
942 term_word(p
, "\\[bu]");
949 term_word(p
, "\\(hy");
953 (pair
->ppair
->ppair
->count
)++;
954 (void)snprintf(buf
, sizeof(buf
), "%d.",
955 pair
->ppair
->ppair
->count
);
963 * If we're not going to process our children, indicate so here.
976 if (MDOC_HEAD
== n
->type
)
980 if (MDOC_BODY
== n
->type
)
993 termp_it_post(DECL_ARGS
)
997 if (MDOC_BODY
!= n
->type
&& MDOC_HEAD
!= n
->type
)
1000 type
= arg_listtype(n
->parent
->parent
->parent
);
1009 if (MDOC_BODY
== n
->type
)
1013 if (MDOC_HEAD
== n
->type
)
1021 p
->flags
= pair
->flag
;
1027 termp_nm_pre(DECL_ARGS
)
1030 if (SEC_SYNOPSIS
== n
->sec
)
1033 if (NULL
== n
->child
)
1034 term_word(p
, m
->name
);
1041 termp_fl_pre(DECL_ARGS
)
1045 term_word(p
, "\\-");
1046 p
->flags
|= TERMP_NOSPACE
;
1053 termp_an_pre(DECL_ARGS
)
1056 if (NULL
== n
->child
)
1060 * If not in the AUTHORS section, `An -split' will cause
1061 * newlines to occur before the author name. If in the AUTHORS
1062 * section, by default, the first `An' invocation is nosplit,
1063 * then all subsequent ones, regardless of whether interspersed
1064 * with other macros/text, are split. -split, in this case,
1065 * will override the condition of the implied first -nosplit.
1068 if (n
->sec
== SEC_AUTHORS
) {
1069 if ( ! (TERMP_ANPREC
& p
->flags
)) {
1070 if (TERMP_SPLIT
& p
->flags
)
1074 if (TERMP_NOSPLIT
& p
->flags
)
1080 if (TERMP_SPLIT
& p
->flags
)
1089 termp_an_post(DECL_ARGS
)
1093 if (SEC_AUTHORS
== n
->sec
)
1094 p
->flags
|= TERMP_ANPREC
;
1098 if (arg_getattr(MDOC_Split
, n
) > -1) {
1099 p
->flags
&= ~TERMP_NOSPLIT
;
1100 p
->flags
|= TERMP_SPLIT
;
1102 p
->flags
&= ~TERMP_SPLIT
;
1103 p
->flags
|= TERMP_NOSPLIT
;
1111 termp_ns_pre(DECL_ARGS
)
1114 p
->flags
|= TERMP_NOSPACE
;
1121 termp_rs_pre(DECL_ARGS
)
1124 if (SEC_SEE_ALSO
!= n
->sec
)
1126 if (MDOC_BLOCK
== n
->type
&& n
->prev
)
1134 termp_rv_pre(DECL_ARGS
)
1136 const struct mdoc_node
*nn
;
1139 term_word(p
, "The");
1141 for (nn
= n
->child
; nn
; nn
= nn
->next
) {
1143 term_word(p
, nn
->string
);
1145 p
->flags
|= TERMP_NOSPACE
;
1146 if (nn
->next
&& NULL
== nn
->next
->next
)
1147 term_word(p
, "(), and");
1149 term_word(p
, "(),");
1155 term_word(p
, "functions return");
1157 term_word(p
, "function returns");
1159 term_word(p
, "the value 0 if successful; otherwise the value "
1160 "-1 is returned and the global variable");
1163 term_word(p
, "errno");
1166 term_word(p
, "is set to indicate the error.");
1174 termp_ex_pre(DECL_ARGS
)
1176 const struct mdoc_node
*nn
;
1178 term_word(p
, "The");
1180 for (nn
= n
->child
; nn
; nn
= nn
->next
) {
1182 term_word(p
, nn
->string
);
1184 p
->flags
|= TERMP_NOSPACE
;
1185 if (nn
->next
&& NULL
== nn
->next
->next
)
1186 term_word(p
, ", and");
1190 p
->flags
&= ~TERMP_NOSPACE
;
1194 term_word(p
, "utilities exit");
1196 term_word(p
, "utility exits");
1198 term_word(p
, "0 on success, and >0 if an error occurs.");
1206 termp_nd_pre(DECL_ARGS
)
1209 if (MDOC_BODY
!= n
->type
)
1212 #if defined(__OpenBSD__) || defined(__linux__)
1213 term_word(p
, "\\(en");
1215 term_word(p
, "\\(em");
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 assert(n
->child
&& MDOC_TEXT
== n
->child
->type
);
1252 term_word(p
, nn
->string
);
1253 if (NULL
== (nn
= nn
->next
))
1255 p
->flags
|= TERMP_NOSPACE
;
1257 p
->flags
|= TERMP_NOSPACE
;
1258 term_word(p
, nn
->string
);
1259 p
->flags
|= TERMP_NOSPACE
;
1268 termp_vt_post(DECL_ARGS
)
1271 if (n
->sec
!= SEC_SYNOPSIS
)
1273 if (n
->next
&& MDOC_Vt
== n
->next
->tok
)
1282 termp_bold_pre(DECL_ARGS
)
1292 termp_fd_post(DECL_ARGS
)
1295 if (n
->sec
!= SEC_SYNOPSIS
)
1299 if (n
->next
&& MDOC_Fd
!= n
->next
->tok
)
1306 termp_sh_pre(DECL_ARGS
)
1309 /* No vspace between consecutive `Sh' calls. */
1313 if (n
->prev
&& MDOC_Sh
== n
->prev
->tok
)
1314 if (NULL
== n
->prev
->body
->child
)
1333 termp_sh_post(DECL_ARGS
)
1352 termp_op_pre(DECL_ARGS
)
1357 term_word(p
, "\\(lB");
1358 p
->flags
|= TERMP_NOSPACE
;
1369 termp_bt_pre(DECL_ARGS
)
1372 term_word(p
, "is currently in beta test.");
1379 termp_lb_post(DECL_ARGS
)
1382 if (SEC_LIBRARY
== n
->sec
)
1389 termp_ud_pre(DECL_ARGS
)
1392 term_word(p
, "currently under development.");
1399 termp_d1_pre(DECL_ARGS
)
1402 if (MDOC_BLOCK
!= n
->type
)
1405 p
->offset
+= (INDENT
+ 1);
1412 termp_d1_post(DECL_ARGS
)
1415 if (MDOC_BLOCK
!= n
->type
)
1423 termp_aq_pre(DECL_ARGS
)
1426 if (MDOC_BODY
!= n
->type
)
1428 term_word(p
, "\\(la");
1429 p
->flags
|= TERMP_NOSPACE
;
1436 termp_aq_post(DECL_ARGS
)
1439 if (MDOC_BODY
!= n
->type
)
1441 p
->flags
|= TERMP_NOSPACE
;
1442 term_word(p
, "\\(ra");
1448 termp_ft_pre(DECL_ARGS
)
1451 if (SEC_SYNOPSIS
== n
->sec
)
1452 if (n
->prev
&& MDOC_Fo
== n
->prev
->tok
)
1461 termp_ft_post(DECL_ARGS
)
1464 if (SEC_SYNOPSIS
== n
->sec
)
1471 termp_fn_pre(DECL_ARGS
)
1473 const struct mdoc_node
*nn
;
1476 term_word(p
, n
->child
->string
);
1479 p
->flags
|= TERMP_NOSPACE
;
1482 for (nn
= n
->child
->next
; nn
; nn
= nn
->next
) {
1484 term_word(p
, nn
->string
);
1492 if (SEC_SYNOPSIS
== n
->sec
)
1501 termp_fn_post(DECL_ARGS
)
1504 if (n
->sec
== SEC_SYNOPSIS
&& n
->next
)
1511 termp_fa_pre(DECL_ARGS
)
1513 const struct mdoc_node
*nn
;
1515 if (n
->parent
->tok
!= MDOC_Fo
) {
1520 for (nn
= n
->child
; nn
; nn
= nn
->next
) {
1522 term_word(p
, nn
->string
);
1528 if (n
->child
&& n
->next
&& n
->next
->tok
== MDOC_Fa
)
1537 termp_bd_pre(DECL_ARGS
)
1540 const struct mdoc_node
*nn
;
1542 if (MDOC_BLOCK
== n
->type
) {
1543 print_bvspace(p
, n
, n
);
1545 } else if (MDOC_BODY
!= n
->type
)
1550 for (type
= -1, i
= 0; i
< (int)nn
->args
->argc
; i
++) {
1551 switch (nn
->args
->argv
[i
].arg
) {
1552 case (MDOC_Centred
):
1558 case (MDOC_Unfilled
):
1560 case (MDOC_Literal
):
1561 type
= nn
->args
->argv
[i
].arg
;
1564 p
->offset
+= arg2offs(&nn
->args
->argv
[i
]);
1572 * If -ragged or -filled are specified, the block does nothing
1573 * but change the indentation. If -unfilled or -literal are
1574 * specified, text is printed exactly as entered in the display:
1575 * for macro lines, a newline is appended to the line. Blank
1576 * lines are allowed.
1580 if (MDOC_Literal
!= type
&& MDOC_Unfilled
!= type
)
1583 for (nn
= n
->child
; nn
; nn
= nn
->next
) {
1584 p
->flags
|= TERMP_NOSPACE
;
1585 print_node(p
, pair
, m
, nn
);
1586 if (NULL
== nn
->next
)
1588 if (nn
->prev
&& nn
->prev
->line
< nn
->line
)
1590 else if (NULL
== nn
->prev
)
1600 termp_bd_post(DECL_ARGS
)
1603 if (MDOC_BODY
!= n
->type
)
1605 p
->flags
|= TERMP_NOSPACE
;
1612 termp_qq_pre(DECL_ARGS
)
1615 if (MDOC_BODY
!= n
->type
)
1618 p
->flags
|= TERMP_NOSPACE
;
1625 termp_qq_post(DECL_ARGS
)
1628 if (MDOC_BODY
!= n
->type
)
1630 p
->flags
|= TERMP_NOSPACE
;
1637 termp_bx_post(DECL_ARGS
)
1641 p
->flags
|= TERMP_NOSPACE
;
1642 term_word(p
, "BSD");
1648 termp_xx_pre(DECL_ARGS
)
1658 pp
= "DragonFlyBSD";
1684 termp_sq_pre(DECL_ARGS
)
1687 if (MDOC_BODY
!= n
->type
)
1689 term_word(p
, "\\(oq");
1690 p
->flags
|= TERMP_NOSPACE
;
1697 termp_sq_post(DECL_ARGS
)
1700 if (MDOC_BODY
!= n
->type
)
1702 p
->flags
|= TERMP_NOSPACE
;
1703 term_word(p
, "\\(aq");
1709 termp_pf_pre(DECL_ARGS
)
1712 p
->flags
|= TERMP_IGNDELIM
;
1719 termp_pf_post(DECL_ARGS
)
1722 p
->flags
&= ~TERMP_IGNDELIM
;
1723 p
->flags
|= TERMP_NOSPACE
;
1729 termp_ss_pre(DECL_ARGS
)
1740 p
->offset
= HALFINDENT
;
1752 termp_ss_post(DECL_ARGS
)
1755 if (MDOC_HEAD
== n
->type
)
1762 termp_cd_pre(DECL_ARGS
)
1773 termp_in_pre(DECL_ARGS
)
1777 if (SEC_SYNOPSIS
== n
->sec
)
1778 term_word(p
, "#include");
1781 p
->flags
|= TERMP_NOSPACE
;
1788 termp_in_post(DECL_ARGS
)
1792 p
->flags
|= TERMP_NOSPACE
;
1796 if (SEC_SYNOPSIS
!= n
->sec
)
1801 * XXX Not entirely correct. If `.In foo bar' is specified in
1802 * the SYNOPSIS section, then it produces a single break after
1803 * the <foo>; mandoc asserts a vertical space. Since this
1804 * construction is rarely used, I think it's fine.
1806 if (n
->next
&& MDOC_In
!= n
->next
->tok
)
1813 termp_sp_pre(DECL_ARGS
)
1819 len
= n
->child
? arg2height(n
->child
) : 1;
1831 for (i
= 0; i
< len
; i
++)
1840 termp_brq_pre(DECL_ARGS
)
1843 if (MDOC_BODY
!= n
->type
)
1845 term_word(p
, "\\(lC");
1846 p
->flags
|= TERMP_NOSPACE
;
1853 termp_brq_post(DECL_ARGS
)
1856 if (MDOC_BODY
!= n
->type
)
1858 p
->flags
|= TERMP_NOSPACE
;
1859 term_word(p
, "\\(rC");
1865 termp_bq_pre(DECL_ARGS
)
1868 if (MDOC_BODY
!= n
->type
)
1870 term_word(p
, "\\(lB");
1871 p
->flags
|= TERMP_NOSPACE
;
1878 termp_bq_post(DECL_ARGS
)
1881 if (MDOC_BODY
!= n
->type
)
1883 p
->flags
|= TERMP_NOSPACE
;
1884 term_word(p
, "\\(rB");
1890 termp_pq_pre(DECL_ARGS
)
1893 if (MDOC_BODY
!= n
->type
)
1895 term_word(p
, "\\&(");
1896 p
->flags
|= TERMP_NOSPACE
;
1903 termp_pq_post(DECL_ARGS
)
1906 if (MDOC_BODY
!= n
->type
)
1914 termp_fo_pre(DECL_ARGS
)
1916 const struct mdoc_node
*nn
;
1918 if (MDOC_BODY
== n
->type
) {
1919 p
->flags
|= TERMP_NOSPACE
;
1921 p
->flags
|= TERMP_NOSPACE
;
1923 } else if (MDOC_HEAD
!= n
->type
)
1927 for (nn
= n
->child
; nn
; nn
= nn
->next
) {
1928 assert(MDOC_TEXT
== nn
->type
);
1929 term_word(p
, nn
->string
);
1939 termp_fo_post(DECL_ARGS
)
1942 if (MDOC_BODY
!= n
->type
)
1944 p
->flags
|= TERMP_NOSPACE
;
1946 p
->flags
|= TERMP_NOSPACE
;
1954 termp_bf_pre(DECL_ARGS
)
1956 const struct mdoc_node
*nn
;
1958 if (MDOC_HEAD
== n
->type
)
1960 else if (MDOC_BLOCK
!= n
->type
)
1963 if (NULL
== (nn
= n
->head
->child
)) {
1964 if (arg_hasattr(MDOC_Emphasis
, n
))
1966 else if (arg_hasattr(MDOC_Symbolic
, n
))
1972 assert(MDOC_TEXT
== nn
->type
);
1973 if (0 == strcmp("Em", nn
->string
))
1975 else if (0 == strcmp("Sy", nn
->string
))
1984 termp_sm_pre(DECL_ARGS
)
1987 assert(n
->child
&& MDOC_TEXT
== n
->child
->type
);
1988 if (0 == strcmp("on", n
->child
->string
)) {
1989 p
->flags
&= ~TERMP_NONOSPACE
;
1990 p
->flags
&= ~TERMP_NOSPACE
;
1992 p
->flags
|= TERMP_NONOSPACE
;
2000 termp_ap_pre(DECL_ARGS
)
2003 p
->flags
|= TERMP_NOSPACE
;
2004 term_word(p
, "\\(aq");
2005 p
->flags
|= TERMP_NOSPACE
;
2012 termp____post(DECL_ARGS
)
2015 p
->flags
|= TERMP_NOSPACE
;
2018 term_word(p
, "\\(rq");
2019 p
->flags
|= TERMP_NOSPACE
;
2024 term_word(p
, n
->next
? "," : ".");
2030 termp_lk_pre(DECL_ARGS
)
2032 const struct mdoc_node
*nn
;
2034 if (NULL
== (nn
= n
->child
->next
)) {
2040 term_word(p
, nn
->string
);
2041 p
->flags
|= TERMP_NOSPACE
;
2046 for (nn
= nn
->next
; nn
; nn
= nn
->next
)
2047 term_word(p
, nn
->string
);
2056 termp_under_pre(DECL_ARGS
)
2066 termp__t_pre(DECL_ARGS
)
2069 term_word(p
, "\\(lq");
2070 p
->flags
|= TERMP_NOSPACE
;