]>
git.cameronkatri.com Git - mandoc.git/blob - mdoc_term.c
fe657446e889160a18dd4e3f8e0f99708d5ddf7f
1 /* $Id: mdoc_term.c,v 1.149 2010/06/12 11:58:22 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>
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 char *);
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 void print_bvspace(struct termp
*,
64 const struct mdoc_node
*,
65 const struct mdoc_node
*);
66 static void print_mdoc_node(DECL_ARGS
);
67 static void print_mdoc_nodelist(DECL_ARGS
);
68 static void print_mdoc_head(struct termp
*, const void *);
69 static void print_mdoc_foot(struct termp
*, const void *);
70 static void synopsis_pre(struct termp
*,
71 const struct mdoc_node
*);
73 static void termp____post(DECL_ARGS
);
74 static void termp_an_post(DECL_ARGS
);
75 static void termp_aq_post(DECL_ARGS
);
76 static void termp_bd_post(DECL_ARGS
);
77 static void termp_bl_post(DECL_ARGS
);
78 static void termp_bq_post(DECL_ARGS
);
79 static void termp_brq_post(DECL_ARGS
);
80 static void termp_bx_post(DECL_ARGS
);
81 static void termp_d1_post(DECL_ARGS
);
82 static void termp_dq_post(DECL_ARGS
);
83 static int termp_fd_pre(DECL_ARGS
);
84 static void termp_fo_post(DECL_ARGS
);
85 static void termp_in_post(DECL_ARGS
);
86 static void termp_it_post(DECL_ARGS
);
87 static void termp_lb_post(DECL_ARGS
);
88 static void termp_op_post(DECL_ARGS
);
89 static void termp_pf_post(DECL_ARGS
);
90 static void termp_pq_post(DECL_ARGS
);
91 static void termp_qq_post(DECL_ARGS
);
92 static void termp_sh_post(DECL_ARGS
);
93 static void termp_sq_post(DECL_ARGS
);
94 static void termp_ss_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_fd_pre
, NULL
}, /* Fd */
165 { termp_fl_pre
, NULL
}, /* Fl */
166 { termp_fn_pre
, NULL
}, /* Fn */
167 { termp_ft_pre
, NULL
}, /* 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
, NULL
}, /* 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 */
261 { NULL
, NULL
}, /* Ta */
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
;
275 p
->maxrmargin
= p
->defrmargin
;
278 if (NULL
== p
->symtab
)
280 case (TERMENC_ASCII
):
281 p
->symtab
= chars_init(CHARS_ASCII
);
291 term_begin(p
, print_mdoc_head
, print_mdoc_foot
, m
);
294 print_mdoc_nodelist(p
, NULL
, m
, n
->child
);
301 print_mdoc_nodelist(DECL_ARGS
)
304 print_mdoc_node(p
, pair
, m
, n
);
306 print_mdoc_nodelist(p
, pair
, m
, n
->next
);
312 print_mdoc_node(DECL_ARGS
)
316 struct termpair npair
;
317 size_t offset
, rmargin
;
321 rmargin
= p
->rmargin
;
322 font
= term_fontq(p
);
324 memset(&npair
, 0, sizeof(struct termpair
));
327 if (MDOC_TEXT
!= n
->type
) {
328 if (termacts
[n
->tok
].pre
)
329 chld
= (*termacts
[n
->tok
].pre
)(p
, &npair
, m
, n
);
331 term_word(p
, n
->string
);
333 if (chld
&& n
->child
)
334 print_mdoc_nodelist(p
, &npair
, m
, n
->child
);
336 term_fontpopq(p
, font
);
338 if (MDOC_TEXT
!= n
->type
)
339 if (termacts
[n
->tok
].post
)
340 (*termacts
[n
->tok
].post
)(p
, &npair
, m
, n
);
342 if (MDOC_EOS
& n
->flags
)
343 p
->flags
|= TERMP_SENTENCE
;
346 p
->rmargin
= rmargin
;
351 print_mdoc_foot(struct termp
*p
, const void *arg
)
353 char buf
[DATESIZ
], os
[BUFSIZ
];
354 const struct mdoc_meta
*m
;
356 m
= (const struct mdoc_meta
*)arg
;
358 term_fontrepl(p
, TERMFONT_NONE
);
361 * Output the footer in new-groff style, that is, three columns
362 * with the middle being the manual date and flanking columns
363 * being the operating system:
368 time2a(m
->date
, buf
, DATESIZ
);
369 strlcpy(os
, m
->os
, BUFSIZ
);
374 p
->rmargin
= (p
->maxrmargin
- strlen(buf
) + 1) / 2;
375 p
->flags
|= TERMP_NOSPACE
| TERMP_NOBREAK
;
380 p
->offset
= p
->rmargin
;
381 p
->rmargin
= p
->maxrmargin
- strlen(os
);
382 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
387 p
->offset
= p
->rmargin
;
388 p
->rmargin
= p
->maxrmargin
;
389 p
->flags
&= ~TERMP_NOBREAK
;
390 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
396 p
->rmargin
= p
->maxrmargin
;
402 print_mdoc_head(struct termp
*p
, const void *arg
)
404 char buf
[BUFSIZ
], title
[BUFSIZ
];
405 const struct mdoc_meta
*m
;
407 m
= (const struct mdoc_meta
*)arg
;
409 p
->rmargin
= p
->maxrmargin
;
413 * The header is strange. It has three components, which are
414 * really two with the first duplicated. It goes like this:
416 * IDENTIFIER TITLE IDENTIFIER
418 * The IDENTIFIER is NAME(SECTION), which is the command-name
419 * (if given, or "unknown" if not) followed by the manual page
420 * section. These are given in `Dt'. The TITLE is a free-form
421 * string depending on the manual volume. If not specified, it
422 * switches on the manual section.
426 strlcpy(buf
, m
->vol
, BUFSIZ
);
429 strlcat(buf
, " (", BUFSIZ
);
430 strlcat(buf
, m
->arch
, BUFSIZ
);
431 strlcat(buf
, ")", BUFSIZ
);
434 snprintf(title
, BUFSIZ
, "%s(%s)", m
->title
, m
->msec
);
437 p
->rmargin
= (p
->maxrmargin
- strlen(buf
) + 1) / 2;
438 p
->flags
|= TERMP_NOBREAK
| TERMP_NOSPACE
;
443 p
->offset
= p
->rmargin
;
444 p
->rmargin
= p
->maxrmargin
- strlen(title
);
445 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
450 p
->offset
= p
->rmargin
;
451 p
->rmargin
= p
->maxrmargin
;
452 p
->flags
&= ~TERMP_NOBREAK
;
453 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
459 p
->rmargin
= p
->maxrmargin
;
460 p
->flags
&= ~TERMP_NOSPACE
;
465 a2height(const struct mdoc_node
*n
)
469 assert(MDOC_TEXT
== n
->type
);
471 if ( ! a2roffsu(n
->string
, &su
, SCALE_VS
))
472 SCALE_VS_INIT(&su
, strlen(n
->string
));
474 return(term_vspan(&su
));
479 a2width(const struct mdoc_argv
*arg
, int pos
)
483 assert(arg
->value
[pos
]);
484 if ( ! a2roffsu(arg
->value
[pos
], &su
, SCALE_MAX
))
485 SCALE_HS_INIT(&su
, strlen(arg
->value
[pos
]));
487 return(term_hspan(&su
));
492 a2offs(const char *v
)
498 else if (0 == strcmp(v
, "left"))
500 else if (0 == strcmp(v
, "indent"))
502 else if (0 == strcmp(v
, "indent-two"))
503 return((INDENT
+ 1) * 2);
504 else if ( ! a2roffsu(v
, &su
, SCALE_MAX
))
505 SCALE_HS_INIT(&su
, strlen(v
));
507 return(term_hspan(&su
));
512 * Return 1 if an argument has a particular argument value or 0 if it
513 * does not. See arg_getattr().
516 arg_hasattr(int arg
, const struct mdoc_node
*n
)
519 return(-1 != arg_getattr(arg
, n
));
524 * Get the index of an argument in a node's argument list or -1 if it
525 * does not exist. See arg_getattrs().
528 arg_getattr(int v
, const struct mdoc_node
*n
)
532 return(arg_getattrs(&v
, &val
, 1, n
) ? val
: -1);
537 * Walk through the argument list for a node and fill an array "vals"
538 * with the positions of the argument structures listed in "keys".
539 * Return the number of elements that were written into "vals", which
543 arg_getattrs(const int *keys
, int *vals
,
544 size_t sz
, const struct mdoc_node
*n
)
551 for (k
= i
= 0; i
< (int)n
->args
->argc
; i
++)
552 for (j
= 0; j
< (int)sz
; j
++)
553 if (n
->args
->argv
[i
].arg
== keys
[j
]) {
562 * Determine how much space to print out before block elements of `It'
563 * (and thus `Bl') and `Bd'. And then go ahead and print that space,
567 print_bvspace(struct termp
*p
,
568 const struct mdoc_node
*bl
,
569 const struct mdoc_node
*n
)
571 const struct mdoc_node
*nn
;
573 /* FIXME: MDOC_Bd == bl->tok && bl->data.Bd.comp */
575 if (arg_hasattr(MDOC_Compact
, bl
))
578 /* Do not vspace directly after Ss/Sh. */
580 for (nn
= n
; nn
; nn
= nn
->parent
) {
581 if (MDOC_BLOCK
!= nn
->type
)
583 if (MDOC_Ss
== nn
->tok
)
585 if (MDOC_Sh
== nn
->tok
)
587 if (NULL
== nn
->prev
)
592 /* A `-column' does not assert vspace within the list. */
594 if (MDOC_Bl
== bl
->tok
&& LIST_column
== bl
->data
.Bl
.type
)
595 if (n
->prev
&& MDOC_It
== n
->prev
->tok
)
598 /* A `-diag' without body does not vspace. */
600 if (MDOC_Bl
== bl
->tok
&& LIST_diag
== bl
->data
.Bl
.type
)
601 if (n
->prev
&& MDOC_It
== n
->prev
->tok
) {
602 assert(n
->prev
->body
);
603 if (NULL
== n
->prev
->body
->child
)
613 termp_dq_pre(DECL_ARGS
)
616 if (MDOC_BODY
!= n
->type
)
619 term_word(p
, "\\(lq");
620 p
->flags
|= TERMP_NOSPACE
;
627 termp_dq_post(DECL_ARGS
)
630 if (MDOC_BODY
!= n
->type
)
633 p
->flags
|= TERMP_NOSPACE
;
634 term_word(p
, "\\(rq");
640 termp_it_pre(DECL_ARGS
)
642 const struct mdoc_node
*bl
, *nn
;
644 int i
, keys
[3], vals
[3];
645 size_t width
, offset
, ncols
, dcol
;
648 if (MDOC_BLOCK
== n
->type
) {
649 print_bvspace(p
, n
->parent
->parent
, n
);
653 bl
= n
->parent
->parent
->parent
;
655 /* Get list width, offset, and list type from argument list. */
657 keys
[0] = MDOC_Width
;
658 keys
[1] = MDOC_Offset
;
659 keys
[2] = MDOC_Column
;
661 vals
[0] = vals
[1] = vals
[2] = -1;
663 arg_getattrs(keys
, vals
, 3, bl
);
665 type
= bl
->data
.Bl
.type
;
668 * First calculate width and offset. This is pretty easy unless
669 * we're a -column list, in which case all prior columns must
676 offset
= a2offs(bl
->args
->argv
[vals
[1]].value
[0]);
680 if (MDOC_HEAD
== n
->type
)
683 * Imitate groff's column handling:
684 * - For each earlier column, add its width.
685 * - For less than 5 columns, add four more blanks per
687 * - For exactly 5 columns, add three more blank per
689 * - For more than 5 columns, add only one column.
691 ncols
= bl
->args
->argv
[vals
[2]].sz
;
693 dcol
= ncols
< 5 ? 4 : ncols
== 5 ? 3 : 1;
696 * Calculate the offset by applying all prior MDOC_BODY,
697 * so we stop at the MDOC_HEAD (NULL == nn->prev).
700 for (i
= 0, nn
= n
->prev
;
701 nn
->prev
&& i
< (int)ncols
;
703 offset
+= dcol
+ a2width
704 (&bl
->args
->argv
[vals
[2]], i
);
708 * When exceeding the declared number of columns, leave
709 * the remaining widths at 0. This will later be
710 * adjusted to the default width of 10, or, for the last
711 * column, stretched to the right margin.
717 * Use the declared column widths, extended as explained
718 * in the preceding paragraph.
720 width
= a2width(&bl
->args
->argv
[vals
[2]], i
) + dcol
;
727 * Note: buffer the width by 2, which is groff's magic
728 * number for buffering single arguments. See the above
729 * handling for column for how this changes.
731 width
= a2width(&bl
->args
->argv
[vals
[0]], 0) + 2;
736 * List-type can override the width in the case of fixed-head
737 * values (bullet, dash/hyphen, enum). Tags need a non-zero
769 * Whitespace control. Inset bodies need an initial space,
770 * while diagonal bodies need two.
773 p
->flags
|= TERMP_NOSPACE
;
777 if (MDOC_BODY
== n
->type
)
778 term_word(p
, "\\ \\ ");
781 if (MDOC_BODY
== n
->type
)
788 p
->flags
|= TERMP_NOSPACE
;
792 if (MDOC_HEAD
== n
->type
)
793 term_fontpush(p
, TERMFONT_BOLD
);
800 * Pad and break control. This is the tricky part. These flags
801 * are documented in term_flushln() in term.c. Note that we're
802 * going to unset all of these flags in termp_it_post() when we
814 if (MDOC_HEAD
== n
->type
)
815 p
->flags
|= TERMP_NOBREAK
;
817 p
->flags
|= TERMP_NOLPAD
;
820 if (MDOC_HEAD
== n
->type
)
821 p
->flags
|= TERMP_NOBREAK
;
823 p
->flags
|= TERMP_NOLPAD
;
825 if (MDOC_HEAD
!= n
->type
)
829 * This is ugly. If `-hang' is specified and the body
830 * is a `Bl' or `Bd', then we want basically to nullify
831 * the "overstep" effect in term_flushln() and treat
832 * this as a `-ohang' list instead.
834 if (n
->next
->child
&&
835 (MDOC_Bl
== n
->next
->child
->tok
||
836 MDOC_Bd
== n
->next
->child
->tok
)) {
837 p
->flags
&= ~TERMP_NOBREAK
;
838 p
->flags
&= ~TERMP_NOLPAD
;
840 p
->flags
|= TERMP_HANG
;
843 if (MDOC_HEAD
== n
->type
)
844 p
->flags
|= TERMP_NOBREAK
| TERMP_TWOSPACE
;
846 p
->flags
|= TERMP_NOLPAD
;
848 if (MDOC_HEAD
!= n
->type
)
850 if (NULL
== n
->next
|| NULL
== n
->next
->child
)
851 p
->flags
|= TERMP_DANGLE
;
854 if (MDOC_HEAD
== n
->type
)
858 p
->flags
&= ~TERMP_NOBREAK
;
860 p
->flags
|= TERMP_NOBREAK
;
863 if (MDOC_BODY
== n
->prev
->type
)
864 p
->flags
|= TERMP_NOLPAD
;
868 if (MDOC_HEAD
== n
->type
)
869 p
->flags
|= TERMP_NOBREAK
;
876 * Margin control. Set-head-width lists have their right
877 * margins shortened. The body for these lists has the offset
878 * necessarily lengthened. Everybody gets the offset.
886 * Same stipulation as above, regarding `-hang'. We
887 * don't want to recalculate rmargin and offsets when
888 * using `Bd' or `Bl' within `-hang' overstep lists.
890 if (MDOC_HEAD
== n
->type
&& n
->next
->child
&&
891 (MDOC_Bl
== n
->next
->child
->tok
||
892 MDOC_Bd
== n
->next
->child
->tok
))
905 if (MDOC_HEAD
== n
->type
)
906 p
->rmargin
= p
->offset
+ width
;
912 p
->rmargin
= p
->offset
+ width
;
914 * XXX - this behaviour is not documented: the
915 * right-most column is filled to the right margin.
917 if (MDOC_HEAD
== n
->type
)
919 if (NULL
== n
->next
&& p
->rmargin
< p
->maxrmargin
)
920 p
->rmargin
= p
->maxrmargin
;
927 * The dash, hyphen, bullet and enum lists all have a special
928 * HEAD character (temporarily bold, in some cases).
931 if (MDOC_HEAD
== n
->type
)
934 term_fontpush(p
, TERMFONT_BOLD
);
935 term_word(p
, "\\[bu]");
941 term_fontpush(p
, TERMFONT_BOLD
);
942 term_word(p
, "\\(hy");
946 (pair
->ppair
->ppair
->count
)++;
947 snprintf(buf
, sizeof(buf
), "%d.",
948 pair
->ppair
->ppair
->count
);
956 * If we're not going to process our children, indicate so here.
969 if (MDOC_HEAD
== n
->type
)
973 if (MDOC_HEAD
== n
->type
)
986 termp_it_post(DECL_ARGS
)
990 if (MDOC_BLOCK
== n
->type
)
993 type
= n
->parent
->parent
->parent
->data
.Bl
.type
;
1001 if (MDOC_BODY
== n
->type
)
1005 if (MDOC_BODY
== n
->type
)
1014 * Now that our output is flushed, we can reset our tags. Since
1015 * only `It' sets these flags, we're free to assume that nobody
1016 * has munged them in the meanwhile.
1019 p
->flags
&= ~TERMP_DANGLE
;
1020 p
->flags
&= ~TERMP_NOBREAK
;
1021 p
->flags
&= ~TERMP_TWOSPACE
;
1022 p
->flags
&= ~TERMP_NOLPAD
;
1023 p
->flags
&= ~TERMP_HANG
;
1029 termp_nm_pre(DECL_ARGS
)
1032 if (NULL
== n
->child
&& NULL
== m
->name
)
1037 term_fontpush(p
, TERMFONT_BOLD
);
1038 if (NULL
== n
->child
)
1039 term_word(p
, m
->name
);
1046 termp_fl_pre(DECL_ARGS
)
1049 term_fontpush(p
, TERMFONT_BOLD
);
1050 term_word(p
, "\\-");
1053 p
->flags
|= TERMP_NOSPACE
;
1054 else if (n
->next
&& n
->next
->line
== n
->line
)
1055 p
->flags
|= TERMP_NOSPACE
;
1063 termp_an_pre(DECL_ARGS
)
1066 if (NULL
== n
->child
)
1070 * If not in the AUTHORS section, `An -split' will cause
1071 * newlines to occur before the author name. If in the AUTHORS
1072 * section, by default, the first `An' invocation is nosplit,
1073 * then all subsequent ones, regardless of whether interspersed
1074 * with other macros/text, are split. -split, in this case,
1075 * will override the condition of the implied first -nosplit.
1078 if (n
->sec
== SEC_AUTHORS
) {
1079 if ( ! (TERMP_ANPREC
& p
->flags
)) {
1080 if (TERMP_SPLIT
& p
->flags
)
1084 if (TERMP_NOSPLIT
& p
->flags
)
1090 if (TERMP_SPLIT
& p
->flags
)
1099 termp_an_post(DECL_ARGS
)
1103 if (SEC_AUTHORS
== n
->sec
)
1104 p
->flags
|= TERMP_ANPREC
;
1108 if (arg_hasattr(MDOC_Split
, n
)) {
1109 p
->flags
&= ~TERMP_NOSPLIT
;
1110 p
->flags
|= TERMP_SPLIT
;
1112 p
->flags
&= ~TERMP_SPLIT
;
1113 p
->flags
|= TERMP_NOSPLIT
;
1121 termp_ns_pre(DECL_ARGS
)
1124 p
->flags
|= TERMP_NOSPACE
;
1131 termp_rs_pre(DECL_ARGS
)
1134 if (SEC_SEE_ALSO
!= n
->sec
)
1136 if (MDOC_BLOCK
== n
->type
&& n
->prev
)
1144 termp_rv_pre(DECL_ARGS
)
1146 const struct mdoc_node
*nn
;
1149 term_word(p
, "The");
1151 for (nn
= n
->child
; nn
; nn
= nn
->next
) {
1152 term_fontpush(p
, TERMFONT_BOLD
);
1153 term_word(p
, nn
->string
);
1155 p
->flags
|= TERMP_NOSPACE
;
1156 if (nn
->next
&& NULL
== nn
->next
->next
)
1157 term_word(p
, "(), and");
1159 term_word(p
, "(),");
1164 if (n
->child
&& n
->child
->next
)
1165 term_word(p
, "functions return");
1167 term_word(p
, "function returns");
1169 term_word(p
, "the value 0 if successful; otherwise the value "
1170 "-1 is returned and the global variable");
1172 term_fontpush(p
, TERMFONT_UNDER
);
1173 term_word(p
, "errno");
1176 term_word(p
, "is set to indicate the error.");
1177 p
->flags
|= TERMP_SENTENCE
;
1185 termp_ex_pre(DECL_ARGS
)
1187 const struct mdoc_node
*nn
;
1189 term_word(p
, "The");
1191 for (nn
= n
->child
; nn
; nn
= nn
->next
) {
1192 term_fontpush(p
, TERMFONT_BOLD
);
1193 term_word(p
, nn
->string
);
1195 p
->flags
|= TERMP_NOSPACE
;
1196 if (nn
->next
&& NULL
== nn
->next
->next
)
1197 term_word(p
, ", and");
1201 p
->flags
&= ~TERMP_NOSPACE
;
1204 if (n
->child
&& n
->child
->next
)
1205 term_word(p
, "utilities exit");
1207 term_word(p
, "utility exits");
1209 term_word(p
, "0 on success, and >0 if an error occurs.");
1210 p
->flags
|= TERMP_SENTENCE
;
1218 termp_nd_pre(DECL_ARGS
)
1221 if (MDOC_BODY
!= n
->type
)
1224 #if defined(__OpenBSD__) || defined(__linux__)
1225 term_word(p
, "\\(en");
1227 term_word(p
, "\\(em");
1235 termp_bl_pre(DECL_ARGS
)
1238 return(MDOC_HEAD
!= n
->type
);
1244 termp_bl_post(DECL_ARGS
)
1247 if (MDOC_BLOCK
== n
->type
)
1254 termp_op_post(DECL_ARGS
)
1257 if (MDOC_BODY
!= n
->type
)
1259 p
->flags
|= TERMP_NOSPACE
;
1260 term_word(p
, "\\(rB");
1266 termp_xr_pre(DECL_ARGS
)
1268 const struct mdoc_node
*nn
;
1270 if (NULL
== n
->child
)
1273 assert(MDOC_TEXT
== n
->child
->type
);
1276 term_word(p
, nn
->string
);
1277 if (NULL
== (nn
= nn
->next
))
1279 p
->flags
|= TERMP_NOSPACE
;
1281 p
->flags
|= TERMP_NOSPACE
;
1282 term_word(p
, nn
->string
);
1283 p
->flags
|= TERMP_NOSPACE
;
1291 * This decides how to assert whitespace before any of the SYNOPSIS set
1292 * of macros (which, as in the case of Ft/Fo and Ft/Fn, may contain
1296 synopsis_pre(struct termp
*p
, const struct mdoc_node
*n
)
1299 * Obviously, if we're not in a SYNOPSIS or no prior macros
1300 * exist, do nothing.
1302 if (NULL
== n
->prev
|| SEC_SYNOPSIS
!= n
->sec
)
1306 * If we're the second in a pair of like elements, emit our
1307 * newline and return. UNLESS we're `Fo', `Fn', `Fn', in which
1308 * case we soldier on.
1310 if (n
->prev
->tok
== n
->tok
&&
1311 MDOC_Ft
!= n
->tok
&&
1312 MDOC_Fo
!= n
->tok
&&
1313 MDOC_Fn
!= n
->tok
) {
1319 * If we're one of the SYNOPSIS set and non-like pair-wise after
1320 * another (or Fn/Fo, which we've let slip through) then assert
1321 * vertical space, else only newline and move on.
1323 switch (n
->prev
->tok
) {
1336 if (MDOC_Fn
!= n
->tok
&& MDOC_Fo
!= n
->tok
) {
1349 termp_vt_pre(DECL_ARGS
)
1352 if (MDOC_ELEM
== n
->type
) {
1354 return(termp_under_pre(p
, pair
, m
, n
));
1355 } else if (MDOC_BLOCK
== n
->type
) {
1358 } else if (MDOC_HEAD
== n
->type
)
1361 return(termp_under_pre(p
, pair
, m
, n
));
1367 termp_bold_pre(DECL_ARGS
)
1370 term_fontpush(p
, TERMFONT_BOLD
);
1377 termp_fd_pre(DECL_ARGS
)
1381 return(termp_bold_pre(p
, pair
, m
, n
));
1387 termp_sh_pre(DECL_ARGS
)
1390 /* No vspace between consecutive `Sh' calls. */
1394 if (n
->prev
&& MDOC_Sh
== n
->prev
->tok
)
1395 if (NULL
== n
->prev
->body
->child
)
1400 term_fontpush(p
, TERMFONT_BOLD
);
1414 termp_sh_post(DECL_ARGS
)
1433 termp_op_pre(DECL_ARGS
)
1438 term_word(p
, "\\(lB");
1439 p
->flags
|= TERMP_NOSPACE
;
1450 termp_bt_pre(DECL_ARGS
)
1453 term_word(p
, "is currently in beta test.");
1454 p
->flags
|= TERMP_SENTENCE
;
1461 termp_lb_post(DECL_ARGS
)
1464 if (SEC_LIBRARY
== n
->sec
&& MDOC_LINE
& n
->flags
)
1471 termp_ud_pre(DECL_ARGS
)
1474 term_word(p
, "currently under development.");
1475 p
->flags
|= TERMP_SENTENCE
;
1482 termp_d1_pre(DECL_ARGS
)
1485 if (MDOC_BLOCK
!= n
->type
)
1488 p
->offset
+= (INDENT
+ 1);
1495 termp_d1_post(DECL_ARGS
)
1498 if (MDOC_BLOCK
!= n
->type
)
1506 termp_aq_pre(DECL_ARGS
)
1509 if (MDOC_BODY
!= n
->type
)
1511 term_word(p
, "\\(la");
1512 p
->flags
|= TERMP_NOSPACE
;
1519 termp_aq_post(DECL_ARGS
)
1522 if (MDOC_BODY
!= n
->type
)
1524 p
->flags
|= TERMP_NOSPACE
;
1525 term_word(p
, "\\(ra");
1531 termp_ft_pre(DECL_ARGS
)
1534 /* NB: MDOC_LINE does not effect this! */
1536 term_fontpush(p
, TERMFONT_UNDER
);
1543 termp_fn_pre(DECL_ARGS
)
1545 const struct mdoc_node
*nn
;
1549 term_fontpush(p
, TERMFONT_BOLD
);
1550 term_word(p
, n
->child
->string
);
1553 p
->flags
|= TERMP_NOSPACE
;
1556 for (nn
= n
->child
->next
; nn
; nn
= nn
->next
) {
1557 term_fontpush(p
, TERMFONT_UNDER
);
1558 term_word(p
, nn
->string
);
1567 if (SEC_SYNOPSIS
== n
->sec
)
1576 termp_fa_pre(DECL_ARGS
)
1578 const struct mdoc_node
*nn
;
1580 if (n
->parent
->tok
!= MDOC_Fo
) {
1581 term_fontpush(p
, TERMFONT_UNDER
);
1585 for (nn
= n
->child
; nn
; nn
= nn
->next
) {
1586 term_fontpush(p
, TERMFONT_UNDER
);
1587 term_word(p
, nn
->string
);
1594 if (n
->child
&& n
->next
&& n
->next
->tok
== MDOC_Fa
)
1603 termp_bd_pre(DECL_ARGS
)
1607 const struct mdoc_node
*nn
;
1609 if (MDOC_BLOCK
== n
->type
) {
1610 print_bvspace(p
, n
, n
);
1612 } else if (MDOC_HEAD
== n
->type
)
1615 if (n
->data
.Bd
.offs
)
1616 p
->offset
+= a2offs(n
->data
.Bd
.offs
);
1619 * If -ragged or -filled are specified, the block does nothing
1620 * but change the indentation. If -unfilled or -literal are
1621 * specified, text is printed exactly as entered in the display:
1622 * for macro lines, a newline is appended to the line. Blank
1623 * lines are allowed.
1626 if (DISP_literal
!= n
->data
.Bd
.type
&&
1627 DISP_unfilled
!= n
->data
.Bd
.type
)
1630 tabwidth
= p
->tabwidth
;
1633 rmax
= p
->maxrmargin
;
1634 p
->rmargin
= p
->maxrmargin
= TERM_MAXMARGIN
;
1636 for (nn
= n
->child
; nn
; nn
= nn
->next
) {
1637 p
->flags
|= TERMP_NOSPACE
;
1638 print_mdoc_node(p
, pair
, m
, nn
);
1639 if (NULL
== nn
->prev
||
1640 nn
->prev
->line
< nn
->line
||
1645 p
->tabwidth
= tabwidth
;
1647 p
->maxrmargin
= rmax
;
1654 termp_bd_post(DECL_ARGS
)
1658 if (MDOC_BODY
!= n
->type
)
1662 rmax
= p
->maxrmargin
;
1664 if (DISP_literal
== n
->data
.Bd
.type
||
1665 DISP_unfilled
== n
->data
.Bd
.type
)
1666 p
->rmargin
= p
->maxrmargin
= TERM_MAXMARGIN
;
1668 p
->flags
|= TERMP_NOSPACE
;
1672 p
->maxrmargin
= rmax
;
1678 termp_qq_pre(DECL_ARGS
)
1681 if (MDOC_BODY
!= n
->type
)
1684 p
->flags
|= TERMP_NOSPACE
;
1691 termp_qq_post(DECL_ARGS
)
1694 if (MDOC_BODY
!= n
->type
)
1696 p
->flags
|= TERMP_NOSPACE
;
1703 termp_bx_post(DECL_ARGS
)
1707 p
->flags
|= TERMP_NOSPACE
;
1708 term_word(p
, "BSD");
1714 termp_xx_pre(DECL_ARGS
)
1750 termp_sq_pre(DECL_ARGS
)
1753 if (MDOC_BODY
!= n
->type
)
1755 term_word(p
, "\\(oq");
1756 p
->flags
|= TERMP_NOSPACE
;
1763 termp_sq_post(DECL_ARGS
)
1766 if (MDOC_BODY
!= n
->type
)
1768 p
->flags
|= TERMP_NOSPACE
;
1769 term_word(p
, "\\(aq");
1775 termp_pf_pre(DECL_ARGS
)
1778 p
->flags
|= TERMP_IGNDELIM
;
1785 termp_pf_post(DECL_ARGS
)
1788 p
->flags
&= ~TERMP_IGNDELIM
;
1789 p
->flags
|= TERMP_NOSPACE
;
1795 termp_ss_pre(DECL_ARGS
)
1805 term_fontpush(p
, TERMFONT_BOLD
);
1806 p
->offset
= HALFINDENT
;
1818 termp_ss_post(DECL_ARGS
)
1821 if (MDOC_HEAD
== n
->type
)
1828 termp_cd_pre(DECL_ARGS
)
1832 term_fontpush(p
, TERMFONT_BOLD
);
1839 termp_in_pre(DECL_ARGS
)
1844 if (SEC_SYNOPSIS
== n
->sec
&& MDOC_LINE
& n
->flags
) {
1845 term_fontpush(p
, TERMFONT_BOLD
);
1846 term_word(p
, "#include");
1850 term_fontpush(p
, TERMFONT_UNDER
);
1853 p
->flags
|= TERMP_NOSPACE
;
1860 termp_in_post(DECL_ARGS
)
1863 if (SEC_SYNOPSIS
== n
->sec
)
1864 term_fontpush(p
, TERMFONT_BOLD
);
1866 p
->flags
|= TERMP_NOSPACE
;
1869 if (SEC_SYNOPSIS
== n
->sec
)
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
)
1980 if (MDOC_BLOCK
== n
->type
) {
1983 } else if (MDOC_BODY
== n
->type
) {
1984 p
->flags
|= TERMP_NOSPACE
;
1986 p
->flags
|= TERMP_NOSPACE
;
1990 /* XXX: we drop non-initial arguments as per groff. */
1993 assert(n
->child
->string
);
1994 term_fontpush(p
, TERMFONT_BOLD
);
1995 term_word(p
, n
->child
->string
);
2002 termp_fo_post(DECL_ARGS
)
2005 if (MDOC_BODY
!= n
->type
)
2008 p
->flags
|= TERMP_NOSPACE
;
2011 if (SEC_SYNOPSIS
== n
->sec
) {
2012 p
->flags
|= TERMP_NOSPACE
;
2020 termp_bf_pre(DECL_ARGS
)
2022 const struct mdoc_node
*nn
;
2024 if (MDOC_HEAD
== n
->type
)
2026 else if (MDOC_BLOCK
!= n
->type
)
2029 if (NULL
== (nn
= n
->head
->child
)) {
2030 if (arg_hasattr(MDOC_Emphasis
, n
))
2031 term_fontpush(p
, TERMFONT_UNDER
);
2032 else if (arg_hasattr(MDOC_Symbolic
, n
))
2033 term_fontpush(p
, TERMFONT_BOLD
);
2035 term_fontpush(p
, TERMFONT_NONE
);
2040 assert(MDOC_TEXT
== nn
->type
);
2041 if (0 == strcmp("Em", nn
->string
))
2042 term_fontpush(p
, TERMFONT_UNDER
);
2043 else if (0 == strcmp("Sy", nn
->string
))
2044 term_fontpush(p
, TERMFONT_BOLD
);
2046 term_fontpush(p
, TERMFONT_NONE
);
2054 termp_sm_pre(DECL_ARGS
)
2057 assert(n
->child
&& MDOC_TEXT
== n
->child
->type
);
2058 if (0 == strcmp("on", n
->child
->string
))
2059 p
->flags
&= ~TERMP_NONOSPACE
;
2061 p
->flags
|= TERMP_NONOSPACE
;
2069 termp_ap_pre(DECL_ARGS
)
2072 p
->flags
|= TERMP_NOSPACE
;
2073 term_word(p
, "\\(aq");
2074 p
->flags
|= TERMP_NOSPACE
;
2081 termp____post(DECL_ARGS
)
2086 p
->flags
|= TERMP_NOSPACE
;
2087 term_word(p
, n
->next
? "," : ".");
2093 termp_li_pre(DECL_ARGS
)
2096 term_fontpush(p
, TERMFONT_NONE
);
2103 termp_lk_pre(DECL_ARGS
)
2105 const struct mdoc_node
*nn
;
2107 term_fontpush(p
, TERMFONT_UNDER
);
2110 if (NULL
== nn
->next
)
2113 term_word(p
, nn
->string
);
2116 p
->flags
|= TERMP_NOSPACE
;
2119 term_fontpush(p
, TERMFONT_BOLD
);
2120 for (nn
= nn
->next
; nn
; nn
= nn
->next
)
2121 term_word(p
, nn
->string
);
2130 termp_under_pre(DECL_ARGS
)
2133 term_fontpush(p
, TERMFONT_UNDER
);