]>
git.cameronkatri.com Git - mandoc.git/blob - mdoc_term.c
1 /* $Id: mdoc_term.c,v 1.127 2010/05/22 20:41:48 joerg 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>
41 struct termpair
*ppair
;
45 #define DECL_ARGS struct termp *p, \
46 struct termpair *pair, \
47 const struct mdoc_meta *m, \
48 const struct mdoc_node *n
51 int (*pre
)(DECL_ARGS
);
52 void (*post
)(DECL_ARGS
);
55 static size_t a2width(const struct mdoc_argv
*, int);
56 static size_t a2height(const struct mdoc_node
*);
57 static size_t a2offs(const struct mdoc_argv
*);
59 static int arg_hasattr(int, const struct mdoc_node
*);
60 static int arg_getattrs(const int *, int *, size_t,
61 const struct mdoc_node
*);
62 static int arg_getattr(int, const struct mdoc_node
*);
63 static int arg_disptype(const struct mdoc_node
*);
64 static void print_bvspace(struct termp
*,
65 const struct mdoc_node
*,
66 const struct mdoc_node
*);
67 static void print_mdoc_node(DECL_ARGS
);
68 static void print_mdoc_head(DECL_ARGS
);
69 static void print_mdoc_nodelist(DECL_ARGS
);
70 static void print_foot(DECL_ARGS
);
72 static void termp____post(DECL_ARGS
);
73 static void termp_an_post(DECL_ARGS
);
74 static void termp_aq_post(DECL_ARGS
);
75 static void termp_bd_post(DECL_ARGS
);
76 static void termp_bl_post(DECL_ARGS
);
77 static void termp_bq_post(DECL_ARGS
);
78 static void termp_brq_post(DECL_ARGS
);
79 static void termp_bx_post(DECL_ARGS
);
80 static void termp_d1_post(DECL_ARGS
);
81 static void termp_dq_post(DECL_ARGS
);
82 static void termp_fd_post(DECL_ARGS
);
83 static void termp_fn_post(DECL_ARGS
);
84 static void termp_fo_post(DECL_ARGS
);
85 static void termp_ft_post(DECL_ARGS
);
86 static void termp_in_post(DECL_ARGS
);
87 static void termp_it_post(DECL_ARGS
);
88 static void termp_lb_post(DECL_ARGS
);
89 static void termp_op_post(DECL_ARGS
);
90 static void termp_pf_post(DECL_ARGS
);
91 static void termp_pq_post(DECL_ARGS
);
92 static void termp_qq_post(DECL_ARGS
);
93 static void termp_sh_post(DECL_ARGS
);
94 static void termp_sq_post(DECL_ARGS
);
95 static void termp_ss_post(DECL_ARGS
);
96 static void termp_vt_post(DECL_ARGS
);
98 static int termp_an_pre(DECL_ARGS
);
99 static int termp_ap_pre(DECL_ARGS
);
100 static int termp_aq_pre(DECL_ARGS
);
101 static int termp_bd_pre(DECL_ARGS
);
102 static int termp_bf_pre(DECL_ARGS
);
103 static int termp_bl_pre(DECL_ARGS
);
104 static int termp_bold_pre(DECL_ARGS
);
105 static int termp_bq_pre(DECL_ARGS
);
106 static int termp_brq_pre(DECL_ARGS
);
107 static int termp_bt_pre(DECL_ARGS
);
108 static int termp_cd_pre(DECL_ARGS
);
109 static int termp_d1_pre(DECL_ARGS
);
110 static int termp_dq_pre(DECL_ARGS
);
111 static int termp_ex_pre(DECL_ARGS
);
112 static int termp_fa_pre(DECL_ARGS
);
113 static int termp_fl_pre(DECL_ARGS
);
114 static int termp_fn_pre(DECL_ARGS
);
115 static int termp_fo_pre(DECL_ARGS
);
116 static int termp_ft_pre(DECL_ARGS
);
117 static int termp_in_pre(DECL_ARGS
);
118 static int termp_it_pre(DECL_ARGS
);
119 static int termp_li_pre(DECL_ARGS
);
120 static int termp_lk_pre(DECL_ARGS
);
121 static int termp_nd_pre(DECL_ARGS
);
122 static int termp_nm_pre(DECL_ARGS
);
123 static int termp_ns_pre(DECL_ARGS
);
124 static int termp_op_pre(DECL_ARGS
);
125 static int termp_pf_pre(DECL_ARGS
);
126 static int termp_pq_pre(DECL_ARGS
);
127 static int termp_qq_pre(DECL_ARGS
);
128 static int termp_rs_pre(DECL_ARGS
);
129 static int termp_rv_pre(DECL_ARGS
);
130 static int termp_sh_pre(DECL_ARGS
);
131 static int termp_sm_pre(DECL_ARGS
);
132 static int termp_sp_pre(DECL_ARGS
);
133 static int termp_sq_pre(DECL_ARGS
);
134 static int termp_ss_pre(DECL_ARGS
);
135 static int termp_under_pre(DECL_ARGS
);
136 static int termp_ud_pre(DECL_ARGS
);
137 static int termp_vt_pre(DECL_ARGS
);
138 static int termp_xr_pre(DECL_ARGS
);
139 static int termp_xx_pre(DECL_ARGS
);
141 static const struct termact termacts
[MDOC_MAX
] = {
142 { termp_ap_pre
, NULL
}, /* Ap */
143 { NULL
, NULL
}, /* Dd */
144 { NULL
, NULL
}, /* Dt */
145 { NULL
, NULL
}, /* Os */
146 { termp_sh_pre
, termp_sh_post
}, /* Sh */
147 { termp_ss_pre
, termp_ss_post
}, /* Ss */
148 { termp_sp_pre
, NULL
}, /* Pp */
149 { termp_d1_pre
, termp_d1_post
}, /* D1 */
150 { termp_d1_pre
, termp_d1_post
}, /* Dl */
151 { termp_bd_pre
, termp_bd_post
}, /* Bd */
152 { NULL
, NULL
}, /* Ed */
153 { termp_bl_pre
, termp_bl_post
}, /* Bl */
154 { NULL
, NULL
}, /* El */
155 { termp_it_pre
, termp_it_post
}, /* It */
156 { NULL
, NULL
}, /* Ad */
157 { termp_an_pre
, termp_an_post
}, /* An */
158 { termp_under_pre
, NULL
}, /* Ar */
159 { termp_cd_pre
, NULL
}, /* Cd */
160 { termp_bold_pre
, NULL
}, /* Cm */
161 { NULL
, NULL
}, /* Dv */
162 { NULL
, NULL
}, /* Er */
163 { NULL
, NULL
}, /* Ev */
164 { termp_ex_pre
, NULL
}, /* Ex */
165 { termp_fa_pre
, NULL
}, /* Fa */
166 { termp_bold_pre
, termp_fd_post
}, /* Fd */
167 { termp_fl_pre
, NULL
}, /* Fl */
168 { termp_fn_pre
, termp_fn_post
}, /* Fn */
169 { termp_ft_pre
, termp_ft_post
}, /* Ft */
170 { termp_bold_pre
, NULL
}, /* Ic */
171 { termp_in_pre
, termp_in_post
}, /* In */
172 { termp_li_pre
, NULL
}, /* Li */
173 { termp_nd_pre
, NULL
}, /* Nd */
174 { termp_nm_pre
, NULL
}, /* Nm */
175 { termp_op_pre
, termp_op_post
}, /* Op */
176 { NULL
, NULL
}, /* Ot */
177 { termp_under_pre
, NULL
}, /* Pa */
178 { termp_rv_pre
, NULL
}, /* Rv */
179 { NULL
, NULL
}, /* St */
180 { termp_under_pre
, NULL
}, /* Va */
181 { termp_vt_pre
, termp_vt_post
}, /* Vt */
182 { termp_xr_pre
, NULL
}, /* Xr */
183 { NULL
, termp____post
}, /* %A */
184 { termp_under_pre
, termp____post
}, /* %B */
185 { NULL
, termp____post
}, /* %D */
186 { termp_under_pre
, termp____post
}, /* %I */
187 { termp_under_pre
, termp____post
}, /* %J */
188 { NULL
, termp____post
}, /* %N */
189 { NULL
, termp____post
}, /* %O */
190 { NULL
, termp____post
}, /* %P */
191 { NULL
, termp____post
}, /* %R */
192 { termp_under_pre
, termp____post
}, /* %T */
193 { NULL
, termp____post
}, /* %V */
194 { NULL
, NULL
}, /* Ac */
195 { termp_aq_pre
, termp_aq_post
}, /* Ao */
196 { termp_aq_pre
, termp_aq_post
}, /* Aq */
197 { NULL
, NULL
}, /* At */
198 { NULL
, NULL
}, /* Bc */
199 { termp_bf_pre
, NULL
}, /* Bf */
200 { termp_bq_pre
, termp_bq_post
}, /* Bo */
201 { termp_bq_pre
, termp_bq_post
}, /* Bq */
202 { termp_xx_pre
, NULL
}, /* Bsx */
203 { NULL
, termp_bx_post
}, /* Bx */
204 { NULL
, NULL
}, /* Db */
205 { NULL
, NULL
}, /* Dc */
206 { termp_dq_pre
, termp_dq_post
}, /* Do */
207 { termp_dq_pre
, termp_dq_post
}, /* Dq */
208 { NULL
, NULL
}, /* Ec */ /* FIXME: no space */
209 { NULL
, NULL
}, /* Ef */
210 { termp_under_pre
, NULL
}, /* Em */
211 { NULL
, NULL
}, /* Eo */
212 { termp_xx_pre
, NULL
}, /* Fx */
213 { termp_bold_pre
, NULL
}, /* Ms */ /* FIXME: convert to symbol? */
214 { NULL
, NULL
}, /* No */
215 { termp_ns_pre
, NULL
}, /* Ns */
216 { termp_xx_pre
, NULL
}, /* Nx */
217 { termp_xx_pre
, NULL
}, /* Ox */
218 { NULL
, NULL
}, /* Pc */
219 { termp_pf_pre
, termp_pf_post
}, /* Pf */
220 { termp_pq_pre
, termp_pq_post
}, /* Po */
221 { termp_pq_pre
, termp_pq_post
}, /* Pq */
222 { NULL
, NULL
}, /* Qc */
223 { termp_sq_pre
, termp_sq_post
}, /* Ql */
224 { termp_qq_pre
, termp_qq_post
}, /* Qo */
225 { termp_qq_pre
, termp_qq_post
}, /* Qq */
226 { NULL
, NULL
}, /* Re */
227 { termp_rs_pre
, NULL
}, /* Rs */
228 { NULL
, NULL
}, /* Sc */
229 { termp_sq_pre
, termp_sq_post
}, /* So */
230 { termp_sq_pre
, termp_sq_post
}, /* Sq */
231 { termp_sm_pre
, NULL
}, /* Sm */
232 { termp_under_pre
, NULL
}, /* Sx */
233 { termp_bold_pre
, NULL
}, /* Sy */
234 { NULL
, NULL
}, /* Tn */
235 { termp_xx_pre
, NULL
}, /* Ux */
236 { NULL
, NULL
}, /* Xc */
237 { NULL
, NULL
}, /* Xo */
238 { termp_fo_pre
, termp_fo_post
}, /* Fo */
239 { NULL
, NULL
}, /* Fc */
240 { termp_op_pre
, termp_op_post
}, /* Oo */
241 { NULL
, NULL
}, /* Oc */
242 { NULL
, NULL
}, /* Bk */
243 { NULL
, NULL
}, /* Ek */
244 { termp_bt_pre
, NULL
}, /* Bt */
245 { NULL
, NULL
}, /* Hf */
246 { NULL
, NULL
}, /* Fr */
247 { termp_ud_pre
, NULL
}, /* Ud */
248 { NULL
, termp_lb_post
}, /* Lb */
249 { termp_sp_pre
, NULL
}, /* Lp */
250 { termp_lk_pre
, NULL
}, /* Lk */
251 { termp_under_pre
, NULL
}, /* Mt */
252 { termp_brq_pre
, termp_brq_post
}, /* Brq */
253 { termp_brq_pre
, termp_brq_post
}, /* Bro */
254 { NULL
, NULL
}, /* Brc */
255 { NULL
, termp____post
}, /* %C */
256 { NULL
, NULL
}, /* Es */ /* TODO */
257 { NULL
, NULL
}, /* En */ /* TODO */
258 { termp_xx_pre
, NULL
}, /* Dx */
259 { NULL
, termp____post
}, /* %Q */
260 { termp_sp_pre
, NULL
}, /* br */
261 { termp_sp_pre
, NULL
}, /* sp */
262 { termp_under_pre
, termp____post
}, /* %U */
267 terminal_mdoc(void *arg
, const struct mdoc
*mdoc
)
269 const struct mdoc_node
*n
;
270 const struct mdoc_meta
*m
;
273 p
= (struct termp
*)arg
;
276 p
->maxrmargin
= p
->defrmargin
;
278 if (NULL
== p
->symtab
)
280 case (TERMENC_ASCII
):
281 p
->symtab
= chars_init(CHARS_ASCII
);
291 print_mdoc_head(p
, NULL
, m
, n
);
293 print_mdoc_nodelist(p
, NULL
, m
, n
->child
);
294 print_foot(p
, NULL
, m
, n
);
299 print_mdoc_nodelist(DECL_ARGS
)
302 print_mdoc_node(p
, pair
, m
, n
);
304 print_mdoc_nodelist(p
, pair
, m
, n
->next
);
310 print_mdoc_node(DECL_ARGS
)
314 struct termpair npair
;
315 size_t offset
, rmargin
;
319 rmargin
= p
->rmargin
;
320 font
= term_fontq(p
);
322 memset(&npair
, 0, sizeof(struct termpair
));
325 if (MDOC_TEXT
!= n
->type
) {
326 if (termacts
[n
->tok
].pre
)
327 chld
= (*termacts
[n
->tok
].pre
)(p
, &npair
, m
, n
);
329 term_word(p
, n
->string
);
331 if (chld
&& n
->child
)
332 print_mdoc_nodelist(p
, &npair
, m
, n
->child
);
334 term_fontpopq(p
, font
);
336 if (MDOC_TEXT
!= n
->type
)
337 if (termacts
[n
->tok
].post
)
338 (*termacts
[n
->tok
].post
)(p
, &npair
, m
, n
);
340 if (MDOC_EOS
& n
->flags
)
341 p
->flags
|= TERMP_SENTENCE
;
344 p
->rmargin
= rmargin
;
350 print_foot(DECL_ARGS
)
352 char buf
[DATESIZ
], os
[BUFSIZ
];
354 term_fontrepl(p
, TERMFONT_NONE
);
357 * Output the footer in new-groff style, that is, three columns
358 * with the middle being the manual date and flanking columns
359 * being the operating system:
364 time2a(m
->date
, buf
, DATESIZ
);
365 strlcpy(os
, m
->os
, BUFSIZ
);
370 p
->rmargin
= (p
->maxrmargin
- strlen(buf
) + 1) / 2;
371 p
->flags
|= TERMP_NOSPACE
| TERMP_NOBREAK
;
376 p
->offset
= p
->rmargin
;
377 p
->rmargin
= p
->maxrmargin
- strlen(os
);
378 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
383 p
->offset
= p
->rmargin
;
384 p
->rmargin
= p
->maxrmargin
;
385 p
->flags
&= ~TERMP_NOBREAK
;
386 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
392 p
->rmargin
= p
->maxrmargin
;
399 print_mdoc_head(DECL_ARGS
)
401 char buf
[BUFSIZ
], title
[BUFSIZ
];
403 p
->rmargin
= p
->maxrmargin
;
407 * The header is strange. It has three components, which are
408 * really two with the first duplicated. It goes like this:
410 * IDENTIFIER TITLE IDENTIFIER
412 * The IDENTIFIER is NAME(SECTION), which is the command-name
413 * (if given, or "unknown" if not) followed by the manual page
414 * section. These are given in `Dt'. The TITLE is a free-form
415 * string depending on the manual volume. If not specified, it
416 * switches on the manual section.
420 strlcpy(buf
, m
->vol
, BUFSIZ
);
423 strlcat(buf
, " (", BUFSIZ
);
424 strlcat(buf
, m
->arch
, BUFSIZ
);
425 strlcat(buf
, ")", BUFSIZ
);
428 snprintf(title
, BUFSIZ
, "%s(%s)", m
->title
, m
->msec
);
431 p
->rmargin
= (p
->maxrmargin
- strlen(buf
) + 1) / 2;
432 p
->flags
|= TERMP_NOBREAK
| TERMP_NOSPACE
;
437 p
->offset
= p
->rmargin
;
438 p
->rmargin
= p
->maxrmargin
- strlen(title
);
439 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
444 p
->offset
= p
->rmargin
;
445 p
->rmargin
= p
->maxrmargin
;
446 p
->flags
&= ~TERMP_NOBREAK
;
447 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
453 p
->rmargin
= p
->maxrmargin
;
454 p
->flags
&= ~TERMP_NOSPACE
;
459 a2height(const struct mdoc_node
*n
)
463 assert(MDOC_TEXT
== n
->type
);
465 if ( ! a2roffsu(n
->string
, &su
, SCALE_VS
))
466 SCALE_VS_INIT(&su
, strlen(n
->string
));
468 return(term_vspan(&su
));
473 a2width(const struct mdoc_argv
*arg
, int pos
)
477 assert(arg
->value
[pos
]);
478 if ( ! a2roffsu(arg
->value
[pos
], &su
, SCALE_MAX
))
479 SCALE_HS_INIT(&su
, strlen(arg
->value
[pos
]));
481 return(term_hspan(&su
));
486 arg_disptype(const struct mdoc_node
*n
)
490 assert(MDOC_BLOCK
== n
->type
);
492 len
= (int)(n
->args
? n
->args
->argc
: 0);
494 for (i
= 0; i
< len
; i
++)
495 switch (n
->args
->argv
[i
].arg
) {
502 case (MDOC_Unfilled
):
505 return(n
->args
->argv
[i
].arg
);
515 a2offs(const struct mdoc_argv
*arg
)
519 if ('\0' == arg
->value
[0][0])
521 else if (0 == strcmp(arg
->value
[0], "left"))
523 else if (0 == strcmp(arg
->value
[0], "indent"))
525 else if (0 == strcmp(arg
->value
[0], "indent-two"))
526 return((INDENT
+ 1) * 2);
527 else if ( ! a2roffsu(arg
->value
[0], &su
, SCALE_MAX
))
528 SCALE_HS_INIT(&su
, strlen(arg
->value
[0]));
530 return(term_hspan(&su
));
535 * Return 1 if an argument has a particular argument value or 0 if it
536 * does not. See arg_getattr().
539 arg_hasattr(int arg
, const struct mdoc_node
*n
)
542 return(-1 != arg_getattr(arg
, n
));
547 * Get the index of an argument in a node's argument list or -1 if it
548 * does not exist. See arg_getattrs().
551 arg_getattr(int v
, const struct mdoc_node
*n
)
555 return(arg_getattrs(&v
, &val
, 1, n
) ? val
: -1);
560 * Walk through the argument list for a node and fill an array "vals"
561 * with the positions of the argument structures listed in "keys".
562 * Return the number of elements that were written into "vals", which
566 arg_getattrs(const int *keys
, int *vals
,
567 size_t sz
, const struct mdoc_node
*n
)
574 for (k
= i
= 0; i
< (int)n
->args
->argc
; i
++)
575 for (j
= 0; j
< (int)sz
; j
++)
576 if (n
->args
->argv
[i
].arg
== keys
[j
]) {
585 * Determine how much space to print out before block elements of `It'
586 * (and thus `Bl') and `Bd'. And then go ahead and print that space,
590 print_bvspace(struct termp
*p
,
591 const struct mdoc_node
*bl
,
592 const struct mdoc_node
*n
)
594 const struct mdoc_node
*nn
;
597 if (arg_hasattr(MDOC_Compact
, bl
))
600 /* Do not vspace directly after Ss/Sh. */
602 for (nn
= n
; nn
; nn
= nn
->parent
) {
603 if (MDOC_BLOCK
!= nn
->type
)
605 if (MDOC_Ss
== nn
->tok
)
607 if (MDOC_Sh
== nn
->tok
)
609 if (NULL
== nn
->prev
)
614 /* A `-column' does not assert vspace within the list. */
616 if (MDOC_Bl
== bl
->tok
&& LIST_diag
== bl
->data
.list
)
617 if (n
->prev
&& MDOC_It
== n
->prev
->tok
)
620 /* A `-diag' without body does not vspace. */
622 if (MDOC_Bl
== bl
->tok
&& LIST_diag
== bl
->data
.list
)
623 if (n
->prev
&& MDOC_It
== n
->prev
->tok
) {
624 assert(n
->prev
->body
);
625 if (NULL
== n
->prev
->body
->child
)
635 termp_dq_pre(DECL_ARGS
)
638 if (MDOC_BODY
!= n
->type
)
641 term_word(p
, "\\(lq");
642 p
->flags
|= TERMP_NOSPACE
;
649 termp_dq_post(DECL_ARGS
)
652 if (MDOC_BODY
!= n
->type
)
655 p
->flags
|= TERMP_NOSPACE
;
656 term_word(p
, "\\(rq");
662 termp_it_pre(DECL_ARGS
)
664 const struct mdoc_node
*bl
, *nn
;
666 int i
, keys
[3], vals
[3];
667 size_t width
, offset
, ncols
, dcol
;
670 if (MDOC_BLOCK
== n
->type
) {
671 print_bvspace(p
, n
->parent
->parent
, n
);
675 bl
= n
->parent
->parent
->parent
;
677 /* Get list width, offset, and list type from argument list. */
679 keys
[0] = MDOC_Width
;
680 keys
[1] = MDOC_Offset
;
681 keys
[2] = MDOC_Column
;
683 vals
[0] = vals
[1] = vals
[2] = -1;
685 arg_getattrs(keys
, vals
, 3, bl
);
687 type
= bl
->data
.list
;
690 * First calculate width and offset. This is pretty easy unless
691 * we're a -column list, in which case all prior columns must
698 offset
= a2offs(&bl
->args
->argv
[vals
[1]]);
702 if (MDOC_BODY
== n
->type
)
705 * Imitate groff's column handling:
706 * - For each earlier column, add its width.
707 * - For less than 5 columns, add four more blanks per
709 * - For exactly 5 columns, add three more blank per
711 * - For more than 5 columns, add only one column.
713 ncols
= bl
->args
->argv
[vals
[2]].sz
;
715 dcol
= ncols
< 5 ? 4 : ncols
== 5 ? 3 : 1;
717 for (i
= 0, nn
= n
->prev
;
718 nn
&& i
< (int)ncols
;
720 offset
+= dcol
+ a2width
721 (&bl
->args
->argv
[vals
[2]], i
);
725 * When exceeding the declared number of columns, leave
726 * the remaining widths at 0. This will later be
727 * adjusted to the default width of 10, or, for the last
728 * column, stretched to the right margin.
734 * Use the declared column widths, extended as explained
735 * in the preceding paragraph.
737 width
= a2width(&bl
->args
->argv
[vals
[2]], i
) + dcol
;
744 * Note: buffer the width by 2, which is groff's magic
745 * number for buffering single arguments. See the above
746 * handling for column for how this changes.
748 width
= a2width(&bl
->args
->argv
[vals
[0]], 0) + 2;
753 * List-type can override the width in the case of fixed-head
754 * values (bullet, dash/hyphen, enum). Tags need a non-zero
786 * Whitespace control. Inset bodies need an initial space,
787 * while diagonal bodies need two.
790 p
->flags
|= TERMP_NOSPACE
;
794 if (MDOC_BODY
== n
->type
)
795 term_word(p
, "\\ \\ ");
798 if (MDOC_BODY
== n
->type
)
805 p
->flags
|= TERMP_NOSPACE
;
809 if (MDOC_HEAD
== n
->type
)
810 term_fontpush(p
, TERMFONT_BOLD
);
817 * Pad and break control. This is the tricky part. These flags
818 * are documented in term_flushln() in term.c. Note that we're
819 * going to unset all of these flags in termp_it_post() when we
831 if (MDOC_HEAD
== n
->type
)
832 p
->flags
|= TERMP_NOBREAK
;
834 p
->flags
|= TERMP_NOLPAD
;
837 if (MDOC_HEAD
== n
->type
)
838 p
->flags
|= TERMP_NOBREAK
;
840 p
->flags
|= TERMP_NOLPAD
;
842 if (MDOC_HEAD
!= n
->type
)
846 * This is ugly. If `-hang' is specified and the body
847 * is a `Bl' or `Bd', then we want basically to nullify
848 * the "overstep" effect in term_flushln() and treat
849 * this as a `-ohang' list instead.
851 if (n
->next
->child
&&
852 (MDOC_Bl
== n
->next
->child
->tok
||
853 MDOC_Bd
== n
->next
->child
->tok
)) {
854 p
->flags
&= ~TERMP_NOBREAK
;
855 p
->flags
&= ~TERMP_NOLPAD
;
857 p
->flags
|= TERMP_HANG
;
860 if (MDOC_HEAD
== n
->type
)
861 p
->flags
|= TERMP_NOBREAK
| TERMP_TWOSPACE
;
863 p
->flags
|= TERMP_NOLPAD
;
865 if (MDOC_HEAD
!= n
->type
)
867 if (NULL
== n
->next
|| NULL
== n
->next
->child
)
868 p
->flags
|= TERMP_DANGLE
;
871 if (MDOC_HEAD
== n
->type
) {
873 if (MDOC_BODY
== n
->next
->type
)
874 p
->flags
&= ~TERMP_NOBREAK
;
876 p
->flags
|= TERMP_NOBREAK
;
878 p
->flags
|= TERMP_NOLPAD
;
882 if (MDOC_HEAD
== n
->type
)
883 p
->flags
|= TERMP_NOBREAK
;
890 * Margin control. Set-head-width lists have their right
891 * margins shortened. The body for these lists has the offset
892 * necessarily lengthened. Everybody gets the offset.
900 * Same stipulation as above, regarding `-hang'. We
901 * don't want to recalculate rmargin and offsets when
902 * using `Bd' or `Bl' within `-hang' overstep lists.
904 if (MDOC_HEAD
== n
->type
&& n
->next
->child
&&
905 (MDOC_Bl
== n
->next
->child
->tok
||
906 MDOC_Bd
== n
->next
->child
->tok
))
919 if (MDOC_HEAD
== n
->type
)
920 p
->rmargin
= p
->offset
+ width
;
926 p
->rmargin
= p
->offset
+ width
;
928 * XXX - this behaviour is not documented: the
929 * right-most column is filled to the right margin.
931 if (MDOC_HEAD
== n
->type
&&
932 MDOC_BODY
== n
->next
->type
&&
933 p
->rmargin
< p
->maxrmargin
)
934 p
->rmargin
= p
->maxrmargin
;
941 * The dash, hyphen, bullet and enum lists all have a special
942 * HEAD character (temporarily bold, in some cases).
945 if (MDOC_HEAD
== n
->type
)
948 term_fontpush(p
, TERMFONT_BOLD
);
949 term_word(p
, "\\[bu]");
955 term_fontpush(p
, TERMFONT_BOLD
);
956 term_word(p
, "\\(hy");
960 (pair
->ppair
->ppair
->count
)++;
961 snprintf(buf
, sizeof(buf
), "%d.",
962 pair
->ppair
->ppair
->count
);
970 * If we're not going to process our children, indicate so here.
983 if (MDOC_HEAD
== n
->type
)
987 if (MDOC_BODY
== n
->type
)
1000 termp_it_post(DECL_ARGS
)
1002 enum mdoc_list type
;
1004 if (MDOC_BLOCK
== n
->type
)
1007 type
= n
->parent
->parent
->parent
->data
.list
;
1015 if (MDOC_BODY
== n
->type
)
1019 if (MDOC_HEAD
== n
->type
)
1028 * Now that our output is flushed, we can reset our tags. Since
1029 * only `It' sets these flags, we're free to assume that nobody
1030 * has munged them in the meanwhile.
1033 p
->flags
&= ~TERMP_DANGLE
;
1034 p
->flags
&= ~TERMP_NOBREAK
;
1035 p
->flags
&= ~TERMP_TWOSPACE
;
1036 p
->flags
&= ~TERMP_NOLPAD
;
1037 p
->flags
&= ~TERMP_HANG
;
1043 termp_nm_pre(DECL_ARGS
)
1046 if (NULL
== n
->child
&& NULL
== m
->name
)
1049 if (SEC_SYNOPSIS
== n
->sec
&& MDOC_LINE
& n
->flags
)
1052 term_fontpush(p
, TERMFONT_BOLD
);
1054 if (NULL
== n
->child
)
1055 term_word(p
, m
->name
);
1063 termp_fl_pre(DECL_ARGS
)
1066 term_fontpush(p
, TERMFONT_BOLD
);
1067 term_word(p
, "\\-");
1070 p
->flags
|= TERMP_NOSPACE
;
1071 else if (n
->next
&& n
->next
->line
== n
->line
)
1072 p
->flags
|= TERMP_NOSPACE
;
1080 termp_an_pre(DECL_ARGS
)
1083 if (NULL
== n
->child
)
1087 * If not in the AUTHORS section, `An -split' will cause
1088 * newlines to occur before the author name. If in the AUTHORS
1089 * section, by default, the first `An' invocation is nosplit,
1090 * then all subsequent ones, regardless of whether interspersed
1091 * with other macros/text, are split. -split, in this case,
1092 * will override the condition of the implied first -nosplit.
1095 if (n
->sec
== SEC_AUTHORS
) {
1096 if ( ! (TERMP_ANPREC
& p
->flags
)) {
1097 if (TERMP_SPLIT
& p
->flags
)
1101 if (TERMP_NOSPLIT
& p
->flags
)
1107 if (TERMP_SPLIT
& p
->flags
)
1116 termp_an_post(DECL_ARGS
)
1120 if (SEC_AUTHORS
== n
->sec
)
1121 p
->flags
|= TERMP_ANPREC
;
1125 if (arg_hasattr(MDOC_Split
, n
)) {
1126 p
->flags
&= ~TERMP_NOSPLIT
;
1127 p
->flags
|= TERMP_SPLIT
;
1129 p
->flags
&= ~TERMP_SPLIT
;
1130 p
->flags
|= TERMP_NOSPLIT
;
1138 termp_ns_pre(DECL_ARGS
)
1141 p
->flags
|= TERMP_NOSPACE
;
1148 termp_rs_pre(DECL_ARGS
)
1151 if (SEC_SEE_ALSO
!= n
->sec
)
1153 if (MDOC_BLOCK
== n
->type
&& n
->prev
)
1161 termp_rv_pre(DECL_ARGS
)
1163 const struct mdoc_node
*nn
;
1166 term_word(p
, "The");
1168 for (nn
= n
->child
; nn
; nn
= nn
->next
) {
1169 term_fontpush(p
, TERMFONT_BOLD
);
1170 term_word(p
, nn
->string
);
1172 p
->flags
|= TERMP_NOSPACE
;
1173 if (nn
->next
&& NULL
== nn
->next
->next
)
1174 term_word(p
, "(), and");
1176 term_word(p
, "(),");
1181 if (n
->child
&& n
->child
->next
)
1182 term_word(p
, "functions return");
1184 term_word(p
, "function returns");
1186 term_word(p
, "the value 0 if successful; otherwise the value "
1187 "-1 is returned and the global variable");
1189 term_fontpush(p
, TERMFONT_UNDER
);
1190 term_word(p
, "errno");
1193 term_word(p
, "is set to indicate the error.");
1201 termp_ex_pre(DECL_ARGS
)
1203 const struct mdoc_node
*nn
;
1205 term_word(p
, "The");
1207 for (nn
= n
->child
; nn
; nn
= nn
->next
) {
1208 term_fontpush(p
, TERMFONT_BOLD
);
1209 term_word(p
, nn
->string
);
1211 p
->flags
|= TERMP_NOSPACE
;
1212 if (nn
->next
&& NULL
== nn
->next
->next
)
1213 term_word(p
, ", and");
1217 p
->flags
&= ~TERMP_NOSPACE
;
1220 if (n
->child
&& n
->child
->next
)
1221 term_word(p
, "utilities exit");
1223 term_word(p
, "utility exits");
1225 term_word(p
, "0 on success, and >0 if an error occurs.");
1233 termp_nd_pre(DECL_ARGS
)
1236 if (MDOC_BODY
!= n
->type
)
1239 #if defined(__OpenBSD__) || defined(__linux__)
1240 term_word(p
, "\\(en");
1242 term_word(p
, "\\(em");
1250 termp_bl_pre(DECL_ARGS
)
1253 return(MDOC_HEAD
!= n
->type
);
1259 termp_bl_post(DECL_ARGS
)
1262 if (MDOC_BLOCK
== n
->type
)
1269 termp_op_post(DECL_ARGS
)
1272 if (MDOC_BODY
!= n
->type
)
1274 p
->flags
|= TERMP_NOSPACE
;
1275 term_word(p
, "\\(rB");
1281 termp_xr_pre(DECL_ARGS
)
1283 const struct mdoc_node
*nn
;
1285 if (NULL
== n
->child
)
1288 assert(MDOC_TEXT
== n
->child
->type
);
1291 term_word(p
, nn
->string
);
1292 if (NULL
== (nn
= nn
->next
))
1294 p
->flags
|= TERMP_NOSPACE
;
1296 p
->flags
|= TERMP_NOSPACE
;
1297 term_word(p
, nn
->string
);
1298 p
->flags
|= TERMP_NOSPACE
;
1306 termp_vt_pre(DECL_ARGS
)
1309 if (MDOC_ELEM
== n
->type
)
1310 return(termp_under_pre(p
, pair
, m
, n
));
1311 else if (MDOC_HEAD
== n
->type
)
1313 else if (MDOC_BLOCK
== n
->type
)
1316 return(termp_under_pre(p
, pair
, m
, n
));
1322 termp_vt_post(DECL_ARGS
)
1325 if (MDOC_BLOCK
!= n
->type
)
1327 if (n
->next
&& MDOC_Vt
== n
->next
->tok
)
1336 termp_bold_pre(DECL_ARGS
)
1339 term_fontpush(p
, TERMFONT_BOLD
);
1346 termp_fd_post(DECL_ARGS
)
1349 if (n
->sec
!= SEC_SYNOPSIS
|| ! (MDOC_LINE
& n
->flags
))
1353 if (n
->next
&& MDOC_Fd
!= n
->next
->tok
)
1360 termp_sh_pre(DECL_ARGS
)
1363 /* No vspace between consecutive `Sh' calls. */
1367 if (n
->prev
&& MDOC_Sh
== n
->prev
->tok
)
1368 if (NULL
== n
->prev
->body
->child
)
1373 term_fontpush(p
, TERMFONT_BOLD
);
1387 termp_sh_post(DECL_ARGS
)
1406 termp_op_pre(DECL_ARGS
)
1411 term_word(p
, "\\(lB");
1412 p
->flags
|= TERMP_NOSPACE
;
1423 termp_bt_pre(DECL_ARGS
)
1426 term_word(p
, "is currently in beta test.");
1433 termp_lb_post(DECL_ARGS
)
1436 if (SEC_LIBRARY
== n
->sec
&& MDOC_LINE
& n
->flags
)
1443 termp_ud_pre(DECL_ARGS
)
1446 term_word(p
, "currently under development.");
1453 termp_d1_pre(DECL_ARGS
)
1456 if (MDOC_BLOCK
!= n
->type
)
1459 p
->offset
+= (INDENT
+ 1);
1466 termp_d1_post(DECL_ARGS
)
1469 if (MDOC_BLOCK
!= n
->type
)
1477 termp_aq_pre(DECL_ARGS
)
1480 if (MDOC_BODY
!= n
->type
)
1482 term_word(p
, "\\(la");
1483 p
->flags
|= TERMP_NOSPACE
;
1490 termp_aq_post(DECL_ARGS
)
1493 if (MDOC_BODY
!= n
->type
)
1495 p
->flags
|= TERMP_NOSPACE
;
1496 term_word(p
, "\\(ra");
1502 termp_ft_pre(DECL_ARGS
)
1505 if (SEC_SYNOPSIS
== n
->sec
&& MDOC_LINE
& n
->flags
)
1506 if (n
->prev
&& MDOC_Fo
== n
->prev
->tok
)
1509 term_fontpush(p
, TERMFONT_UNDER
);
1516 termp_ft_post(DECL_ARGS
)
1519 if (SEC_SYNOPSIS
== n
->sec
&& MDOC_LINE
& n
->flags
)
1526 termp_fn_pre(DECL_ARGS
)
1528 const struct mdoc_node
*nn
;
1530 term_fontpush(p
, TERMFONT_BOLD
);
1531 term_word(p
, n
->child
->string
);
1534 p
->flags
|= TERMP_NOSPACE
;
1537 for (nn
= n
->child
->next
; nn
; nn
= nn
->next
) {
1538 term_fontpush(p
, TERMFONT_UNDER
);
1539 term_word(p
, nn
->string
);
1548 if (SEC_SYNOPSIS
== n
->sec
)
1557 termp_fn_post(DECL_ARGS
)
1560 if (n
->sec
== SEC_SYNOPSIS
&& n
->next
&& MDOC_LINE
& n
->flags
)
1567 termp_fa_pre(DECL_ARGS
)
1569 const struct mdoc_node
*nn
;
1571 if (n
->parent
->tok
!= MDOC_Fo
) {
1572 term_fontpush(p
, TERMFONT_UNDER
);
1576 for (nn
= n
->child
; nn
; nn
= nn
->next
) {
1577 term_fontpush(p
, TERMFONT_UNDER
);
1578 term_word(p
, nn
->string
);
1585 if (n
->child
&& n
->next
&& n
->next
->tok
== MDOC_Fa
)
1594 termp_bd_pre(DECL_ARGS
)
1598 const struct mdoc_node
*nn
;
1600 if (MDOC_BLOCK
== n
->type
) {
1601 print_bvspace(p
, n
, n
);
1603 } else if (MDOC_HEAD
== n
->type
)
1608 type
= arg_disptype(nn
);
1611 if (-1 != (i
= arg_getattr(MDOC_Offset
, nn
)))
1612 p
->offset
+= a2offs(&nn
->args
->argv
[i
]);
1615 * If -ragged or -filled are specified, the block does nothing
1616 * but change the indentation. If -unfilled or -literal are
1617 * specified, text is printed exactly as entered in the display:
1618 * for macro lines, a newline is appended to the line. Blank
1619 * lines are allowed.
1622 if (MDOC_Literal
!= type
&& MDOC_Unfilled
!= type
)
1626 rmax
= p
->maxrmargin
;
1627 p
->rmargin
= p
->maxrmargin
= TERM_MAXMARGIN
;
1629 for (nn
= n
->child
; nn
; nn
= nn
->next
) {
1630 p
->flags
|= TERMP_NOSPACE
;
1631 print_mdoc_node(p
, pair
, m
, nn
);
1632 if (NULL
== nn
->next
)
1634 if (nn
->prev
&& nn
->prev
->line
< nn
->line
)
1636 else if (NULL
== nn
->prev
)
1641 p
->maxrmargin
= rmax
;
1648 termp_bd_post(DECL_ARGS
)
1653 if (MDOC_BODY
!= n
->type
)
1656 type
= arg_disptype(n
->parent
);
1660 rmax
= p
->maxrmargin
;
1662 if (MDOC_Literal
== type
|| MDOC_Unfilled
== type
)
1663 p
->rmargin
= p
->maxrmargin
= TERM_MAXMARGIN
;
1665 p
->flags
|= TERMP_NOSPACE
;
1669 p
->maxrmargin
= rmax
;
1675 termp_qq_pre(DECL_ARGS
)
1678 if (MDOC_BODY
!= n
->type
)
1681 p
->flags
|= TERMP_NOSPACE
;
1688 termp_qq_post(DECL_ARGS
)
1691 if (MDOC_BODY
!= n
->type
)
1693 p
->flags
|= TERMP_NOSPACE
;
1700 termp_bx_post(DECL_ARGS
)
1704 p
->flags
|= TERMP_NOSPACE
;
1705 term_word(p
, "BSD");
1711 termp_xx_pre(DECL_ARGS
)
1747 termp_sq_pre(DECL_ARGS
)
1750 if (MDOC_BODY
!= n
->type
)
1752 term_word(p
, "\\(oq");
1753 p
->flags
|= TERMP_NOSPACE
;
1760 termp_sq_post(DECL_ARGS
)
1763 if (MDOC_BODY
!= n
->type
)
1765 p
->flags
|= TERMP_NOSPACE
;
1766 term_word(p
, "\\(aq");
1772 termp_pf_pre(DECL_ARGS
)
1775 p
->flags
|= TERMP_IGNDELIM
;
1782 termp_pf_post(DECL_ARGS
)
1785 p
->flags
&= ~TERMP_IGNDELIM
;
1786 p
->flags
|= TERMP_NOSPACE
;
1792 termp_ss_pre(DECL_ARGS
)
1802 term_fontpush(p
, TERMFONT_BOLD
);
1803 p
->offset
= HALFINDENT
;
1815 termp_ss_post(DECL_ARGS
)
1818 if (MDOC_HEAD
== n
->type
)
1825 termp_cd_pre(DECL_ARGS
)
1828 term_fontpush(p
, TERMFONT_BOLD
);
1836 termp_in_pre(DECL_ARGS
)
1839 term_fontpush(p
, TERMFONT_BOLD
);
1840 if (SEC_SYNOPSIS
== n
->sec
)
1841 term_word(p
, "#include");
1844 p
->flags
|= TERMP_NOSPACE
;
1851 termp_in_post(DECL_ARGS
)
1854 term_fontpush(p
, TERMFONT_BOLD
);
1855 p
->flags
|= TERMP_NOSPACE
;
1859 if (SEC_SYNOPSIS
!= n
->sec
&& ! (MDOC_LINE
& n
->flags
))
1864 * XXX Not entirely correct. If `.In foo bar' is specified in
1865 * the SYNOPSIS section, then it produces a single break after
1866 * the <foo>; mandoc asserts a vertical space. Since this
1867 * construction is rarely used, I think it's fine.
1869 if (n
->next
&& MDOC_In
!= n
->next
->tok
)
1876 termp_sp_pre(DECL_ARGS
)
1882 len
= n
->child
? a2height(n
->child
) : 1;
1894 for (i
= 0; i
< len
; i
++)
1903 termp_brq_pre(DECL_ARGS
)
1906 if (MDOC_BODY
!= n
->type
)
1908 term_word(p
, "\\(lC");
1909 p
->flags
|= TERMP_NOSPACE
;
1916 termp_brq_post(DECL_ARGS
)
1919 if (MDOC_BODY
!= n
->type
)
1921 p
->flags
|= TERMP_NOSPACE
;
1922 term_word(p
, "\\(rC");
1928 termp_bq_pre(DECL_ARGS
)
1931 if (MDOC_BODY
!= n
->type
)
1933 term_word(p
, "\\(lB");
1934 p
->flags
|= TERMP_NOSPACE
;
1941 termp_bq_post(DECL_ARGS
)
1944 if (MDOC_BODY
!= n
->type
)
1946 p
->flags
|= TERMP_NOSPACE
;
1947 term_word(p
, "\\(rB");
1953 termp_pq_pre(DECL_ARGS
)
1956 if (MDOC_BODY
!= n
->type
)
1958 term_word(p
, "\\&(");
1959 p
->flags
|= TERMP_NOSPACE
;
1966 termp_pq_post(DECL_ARGS
)
1969 if (MDOC_BODY
!= n
->type
)
1977 termp_fo_pre(DECL_ARGS
)
1979 const struct mdoc_node
*nn
;
1981 if (MDOC_BODY
== n
->type
) {
1982 p
->flags
|= TERMP_NOSPACE
;
1984 p
->flags
|= TERMP_NOSPACE
;
1986 } else if (MDOC_HEAD
!= n
->type
)
1989 term_fontpush(p
, TERMFONT_BOLD
);
1990 for (nn
= n
->child
; nn
; nn
= nn
->next
) {
1991 assert(MDOC_TEXT
== nn
->type
);
1992 term_word(p
, nn
->string
);
2002 termp_fo_post(DECL_ARGS
)
2005 if (MDOC_BODY
!= n
->type
)
2007 p
->flags
|= TERMP_NOSPACE
;
2009 p
->flags
|= TERMP_NOSPACE
;
2017 termp_bf_pre(DECL_ARGS
)
2019 const struct mdoc_node
*nn
;
2021 if (MDOC_HEAD
== n
->type
)
2023 else if (MDOC_BLOCK
!= n
->type
)
2026 if (NULL
== (nn
= n
->head
->child
)) {
2027 if (arg_hasattr(MDOC_Emphasis
, n
))
2028 term_fontpush(p
, TERMFONT_UNDER
);
2029 else if (arg_hasattr(MDOC_Symbolic
, n
))
2030 term_fontpush(p
, TERMFONT_BOLD
);
2032 term_fontpush(p
, TERMFONT_NONE
);
2037 assert(MDOC_TEXT
== nn
->type
);
2038 if (0 == strcmp("Em", nn
->string
))
2039 term_fontpush(p
, TERMFONT_UNDER
);
2040 else if (0 == strcmp("Sy", nn
->string
))
2041 term_fontpush(p
, TERMFONT_BOLD
);
2043 term_fontpush(p
, TERMFONT_NONE
);
2051 termp_sm_pre(DECL_ARGS
)
2054 assert(n
->child
&& MDOC_TEXT
== n
->child
->type
);
2055 if (0 == strcmp("on", n
->child
->string
))
2056 p
->flags
&= ~TERMP_NONOSPACE
;
2058 p
->flags
|= TERMP_NONOSPACE
;
2066 termp_ap_pre(DECL_ARGS
)
2069 p
->flags
|= TERMP_NOSPACE
;
2070 term_word(p
, "\\(aq");
2071 p
->flags
|= TERMP_NOSPACE
;
2078 termp____post(DECL_ARGS
)
2083 p
->flags
|= TERMP_NOSPACE
;
2084 term_word(p
, n
->next
? "," : ".");
2090 termp_li_pre(DECL_ARGS
)
2093 term_fontpush(p
, TERMFONT_NONE
);
2100 termp_lk_pre(DECL_ARGS
)
2102 const struct mdoc_node
*nn
;
2104 term_fontpush(p
, TERMFONT_UNDER
);
2107 if (NULL
== nn
->next
)
2110 term_word(p
, nn
->string
);
2113 p
->flags
|= TERMP_NOSPACE
;
2116 term_fontpush(p
, TERMFONT_BOLD
);
2117 for (nn
= nn
->next
; nn
; nn
= nn
->next
)
2118 term_word(p
, nn
->string
);
2127 termp_under_pre(DECL_ARGS
)
2130 term_fontpush(p
, TERMFONT_UNDER
);