]>
git.cameronkatri.com Git - mandoc.git/blob - mdoc_term.c
1 /* $Id: mdoc_term.c,v 1.144 2010/06/07 20:57:09 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 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(struct termp
*, const void *);
69 static void print_mdoc_nodelist(DECL_ARGS
);
70 static void print_foot(struct termp
*, const void *);
71 static void synopsis_pre(struct termp
*,
72 const struct mdoc_node
*);
74 static void termp____post(DECL_ARGS
);
75 static void termp_an_post(DECL_ARGS
);
76 static void termp_aq_post(DECL_ARGS
);
77 static void termp_bd_post(DECL_ARGS
);
78 static void termp_bl_post(DECL_ARGS
);
79 static void termp_bq_post(DECL_ARGS
);
80 static void termp_brq_post(DECL_ARGS
);
81 static void termp_bx_post(DECL_ARGS
);
82 static void termp_d1_post(DECL_ARGS
);
83 static void termp_dq_post(DECL_ARGS
);
84 static int termp_fd_pre(DECL_ARGS
);
85 static void termp_fo_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
);
97 static int termp_an_pre(DECL_ARGS
);
98 static int termp_ap_pre(DECL_ARGS
);
99 static int termp_aq_pre(DECL_ARGS
);
100 static int termp_bd_pre(DECL_ARGS
);
101 static int termp_bf_pre(DECL_ARGS
);
102 static int termp_bl_pre(DECL_ARGS
);
103 static int termp_bold_pre(DECL_ARGS
);
104 static int termp_bq_pre(DECL_ARGS
);
105 static int termp_brq_pre(DECL_ARGS
);
106 static int termp_bt_pre(DECL_ARGS
);
107 static int termp_cd_pre(DECL_ARGS
);
108 static int termp_d1_pre(DECL_ARGS
);
109 static int termp_dq_pre(DECL_ARGS
);
110 static int termp_ex_pre(DECL_ARGS
);
111 static int termp_fa_pre(DECL_ARGS
);
112 static int termp_fl_pre(DECL_ARGS
);
113 static int termp_fn_pre(DECL_ARGS
);
114 static int termp_fo_pre(DECL_ARGS
);
115 static int termp_ft_pre(DECL_ARGS
);
116 static int termp_in_pre(DECL_ARGS
);
117 static int termp_it_pre(DECL_ARGS
);
118 static int termp_li_pre(DECL_ARGS
);
119 static int termp_lk_pre(DECL_ARGS
);
120 static int termp_nd_pre(DECL_ARGS
);
121 static int termp_nm_pre(DECL_ARGS
);
122 static int termp_ns_pre(DECL_ARGS
);
123 static int termp_op_pre(DECL_ARGS
);
124 static int termp_pf_pre(DECL_ARGS
);
125 static int termp_pq_pre(DECL_ARGS
);
126 static int termp_qq_pre(DECL_ARGS
);
127 static int termp_rs_pre(DECL_ARGS
);
128 static int termp_rv_pre(DECL_ARGS
);
129 static int termp_sh_pre(DECL_ARGS
);
130 static int termp_sm_pre(DECL_ARGS
);
131 static int termp_sp_pre(DECL_ARGS
);
132 static int termp_sq_pre(DECL_ARGS
);
133 static int termp_ss_pre(DECL_ARGS
);
134 static int termp_under_pre(DECL_ARGS
);
135 static int termp_ud_pre(DECL_ARGS
);
136 static int termp_vt_pre(DECL_ARGS
);
137 static int termp_xr_pre(DECL_ARGS
);
138 static int termp_xx_pre(DECL_ARGS
);
140 static const struct termact termacts
[MDOC_MAX
] = {
141 { termp_ap_pre
, NULL
}, /* Ap */
142 { NULL
, NULL
}, /* Dd */
143 { NULL
, NULL
}, /* Dt */
144 { NULL
, NULL
}, /* Os */
145 { termp_sh_pre
, termp_sh_post
}, /* Sh */
146 { termp_ss_pre
, termp_ss_post
}, /* Ss */
147 { termp_sp_pre
, NULL
}, /* Pp */
148 { termp_d1_pre
, termp_d1_post
}, /* D1 */
149 { termp_d1_pre
, termp_d1_post
}, /* Dl */
150 { termp_bd_pre
, termp_bd_post
}, /* Bd */
151 { NULL
, NULL
}, /* Ed */
152 { termp_bl_pre
, termp_bl_post
}, /* Bl */
153 { NULL
, NULL
}, /* El */
154 { termp_it_pre
, termp_it_post
}, /* It */
155 { NULL
, NULL
}, /* Ad */
156 { termp_an_pre
, termp_an_post
}, /* An */
157 { termp_under_pre
, NULL
}, /* Ar */
158 { termp_cd_pre
, NULL
}, /* Cd */
159 { termp_bold_pre
, NULL
}, /* Cm */
160 { NULL
, NULL
}, /* Dv */
161 { NULL
, NULL
}, /* Er */
162 { NULL
, NULL
}, /* Ev */
163 { termp_ex_pre
, NULL
}, /* Ex */
164 { termp_fa_pre
, NULL
}, /* Fa */
165 { termp_fd_pre
, NULL
}, /* Fd */
166 { termp_fl_pre
, NULL
}, /* Fl */
167 { termp_fn_pre
, NULL
}, /* Fn */
168 { termp_ft_pre
, NULL
}, /* Ft */
169 { termp_bold_pre
, NULL
}, /* Ic */
170 { termp_in_pre
, termp_in_post
}, /* In */
171 { termp_li_pre
, NULL
}, /* Li */
172 { termp_nd_pre
, NULL
}, /* Nd */
173 { termp_nm_pre
, NULL
}, /* Nm */
174 { termp_op_pre
, termp_op_post
}, /* Op */
175 { NULL
, NULL
}, /* Ot */
176 { termp_under_pre
, NULL
}, /* Pa */
177 { termp_rv_pre
, NULL
}, /* Rv */
178 { NULL
, NULL
}, /* St */
179 { termp_under_pre
, NULL
}, /* Va */
180 { termp_vt_pre
, NULL
}, /* Vt */
181 { termp_xr_pre
, NULL
}, /* Xr */
182 { NULL
, termp____post
}, /* %A */
183 { termp_under_pre
, termp____post
}, /* %B */
184 { NULL
, termp____post
}, /* %D */
185 { termp_under_pre
, termp____post
}, /* %I */
186 { termp_under_pre
, termp____post
}, /* %J */
187 { NULL
, termp____post
}, /* %N */
188 { NULL
, termp____post
}, /* %O */
189 { NULL
, termp____post
}, /* %P */
190 { NULL
, termp____post
}, /* %R */
191 { termp_under_pre
, termp____post
}, /* %T */
192 { NULL
, termp____post
}, /* %V */
193 { NULL
, NULL
}, /* Ac */
194 { termp_aq_pre
, termp_aq_post
}, /* Ao */
195 { termp_aq_pre
, termp_aq_post
}, /* Aq */
196 { NULL
, NULL
}, /* At */
197 { NULL
, NULL
}, /* Bc */
198 { termp_bf_pre
, NULL
}, /* Bf */
199 { termp_bq_pre
, termp_bq_post
}, /* Bo */
200 { termp_bq_pre
, termp_bq_post
}, /* Bq */
201 { termp_xx_pre
, NULL
}, /* Bsx */
202 { NULL
, termp_bx_post
}, /* Bx */
203 { NULL
, NULL
}, /* Db */
204 { NULL
, NULL
}, /* Dc */
205 { termp_dq_pre
, termp_dq_post
}, /* Do */
206 { termp_dq_pre
, termp_dq_post
}, /* Dq */
207 { NULL
, NULL
}, /* Ec */ /* FIXME: no space */
208 { NULL
, NULL
}, /* Ef */
209 { termp_under_pre
, NULL
}, /* Em */
210 { NULL
, NULL
}, /* Eo */
211 { termp_xx_pre
, NULL
}, /* Fx */
212 { termp_bold_pre
, NULL
}, /* Ms */ /* FIXME: convert to symbol? */
213 { NULL
, NULL
}, /* No */
214 { termp_ns_pre
, NULL
}, /* Ns */
215 { termp_xx_pre
, NULL
}, /* Nx */
216 { termp_xx_pre
, NULL
}, /* Ox */
217 { NULL
, NULL
}, /* Pc */
218 { termp_pf_pre
, termp_pf_post
}, /* Pf */
219 { termp_pq_pre
, termp_pq_post
}, /* Po */
220 { termp_pq_pre
, termp_pq_post
}, /* Pq */
221 { NULL
, NULL
}, /* Qc */
222 { termp_sq_pre
, termp_sq_post
}, /* Ql */
223 { termp_qq_pre
, termp_qq_post
}, /* Qo */
224 { termp_qq_pre
, termp_qq_post
}, /* Qq */
225 { NULL
, NULL
}, /* Re */
226 { termp_rs_pre
, NULL
}, /* Rs */
227 { NULL
, NULL
}, /* Sc */
228 { termp_sq_pre
, termp_sq_post
}, /* So */
229 { termp_sq_pre
, termp_sq_post
}, /* Sq */
230 { termp_sm_pre
, NULL
}, /* Sm */
231 { termp_under_pre
, NULL
}, /* Sx */
232 { termp_bold_pre
, NULL
}, /* Sy */
233 { NULL
, NULL
}, /* Tn */
234 { termp_xx_pre
, NULL
}, /* Ux */
235 { NULL
, NULL
}, /* Xc */
236 { NULL
, NULL
}, /* Xo */
237 { termp_fo_pre
, termp_fo_post
}, /* Fo */
238 { NULL
, NULL
}, /* Fc */
239 { termp_op_pre
, termp_op_post
}, /* Oo */
240 { NULL
, NULL
}, /* Oc */
241 { NULL
, NULL
}, /* Bk */
242 { NULL
, NULL
}, /* Ek */
243 { termp_bt_pre
, NULL
}, /* Bt */
244 { NULL
, NULL
}, /* Hf */
245 { NULL
, NULL
}, /* Fr */
246 { termp_ud_pre
, NULL
}, /* Ud */
247 { NULL
, termp_lb_post
}, /* Lb */
248 { termp_sp_pre
, NULL
}, /* Lp */
249 { termp_lk_pre
, NULL
}, /* Lk */
250 { termp_under_pre
, NULL
}, /* Mt */
251 { termp_brq_pre
, termp_brq_post
}, /* Brq */
252 { termp_brq_pre
, termp_brq_post
}, /* Bro */
253 { NULL
, NULL
}, /* Brc */
254 { NULL
, termp____post
}, /* %C */
255 { NULL
, NULL
}, /* Es */ /* TODO */
256 { NULL
, NULL
}, /* En */ /* TODO */
257 { termp_xx_pre
, NULL
}, /* Dx */
258 { NULL
, termp____post
}, /* %Q */
259 { termp_sp_pre
, NULL
}, /* br */
260 { termp_sp_pre
, NULL
}, /* sp */
261 { termp_under_pre
, termp____post
}, /* %U */
262 { NULL
, NULL
}, /* Ta */
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
;
279 term_begin(p
, print_mdoc_head
, print_foot
, mdoc_meta(mdoc
));
281 if (NULL
== p
->symtab
)
283 case (TERMENC_ASCII
):
284 p
->symtab
= chars_init(CHARS_ASCII
);
295 print_mdoc_nodelist(p
, NULL
, m
, n
->child
);
302 print_mdoc_nodelist(DECL_ARGS
)
305 print_mdoc_node(p
, pair
, m
, n
);
307 print_mdoc_nodelist(p
, pair
, m
, n
->next
);
313 print_mdoc_node(DECL_ARGS
)
317 struct termpair npair
;
318 size_t offset
, rmargin
;
322 rmargin
= p
->rmargin
;
323 font
= term_fontq(p
);
325 memset(&npair
, 0, sizeof(struct termpair
));
328 if (MDOC_TEXT
!= n
->type
) {
329 if (termacts
[n
->tok
].pre
)
330 chld
= (*termacts
[n
->tok
].pre
)(p
, &npair
, m
, n
);
332 term_word(p
, n
->string
);
334 if (chld
&& n
->child
)
335 print_mdoc_nodelist(p
, &npair
, m
, n
->child
);
337 term_fontpopq(p
, font
);
339 if (MDOC_TEXT
!= n
->type
)
340 if (termacts
[n
->tok
].post
)
341 (*termacts
[n
->tok
].post
)(p
, &npair
, m
, n
);
343 if (MDOC_EOS
& n
->flags
)
344 p
->flags
|= TERMP_SENTENCE
;
347 p
->rmargin
= rmargin
;
353 print_foot(struct termp
*p
, const void *arg
)
355 char buf
[DATESIZ
], os
[BUFSIZ
];
356 const struct mdoc_meta
*m
;
358 m
= (const struct mdoc_meta
*)arg
;
360 term_fontrepl(p
, TERMFONT_NONE
);
363 * Output the footer in new-groff style, that is, three columns
364 * with the middle being the manual date and flanking columns
365 * being the operating system:
370 time2a(m
->date
, buf
, DATESIZ
);
371 strlcpy(os
, m
->os
, BUFSIZ
);
376 p
->rmargin
= (p
->maxrmargin
- strlen(buf
) + 1) / 2;
377 p
->flags
|= TERMP_NOSPACE
| TERMP_NOBREAK
;
382 p
->offset
= p
->rmargin
;
383 p
->rmargin
= p
->maxrmargin
- strlen(os
);
384 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
389 p
->offset
= p
->rmargin
;
390 p
->rmargin
= p
->maxrmargin
;
391 p
->flags
&= ~TERMP_NOBREAK
;
392 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
398 p
->rmargin
= p
->maxrmargin
;
405 print_mdoc_head(struct termp
*p
, const void *arg
)
407 char buf
[BUFSIZ
], title
[BUFSIZ
];
408 const struct mdoc_meta
*m
;
410 m
= (const struct mdoc_meta
*)arg
;
412 p
->rmargin
= p
->maxrmargin
;
416 * The header is strange. It has three components, which are
417 * really two with the first duplicated. It goes like this:
419 * IDENTIFIER TITLE IDENTIFIER
421 * The IDENTIFIER is NAME(SECTION), which is the command-name
422 * (if given, or "unknown" if not) followed by the manual page
423 * section. These are given in `Dt'. The TITLE is a free-form
424 * string depending on the manual volume. If not specified, it
425 * switches on the manual section.
429 strlcpy(buf
, m
->vol
, BUFSIZ
);
432 strlcat(buf
, " (", BUFSIZ
);
433 strlcat(buf
, m
->arch
, BUFSIZ
);
434 strlcat(buf
, ")", BUFSIZ
);
437 snprintf(title
, BUFSIZ
, "%s(%s)", m
->title
, m
->msec
);
440 p
->rmargin
= (p
->maxrmargin
- strlen(buf
) + 1) / 2;
441 p
->flags
|= TERMP_NOBREAK
| TERMP_NOSPACE
;
446 p
->offset
= p
->rmargin
;
447 p
->rmargin
= p
->maxrmargin
- strlen(title
);
448 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
453 p
->offset
= p
->rmargin
;
454 p
->rmargin
= p
->maxrmargin
;
455 p
->flags
&= ~TERMP_NOBREAK
;
456 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
462 p
->rmargin
= p
->maxrmargin
;
463 p
->flags
&= ~TERMP_NOSPACE
;
468 a2height(const struct mdoc_node
*n
)
472 assert(MDOC_TEXT
== n
->type
);
474 if ( ! a2roffsu(n
->string
, &su
, SCALE_VS
))
475 SCALE_VS_INIT(&su
, strlen(n
->string
));
477 return(term_vspan(&su
));
482 a2width(const struct mdoc_argv
*arg
, int pos
)
486 assert(arg
->value
[pos
]);
487 if ( ! a2roffsu(arg
->value
[pos
], &su
, SCALE_MAX
))
488 SCALE_HS_INIT(&su
, strlen(arg
->value
[pos
]));
490 return(term_hspan(&su
));
495 arg_disptype(const struct mdoc_node
*n
)
499 assert(MDOC_BLOCK
== n
->type
);
501 len
= (int)(n
->args
? n
->args
->argc
: 0);
503 for (i
= 0; i
< len
; i
++)
504 switch (n
->args
->argv
[i
].arg
) {
511 case (MDOC_Unfilled
):
514 return(n
->args
->argv
[i
].arg
);
524 a2offs(const struct mdoc_argv
*arg
)
528 if ('\0' == arg
->value
[0][0])
530 else if (0 == strcmp(arg
->value
[0], "left"))
532 else if (0 == strcmp(arg
->value
[0], "indent"))
534 else if (0 == strcmp(arg
->value
[0], "indent-two"))
535 return((INDENT
+ 1) * 2);
536 else if ( ! a2roffsu(arg
->value
[0], &su
, SCALE_MAX
))
537 SCALE_HS_INIT(&su
, strlen(arg
->value
[0]));
539 return(term_hspan(&su
));
544 * Return 1 if an argument has a particular argument value or 0 if it
545 * does not. See arg_getattr().
548 arg_hasattr(int arg
, const struct mdoc_node
*n
)
551 return(-1 != arg_getattr(arg
, n
));
556 * Get the index of an argument in a node's argument list or -1 if it
557 * does not exist. See arg_getattrs().
560 arg_getattr(int v
, const struct mdoc_node
*n
)
564 return(arg_getattrs(&v
, &val
, 1, n
) ? val
: -1);
569 * Walk through the argument list for a node and fill an array "vals"
570 * with the positions of the argument structures listed in "keys".
571 * Return the number of elements that were written into "vals", which
575 arg_getattrs(const int *keys
, int *vals
,
576 size_t sz
, const struct mdoc_node
*n
)
583 for (k
= i
= 0; i
< (int)n
->args
->argc
; i
++)
584 for (j
= 0; j
< (int)sz
; j
++)
585 if (n
->args
->argv
[i
].arg
== keys
[j
]) {
594 * Determine how much space to print out before block elements of `It'
595 * (and thus `Bl') and `Bd'. And then go ahead and print that space,
599 print_bvspace(struct termp
*p
,
600 const struct mdoc_node
*bl
,
601 const struct mdoc_node
*n
)
603 const struct mdoc_node
*nn
;
606 if (arg_hasattr(MDOC_Compact
, bl
))
609 /* Do not vspace directly after Ss/Sh. */
611 for (nn
= n
; nn
; nn
= nn
->parent
) {
612 if (MDOC_BLOCK
!= nn
->type
)
614 if (MDOC_Ss
== nn
->tok
)
616 if (MDOC_Sh
== nn
->tok
)
618 if (NULL
== nn
->prev
)
623 /* A `-column' does not assert vspace within the list. */
625 if (MDOC_Bl
== bl
->tok
&& LIST_column
== bl
->data
.list
)
626 if (n
->prev
&& MDOC_It
== n
->prev
->tok
)
629 /* A `-diag' without body does not vspace. */
631 if (MDOC_Bl
== bl
->tok
&& LIST_diag
== bl
->data
.list
)
632 if (n
->prev
&& MDOC_It
== n
->prev
->tok
) {
633 assert(n
->prev
->body
);
634 if (NULL
== n
->prev
->body
->child
)
644 termp_dq_pre(DECL_ARGS
)
647 if (MDOC_BODY
!= n
->type
)
650 term_word(p
, "\\(lq");
651 p
->flags
|= TERMP_NOSPACE
;
658 termp_dq_post(DECL_ARGS
)
661 if (MDOC_BODY
!= n
->type
)
664 p
->flags
|= TERMP_NOSPACE
;
665 term_word(p
, "\\(rq");
671 termp_it_pre(DECL_ARGS
)
673 const struct mdoc_node
*bl
, *nn
;
675 int i
, keys
[3], vals
[3];
676 size_t width
, offset
, ncols
, dcol
;
679 if (MDOC_BLOCK
== n
->type
) {
680 print_bvspace(p
, n
->parent
->parent
, n
);
684 bl
= n
->parent
->parent
->parent
;
686 /* Get list width, offset, and list type from argument list. */
688 keys
[0] = MDOC_Width
;
689 keys
[1] = MDOC_Offset
;
690 keys
[2] = MDOC_Column
;
692 vals
[0] = vals
[1] = vals
[2] = -1;
694 arg_getattrs(keys
, vals
, 3, bl
);
696 type
= bl
->data
.list
;
699 * First calculate width and offset. This is pretty easy unless
700 * we're a -column list, in which case all prior columns must
707 offset
= a2offs(&bl
->args
->argv
[vals
[1]]);
711 if (MDOC_HEAD
== n
->type
)
714 * Imitate groff's column handling:
715 * - For each earlier column, add its width.
716 * - For less than 5 columns, add four more blanks per
718 * - For exactly 5 columns, add three more blank per
720 * - For more than 5 columns, add only one column.
722 ncols
= bl
->args
->argv
[vals
[2]].sz
;
724 dcol
= ncols
< 5 ? 4 : ncols
== 5 ? 3 : 1;
727 * Calculate the offset by applying all prior MDOC_BODY,
728 * so we stop at the MDOC_HEAD (NULL == nn->prev).
731 for (i
= 0, nn
= n
->prev
;
732 nn
->prev
&& i
< (int)ncols
;
734 offset
+= dcol
+ a2width
735 (&bl
->args
->argv
[vals
[2]], i
);
739 * When exceeding the declared number of columns, leave
740 * the remaining widths at 0. This will later be
741 * adjusted to the default width of 10, or, for the last
742 * column, stretched to the right margin.
748 * Use the declared column widths, extended as explained
749 * in the preceding paragraph.
751 width
= a2width(&bl
->args
->argv
[vals
[2]], i
) + dcol
;
758 * Note: buffer the width by 2, which is groff's magic
759 * number for buffering single arguments. See the above
760 * handling for column for how this changes.
762 width
= a2width(&bl
->args
->argv
[vals
[0]], 0) + 2;
767 * List-type can override the width in the case of fixed-head
768 * values (bullet, dash/hyphen, enum). Tags need a non-zero
800 * Whitespace control. Inset bodies need an initial space,
801 * while diagonal bodies need two.
804 p
->flags
|= TERMP_NOSPACE
;
808 if (MDOC_BODY
== n
->type
)
809 term_word(p
, "\\ \\ ");
812 if (MDOC_BODY
== n
->type
)
819 p
->flags
|= TERMP_NOSPACE
;
823 if (MDOC_HEAD
== n
->type
)
824 term_fontpush(p
, TERMFONT_BOLD
);
831 * Pad and break control. This is the tricky part. These flags
832 * are documented in term_flushln() in term.c. Note that we're
833 * going to unset all of these flags in termp_it_post() when we
845 if (MDOC_HEAD
== n
->type
)
846 p
->flags
|= TERMP_NOBREAK
;
848 p
->flags
|= TERMP_NOLPAD
;
851 if (MDOC_HEAD
== n
->type
)
852 p
->flags
|= TERMP_NOBREAK
;
854 p
->flags
|= TERMP_NOLPAD
;
856 if (MDOC_HEAD
!= n
->type
)
860 * This is ugly. If `-hang' is specified and the body
861 * is a `Bl' or `Bd', then we want basically to nullify
862 * the "overstep" effect in term_flushln() and treat
863 * this as a `-ohang' list instead.
865 if (n
->next
->child
&&
866 (MDOC_Bl
== n
->next
->child
->tok
||
867 MDOC_Bd
== n
->next
->child
->tok
)) {
868 p
->flags
&= ~TERMP_NOBREAK
;
869 p
->flags
&= ~TERMP_NOLPAD
;
871 p
->flags
|= TERMP_HANG
;
874 if (MDOC_HEAD
== n
->type
)
875 p
->flags
|= TERMP_NOBREAK
| TERMP_TWOSPACE
;
877 p
->flags
|= TERMP_NOLPAD
;
879 if (MDOC_HEAD
!= n
->type
)
881 if (NULL
== n
->next
|| NULL
== n
->next
->child
)
882 p
->flags
|= TERMP_DANGLE
;
885 if (MDOC_HEAD
== n
->type
)
889 p
->flags
&= ~TERMP_NOBREAK
;
891 p
->flags
|= TERMP_NOBREAK
;
894 if (MDOC_BODY
== n
->prev
->type
)
895 p
->flags
|= TERMP_NOLPAD
;
899 if (MDOC_HEAD
== n
->type
)
900 p
->flags
|= TERMP_NOBREAK
;
907 * Margin control. Set-head-width lists have their right
908 * margins shortened. The body for these lists has the offset
909 * necessarily lengthened. Everybody gets the offset.
917 * Same stipulation as above, regarding `-hang'. We
918 * don't want to recalculate rmargin and offsets when
919 * using `Bd' or `Bl' within `-hang' overstep lists.
921 if (MDOC_HEAD
== n
->type
&& n
->next
->child
&&
922 (MDOC_Bl
== n
->next
->child
->tok
||
923 MDOC_Bd
== n
->next
->child
->tok
))
936 if (MDOC_HEAD
== n
->type
)
937 p
->rmargin
= p
->offset
+ width
;
943 p
->rmargin
= p
->offset
+ width
;
945 * XXX - this behaviour is not documented: the
946 * right-most column is filled to the right margin.
948 if (MDOC_HEAD
== n
->type
)
950 if (NULL
== n
->next
&& p
->rmargin
< p
->maxrmargin
)
951 p
->rmargin
= p
->maxrmargin
;
958 * The dash, hyphen, bullet and enum lists all have a special
959 * HEAD character (temporarily bold, in some cases).
962 if (MDOC_HEAD
== n
->type
)
965 term_fontpush(p
, TERMFONT_BOLD
);
966 term_word(p
, "\\[bu]");
972 term_fontpush(p
, TERMFONT_BOLD
);
973 term_word(p
, "\\(hy");
977 (pair
->ppair
->ppair
->count
)++;
978 snprintf(buf
, sizeof(buf
), "%d.",
979 pair
->ppair
->ppair
->count
);
987 * If we're not going to process our children, indicate so here.
1000 if (MDOC_HEAD
== n
->type
)
1004 if (MDOC_HEAD
== n
->type
)
1017 termp_it_post(DECL_ARGS
)
1019 enum mdoc_list type
;
1021 if (MDOC_BLOCK
== n
->type
)
1024 type
= n
->parent
->parent
->parent
->data
.list
;
1032 if (MDOC_BODY
== n
->type
)
1036 if (MDOC_BODY
== n
->type
)
1045 * Now that our output is flushed, we can reset our tags. Since
1046 * only `It' sets these flags, we're free to assume that nobody
1047 * has munged them in the meanwhile.
1050 p
->flags
&= ~TERMP_DANGLE
;
1051 p
->flags
&= ~TERMP_NOBREAK
;
1052 p
->flags
&= ~TERMP_TWOSPACE
;
1053 p
->flags
&= ~TERMP_NOLPAD
;
1054 p
->flags
&= ~TERMP_HANG
;
1060 termp_nm_pre(DECL_ARGS
)
1063 if (NULL
== n
->child
&& NULL
== m
->name
)
1068 term_fontpush(p
, TERMFONT_BOLD
);
1069 if (NULL
== n
->child
)
1070 term_word(p
, m
->name
);
1077 termp_fl_pre(DECL_ARGS
)
1080 term_fontpush(p
, TERMFONT_BOLD
);
1081 term_word(p
, "\\-");
1084 p
->flags
|= TERMP_NOSPACE
;
1085 else if (n
->next
&& n
->next
->line
== n
->line
)
1086 p
->flags
|= TERMP_NOSPACE
;
1094 termp_an_pre(DECL_ARGS
)
1097 if (NULL
== n
->child
)
1101 * If not in the AUTHORS section, `An -split' will cause
1102 * newlines to occur before the author name. If in the AUTHORS
1103 * section, by default, the first `An' invocation is nosplit,
1104 * then all subsequent ones, regardless of whether interspersed
1105 * with other macros/text, are split. -split, in this case,
1106 * will override the condition of the implied first -nosplit.
1109 if (n
->sec
== SEC_AUTHORS
) {
1110 if ( ! (TERMP_ANPREC
& p
->flags
)) {
1111 if (TERMP_SPLIT
& p
->flags
)
1115 if (TERMP_NOSPLIT
& p
->flags
)
1121 if (TERMP_SPLIT
& p
->flags
)
1130 termp_an_post(DECL_ARGS
)
1134 if (SEC_AUTHORS
== n
->sec
)
1135 p
->flags
|= TERMP_ANPREC
;
1139 if (arg_hasattr(MDOC_Split
, n
)) {
1140 p
->flags
&= ~TERMP_NOSPLIT
;
1141 p
->flags
|= TERMP_SPLIT
;
1143 p
->flags
&= ~TERMP_SPLIT
;
1144 p
->flags
|= TERMP_NOSPLIT
;
1152 termp_ns_pre(DECL_ARGS
)
1155 p
->flags
|= TERMP_NOSPACE
;
1162 termp_rs_pre(DECL_ARGS
)
1165 if (SEC_SEE_ALSO
!= n
->sec
)
1167 if (MDOC_BLOCK
== n
->type
&& n
->prev
)
1175 termp_rv_pre(DECL_ARGS
)
1177 const struct mdoc_node
*nn
;
1180 term_word(p
, "The");
1182 for (nn
= n
->child
; nn
; nn
= nn
->next
) {
1183 term_fontpush(p
, TERMFONT_BOLD
);
1184 term_word(p
, nn
->string
);
1186 p
->flags
|= TERMP_NOSPACE
;
1187 if (nn
->next
&& NULL
== nn
->next
->next
)
1188 term_word(p
, "(), and");
1190 term_word(p
, "(),");
1195 if (n
->child
&& n
->child
->next
)
1196 term_word(p
, "functions return");
1198 term_word(p
, "function returns");
1200 term_word(p
, "the value 0 if successful; otherwise the value "
1201 "-1 is returned and the global variable");
1203 term_fontpush(p
, TERMFONT_UNDER
);
1204 term_word(p
, "errno");
1207 term_word(p
, "is set to indicate the error.");
1208 p
->flags
|= TERMP_SENTENCE
;
1216 termp_ex_pre(DECL_ARGS
)
1218 const struct mdoc_node
*nn
;
1220 term_word(p
, "The");
1222 for (nn
= n
->child
; nn
; nn
= nn
->next
) {
1223 term_fontpush(p
, TERMFONT_BOLD
);
1224 term_word(p
, nn
->string
);
1226 p
->flags
|= TERMP_NOSPACE
;
1227 if (nn
->next
&& NULL
== nn
->next
->next
)
1228 term_word(p
, ", and");
1232 p
->flags
&= ~TERMP_NOSPACE
;
1235 if (n
->child
&& n
->child
->next
)
1236 term_word(p
, "utilities exit");
1238 term_word(p
, "utility exits");
1240 term_word(p
, "0 on success, and >0 if an error occurs.");
1241 p
->flags
|= TERMP_SENTENCE
;
1249 termp_nd_pre(DECL_ARGS
)
1252 if (MDOC_BODY
!= n
->type
)
1255 #if defined(__OpenBSD__) || defined(__linux__)
1256 term_word(p
, "\\(en");
1258 term_word(p
, "\\(em");
1266 termp_bl_pre(DECL_ARGS
)
1269 return(MDOC_HEAD
!= n
->type
);
1275 termp_bl_post(DECL_ARGS
)
1278 if (MDOC_BLOCK
== n
->type
)
1285 termp_op_post(DECL_ARGS
)
1288 if (MDOC_BODY
!= n
->type
)
1290 p
->flags
|= TERMP_NOSPACE
;
1291 term_word(p
, "\\(rB");
1297 termp_xr_pre(DECL_ARGS
)
1299 const struct mdoc_node
*nn
;
1301 if (NULL
== n
->child
)
1304 assert(MDOC_TEXT
== n
->child
->type
);
1307 term_word(p
, nn
->string
);
1308 if (NULL
== (nn
= nn
->next
))
1310 p
->flags
|= TERMP_NOSPACE
;
1312 p
->flags
|= TERMP_NOSPACE
;
1313 term_word(p
, nn
->string
);
1314 p
->flags
|= TERMP_NOSPACE
;
1322 * This decides how to assert whitespace before any of the SYNOPSIS set
1323 * of macros (which, as in the case of Ft/Fo and Ft/Fn, may contain
1327 synopsis_pre(struct termp
*p
, const struct mdoc_node
*n
)
1330 * Obviously, if we're not in a SYNOPSIS or no prior macros
1331 * exist, do nothing.
1333 if (NULL
== n
->prev
|| SEC_SYNOPSIS
!= n
->sec
)
1337 * If we're the second in a pair of like elements, emit our
1338 * newline and return. UNLESS we're `Fo', `Fn', `Fn', in which
1339 * case we soldier on.
1341 if (n
->prev
->tok
== n
->tok
&&
1342 MDOC_Ft
!= n
->tok
&&
1343 MDOC_Fo
!= n
->tok
&&
1344 MDOC_Fn
!= n
->tok
) {
1350 * If we're one of the SYNOPSIS set and non-like pair-wise after
1351 * another (or Fn/Fo, which we've let slip through) then assert
1352 * vertical space, else only newline and move on.
1354 switch (n
->prev
->tok
) {
1367 if (MDOC_Fn
!= n
->tok
&& MDOC_Fo
!= n
->tok
) {
1380 termp_vt_pre(DECL_ARGS
)
1383 if (MDOC_ELEM
== n
->type
) {
1385 return(termp_under_pre(p
, pair
, m
, n
));
1386 } else if (MDOC_BLOCK
== n
->type
) {
1389 } else if (MDOC_HEAD
== n
->type
)
1392 return(termp_under_pre(p
, pair
, m
, n
));
1398 termp_bold_pre(DECL_ARGS
)
1401 term_fontpush(p
, TERMFONT_BOLD
);
1408 termp_fd_pre(DECL_ARGS
)
1412 return(termp_bold_pre(p
, pair
, m
, n
));
1418 termp_sh_pre(DECL_ARGS
)
1421 /* No vspace between consecutive `Sh' calls. */
1425 if (n
->prev
&& MDOC_Sh
== n
->prev
->tok
)
1426 if (NULL
== n
->prev
->body
->child
)
1431 term_fontpush(p
, TERMFONT_BOLD
);
1445 termp_sh_post(DECL_ARGS
)
1464 termp_op_pre(DECL_ARGS
)
1469 term_word(p
, "\\(lB");
1470 p
->flags
|= TERMP_NOSPACE
;
1481 termp_bt_pre(DECL_ARGS
)
1484 term_word(p
, "is currently in beta test.");
1485 p
->flags
|= TERMP_SENTENCE
;
1492 termp_lb_post(DECL_ARGS
)
1495 if (SEC_LIBRARY
== n
->sec
&& MDOC_LINE
& n
->flags
)
1502 termp_ud_pre(DECL_ARGS
)
1505 term_word(p
, "currently under development.");
1506 p
->flags
|= TERMP_SENTENCE
;
1513 termp_d1_pre(DECL_ARGS
)
1516 if (MDOC_BLOCK
!= n
->type
)
1519 p
->offset
+= (INDENT
+ 1);
1526 termp_d1_post(DECL_ARGS
)
1529 if (MDOC_BLOCK
!= n
->type
)
1537 termp_aq_pre(DECL_ARGS
)
1540 if (MDOC_BODY
!= n
->type
)
1542 term_word(p
, "\\(la");
1543 p
->flags
|= TERMP_NOSPACE
;
1550 termp_aq_post(DECL_ARGS
)
1553 if (MDOC_BODY
!= n
->type
)
1555 p
->flags
|= TERMP_NOSPACE
;
1556 term_word(p
, "\\(ra");
1562 termp_ft_pre(DECL_ARGS
)
1565 /* NB: MDOC_LINE does not effect this! */
1567 term_fontpush(p
, TERMFONT_UNDER
);
1574 termp_fn_pre(DECL_ARGS
)
1576 const struct mdoc_node
*nn
;
1580 term_fontpush(p
, TERMFONT_BOLD
);
1581 term_word(p
, n
->child
->string
);
1584 p
->flags
|= TERMP_NOSPACE
;
1587 for (nn
= n
->child
->next
; nn
; nn
= nn
->next
) {
1588 term_fontpush(p
, TERMFONT_UNDER
);
1589 term_word(p
, nn
->string
);
1598 if (SEC_SYNOPSIS
== n
->sec
)
1607 termp_fa_pre(DECL_ARGS
)
1609 const struct mdoc_node
*nn
;
1611 if (n
->parent
->tok
!= MDOC_Fo
) {
1612 term_fontpush(p
, TERMFONT_UNDER
);
1616 for (nn
= n
->child
; nn
; nn
= nn
->next
) {
1617 term_fontpush(p
, TERMFONT_UNDER
);
1618 term_word(p
, nn
->string
);
1625 if (n
->child
&& n
->next
&& n
->next
->tok
== MDOC_Fa
)
1634 termp_bd_pre(DECL_ARGS
)
1639 const struct mdoc_node
*nn
;
1641 if (MDOC_BLOCK
== n
->type
) {
1642 print_bvspace(p
, n
, n
);
1644 } else if (MDOC_HEAD
== n
->type
)
1649 type
= arg_disptype(nn
);
1652 if (-1 != (i
= arg_getattr(MDOC_Offset
, nn
)))
1653 p
->offset
+= a2offs(&nn
->args
->argv
[i
]);
1656 * If -ragged or -filled are specified, the block does nothing
1657 * but change the indentation. If -unfilled or -literal are
1658 * specified, text is printed exactly as entered in the display:
1659 * for macro lines, a newline is appended to the line. Blank
1660 * lines are allowed.
1663 if (MDOC_Literal
!= type
&& MDOC_Unfilled
!= type
)
1666 tabwidth
= p
->tabwidth
;
1669 rmax
= p
->maxrmargin
;
1670 p
->rmargin
= p
->maxrmargin
= TERM_MAXMARGIN
;
1672 for (nn
= n
->child
; nn
; nn
= nn
->next
) {
1673 p
->flags
|= TERMP_NOSPACE
;
1674 print_mdoc_node(p
, pair
, m
, nn
);
1675 if (NULL
== nn
->prev
||
1676 nn
->prev
->line
< nn
->line
||
1680 p
->tabwidth
= tabwidth
;
1683 p
->maxrmargin
= rmax
;
1690 termp_bd_post(DECL_ARGS
)
1695 if (MDOC_BODY
!= n
->type
)
1698 type
= arg_disptype(n
->parent
);
1702 rmax
= p
->maxrmargin
;
1704 if (MDOC_Literal
== type
|| MDOC_Unfilled
== type
)
1705 p
->rmargin
= p
->maxrmargin
= TERM_MAXMARGIN
;
1707 p
->flags
|= TERMP_NOSPACE
;
1711 p
->maxrmargin
= rmax
;
1717 termp_qq_pre(DECL_ARGS
)
1720 if (MDOC_BODY
!= n
->type
)
1723 p
->flags
|= TERMP_NOSPACE
;
1730 termp_qq_post(DECL_ARGS
)
1733 if (MDOC_BODY
!= n
->type
)
1735 p
->flags
|= TERMP_NOSPACE
;
1742 termp_bx_post(DECL_ARGS
)
1746 p
->flags
|= TERMP_NOSPACE
;
1747 term_word(p
, "BSD");
1753 termp_xx_pre(DECL_ARGS
)
1789 termp_sq_pre(DECL_ARGS
)
1792 if (MDOC_BODY
!= n
->type
)
1794 term_word(p
, "\\(oq");
1795 p
->flags
|= TERMP_NOSPACE
;
1802 termp_sq_post(DECL_ARGS
)
1805 if (MDOC_BODY
!= n
->type
)
1807 p
->flags
|= TERMP_NOSPACE
;
1808 term_word(p
, "\\(aq");
1814 termp_pf_pre(DECL_ARGS
)
1817 p
->flags
|= TERMP_IGNDELIM
;
1824 termp_pf_post(DECL_ARGS
)
1827 p
->flags
&= ~TERMP_IGNDELIM
;
1828 p
->flags
|= TERMP_NOSPACE
;
1834 termp_ss_pre(DECL_ARGS
)
1844 term_fontpush(p
, TERMFONT_BOLD
);
1845 p
->offset
= HALFINDENT
;
1857 termp_ss_post(DECL_ARGS
)
1860 if (MDOC_HEAD
== n
->type
)
1867 termp_cd_pre(DECL_ARGS
)
1871 term_fontpush(p
, TERMFONT_BOLD
);
1878 termp_in_pre(DECL_ARGS
)
1883 if (SEC_SYNOPSIS
== n
->sec
&& MDOC_LINE
& n
->flags
) {
1884 term_fontpush(p
, TERMFONT_BOLD
);
1885 term_word(p
, "#include");
1889 term_fontpush(p
, TERMFONT_UNDER
);
1892 p
->flags
|= TERMP_NOSPACE
;
1899 termp_in_post(DECL_ARGS
)
1902 if (SEC_SYNOPSIS
== n
->sec
)
1903 term_fontpush(p
, TERMFONT_BOLD
);
1905 p
->flags
|= TERMP_NOSPACE
;
1908 if (SEC_SYNOPSIS
== n
->sec
)
1915 termp_sp_pre(DECL_ARGS
)
1921 len
= n
->child
? a2height(n
->child
) : 1;
1933 for (i
= 0; i
< len
; i
++)
1942 termp_brq_pre(DECL_ARGS
)
1945 if (MDOC_BODY
!= n
->type
)
1947 term_word(p
, "\\(lC");
1948 p
->flags
|= TERMP_NOSPACE
;
1955 termp_brq_post(DECL_ARGS
)
1958 if (MDOC_BODY
!= n
->type
)
1960 p
->flags
|= TERMP_NOSPACE
;
1961 term_word(p
, "\\(rC");
1967 termp_bq_pre(DECL_ARGS
)
1970 if (MDOC_BODY
!= n
->type
)
1972 term_word(p
, "\\(lB");
1973 p
->flags
|= TERMP_NOSPACE
;
1980 termp_bq_post(DECL_ARGS
)
1983 if (MDOC_BODY
!= n
->type
)
1985 p
->flags
|= TERMP_NOSPACE
;
1986 term_word(p
, "\\(rB");
1992 termp_pq_pre(DECL_ARGS
)
1995 if (MDOC_BODY
!= n
->type
)
1997 term_word(p
, "\\&(");
1998 p
->flags
|= TERMP_NOSPACE
;
2005 termp_pq_post(DECL_ARGS
)
2008 if (MDOC_BODY
!= n
->type
)
2016 termp_fo_pre(DECL_ARGS
)
2019 if (MDOC_BLOCK
== n
->type
) {
2022 } else if (MDOC_BODY
== n
->type
) {
2023 p
->flags
|= TERMP_NOSPACE
;
2025 p
->flags
|= TERMP_NOSPACE
;
2029 /* XXX: we drop non-initial arguments as per groff. */
2032 assert(n
->child
->string
);
2033 term_fontpush(p
, TERMFONT_BOLD
);
2034 term_word(p
, n
->child
->string
);
2041 termp_fo_post(DECL_ARGS
)
2044 if (MDOC_BODY
!= n
->type
)
2047 p
->flags
|= TERMP_NOSPACE
;
2050 if (SEC_SYNOPSIS
== n
->sec
) {
2051 p
->flags
|= TERMP_NOSPACE
;
2059 termp_bf_pre(DECL_ARGS
)
2061 const struct mdoc_node
*nn
;
2063 if (MDOC_HEAD
== n
->type
)
2065 else if (MDOC_BLOCK
!= n
->type
)
2068 if (NULL
== (nn
= n
->head
->child
)) {
2069 if (arg_hasattr(MDOC_Emphasis
, n
))
2070 term_fontpush(p
, TERMFONT_UNDER
);
2071 else if (arg_hasattr(MDOC_Symbolic
, n
))
2072 term_fontpush(p
, TERMFONT_BOLD
);
2074 term_fontpush(p
, TERMFONT_NONE
);
2079 assert(MDOC_TEXT
== nn
->type
);
2080 if (0 == strcmp("Em", nn
->string
))
2081 term_fontpush(p
, TERMFONT_UNDER
);
2082 else if (0 == strcmp("Sy", nn
->string
))
2083 term_fontpush(p
, TERMFONT_BOLD
);
2085 term_fontpush(p
, TERMFONT_NONE
);
2093 termp_sm_pre(DECL_ARGS
)
2096 assert(n
->child
&& MDOC_TEXT
== n
->child
->type
);
2097 if (0 == strcmp("on", n
->child
->string
))
2098 p
->flags
&= ~TERMP_NONOSPACE
;
2100 p
->flags
|= TERMP_NONOSPACE
;
2108 termp_ap_pre(DECL_ARGS
)
2111 p
->flags
|= TERMP_NOSPACE
;
2112 term_word(p
, "\\(aq");
2113 p
->flags
|= TERMP_NOSPACE
;
2120 termp____post(DECL_ARGS
)
2125 p
->flags
|= TERMP_NOSPACE
;
2126 term_word(p
, n
->next
? "," : ".");
2132 termp_li_pre(DECL_ARGS
)
2135 term_fontpush(p
, TERMFONT_NONE
);
2142 termp_lk_pre(DECL_ARGS
)
2144 const struct mdoc_node
*nn
;
2146 term_fontpush(p
, TERMFONT_UNDER
);
2149 if (NULL
== nn
->next
)
2152 term_word(p
, nn
->string
);
2155 p
->flags
|= TERMP_NOSPACE
;
2158 term_fontpush(p
, TERMFONT_BOLD
);
2159 for (nn
= nn
->next
; nn
; nn
= nn
->next
)
2160 term_word(p
, nn
->string
);
2169 termp_under_pre(DECL_ARGS
)
2172 term_fontpush(p
, TERMFONT_UNDER
);