]>
git.cameronkatri.com Git - mandoc.git/blob - mdoc_term.c
1 /* $Id: mdoc_term.c,v 1.95 2009/10/24 05:52:14 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 */
239 { NULL
, termp____post
}, /* %U */
243 extern size_t strlcpy(char *, const char *, size_t);
244 extern size_t strlcat(char *, const char *, size_t);
247 static size_t arg2width(const struct mdoc_argv
*, int);
248 static size_t arg2height(const struct mdoc_node
*);
249 static size_t arg2offs(const struct mdoc_argv
*);
251 static int arg_hasattr(int, const struct mdoc_node
*);
252 static int arg_getattrs(const int *, int *, size_t,
253 const struct mdoc_node
*);
254 static int arg_getattr(int, const struct mdoc_node
*);
255 static int arg_listtype(const struct mdoc_node
*);
256 static void print_bvspace(struct termp
*,
257 const struct mdoc_node
*,
258 const struct mdoc_node
*);
259 static void print_node(DECL_ARGS
);
260 static void print_head(DECL_ARGS
);
261 static void print_body(DECL_ARGS
);
262 static void print_foot(DECL_ARGS
);
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_head(p
, NULL
, m
, n
);
289 print_body(p
, NULL
, m
, n
->child
);
290 print_foot(p
, NULL
, m
, n
);
295 print_body(DECL_ARGS
)
298 print_node(p
, pair
, m
, n
);
300 print_body(p
, pair
, m
, n
->next
);
306 print_node(DECL_ARGS
)
308 int chld
, bold
, under
;
309 struct termpair npair
;
310 size_t offset
, rmargin
;
314 rmargin
= p
->rmargin
;
318 bzero(&npair
, 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
);
326 if (chld
&& n
->child
)
327 print_body(p
, &npair
, m
, n
->child
);
330 * XXX - if bold/under were to span scopes, this wouldn't be
331 * possible, but because decoration is always in-scope, we can
332 * get away with this.
338 if (MDOC_TEXT
!= n
->type
)
339 if (termacts
[n
->tok
].post
)
340 (*termacts
[n
->tok
].post
)(p
, &npair
, m
, n
);
343 p
->rmargin
= rmargin
;
349 print_foot(DECL_ARGS
)
355 * Output the footer in new-groff style, that is, three columns
356 * with the middle being the manual date and flanking columns
357 * being the operating system:
362 if (NULL
== (os
= malloc(p
->rmargin
)))
363 err(EXIT_FAILURE
, "malloc");
365 time2a(m
->date
, buf
, DATESIZ
);
367 (void)strlcpy(os
, m
->os
, p
->rmargin
);
372 p
->rmargin
= (p
->maxrmargin
- strlen(buf
) + 1) / 2;
373 p
->flags
|= TERMP_NOSPACE
| TERMP_NOBREAK
;
378 p
->offset
= p
->rmargin
;
379 p
->rmargin
= p
->maxrmargin
- strlen(os
);
380 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
385 p
->offset
= p
->rmargin
;
386 p
->rmargin
= p
->maxrmargin
;
387 p
->flags
&= ~TERMP_NOBREAK
;
388 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
394 p
->rmargin
= p
->maxrmargin
;
401 /* FIXME: put in utility library. */
404 print_head(DECL_ARGS
)
408 p
->rmargin
= p
->maxrmargin
;
411 if (NULL
== (buf
= malloc(p
->rmargin
)))
412 err(EXIT_FAILURE
, "malloc");
413 if (NULL
== (title
= malloc(p
->rmargin
)))
414 err(EXIT_FAILURE
, "malloc");
417 * The header is strange. It has three components, which are
418 * really two with the first duplicated. It goes like this:
420 * IDENTIFIER TITLE IDENTIFIER
422 * The IDENTIFIER is NAME(SECTION), which is the command-name
423 * (if given, or "unknown" if not) followed by the manual page
424 * section. These are given in `Dt'. The TITLE is a free-form
425 * string depending on the manual volume. If not specified, it
426 * switches on the manual section.
430 (void)strlcpy(buf
, m
->vol
, p
->rmargin
);
433 (void)strlcat(buf
, " (", p
->rmargin
);
434 (void)strlcat(buf
, m
->arch
, p
->rmargin
);
435 (void)strlcat(buf
, ")", p
->rmargin
);
438 snprintf(title
, p
->rmargin
, "%s(%d)", m
->title
, m
->msec
);
441 p
->rmargin
= (p
->maxrmargin
- strlen(buf
) + 1) / 2;
442 p
->flags
|= TERMP_NOBREAK
| TERMP_NOSPACE
;
447 p
->offset
= p
->rmargin
;
448 p
->rmargin
= p
->maxrmargin
- strlen(title
);
449 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
454 p
->offset
= p
->rmargin
;
455 p
->rmargin
= p
->maxrmargin
;
456 p
->flags
&= ~TERMP_NOBREAK
;
457 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
463 p
->rmargin
= p
->maxrmargin
;
464 p
->flags
&= ~TERMP_NOSPACE
;
472 arg2height(const struct mdoc_node
*n
)
476 assert(MDOC_TEXT
== n
->type
);
478 if ( ! a2roffsu(n
->string
, &su
, SCALE_VS
))
479 SCALE_VS_INIT(&su
, strlen(n
->string
));
481 return(term_vspan(&su
));
486 arg2width(const struct mdoc_argv
*arg
, int pos
)
490 assert(arg
->value
[pos
]);
491 if ( ! a2roffsu(arg
->value
[pos
], &su
, SCALE_MAX
))
492 SCALE_HS_INIT(&su
, strlen(arg
->value
[pos
]));
495 return(term_hspan(&su
) + 2);
500 arg_listtype(const struct mdoc_node
*n
)
504 assert(MDOC_BLOCK
== n
->type
);
506 len
= (int)(n
->args
? n
->args
->argc
: 0);
508 for (i
= 0; i
< len
; i
++)
509 switch (n
->args
->argv
[i
].arg
) {
531 return(n
->args
->argv
[i
].arg
);
541 arg2offs(const struct mdoc_argv
*arg
)
545 if ('\0' == arg
->value
[0][0])
547 else if (0 == strcmp(arg
->value
[0], "left"))
549 else if (0 == strcmp(arg
->value
[0], "indent"))
551 else if (0 == strcmp(arg
->value
[0], "indent-two"))
552 return((INDENT
+ 1) * 2);
553 else if ( ! a2roffsu(arg
->value
[0], &su
, SCALE_MAX
))
554 SCALE_HS_INIT(&su
, strlen(arg
->value
[0]));
556 return(term_hspan(&su
));
561 arg_hasattr(int arg
, const struct mdoc_node
*n
)
564 return(-1 != arg_getattr(arg
, n
));
569 arg_getattr(int v
, const struct mdoc_node
*n
)
573 return(arg_getattrs(&v
, &val
, 1, n
) ? val
: -1);
578 arg_getattrs(const int *keys
, int *vals
,
579 size_t sz
, const struct mdoc_node
*n
)
586 for (k
= i
= 0; i
< (int)n
->args
->argc
; i
++)
587 for (j
= 0; j
< (int)sz
; j
++)
588 if (n
->args
->argv
[i
].arg
== keys
[j
]) {
597 print_bvspace(struct termp
*p
,
598 const struct mdoc_node
*bl
,
599 const struct mdoc_node
*n
)
601 const struct mdoc_node
*nn
;
604 if (arg_hasattr(MDOC_Compact
, bl
))
607 /* Do not vspace directly after Ss/Sh. */
609 for (nn
= n
; nn
; nn
= nn
->parent
) {
610 if (MDOC_BLOCK
!= nn
->type
)
612 if (MDOC_Ss
== nn
->tok
)
614 if (MDOC_Sh
== nn
->tok
)
616 if (NULL
== nn
->prev
)
621 /* A `-column' does not assert vspace within the list. */
623 if (MDOC_Bl
== bl
->tok
&& arg_hasattr(MDOC_Column
, bl
))
624 if (n
->prev
&& MDOC_It
== n
->prev
->tok
)
627 /* A `-diag' without body does not vspace. */
629 if (MDOC_Bl
== bl
->tok
&& arg_hasattr(MDOC_Diag
, bl
))
630 if (n
->prev
&& MDOC_It
== n
->prev
->tok
) {
631 assert(n
->prev
->body
);
632 if (NULL
== n
->prev
->body
->child
)
642 termp_dq_pre(DECL_ARGS
)
645 if (MDOC_BODY
!= n
->type
)
648 term_word(p
, "\\(lq");
649 p
->flags
|= TERMP_NOSPACE
;
656 termp_dq_post(DECL_ARGS
)
659 if (MDOC_BODY
!= n
->type
)
662 p
->flags
|= TERMP_NOSPACE
;
663 term_word(p
, "\\(rq");
669 termp_it_pre(DECL_ARGS
)
671 const struct mdoc_node
*bl
, *nn
;
673 int i
, type
, keys
[3], vals
[3];
674 size_t width
, offset
;
676 if (MDOC_BLOCK
== n
->type
) {
677 print_bvspace(p
, n
->parent
->parent
, n
);
681 bl
= n
->parent
->parent
->parent
;
683 /* Save parent attributes. */
685 pair
->flag
= p
->flags
;
687 /* Get list width and offset. */
689 keys
[0] = MDOC_Width
;
690 keys
[1] = MDOC_Offset
;
691 keys
[2] = MDOC_Column
;
693 vals
[0] = vals
[1] = vals
[2] = -1;
697 (void)arg_getattrs(keys
, vals
, 3, bl
);
699 type
= arg_listtype(bl
);
702 /* Calculate real width and offset. */
706 if (MDOC_BODY
== n
->type
)
709 * Work around groff's column handling. The offset is
710 * equal to the sum of all widths leading to the current
711 * column (plus the -offset value). If this column
712 * exceeds the stated number of columns, the width is
713 * set as 0, else it's the stated column width (later
714 * the 0 will be adjusted to default 10 or, if in the
715 * last column case, set to stretch to the margin).
717 for (i
= 0, nn
= n
->prev
; nn
&&
718 i
< (int)bl
->args
->argv
[vals
[2]].sz
;
721 (&bl
->args
->argv
[vals
[2]], i
);
723 /* Whether exceeds maximum column. */
724 if (i
< (int)bl
->args
->argv
[vals
[2]].sz
)
725 width
= arg2width(&bl
->args
->argv
[vals
[2]], i
);
730 offset
+= arg2offs(&bl
->args
->argv
[vals
[1]]);
734 width
= arg2width(&bl
->args
->argv
[vals
[0]], 0);
736 offset
+= arg2offs(&bl
->args
->argv
[vals
[1]]);
741 * List-type can override the width in the case of fixed-head
742 * values (bullet, dash/hyphen, enum). Tags need a non-zero
774 * Whitespace control. Inset bodies need an initial space,
775 * while diagonal bodies need two.
778 p
->flags
|= TERMP_NOSPACE
;
782 if (MDOC_BODY
== n
->type
)
783 term_word(p
, "\\ \\ ");
786 if (MDOC_BODY
== n
->type
)
793 p
->flags
|= TERMP_NOSPACE
;
797 if (MDOC_HEAD
== n
->type
)
805 * Pad and break control. This is the tricker part. Lists with
806 * set right-margins for the head get TERMP_NOBREAK because, if
807 * they overrun the margin, they wrap to the new margin.
808 * Correspondingly, the body for these types don't left-pad, as
809 * the head will pad out to to the right.
820 if (MDOC_HEAD
== n
->type
)
821 p
->flags
|= TERMP_NOBREAK
;
823 p
->flags
|= TERMP_NOLPAD
;
826 if (MDOC_HEAD
== n
->type
)
827 p
->flags
|= TERMP_NOBREAK
;
829 p
->flags
|= TERMP_NOLPAD
;
831 if (MDOC_HEAD
!= n
->type
)
835 * This is ugly. If `-hang' is specified and the body
836 * is a `Bl' or `Bd', then we want basically to nullify
837 * the "overstep" effect in term_flushln() and treat
838 * this as a `-ohang' list instead.
840 if (n
->next
->child
&&
841 (MDOC_Bl
== n
->next
->child
->tok
||
842 MDOC_Bd
== n
->next
->child
->tok
)) {
843 p
->flags
&= ~TERMP_NOBREAK
;
844 p
->flags
&= ~TERMP_NOLPAD
;
846 p
->flags
|= TERMP_HANG
;
849 if (MDOC_HEAD
== n
->type
)
850 p
->flags
|= TERMP_NOBREAK
| TERMP_TWOSPACE
;
852 p
->flags
|= TERMP_NOLPAD
;
854 if (MDOC_HEAD
!= n
->type
)
856 if (NULL
== n
->next
|| NULL
== n
->next
->child
)
857 p
->flags
|= TERMP_DANGLE
;
860 if (MDOC_HEAD
== n
->type
) {
862 if (MDOC_BODY
== n
->next
->type
)
863 p
->flags
&= ~TERMP_NOBREAK
;
865 p
->flags
|= TERMP_NOBREAK
;
867 p
->flags
|= TERMP_NOLPAD
;
871 if (MDOC_HEAD
== n
->type
)
872 p
->flags
|= TERMP_NOBREAK
;
879 * Margin control. Set-head-width lists have their right
880 * margins shortened. The body for these lists has the offset
881 * necessarily lengthened. Everybody gets the offset.
889 * Same stipulation as above, regarding `-hang'. We
890 * don't want to recalculate rmargin and offsets when
891 * using `Bd' or `Bl' within `-hang' overstep lists.
893 if (MDOC_HEAD
== n
->type
&& n
->next
->child
&&
894 (MDOC_Bl
== n
->next
->child
->tok
||
895 MDOC_Bd
== n
->next
->child
->tok
))
908 if (MDOC_HEAD
== n
->type
)
909 p
->rmargin
= p
->offset
+ width
;
915 p
->rmargin
= p
->offset
+ width
;
917 * XXX - this behaviour is not documented: the
918 * right-most column is filled to the right margin.
920 if (MDOC_HEAD
== n
->type
&&
921 MDOC_BODY
== n
->next
->type
)
922 p
->rmargin
= p
->maxrmargin
;
929 * The dash, hyphen, bullet and enum lists all have a special
930 * HEAD character (temporarily bold, in some cases).
933 if (MDOC_HEAD
== n
->type
)
937 term_word(p
, "\\[bu]");
944 term_word(p
, "\\(hy");
948 (pair
->ppair
->ppair
->count
)++;
949 (void)snprintf(buf
, sizeof(buf
), "%d.",
950 pair
->ppair
->ppair
->count
);
958 * If we're not going to process our children, indicate so here.
971 if (MDOC_HEAD
== n
->type
)
975 if (MDOC_BODY
== n
->type
)
988 termp_it_post(DECL_ARGS
)
992 if (MDOC_BODY
!= n
->type
&& MDOC_HEAD
!= n
->type
)
995 type
= arg_listtype(n
->parent
->parent
->parent
);
1004 if (MDOC_BODY
== n
->type
)
1008 if (MDOC_HEAD
== n
->type
)
1016 p
->flags
= pair
->flag
;
1022 termp_nm_pre(DECL_ARGS
)
1025 if (SEC_SYNOPSIS
== n
->sec
)
1028 if (NULL
== n
->child
)
1029 term_word(p
, m
->name
);
1036 termp_fl_pre(DECL_ARGS
)
1040 term_word(p
, "\\-");
1041 p
->flags
|= TERMP_NOSPACE
;
1048 termp_an_pre(DECL_ARGS
)
1051 if (NULL
== n
->child
)
1055 * If not in the AUTHORS section, `An -split' will cause
1056 * newlines to occur before the author name. If in the AUTHORS
1057 * section, by default, the first `An' invocation is nosplit,
1058 * then all subsequent ones, regardless of whether interspersed
1059 * with other macros/text, are split. -split, in this case,
1060 * will override the condition of the implied first -nosplit.
1063 if (n
->sec
== SEC_AUTHORS
) {
1064 if ( ! (TERMP_ANPREC
& p
->flags
)) {
1065 if (TERMP_SPLIT
& p
->flags
)
1069 if (TERMP_NOSPLIT
& p
->flags
)
1075 if (TERMP_SPLIT
& p
->flags
)
1084 termp_an_post(DECL_ARGS
)
1088 if (SEC_AUTHORS
== n
->sec
)
1089 p
->flags
|= TERMP_ANPREC
;
1093 if (arg_getattr(MDOC_Split
, n
) > -1) {
1094 p
->flags
&= ~TERMP_NOSPLIT
;
1095 p
->flags
|= TERMP_SPLIT
;
1097 p
->flags
&= ~TERMP_SPLIT
;
1098 p
->flags
|= TERMP_NOSPLIT
;
1106 termp_ns_pre(DECL_ARGS
)
1109 p
->flags
|= TERMP_NOSPACE
;
1116 termp_rs_pre(DECL_ARGS
)
1119 if (SEC_SEE_ALSO
!= n
->sec
)
1121 if (MDOC_BLOCK
== n
->type
&& n
->prev
)
1129 termp_rv_pre(DECL_ARGS
)
1131 const struct mdoc_node
*nn
;
1134 term_word(p
, "The");
1136 for (nn
= n
->child
; nn
; nn
= nn
->next
) {
1138 term_word(p
, nn
->string
);
1140 p
->flags
|= TERMP_NOSPACE
;
1141 if (nn
->next
&& NULL
== nn
->next
->next
)
1142 term_word(p
, "(), and");
1144 term_word(p
, "(),");
1150 term_word(p
, "functions return");
1152 term_word(p
, "function returns");
1154 term_word(p
, "the value 0 if successful; otherwise the value "
1155 "-1 is returned and the global variable");
1158 term_word(p
, "errno");
1161 term_word(p
, "is set to indicate the error.");
1169 termp_ex_pre(DECL_ARGS
)
1171 const struct mdoc_node
*nn
;
1173 term_word(p
, "The");
1175 for (nn
= n
->child
; nn
; nn
= nn
->next
) {
1177 term_word(p
, nn
->string
);
1179 p
->flags
|= TERMP_NOSPACE
;
1180 if (nn
->next
&& NULL
== nn
->next
->next
)
1181 term_word(p
, ", and");
1185 p
->flags
&= ~TERMP_NOSPACE
;
1189 term_word(p
, "utilities exit");
1191 term_word(p
, "utility exits");
1193 term_word(p
, "0 on success, and >0 if an error occurs.");
1201 termp_nd_pre(DECL_ARGS
)
1204 if (MDOC_BODY
!= n
->type
)
1207 #if defined(__OpenBSD__) || defined(__linux__)
1208 term_word(p
, "\\(en");
1210 term_word(p
, "\\(em");
1218 termp_bl_post(DECL_ARGS
)
1221 if (MDOC_BLOCK
== n
->type
)
1228 termp_op_post(DECL_ARGS
)
1231 if (MDOC_BODY
!= n
->type
)
1233 p
->flags
|= TERMP_NOSPACE
;
1234 term_word(p
, "\\(rB");
1240 termp_xr_pre(DECL_ARGS
)
1242 const struct mdoc_node
*nn
;
1244 assert(n
->child
&& MDOC_TEXT
== n
->child
->type
);
1247 term_word(p
, nn
->string
);
1248 if (NULL
== (nn
= nn
->next
))
1250 p
->flags
|= TERMP_NOSPACE
;
1252 p
->flags
|= TERMP_NOSPACE
;
1253 term_word(p
, nn
->string
);
1254 p
->flags
|= TERMP_NOSPACE
;
1263 termp_vt_post(DECL_ARGS
)
1266 if (n
->sec
!= SEC_SYNOPSIS
)
1268 if (n
->next
&& MDOC_Vt
== n
->next
->tok
)
1277 termp_bold_pre(DECL_ARGS
)
1287 termp_fd_post(DECL_ARGS
)
1290 if (n
->sec
!= SEC_SYNOPSIS
)
1294 if (n
->next
&& MDOC_Fd
!= n
->next
->tok
)
1301 termp_sh_pre(DECL_ARGS
)
1304 /* No vspace between consecutive `Sh' calls. */
1308 if (n
->prev
&& MDOC_Sh
== n
->prev
->tok
)
1309 if (NULL
== n
->prev
->body
->child
)
1328 termp_sh_post(DECL_ARGS
)
1347 termp_op_pre(DECL_ARGS
)
1352 term_word(p
, "\\(lB");
1353 p
->flags
|= TERMP_NOSPACE
;
1364 termp_bt_pre(DECL_ARGS
)
1367 term_word(p
, "is currently in beta test.");
1374 termp_lb_post(DECL_ARGS
)
1377 if (SEC_LIBRARY
== n
->sec
)
1384 termp_ud_pre(DECL_ARGS
)
1387 term_word(p
, "currently under development.");
1394 termp_d1_pre(DECL_ARGS
)
1397 if (MDOC_BLOCK
!= n
->type
)
1400 p
->offset
+= (INDENT
+ 1);
1407 termp_d1_post(DECL_ARGS
)
1410 if (MDOC_BLOCK
!= n
->type
)
1418 termp_aq_pre(DECL_ARGS
)
1421 if (MDOC_BODY
!= n
->type
)
1423 term_word(p
, "\\(la");
1424 p
->flags
|= TERMP_NOSPACE
;
1431 termp_aq_post(DECL_ARGS
)
1434 if (MDOC_BODY
!= n
->type
)
1436 p
->flags
|= TERMP_NOSPACE
;
1437 term_word(p
, "\\(ra");
1443 termp_ft_pre(DECL_ARGS
)
1446 if (SEC_SYNOPSIS
== n
->sec
)
1447 if (n
->prev
&& MDOC_Fo
== n
->prev
->tok
)
1456 termp_ft_post(DECL_ARGS
)
1459 if (SEC_SYNOPSIS
== n
->sec
)
1466 termp_fn_pre(DECL_ARGS
)
1468 const struct mdoc_node
*nn
;
1471 term_word(p
, n
->child
->string
);
1474 p
->flags
|= TERMP_NOSPACE
;
1477 for (nn
= n
->child
->next
; nn
; nn
= nn
->next
) {
1479 term_word(p
, nn
->string
);
1487 if (SEC_SYNOPSIS
== n
->sec
)
1496 termp_fn_post(DECL_ARGS
)
1499 if (n
->sec
== SEC_SYNOPSIS
&& n
->next
)
1506 termp_fa_pre(DECL_ARGS
)
1508 const struct mdoc_node
*nn
;
1510 if (n
->parent
->tok
!= MDOC_Fo
) {
1515 for (nn
= n
->child
; nn
; nn
= nn
->next
) {
1517 term_word(p
, nn
->string
);
1523 if (n
->child
&& n
->next
&& n
->next
->tok
== MDOC_Fa
)
1532 termp_bd_pre(DECL_ARGS
)
1535 const struct mdoc_node
*nn
;
1537 if (MDOC_BLOCK
== n
->type
) {
1538 print_bvspace(p
, n
, n
);
1540 } else if (MDOC_BODY
!= n
->type
)
1545 for (type
= -1, i
= 0; i
< (int)nn
->args
->argc
; i
++) {
1546 switch (nn
->args
->argv
[i
].arg
) {
1547 case (MDOC_Centred
):
1553 case (MDOC_Unfilled
):
1555 case (MDOC_Literal
):
1556 type
= nn
->args
->argv
[i
].arg
;
1559 p
->offset
+= arg2offs(&nn
->args
->argv
[i
]);
1567 * If -ragged or -filled are specified, the block does nothing
1568 * but change the indentation. If -unfilled or -literal are
1569 * specified, text is printed exactly as entered in the display:
1570 * for macro lines, a newline is appended to the line. Blank
1571 * lines are allowed.
1575 if (MDOC_Literal
!= type
&& MDOC_Unfilled
!= type
)
1578 for (nn
= n
->child
; nn
; nn
= nn
->next
) {
1579 p
->flags
|= TERMP_NOSPACE
;
1580 print_node(p
, pair
, m
, nn
);
1581 if (NULL
== nn
->next
)
1583 if (nn
->prev
&& nn
->prev
->line
< nn
->line
)
1585 else if (NULL
== nn
->prev
)
1595 termp_bd_post(DECL_ARGS
)
1598 if (MDOC_BODY
!= n
->type
)
1600 p
->flags
|= TERMP_NOSPACE
;
1607 termp_qq_pre(DECL_ARGS
)
1610 if (MDOC_BODY
!= n
->type
)
1613 p
->flags
|= TERMP_NOSPACE
;
1620 termp_qq_post(DECL_ARGS
)
1623 if (MDOC_BODY
!= n
->type
)
1625 p
->flags
|= TERMP_NOSPACE
;
1632 termp_bx_post(DECL_ARGS
)
1636 p
->flags
|= TERMP_NOSPACE
;
1637 term_word(p
, "BSD");
1643 termp_xx_pre(DECL_ARGS
)
1653 pp
= "DragonFlyBSD";
1679 termp_sq_pre(DECL_ARGS
)
1682 if (MDOC_BODY
!= n
->type
)
1684 term_word(p
, "\\(oq");
1685 p
->flags
|= TERMP_NOSPACE
;
1692 termp_sq_post(DECL_ARGS
)
1695 if (MDOC_BODY
!= n
->type
)
1697 p
->flags
|= TERMP_NOSPACE
;
1698 term_word(p
, "\\(aq");
1704 termp_pf_pre(DECL_ARGS
)
1707 p
->flags
|= TERMP_IGNDELIM
;
1714 termp_pf_post(DECL_ARGS
)
1717 p
->flags
&= ~TERMP_IGNDELIM
;
1718 p
->flags
|= TERMP_NOSPACE
;
1724 termp_ss_pre(DECL_ARGS
)
1735 p
->offset
= HALFINDENT
;
1747 termp_ss_post(DECL_ARGS
)
1750 if (MDOC_HEAD
== n
->type
)
1757 termp_cd_pre(DECL_ARGS
)
1768 termp_in_pre(DECL_ARGS
)
1772 if (SEC_SYNOPSIS
== n
->sec
)
1773 term_word(p
, "#include");
1776 p
->flags
|= TERMP_NOSPACE
;
1783 termp_in_post(DECL_ARGS
)
1787 p
->flags
|= TERMP_NOSPACE
;
1791 if (SEC_SYNOPSIS
!= n
->sec
)
1796 * XXX Not entirely correct. If `.In foo bar' is specified in
1797 * the SYNOPSIS section, then it produces a single break after
1798 * the <foo>; mandoc asserts a vertical space. Since this
1799 * construction is rarely used, I think it's fine.
1801 if (n
->next
&& MDOC_In
!= n
->next
->tok
)
1808 termp_sp_pre(DECL_ARGS
)
1814 len
= n
->child
? arg2height(n
->child
) : 1;
1826 for (i
= 0; i
< len
; i
++)
1835 termp_brq_pre(DECL_ARGS
)
1838 if (MDOC_BODY
!= n
->type
)
1840 term_word(p
, "\\(lC");
1841 p
->flags
|= TERMP_NOSPACE
;
1848 termp_brq_post(DECL_ARGS
)
1851 if (MDOC_BODY
!= n
->type
)
1853 p
->flags
|= TERMP_NOSPACE
;
1854 term_word(p
, "\\(rC");
1860 termp_bq_pre(DECL_ARGS
)
1863 if (MDOC_BODY
!= n
->type
)
1865 term_word(p
, "\\(lB");
1866 p
->flags
|= TERMP_NOSPACE
;
1873 termp_bq_post(DECL_ARGS
)
1876 if (MDOC_BODY
!= n
->type
)
1878 p
->flags
|= TERMP_NOSPACE
;
1879 term_word(p
, "\\(rB");
1885 termp_pq_pre(DECL_ARGS
)
1888 if (MDOC_BODY
!= n
->type
)
1890 term_word(p
, "\\&(");
1891 p
->flags
|= TERMP_NOSPACE
;
1898 termp_pq_post(DECL_ARGS
)
1901 if (MDOC_BODY
!= n
->type
)
1909 termp_fo_pre(DECL_ARGS
)
1911 const struct mdoc_node
*nn
;
1913 if (MDOC_BODY
== n
->type
) {
1914 p
->flags
|= TERMP_NOSPACE
;
1916 p
->flags
|= TERMP_NOSPACE
;
1918 } else if (MDOC_HEAD
!= n
->type
)
1922 for (nn
= n
->child
; nn
; nn
= nn
->next
) {
1923 assert(MDOC_TEXT
== nn
->type
);
1924 term_word(p
, nn
->string
);
1934 termp_fo_post(DECL_ARGS
)
1937 if (MDOC_BODY
!= n
->type
)
1939 p
->flags
|= TERMP_NOSPACE
;
1941 p
->flags
|= TERMP_NOSPACE
;
1949 termp_bf_pre(DECL_ARGS
)
1951 const struct mdoc_node
*nn
;
1953 if (MDOC_HEAD
== n
->type
)
1955 else if (MDOC_BLOCK
!= n
->type
)
1958 if (NULL
== (nn
= n
->head
->child
)) {
1959 if (arg_hasattr(MDOC_Emphasis
, n
))
1961 else if (arg_hasattr(MDOC_Symbolic
, n
))
1967 assert(MDOC_TEXT
== nn
->type
);
1968 if (0 == strcmp("Em", nn
->string
))
1970 else if (0 == strcmp("Sy", nn
->string
))
1979 termp_sm_pre(DECL_ARGS
)
1982 assert(n
->child
&& MDOC_TEXT
== n
->child
->type
);
1983 if (0 == strcmp("on", n
->child
->string
)) {
1984 p
->flags
&= ~TERMP_NONOSPACE
;
1985 p
->flags
&= ~TERMP_NOSPACE
;
1987 p
->flags
|= TERMP_NONOSPACE
;
1995 termp_ap_pre(DECL_ARGS
)
1998 p
->flags
|= TERMP_NOSPACE
;
1999 term_word(p
, "\\(aq");
2000 p
->flags
|= TERMP_NOSPACE
;
2007 termp____post(DECL_ARGS
)
2012 p
->flags
|= TERMP_NOSPACE
;
2015 term_word(p
, "\\(rq");
2016 p
->flags
|= TERMP_NOSPACE
;
2021 term_word(p
, n
->next
? "," : ".");
2027 termp_lk_pre(DECL_ARGS
)
2029 const struct mdoc_node
*nn
;
2031 if (NULL
== (nn
= n
->child
->next
)) {
2037 term_word(p
, nn
->string
);
2038 p
->flags
|= TERMP_NOSPACE
;
2043 for (nn
= nn
->next
; nn
; nn
= nn
->next
)
2044 term_word(p
, nn
->string
);
2053 termp_under_pre(DECL_ARGS
)
2063 termp__t_pre(DECL_ARGS
)
2066 term_word(p
, "\\(lq");
2067 p
->flags
|= TERMP_NOSPACE
;