]>
git.cameronkatri.com Git - mandoc.git/blob - mdoc_term.c
1 /* $Id: mdoc_term.c,v 1.117 2010/04/08 08:17:55 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.
21 #include <sys/types.h>
39 struct termpair
*ppair
;
43 #define DECL_ARGS struct termp *p, \
44 struct termpair *pair, \
45 const struct mdoc_meta *m, \
46 const struct mdoc_node *n
49 int (*pre
)(DECL_ARGS
);
50 void (*post
)(DECL_ARGS
);
53 static size_t a2width(const struct mdoc_argv
*, int);
54 static size_t a2height(const struct mdoc_node
*);
55 static size_t a2offs(const struct mdoc_argv
*);
57 static int arg_hasattr(int, const struct mdoc_node
*);
58 static int arg_getattrs(const int *, int *, size_t,
59 const struct mdoc_node
*);
60 static int arg_getattr(int, const struct mdoc_node
*);
61 static int arg_listtype(const struct mdoc_node
*);
62 static void print_bvspace(struct termp
*,
63 const struct mdoc_node
*,
64 const struct mdoc_node
*);
65 static void print_mdoc_node(DECL_ARGS
);
66 static void print_mdoc_head(DECL_ARGS
);
67 static void print_mdoc_nodelist(DECL_ARGS
);
68 static void print_foot(DECL_ARGS
);
70 static void termp____post(DECL_ARGS
);
71 static void termp_an_post(DECL_ARGS
);
72 static void termp_aq_post(DECL_ARGS
);
73 static void termp_bd_post(DECL_ARGS
);
74 static void termp_bl_post(DECL_ARGS
);
75 static void termp_bq_post(DECL_ARGS
);
76 static void termp_brq_post(DECL_ARGS
);
77 static void termp_bx_post(DECL_ARGS
);
78 static void termp_d1_post(DECL_ARGS
);
79 static void termp_dq_post(DECL_ARGS
);
80 static void termp_fd_post(DECL_ARGS
);
81 static void termp_fn_post(DECL_ARGS
);
82 static void termp_fo_post(DECL_ARGS
);
83 static void termp_ft_post(DECL_ARGS
);
84 static void termp_in_post(DECL_ARGS
);
85 static void termp_it_post(DECL_ARGS
);
86 static void termp_lb_post(DECL_ARGS
);
87 static void termp_op_post(DECL_ARGS
);
88 static void termp_pf_post(DECL_ARGS
);
89 static void termp_pq_post(DECL_ARGS
);
90 static void termp_qq_post(DECL_ARGS
);
91 static void termp_sh_post(DECL_ARGS
);
92 static void termp_sq_post(DECL_ARGS
);
93 static void termp_ss_post(DECL_ARGS
);
94 static void termp_vt_post(DECL_ARGS
);
96 static int termp_an_pre(DECL_ARGS
);
97 static int termp_ap_pre(DECL_ARGS
);
98 static int termp_aq_pre(DECL_ARGS
);
99 static int termp_bd_pre(DECL_ARGS
);
100 static int termp_bf_pre(DECL_ARGS
);
101 static int termp_bl_pre(DECL_ARGS
);
102 static int termp_bold_pre(DECL_ARGS
);
103 static int termp_bq_pre(DECL_ARGS
);
104 static int termp_brq_pre(DECL_ARGS
);
105 static int termp_bt_pre(DECL_ARGS
);
106 static int termp_cd_pre(DECL_ARGS
);
107 static int termp_d1_pre(DECL_ARGS
);
108 static int termp_dq_pre(DECL_ARGS
);
109 static int termp_ex_pre(DECL_ARGS
);
110 static int termp_fa_pre(DECL_ARGS
);
111 static int termp_fl_pre(DECL_ARGS
);
112 static int termp_fn_pre(DECL_ARGS
);
113 static int termp_fo_pre(DECL_ARGS
);
114 static int termp_ft_pre(DECL_ARGS
);
115 static int termp_in_pre(DECL_ARGS
);
116 static int termp_it_pre(DECL_ARGS
);
117 static int termp_li_pre(DECL_ARGS
);
118 static int termp_lk_pre(DECL_ARGS
);
119 static int termp_nd_pre(DECL_ARGS
);
120 static int termp_nm_pre(DECL_ARGS
);
121 static int termp_ns_pre(DECL_ARGS
);
122 static int termp_op_pre(DECL_ARGS
);
123 static int termp_pf_pre(DECL_ARGS
);
124 static int termp_pq_pre(DECL_ARGS
);
125 static int termp_qq_pre(DECL_ARGS
);
126 static int termp_rs_pre(DECL_ARGS
);
127 static int termp_rv_pre(DECL_ARGS
);
128 static int termp_sh_pre(DECL_ARGS
);
129 static int termp_sm_pre(DECL_ARGS
);
130 static int termp_sp_pre(DECL_ARGS
);
131 static int termp_sq_pre(DECL_ARGS
);
132 static int termp_ss_pre(DECL_ARGS
);
133 static int termp_under_pre(DECL_ARGS
);
134 static int termp_ud_pre(DECL_ARGS
);
135 static int termp_vt_pre(DECL_ARGS
);
136 static int termp_xr_pre(DECL_ARGS
);
137 static int termp_xx_pre(DECL_ARGS
);
139 static const struct termact termacts
[MDOC_MAX
] = {
140 { termp_ap_pre
, NULL
}, /* Ap */
141 { NULL
, NULL
}, /* Dd */
142 { NULL
, NULL
}, /* Dt */
143 { NULL
, NULL
}, /* Os */
144 { termp_sh_pre
, termp_sh_post
}, /* Sh */
145 { termp_ss_pre
, termp_ss_post
}, /* Ss */
146 { termp_sp_pre
, NULL
}, /* Pp */
147 { termp_d1_pre
, termp_d1_post
}, /* D1 */
148 { termp_d1_pre
, termp_d1_post
}, /* Dl */
149 { termp_bd_pre
, termp_bd_post
}, /* Bd */
150 { NULL
, NULL
}, /* Ed */
151 { termp_bl_pre
, termp_bl_post
}, /* Bl */
152 { NULL
, NULL
}, /* El */
153 { termp_it_pre
, termp_it_post
}, /* It */
154 { NULL
, NULL
}, /* Ad */
155 { termp_an_pre
, termp_an_post
}, /* An */
156 { termp_under_pre
, NULL
}, /* Ar */
157 { termp_cd_pre
, NULL
}, /* Cd */
158 { termp_bold_pre
, NULL
}, /* Cm */
159 { NULL
, NULL
}, /* Dv */
160 { NULL
, NULL
}, /* Er */
161 { NULL
, NULL
}, /* Ev */
162 { termp_ex_pre
, NULL
}, /* Ex */
163 { termp_fa_pre
, NULL
}, /* Fa */
164 { termp_bold_pre
, termp_fd_post
}, /* Fd */
165 { termp_fl_pre
, NULL
}, /* Fl */
166 { termp_fn_pre
, termp_fn_post
}, /* Fn */
167 { termp_ft_pre
, termp_ft_post
}, /* Ft */
168 { termp_bold_pre
, NULL
}, /* Ic */
169 { termp_in_pre
, termp_in_post
}, /* In */
170 { termp_li_pre
, NULL
}, /* Li */
171 { termp_nd_pre
, NULL
}, /* Nd */
172 { termp_nm_pre
, NULL
}, /* Nm */
173 { termp_op_pre
, termp_op_post
}, /* Op */
174 { NULL
, NULL
}, /* Ot */
175 { termp_under_pre
, NULL
}, /* Pa */
176 { termp_rv_pre
, NULL
}, /* Rv */
177 { NULL
, NULL
}, /* St */
178 { termp_under_pre
, NULL
}, /* Va */
179 { termp_vt_pre
, termp_vt_post
}, /* Vt */
180 { termp_xr_pre
, NULL
}, /* Xr */
181 { NULL
, termp____post
}, /* %A */
182 { termp_under_pre
, termp____post
}, /* %B */
183 { NULL
, termp____post
}, /* %D */
184 { termp_under_pre
, termp____post
}, /* %I */
185 { termp_under_pre
, termp____post
}, /* %J */
186 { NULL
, termp____post
}, /* %N */
187 { NULL
, termp____post
}, /* %O */
188 { NULL
, termp____post
}, /* %P */
189 { NULL
, termp____post
}, /* %R */
190 { termp_under_pre
, termp____post
}, /* %T */
191 { NULL
, termp____post
}, /* %V */
192 { NULL
, NULL
}, /* Ac */
193 { termp_aq_pre
, termp_aq_post
}, /* Ao */
194 { termp_aq_pre
, termp_aq_post
}, /* Aq */
195 { NULL
, NULL
}, /* At */
196 { NULL
, NULL
}, /* Bc */
197 { termp_bf_pre
, NULL
}, /* Bf */
198 { termp_bq_pre
, termp_bq_post
}, /* Bo */
199 { termp_bq_pre
, termp_bq_post
}, /* Bq */
200 { termp_xx_pre
, NULL
}, /* Bsx */
201 { NULL
, termp_bx_post
}, /* Bx */
202 { NULL
, NULL
}, /* Db */
203 { NULL
, NULL
}, /* Dc */
204 { termp_dq_pre
, termp_dq_post
}, /* Do */
205 { termp_dq_pre
, termp_dq_post
}, /* Dq */
206 { NULL
, NULL
}, /* Ec */ /* FIXME: no space */
207 { NULL
, NULL
}, /* Ef */
208 { termp_under_pre
, NULL
}, /* Em */
209 { NULL
, NULL
}, /* Eo */
210 { termp_xx_pre
, NULL
}, /* Fx */
211 { termp_bold_pre
, NULL
}, /* Ms */ /* FIXME: convert to symbol? */
212 { NULL
, NULL
}, /* No */
213 { termp_ns_pre
, NULL
}, /* Ns */
214 { termp_xx_pre
, NULL
}, /* Nx */
215 { termp_xx_pre
, NULL
}, /* Ox */
216 { NULL
, NULL
}, /* Pc */
217 { termp_pf_pre
, termp_pf_post
}, /* Pf */
218 { termp_pq_pre
, termp_pq_post
}, /* Po */
219 { termp_pq_pre
, termp_pq_post
}, /* Pq */
220 { NULL
, NULL
}, /* Qc */
221 { termp_sq_pre
, termp_sq_post
}, /* Ql */
222 { termp_qq_pre
, termp_qq_post
}, /* Qo */
223 { termp_qq_pre
, termp_qq_post
}, /* Qq */
224 { NULL
, NULL
}, /* Re */
225 { termp_rs_pre
, NULL
}, /* Rs */
226 { NULL
, NULL
}, /* Sc */
227 { termp_sq_pre
, termp_sq_post
}, /* So */
228 { termp_sq_pre
, termp_sq_post
}, /* Sq */
229 { termp_sm_pre
, NULL
}, /* Sm */
230 { termp_under_pre
, NULL
}, /* Sx */
231 { termp_bold_pre
, NULL
}, /* Sy */
232 { NULL
, NULL
}, /* Tn */
233 { termp_xx_pre
, NULL
}, /* Ux */
234 { NULL
, NULL
}, /* Xc */
235 { NULL
, NULL
}, /* Xo */
236 { termp_fo_pre
, termp_fo_post
}, /* Fo */
237 { NULL
, NULL
}, /* Fc */
238 { termp_op_pre
, termp_op_post
}, /* Oo */
239 { NULL
, NULL
}, /* Oc */
240 { NULL
, NULL
}, /* Bk */
241 { NULL
, NULL
}, /* Ek */
242 { termp_bt_pre
, NULL
}, /* Bt */
243 { NULL
, NULL
}, /* Hf */
244 { NULL
, NULL
}, /* Fr */
245 { termp_ud_pre
, NULL
}, /* Ud */
246 { NULL
, termp_lb_post
}, /* Lb */
247 { termp_sp_pre
, NULL
}, /* Lp */
248 { termp_lk_pre
, NULL
}, /* Lk */
249 { termp_under_pre
, NULL
}, /* Mt */
250 { termp_brq_pre
, termp_brq_post
}, /* Brq */
251 { termp_brq_pre
, termp_brq_post
}, /* Bro */
252 { NULL
, NULL
}, /* Brc */
253 { NULL
, termp____post
}, /* %C */
254 { NULL
, NULL
}, /* Es */ /* TODO */
255 { NULL
, NULL
}, /* En */ /* TODO */
256 { termp_xx_pre
, NULL
}, /* Dx */
257 { NULL
, termp____post
}, /* %Q */
258 { termp_sp_pre
, NULL
}, /* br */
259 { termp_sp_pre
, NULL
}, /* sp */
260 { termp_under_pre
, termp____post
}, /* %U */
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
;
276 if (NULL
== p
->symtab
)
278 case (TERMENC_ASCII
):
279 p
->symtab
= chars_init(CHARS_ASCII
);
289 print_mdoc_head(p
, NULL
, m
, n
);
291 print_mdoc_nodelist(p
, NULL
, m
, n
->child
);
292 print_foot(p
, NULL
, m
, n
);
297 print_mdoc_nodelist(DECL_ARGS
)
300 print_mdoc_node(p
, pair
, m
, n
);
302 print_mdoc_nodelist(p
, pair
, m
, n
->next
);
308 print_mdoc_node(DECL_ARGS
)
312 struct termpair npair
;
313 size_t offset
, rmargin
;
317 rmargin
= p
->rmargin
;
318 font
= term_fontq(p
);
320 memset(&npair
, 0, sizeof(struct termpair
));
323 if (MDOC_TEXT
!= n
->type
) {
324 if (termacts
[n
->tok
].pre
)
325 chld
= (*termacts
[n
->tok
].pre
)(p
, &npair
, m
, n
);
327 term_word(p
, n
->string
);
329 if (chld
&& n
->child
)
330 print_mdoc_nodelist(p
, &npair
, m
, n
->child
);
332 term_fontpopq(p
, font
);
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
)
347 char buf
[DATESIZ
], os
[BUFSIZ
];
349 term_fontrepl(p
, TERMFONT_NONE
);
352 * Output the footer in new-groff style, that is, three columns
353 * with the middle being the manual date and flanking columns
354 * being the operating system:
359 time2a(m
->date
, buf
, DATESIZ
);
360 strlcpy(os
, m
->os
, BUFSIZ
);
365 p
->rmargin
= (p
->maxrmargin
- strlen(buf
) + 1) / 2;
366 p
->flags
|= TERMP_NOSPACE
| TERMP_NOBREAK
;
371 p
->offset
= p
->rmargin
;
372 p
->rmargin
= p
->maxrmargin
- strlen(os
);
373 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
378 p
->offset
= p
->rmargin
;
379 p
->rmargin
= p
->maxrmargin
;
380 p
->flags
&= ~TERMP_NOBREAK
;
381 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
387 p
->rmargin
= p
->maxrmargin
;
394 print_mdoc_head(DECL_ARGS
)
396 char buf
[BUFSIZ
], title
[BUFSIZ
];
398 p
->rmargin
= p
->maxrmargin
;
402 * The header is strange. It has three components, which are
403 * really two with the first duplicated. It goes like this:
405 * IDENTIFIER TITLE IDENTIFIER
407 * The IDENTIFIER is NAME(SECTION), which is the command-name
408 * (if given, or "unknown" if not) followed by the manual page
409 * section. These are given in `Dt'. The TITLE is a free-form
410 * string depending on the manual volume. If not specified, it
411 * switches on the manual section.
415 strlcpy(buf
, m
->vol
, BUFSIZ
);
418 strlcat(buf
, " (", BUFSIZ
);
419 strlcat(buf
, m
->arch
, BUFSIZ
);
420 strlcat(buf
, ")", BUFSIZ
);
423 snprintf(title
, BUFSIZ
, "%s(%d)", m
->title
, m
->msec
);
426 p
->rmargin
= (p
->maxrmargin
- strlen(buf
) + 1) / 2;
427 p
->flags
|= TERMP_NOBREAK
| TERMP_NOSPACE
;
432 p
->offset
= p
->rmargin
;
433 p
->rmargin
= p
->maxrmargin
- strlen(title
);
434 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
439 p
->offset
= p
->rmargin
;
440 p
->rmargin
= p
->maxrmargin
;
441 p
->flags
&= ~TERMP_NOBREAK
;
442 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
448 p
->rmargin
= p
->maxrmargin
;
449 p
->flags
&= ~TERMP_NOSPACE
;
454 a2height(const struct mdoc_node
*n
)
458 assert(MDOC_TEXT
== n
->type
);
460 if ( ! a2roffsu(n
->string
, &su
, SCALE_VS
))
461 SCALE_VS_INIT(&su
, strlen(n
->string
));
463 return(term_vspan(&su
));
468 a2width(const struct mdoc_argv
*arg
, int pos
)
472 assert(arg
->value
[pos
]);
473 if ( ! a2roffsu(arg
->value
[pos
], &su
, SCALE_MAX
))
474 SCALE_HS_INIT(&su
, strlen(arg
->value
[pos
]));
476 return(term_hspan(&su
));
481 arg_listtype(const struct mdoc_node
*n
)
485 assert(MDOC_BLOCK
== n
->type
);
487 len
= (int)(n
->args
? n
->args
->argc
: 0);
489 for (i
= 0; i
< len
; i
++)
490 switch (n
->args
->argv
[i
].arg
) {
512 return(n
->args
->argv
[i
].arg
);
522 a2offs(const struct mdoc_argv
*arg
)
526 if ('\0' == arg
->value
[0][0])
528 else if (0 == strcmp(arg
->value
[0], "left"))
530 else if (0 == strcmp(arg
->value
[0], "indent"))
532 else if (0 == strcmp(arg
->value
[0], "indent-two"))
533 return((INDENT
+ 1) * 2);
534 else if ( ! a2roffsu(arg
->value
[0], &su
, SCALE_MAX
))
535 SCALE_HS_INIT(&su
, strlen(arg
->value
[0]));
537 return(term_hspan(&su
));
542 * Return 1 if an argument has a particular argument value or 0 if it
543 * does not. See arg_getattr().
546 arg_hasattr(int arg
, const struct mdoc_node
*n
)
549 return(-1 != arg_getattr(arg
, n
));
554 * Get the index of an argument in a node's argument list or -1 if it
555 * does not exist. See arg_getattrs().
558 arg_getattr(int v
, const struct mdoc_node
*n
)
562 return(arg_getattrs(&v
, &val
, 1, n
) ? val
: -1);
567 * Walk through the argument list for a node and fill an array "vals"
568 * with the positions of the argument structures listed in "keys".
569 * Return the number of elements that were written into "vals", which
573 arg_getattrs(const int *keys
, int *vals
,
574 size_t sz
, const struct mdoc_node
*n
)
581 for (k
= i
= 0; i
< (int)n
->args
->argc
; i
++)
582 for (j
= 0; j
< (int)sz
; j
++)
583 if (n
->args
->argv
[i
].arg
== keys
[j
]) {
592 * Determine how much space to print out before block elements of `It'
593 * (and thus `Bl') and `Bd'. And then go ahead and print that space,
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
, ncols
, dcol
;
676 if (MDOC_BLOCK
== n
->type
) {
677 print_bvspace(p
, n
->parent
->parent
, n
);
681 bl
= n
->parent
->parent
->parent
;
683 /* Get list width, offset, and list type from argument list. */
685 keys
[0] = MDOC_Width
;
686 keys
[1] = MDOC_Offset
;
687 keys
[2] = MDOC_Column
;
689 vals
[0] = vals
[1] = vals
[2] = -1;
691 arg_getattrs(keys
, vals
, 3, bl
);
693 type
= arg_listtype(bl
);
697 * First calculate width and offset. This is pretty easy unless
698 * we're a -column list, in which case all prior columns must
705 offset
= a2offs(&bl
->args
->argv
[vals
[1]]);
709 if (MDOC_BODY
== n
->type
)
712 * Imitate groff's column handling:
713 * - For each earlier column, add its width.
714 * - For less than 5 columns, add four more blanks per
716 * - For exactly 5 columns, add three more blank per
718 * - For more than 5 columns, add only one column.
720 ncols
= bl
->args
->argv
[vals
[2]].sz
;
722 dcol
= ncols
< 5 ? 4 : ncols
== 5 ? 3 : 1;
724 for (i
= 0, nn
= n
->prev
;
725 nn
&& i
< (int)ncols
;
727 offset
+= dcol
+ a2width
728 (&bl
->args
->argv
[vals
[2]], i
);
732 * When exceeding the declared number of columns, leave
733 * the remaining widths at 0. This will later be
734 * adjusted to the default width of 10, or, for the last
735 * column, stretched to the right margin.
741 * Use the declared column widths, extended as explained
742 * in the preceding paragraph.
744 width
= a2width(&bl
->args
->argv
[vals
[2]], i
) + dcol
;
751 * Note: buffer the width by 2, which is groff's magic
752 * number for buffering single arguments. See the above
753 * handling for column for how this changes.
755 width
= a2width(&bl
->args
->argv
[vals
[0]], 0) + 2;
760 * List-type can override the width in the case of fixed-head
761 * values (bullet, dash/hyphen, enum). Tags need a non-zero
793 * Whitespace control. Inset bodies need an initial space,
794 * while diagonal bodies need two.
797 p
->flags
|= TERMP_NOSPACE
;
801 if (MDOC_BODY
== n
->type
)
802 term_word(p
, "\\ \\ ");
805 if (MDOC_BODY
== n
->type
)
812 p
->flags
|= TERMP_NOSPACE
;
816 if (MDOC_HEAD
== n
->type
)
817 term_fontpush(p
, TERMFONT_BOLD
);
824 * Pad and break control. This is the tricky part. These flags
825 * are documented in term_flushln() in term.c. Note that we're
826 * going to unset all of these flags in termp_it_post() when we
838 if (MDOC_HEAD
== n
->type
)
839 p
->flags
|= TERMP_NOBREAK
;
841 p
->flags
|= TERMP_NOLPAD
;
844 if (MDOC_HEAD
== n
->type
)
845 p
->flags
|= TERMP_NOBREAK
;
847 p
->flags
|= TERMP_NOLPAD
;
849 if (MDOC_HEAD
!= n
->type
)
853 * This is ugly. If `-hang' is specified and the body
854 * is a `Bl' or `Bd', then we want basically to nullify
855 * the "overstep" effect in term_flushln() and treat
856 * this as a `-ohang' list instead.
858 if (n
->next
->child
&&
859 (MDOC_Bl
== n
->next
->child
->tok
||
860 MDOC_Bd
== n
->next
->child
->tok
)) {
861 p
->flags
&= ~TERMP_NOBREAK
;
862 p
->flags
&= ~TERMP_NOLPAD
;
864 p
->flags
|= TERMP_HANG
;
867 if (MDOC_HEAD
== n
->type
)
868 p
->flags
|= TERMP_NOBREAK
| TERMP_TWOSPACE
;
870 p
->flags
|= TERMP_NOLPAD
;
872 if (MDOC_HEAD
!= n
->type
)
874 if (NULL
== n
->next
|| NULL
== n
->next
->child
)
875 p
->flags
|= TERMP_DANGLE
;
878 if (MDOC_HEAD
== n
->type
) {
880 if (MDOC_BODY
== n
->next
->type
)
881 p
->flags
&= ~TERMP_NOBREAK
;
883 p
->flags
|= TERMP_NOBREAK
;
885 p
->flags
|= TERMP_NOLPAD
;
889 if (MDOC_HEAD
== n
->type
)
890 p
->flags
|= TERMP_NOBREAK
;
897 * Margin control. Set-head-width lists have their right
898 * margins shortened. The body for these lists has the offset
899 * necessarily lengthened. Everybody gets the offset.
907 * Same stipulation as above, regarding `-hang'. We
908 * don't want to recalculate rmargin and offsets when
909 * using `Bd' or `Bl' within `-hang' overstep lists.
911 if (MDOC_HEAD
== n
->type
&& n
->next
->child
&&
912 (MDOC_Bl
== n
->next
->child
->tok
||
913 MDOC_Bd
== n
->next
->child
->tok
))
926 if (MDOC_HEAD
== n
->type
)
927 p
->rmargin
= p
->offset
+ width
;
933 p
->rmargin
= p
->offset
+ width
;
935 * XXX - this behaviour is not documented: the
936 * right-most column is filled to the right margin.
938 if (MDOC_HEAD
== n
->type
&&
939 MDOC_BODY
== n
->next
->type
&&
940 p
->rmargin
< p
->maxrmargin
)
941 p
->rmargin
= p
->maxrmargin
;
948 * The dash, hyphen, bullet and enum lists all have a special
949 * HEAD character (temporarily bold, in some cases).
952 if (MDOC_HEAD
== n
->type
)
955 term_fontpush(p
, TERMFONT_BOLD
);
956 term_word(p
, "\\[bu]");
962 term_fontpush(p
, TERMFONT_BOLD
);
963 term_word(p
, "\\(hy");
967 (pair
->ppair
->ppair
->count
)++;
968 snprintf(buf
, sizeof(buf
), "%d.",
969 pair
->ppair
->ppair
->count
);
977 * If we're not going to process our children, indicate so here.
990 if (MDOC_HEAD
== n
->type
)
994 if (MDOC_BODY
== n
->type
)
1007 termp_it_post(DECL_ARGS
)
1011 if (MDOC_BLOCK
== n
->type
)
1014 type
= arg_listtype(n
->parent
->parent
->parent
);
1023 if (MDOC_BODY
== n
->type
)
1027 if (MDOC_HEAD
== n
->type
)
1036 * Now that our output is flushed, we can reset our tags. Since
1037 * only `It' sets these flags, we're free to assume that nobody
1038 * has munged them in the meanwhile.
1041 p
->flags
&= ~TERMP_DANGLE
;
1042 p
->flags
&= ~TERMP_NOBREAK
;
1043 p
->flags
&= ~TERMP_TWOSPACE
;
1044 p
->flags
&= ~TERMP_NOLPAD
;
1045 p
->flags
&= ~TERMP_HANG
;
1051 termp_nm_pre(DECL_ARGS
)
1054 if (SEC_SYNOPSIS
== n
->sec
)
1057 term_fontpush(p
, TERMFONT_BOLD
);
1059 if (NULL
== n
->child
)
1060 term_word(p
, m
->name
);
1067 termp_fl_pre(DECL_ARGS
)
1070 term_fontpush(p
, TERMFONT_BOLD
);
1071 term_word(p
, "\\-");
1074 p
->flags
|= TERMP_NOSPACE
;
1075 else if (n
->next
&& n
->next
->line
== n
->line
)
1076 p
->flags
|= TERMP_NOSPACE
;
1084 termp_an_pre(DECL_ARGS
)
1087 if (NULL
== n
->child
)
1091 * If not in the AUTHORS section, `An -split' will cause
1092 * newlines to occur before the author name. If in the AUTHORS
1093 * section, by default, the first `An' invocation is nosplit,
1094 * then all subsequent ones, regardless of whether interspersed
1095 * with other macros/text, are split. -split, in this case,
1096 * will override the condition of the implied first -nosplit.
1099 if (n
->sec
== SEC_AUTHORS
) {
1100 if ( ! (TERMP_ANPREC
& p
->flags
)) {
1101 if (TERMP_SPLIT
& p
->flags
)
1105 if (TERMP_NOSPLIT
& p
->flags
)
1111 if (TERMP_SPLIT
& p
->flags
)
1120 termp_an_post(DECL_ARGS
)
1124 if (SEC_AUTHORS
== n
->sec
)
1125 p
->flags
|= TERMP_ANPREC
;
1129 if (arg_getattr(MDOC_Split
, n
) > -1) {
1130 p
->flags
&= ~TERMP_NOSPLIT
;
1131 p
->flags
|= TERMP_SPLIT
;
1133 p
->flags
&= ~TERMP_SPLIT
;
1134 p
->flags
|= TERMP_NOSPLIT
;
1142 termp_ns_pre(DECL_ARGS
)
1145 p
->flags
|= TERMP_NOSPACE
;
1152 termp_rs_pre(DECL_ARGS
)
1155 if (SEC_SEE_ALSO
!= n
->sec
)
1157 if (MDOC_BLOCK
== n
->type
&& n
->prev
)
1165 termp_rv_pre(DECL_ARGS
)
1167 const struct mdoc_node
*nn
;
1170 term_word(p
, "The");
1172 for (nn
= n
->child
; nn
; nn
= nn
->next
) {
1173 term_fontpush(p
, TERMFONT_BOLD
);
1174 term_word(p
, nn
->string
);
1176 p
->flags
|= TERMP_NOSPACE
;
1177 if (nn
->next
&& NULL
== nn
->next
->next
)
1178 term_word(p
, "(), and");
1180 term_word(p
, "(),");
1186 term_word(p
, "functions return");
1188 term_word(p
, "function returns");
1190 term_word(p
, "the value 0 if successful; otherwise the value "
1191 "-1 is returned and the global variable");
1193 term_fontpush(p
, TERMFONT_UNDER
);
1194 term_word(p
, "errno");
1197 term_word(p
, "is set to indicate the error.");
1205 termp_ex_pre(DECL_ARGS
)
1207 const struct mdoc_node
*nn
;
1209 term_word(p
, "The");
1211 for (nn
= n
->child
; nn
; nn
= nn
->next
) {
1212 term_fontpush(p
, TERMFONT_BOLD
);
1213 term_word(p
, nn
->string
);
1215 p
->flags
|= TERMP_NOSPACE
;
1216 if (nn
->next
&& NULL
== nn
->next
->next
)
1217 term_word(p
, ", and");
1221 p
->flags
&= ~TERMP_NOSPACE
;
1225 term_word(p
, "utilities exit");
1227 term_word(p
, "utility exits");
1229 term_word(p
, "0 on success, and >0 if an error occurs.");
1237 termp_nd_pre(DECL_ARGS
)
1240 if (MDOC_BODY
!= n
->type
)
1243 #if defined(__OpenBSD__) || defined(__linux__)
1244 term_word(p
, "\\(en");
1246 term_word(p
, "\\(em");
1254 termp_bl_pre(DECL_ARGS
)
1257 return(MDOC_HEAD
!= n
->type
);
1263 termp_bl_post(DECL_ARGS
)
1266 if (MDOC_BLOCK
== n
->type
)
1273 termp_op_post(DECL_ARGS
)
1276 if (MDOC_BODY
!= n
->type
)
1278 p
->flags
|= TERMP_NOSPACE
;
1279 term_word(p
, "\\(rB");
1285 termp_xr_pre(DECL_ARGS
)
1287 const struct mdoc_node
*nn
;
1289 if (NULL
== n
->child
)
1292 assert(MDOC_TEXT
== n
->child
->type
);
1295 term_word(p
, nn
->string
);
1296 if (NULL
== (nn
= nn
->next
))
1298 p
->flags
|= TERMP_NOSPACE
;
1300 p
->flags
|= TERMP_NOSPACE
;
1301 term_word(p
, nn
->string
);
1302 p
->flags
|= TERMP_NOSPACE
;
1310 termp_vt_pre(DECL_ARGS
)
1313 if (MDOC_ELEM
== n
->type
)
1314 return(termp_under_pre(p
, pair
, m
, n
));
1315 else if (MDOC_HEAD
== n
->type
)
1317 else if (MDOC_BLOCK
== n
->type
)
1320 return(termp_under_pre(p
, pair
, m
, n
));
1326 termp_vt_post(DECL_ARGS
)
1329 if (MDOC_BLOCK
!= n
->type
)
1331 if (n
->next
&& MDOC_Vt
== n
->next
->tok
)
1340 termp_bold_pre(DECL_ARGS
)
1343 term_fontpush(p
, TERMFONT_BOLD
);
1350 termp_fd_post(DECL_ARGS
)
1353 if (n
->sec
!= SEC_SYNOPSIS
)
1357 if (n
->next
&& MDOC_Fd
!= n
->next
->tok
)
1364 termp_sh_pre(DECL_ARGS
)
1367 /* No vspace between consecutive `Sh' calls. */
1371 if (n
->prev
&& MDOC_Sh
== n
->prev
->tok
)
1372 if (NULL
== n
->prev
->body
->child
)
1377 term_fontpush(p
, TERMFONT_BOLD
);
1391 termp_sh_post(DECL_ARGS
)
1410 termp_op_pre(DECL_ARGS
)
1415 term_word(p
, "\\(lB");
1416 p
->flags
|= TERMP_NOSPACE
;
1427 termp_bt_pre(DECL_ARGS
)
1430 term_word(p
, "is currently in beta test.");
1437 termp_lb_post(DECL_ARGS
)
1440 if (SEC_LIBRARY
== n
->sec
)
1447 termp_ud_pre(DECL_ARGS
)
1450 term_word(p
, "currently under development.");
1457 termp_d1_pre(DECL_ARGS
)
1460 if (MDOC_BLOCK
!= n
->type
)
1463 p
->offset
+= (INDENT
+ 1);
1470 termp_d1_post(DECL_ARGS
)
1473 if (MDOC_BLOCK
!= n
->type
)
1481 termp_aq_pre(DECL_ARGS
)
1484 if (MDOC_BODY
!= n
->type
)
1486 term_word(p
, "\\(la");
1487 p
->flags
|= TERMP_NOSPACE
;
1494 termp_aq_post(DECL_ARGS
)
1497 if (MDOC_BODY
!= n
->type
)
1499 p
->flags
|= TERMP_NOSPACE
;
1500 term_word(p
, "\\(ra");
1506 termp_ft_pre(DECL_ARGS
)
1509 if (SEC_SYNOPSIS
== n
->sec
)
1510 if (n
->prev
&& MDOC_Fo
== n
->prev
->tok
)
1513 term_fontpush(p
, TERMFONT_UNDER
);
1520 termp_ft_post(DECL_ARGS
)
1523 if (SEC_SYNOPSIS
== n
->sec
)
1530 termp_fn_pre(DECL_ARGS
)
1532 const struct mdoc_node
*nn
;
1534 term_fontpush(p
, TERMFONT_BOLD
);
1535 term_word(p
, n
->child
->string
);
1538 p
->flags
|= TERMP_NOSPACE
;
1541 for (nn
= n
->child
->next
; nn
; nn
= nn
->next
) {
1542 term_fontpush(p
, TERMFONT_UNDER
);
1543 term_word(p
, nn
->string
);
1552 if (SEC_SYNOPSIS
== n
->sec
)
1561 termp_fn_post(DECL_ARGS
)
1564 if (n
->sec
== SEC_SYNOPSIS
&& n
->next
)
1571 termp_fa_pre(DECL_ARGS
)
1573 const struct mdoc_node
*nn
;
1575 if (n
->parent
->tok
!= MDOC_Fo
) {
1576 term_fontpush(p
, TERMFONT_UNDER
);
1580 for (nn
= n
->child
; nn
; nn
= nn
->next
) {
1581 term_fontpush(p
, TERMFONT_UNDER
);
1582 term_word(p
, nn
->string
);
1589 if (n
->child
&& n
->next
&& n
->next
->tok
== MDOC_Fa
)
1598 termp_bd_pre(DECL_ARGS
)
1601 const struct mdoc_node
*nn
;
1603 if (MDOC_BLOCK
== n
->type
) {
1604 print_bvspace(p
, n
, n
);
1606 } else if (MDOC_HEAD
== n
->type
)
1611 for (type
= -1, i
= 0; i
< (int)nn
->args
->argc
; i
++) {
1612 switch (nn
->args
->argv
[i
].arg
) {
1613 case (MDOC_Centred
):
1619 case (MDOC_Unfilled
):
1621 case (MDOC_Literal
):
1622 type
= nn
->args
->argv
[i
].arg
;
1625 p
->offset
+= a2offs(&nn
->args
->argv
[i
]);
1633 * If -ragged or -filled are specified, the block does nothing
1634 * but change the indentation. If -unfilled or -literal are
1635 * specified, text is printed exactly as entered in the display:
1636 * for macro lines, a newline is appended to the line. Blank
1637 * lines are allowed.
1641 if (MDOC_Literal
!= type
&& MDOC_Unfilled
!= type
)
1644 for (nn
= n
->child
; nn
; nn
= nn
->next
) {
1645 p
->flags
|= TERMP_NOSPACE
;
1646 print_mdoc_node(p
, pair
, m
, nn
);
1647 if (NULL
== nn
->next
)
1649 if (nn
->prev
&& nn
->prev
->line
< nn
->line
)
1651 else if (NULL
== nn
->prev
)
1661 termp_bd_post(DECL_ARGS
)
1664 if (MDOC_BODY
!= n
->type
)
1666 p
->flags
|= TERMP_NOSPACE
;
1673 termp_qq_pre(DECL_ARGS
)
1676 if (MDOC_BODY
!= n
->type
)
1679 p
->flags
|= TERMP_NOSPACE
;
1686 termp_qq_post(DECL_ARGS
)
1689 if (MDOC_BODY
!= n
->type
)
1691 p
->flags
|= TERMP_NOSPACE
;
1698 termp_bx_post(DECL_ARGS
)
1702 p
->flags
|= TERMP_NOSPACE
;
1703 term_word(p
, "BSD");
1709 termp_xx_pre(DECL_ARGS
)
1745 termp_sq_pre(DECL_ARGS
)
1748 if (MDOC_BODY
!= n
->type
)
1750 term_word(p
, "\\(oq");
1751 p
->flags
|= TERMP_NOSPACE
;
1758 termp_sq_post(DECL_ARGS
)
1761 if (MDOC_BODY
!= n
->type
)
1763 p
->flags
|= TERMP_NOSPACE
;
1764 term_word(p
, "\\(aq");
1770 termp_pf_pre(DECL_ARGS
)
1773 p
->flags
|= TERMP_IGNDELIM
;
1780 termp_pf_post(DECL_ARGS
)
1783 p
->flags
&= ~TERMP_IGNDELIM
;
1784 p
->flags
|= TERMP_NOSPACE
;
1790 termp_ss_pre(DECL_ARGS
)
1800 term_fontpush(p
, TERMFONT_BOLD
);
1801 p
->offset
= HALFINDENT
;
1813 termp_ss_post(DECL_ARGS
)
1816 if (MDOC_HEAD
== n
->type
)
1823 termp_cd_pre(DECL_ARGS
)
1826 term_fontpush(p
, TERMFONT_BOLD
);
1834 termp_in_pre(DECL_ARGS
)
1837 term_fontpush(p
, TERMFONT_BOLD
);
1838 if (SEC_SYNOPSIS
== n
->sec
)
1839 term_word(p
, "#include");
1842 p
->flags
|= TERMP_NOSPACE
;
1849 termp_in_post(DECL_ARGS
)
1852 term_fontpush(p
, TERMFONT_BOLD
);
1853 p
->flags
|= TERMP_NOSPACE
;
1857 if (SEC_SYNOPSIS
!= n
->sec
)
1862 * XXX Not entirely correct. If `.In foo bar' is specified in
1863 * the SYNOPSIS section, then it produces a single break after
1864 * the <foo>; mandoc asserts a vertical space. Since this
1865 * construction is rarely used, I think it's fine.
1867 if (n
->next
&& MDOC_In
!= n
->next
->tok
)
1874 termp_sp_pre(DECL_ARGS
)
1880 len
= n
->child
? a2height(n
->child
) : 1;
1892 for (i
= 0; i
< len
; i
++)
1901 termp_brq_pre(DECL_ARGS
)
1904 if (MDOC_BODY
!= n
->type
)
1906 term_word(p
, "\\(lC");
1907 p
->flags
|= TERMP_NOSPACE
;
1914 termp_brq_post(DECL_ARGS
)
1917 if (MDOC_BODY
!= n
->type
)
1919 p
->flags
|= TERMP_NOSPACE
;
1920 term_word(p
, "\\(rC");
1926 termp_bq_pre(DECL_ARGS
)
1929 if (MDOC_BODY
!= n
->type
)
1931 term_word(p
, "\\(lB");
1932 p
->flags
|= TERMP_NOSPACE
;
1939 termp_bq_post(DECL_ARGS
)
1942 if (MDOC_BODY
!= n
->type
)
1944 p
->flags
|= TERMP_NOSPACE
;
1945 term_word(p
, "\\(rB");
1951 termp_pq_pre(DECL_ARGS
)
1954 if (MDOC_BODY
!= n
->type
)
1956 term_word(p
, "\\&(");
1957 p
->flags
|= TERMP_NOSPACE
;
1964 termp_pq_post(DECL_ARGS
)
1967 if (MDOC_BODY
!= n
->type
)
1975 termp_fo_pre(DECL_ARGS
)
1977 const struct mdoc_node
*nn
;
1979 if (MDOC_BODY
== n
->type
) {
1980 p
->flags
|= TERMP_NOSPACE
;
1982 p
->flags
|= TERMP_NOSPACE
;
1984 } else if (MDOC_HEAD
!= n
->type
)
1987 term_fontpush(p
, TERMFONT_BOLD
);
1988 for (nn
= n
->child
; nn
; nn
= nn
->next
) {
1989 assert(MDOC_TEXT
== nn
->type
);
1990 term_word(p
, nn
->string
);
2000 termp_fo_post(DECL_ARGS
)
2003 if (MDOC_BODY
!= n
->type
)
2005 p
->flags
|= TERMP_NOSPACE
;
2007 p
->flags
|= TERMP_NOSPACE
;
2015 termp_bf_pre(DECL_ARGS
)
2017 const struct mdoc_node
*nn
;
2019 if (MDOC_HEAD
== n
->type
)
2021 else if (MDOC_BLOCK
!= n
->type
)
2024 if (NULL
== (nn
= n
->head
->child
)) {
2025 if (arg_hasattr(MDOC_Emphasis
, n
))
2026 term_fontpush(p
, TERMFONT_UNDER
);
2027 else if (arg_hasattr(MDOC_Symbolic
, n
))
2028 term_fontpush(p
, TERMFONT_BOLD
);
2030 term_fontpush(p
, TERMFONT_NONE
);
2035 assert(MDOC_TEXT
== nn
->type
);
2036 if (0 == strcmp("Em", nn
->string
))
2037 term_fontpush(p
, TERMFONT_UNDER
);
2038 else if (0 == strcmp("Sy", nn
->string
))
2039 term_fontpush(p
, TERMFONT_BOLD
);
2041 term_fontpush(p
, TERMFONT_NONE
);
2049 termp_sm_pre(DECL_ARGS
)
2052 assert(n
->child
&& MDOC_TEXT
== n
->child
->type
);
2053 if (0 == strcmp("on", n
->child
->string
))
2054 p
->flags
&= ~TERMP_NONOSPACE
;
2056 p
->flags
|= TERMP_NONOSPACE
;
2064 termp_ap_pre(DECL_ARGS
)
2067 p
->flags
|= TERMP_NOSPACE
;
2068 term_word(p
, "\\(aq");
2069 p
->flags
|= TERMP_NOSPACE
;
2076 termp____post(DECL_ARGS
)
2081 p
->flags
|= TERMP_NOSPACE
;
2082 term_word(p
, n
->next
? "," : ".");
2088 termp_li_pre(DECL_ARGS
)
2091 term_fontpush(p
, TERMFONT_NONE
);
2098 termp_lk_pre(DECL_ARGS
)
2100 const struct mdoc_node
*nn
;
2102 term_fontpush(p
, TERMFONT_UNDER
);
2105 if (NULL
== nn
->next
)
2108 term_word(p
, nn
->string
);
2111 p
->flags
|= TERMP_NOSPACE
;
2114 term_fontpush(p
, TERMFONT_BOLD
);
2115 for (nn
= nn
->next
; nn
; nn
= nn
->next
)
2116 term_word(p
, nn
->string
);
2125 termp_under_pre(DECL_ARGS
)
2128 term_fontpush(p
, TERMFONT_UNDER
);