]>
git.cameronkatri.com Git - mandoc.git/blob - mdoc_term.c
1 /* $Id: mdoc_term.c,v 1.124 2010/05/15 16:24:38 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>
40 struct termpair
*ppair
;
44 #define DECL_ARGS struct termp *p, \
45 struct termpair *pair, \
46 const struct mdoc_meta *m, \
47 const struct mdoc_node *n
50 int (*pre
)(DECL_ARGS
);
51 void (*post
)(DECL_ARGS
);
54 static size_t a2width(const struct mdoc_argv
*, int);
55 static size_t a2height(const struct mdoc_node
*);
56 static size_t a2offs(const struct mdoc_argv
*);
58 static int arg_hasattr(int, const struct mdoc_node
*);
59 static int arg_getattrs(const int *, int *, size_t,
60 const struct mdoc_node
*);
61 static int arg_getattr(int, const struct mdoc_node
*);
62 static int arg_disptype(const struct mdoc_node
*);
63 static int arg_listtype(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 arg_listtype(const struct mdoc_node
*n
)
519 assert(MDOC_BLOCK
== n
->type
);
521 len
= (int)(n
->args
? n
->args
->argc
: 0);
523 for (i
= 0; i
< len
; i
++)
524 switch (n
->args
->argv
[i
].arg
) {
546 return(n
->args
->argv
[i
].arg
);
556 a2offs(const struct mdoc_argv
*arg
)
560 if ('\0' == arg
->value
[0][0])
562 else if (0 == strcmp(arg
->value
[0], "left"))
564 else if (0 == strcmp(arg
->value
[0], "indent"))
566 else if (0 == strcmp(arg
->value
[0], "indent-two"))
567 return((INDENT
+ 1) * 2);
568 else if ( ! a2roffsu(arg
->value
[0], &su
, SCALE_MAX
))
569 SCALE_HS_INIT(&su
, strlen(arg
->value
[0]));
571 return(term_hspan(&su
));
576 * Return 1 if an argument has a particular argument value or 0 if it
577 * does not. See arg_getattr().
580 arg_hasattr(int arg
, const struct mdoc_node
*n
)
583 return(-1 != arg_getattr(arg
, n
));
588 * Get the index of an argument in a node's argument list or -1 if it
589 * does not exist. See arg_getattrs().
592 arg_getattr(int v
, const struct mdoc_node
*n
)
596 return(arg_getattrs(&v
, &val
, 1, n
) ? val
: -1);
601 * Walk through the argument list for a node and fill an array "vals"
602 * with the positions of the argument structures listed in "keys".
603 * Return the number of elements that were written into "vals", which
607 arg_getattrs(const int *keys
, int *vals
,
608 size_t sz
, const struct mdoc_node
*n
)
615 for (k
= i
= 0; i
< (int)n
->args
->argc
; i
++)
616 for (j
= 0; j
< (int)sz
; j
++)
617 if (n
->args
->argv
[i
].arg
== keys
[j
]) {
626 * Determine how much space to print out before block elements of `It'
627 * (and thus `Bl') and `Bd'. And then go ahead and print that space,
631 print_bvspace(struct termp
*p
,
632 const struct mdoc_node
*bl
,
633 const struct mdoc_node
*n
)
635 const struct mdoc_node
*nn
;
638 if (arg_hasattr(MDOC_Compact
, bl
))
641 /* Do not vspace directly after Ss/Sh. */
643 for (nn
= n
; nn
; nn
= nn
->parent
) {
644 if (MDOC_BLOCK
!= nn
->type
)
646 if (MDOC_Ss
== nn
->tok
)
648 if (MDOC_Sh
== nn
->tok
)
650 if (NULL
== nn
->prev
)
655 /* A `-column' does not assert vspace within the list. */
657 if (MDOC_Bl
== bl
->tok
&& arg_hasattr(MDOC_Column
, bl
))
658 if (n
->prev
&& MDOC_It
== n
->prev
->tok
)
661 /* A `-diag' without body does not vspace. */
663 if (MDOC_Bl
== bl
->tok
&& arg_hasattr(MDOC_Diag
, bl
))
664 if (n
->prev
&& MDOC_It
== n
->prev
->tok
) {
665 assert(n
->prev
->body
);
666 if (NULL
== n
->prev
->body
->child
)
676 termp_dq_pre(DECL_ARGS
)
679 if (MDOC_BODY
!= n
->type
)
682 term_word(p
, "\\(lq");
683 p
->flags
|= TERMP_NOSPACE
;
690 termp_dq_post(DECL_ARGS
)
693 if (MDOC_BODY
!= n
->type
)
696 p
->flags
|= TERMP_NOSPACE
;
697 term_word(p
, "\\(rq");
703 termp_it_pre(DECL_ARGS
)
705 const struct mdoc_node
*bl
, *nn
;
707 int i
, type
, keys
[3], vals
[3];
708 size_t width
, offset
, ncols
, dcol
;
710 if (MDOC_BLOCK
== n
->type
) {
711 print_bvspace(p
, n
->parent
->parent
, n
);
715 bl
= n
->parent
->parent
->parent
;
717 /* Get list width, offset, and list type from argument list. */
719 keys
[0] = MDOC_Width
;
720 keys
[1] = MDOC_Offset
;
721 keys
[2] = MDOC_Column
;
723 vals
[0] = vals
[1] = vals
[2] = -1;
725 arg_getattrs(keys
, vals
, 3, bl
);
727 type
= arg_listtype(bl
);
731 * First calculate width and offset. This is pretty easy unless
732 * we're a -column list, in which case all prior columns must
739 offset
= a2offs(&bl
->args
->argv
[vals
[1]]);
743 if (MDOC_BODY
== n
->type
)
746 * Imitate groff's column handling:
747 * - For each earlier column, add its width.
748 * - For less than 5 columns, add four more blanks per
750 * - For exactly 5 columns, add three more blank per
752 * - For more than 5 columns, add only one column.
754 ncols
= bl
->args
->argv
[vals
[2]].sz
;
756 dcol
= ncols
< 5 ? 4 : ncols
== 5 ? 3 : 1;
758 for (i
= 0, nn
= n
->prev
;
759 nn
&& i
< (int)ncols
;
761 offset
+= dcol
+ a2width
762 (&bl
->args
->argv
[vals
[2]], i
);
766 * When exceeding the declared number of columns, leave
767 * the remaining widths at 0. This will later be
768 * adjusted to the default width of 10, or, for the last
769 * column, stretched to the right margin.
775 * Use the declared column widths, extended as explained
776 * in the preceding paragraph.
778 width
= a2width(&bl
->args
->argv
[vals
[2]], i
) + dcol
;
785 * Note: buffer the width by 2, which is groff's magic
786 * number for buffering single arguments. See the above
787 * handling for column for how this changes.
789 width
= a2width(&bl
->args
->argv
[vals
[0]], 0) + 2;
794 * List-type can override the width in the case of fixed-head
795 * values (bullet, dash/hyphen, enum). Tags need a non-zero
827 * Whitespace control. Inset bodies need an initial space,
828 * while diagonal bodies need two.
831 p
->flags
|= TERMP_NOSPACE
;
835 if (MDOC_BODY
== n
->type
)
836 term_word(p
, "\\ \\ ");
839 if (MDOC_BODY
== n
->type
)
846 p
->flags
|= TERMP_NOSPACE
;
850 if (MDOC_HEAD
== n
->type
)
851 term_fontpush(p
, TERMFONT_BOLD
);
858 * Pad and break control. This is the tricky part. These flags
859 * are documented in term_flushln() in term.c. Note that we're
860 * going to unset all of these flags in termp_it_post() when we
872 if (MDOC_HEAD
== n
->type
)
873 p
->flags
|= TERMP_NOBREAK
;
875 p
->flags
|= TERMP_NOLPAD
;
878 if (MDOC_HEAD
== n
->type
)
879 p
->flags
|= TERMP_NOBREAK
;
881 p
->flags
|= TERMP_NOLPAD
;
883 if (MDOC_HEAD
!= n
->type
)
887 * This is ugly. If `-hang' is specified and the body
888 * is a `Bl' or `Bd', then we want basically to nullify
889 * the "overstep" effect in term_flushln() and treat
890 * this as a `-ohang' list instead.
892 if (n
->next
->child
&&
893 (MDOC_Bl
== n
->next
->child
->tok
||
894 MDOC_Bd
== n
->next
->child
->tok
)) {
895 p
->flags
&= ~TERMP_NOBREAK
;
896 p
->flags
&= ~TERMP_NOLPAD
;
898 p
->flags
|= TERMP_HANG
;
901 if (MDOC_HEAD
== n
->type
)
902 p
->flags
|= TERMP_NOBREAK
| TERMP_TWOSPACE
;
904 p
->flags
|= TERMP_NOLPAD
;
906 if (MDOC_HEAD
!= n
->type
)
908 if (NULL
== n
->next
|| NULL
== n
->next
->child
)
909 p
->flags
|= TERMP_DANGLE
;
912 if (MDOC_HEAD
== n
->type
) {
914 if (MDOC_BODY
== n
->next
->type
)
915 p
->flags
&= ~TERMP_NOBREAK
;
917 p
->flags
|= TERMP_NOBREAK
;
919 p
->flags
|= TERMP_NOLPAD
;
923 if (MDOC_HEAD
== n
->type
)
924 p
->flags
|= TERMP_NOBREAK
;
931 * Margin control. Set-head-width lists have their right
932 * margins shortened. The body for these lists has the offset
933 * necessarily lengthened. Everybody gets the offset.
941 * Same stipulation as above, regarding `-hang'. We
942 * don't want to recalculate rmargin and offsets when
943 * using `Bd' or `Bl' within `-hang' overstep lists.
945 if (MDOC_HEAD
== n
->type
&& n
->next
->child
&&
946 (MDOC_Bl
== n
->next
->child
->tok
||
947 MDOC_Bd
== n
->next
->child
->tok
))
960 if (MDOC_HEAD
== n
->type
)
961 p
->rmargin
= p
->offset
+ width
;
967 p
->rmargin
= p
->offset
+ width
;
969 * XXX - this behaviour is not documented: the
970 * right-most column is filled to the right margin.
972 if (MDOC_HEAD
== n
->type
&&
973 MDOC_BODY
== n
->next
->type
&&
974 p
->rmargin
< p
->maxrmargin
)
975 p
->rmargin
= p
->maxrmargin
;
982 * The dash, hyphen, bullet and enum lists all have a special
983 * HEAD character (temporarily bold, in some cases).
986 if (MDOC_HEAD
== n
->type
)
989 term_fontpush(p
, TERMFONT_BOLD
);
990 term_word(p
, "\\[bu]");
996 term_fontpush(p
, TERMFONT_BOLD
);
997 term_word(p
, "\\(hy");
1001 (pair
->ppair
->ppair
->count
)++;
1002 snprintf(buf
, sizeof(buf
), "%d.",
1003 pair
->ppair
->ppair
->count
);
1011 * If we're not going to process our children, indicate so here.
1024 if (MDOC_HEAD
== n
->type
)
1028 if (MDOC_BODY
== n
->type
)
1041 termp_it_post(DECL_ARGS
)
1045 if (MDOC_BLOCK
== n
->type
)
1048 type
= arg_listtype(n
->parent
->parent
->parent
);
1057 if (MDOC_BODY
== n
->type
)
1061 if (MDOC_HEAD
== n
->type
)
1070 * Now that our output is flushed, we can reset our tags. Since
1071 * only `It' sets these flags, we're free to assume that nobody
1072 * has munged them in the meanwhile.
1075 p
->flags
&= ~TERMP_DANGLE
;
1076 p
->flags
&= ~TERMP_NOBREAK
;
1077 p
->flags
&= ~TERMP_TWOSPACE
;
1078 p
->flags
&= ~TERMP_NOLPAD
;
1079 p
->flags
&= ~TERMP_HANG
;
1085 termp_nm_pre(DECL_ARGS
)
1088 if (SEC_SYNOPSIS
== n
->sec
&& MDOC_LINE
& n
->flags
)
1091 term_fontpush(p
, TERMFONT_BOLD
);
1093 if (NULL
== n
->child
)
1094 term_word(p
, m
->name
);
1101 termp_fl_pre(DECL_ARGS
)
1104 term_fontpush(p
, TERMFONT_BOLD
);
1105 term_word(p
, "\\-");
1108 p
->flags
|= TERMP_NOSPACE
;
1109 else if (n
->next
&& n
->next
->line
== n
->line
)
1110 p
->flags
|= TERMP_NOSPACE
;
1118 termp_an_pre(DECL_ARGS
)
1121 if (NULL
== n
->child
)
1125 * If not in the AUTHORS section, `An -split' will cause
1126 * newlines to occur before the author name. If in the AUTHORS
1127 * section, by default, the first `An' invocation is nosplit,
1128 * then all subsequent ones, regardless of whether interspersed
1129 * with other macros/text, are split. -split, in this case,
1130 * will override the condition of the implied first -nosplit.
1133 if (n
->sec
== SEC_AUTHORS
) {
1134 if ( ! (TERMP_ANPREC
& p
->flags
)) {
1135 if (TERMP_SPLIT
& p
->flags
)
1139 if (TERMP_NOSPLIT
& p
->flags
)
1145 if (TERMP_SPLIT
& p
->flags
)
1154 termp_an_post(DECL_ARGS
)
1158 if (SEC_AUTHORS
== n
->sec
)
1159 p
->flags
|= TERMP_ANPREC
;
1163 if (arg_hasattr(MDOC_Split
, n
)) {
1164 p
->flags
&= ~TERMP_NOSPLIT
;
1165 p
->flags
|= TERMP_SPLIT
;
1167 p
->flags
&= ~TERMP_SPLIT
;
1168 p
->flags
|= TERMP_NOSPLIT
;
1176 termp_ns_pre(DECL_ARGS
)
1179 p
->flags
|= TERMP_NOSPACE
;
1186 termp_rs_pre(DECL_ARGS
)
1189 if (SEC_SEE_ALSO
!= n
->sec
)
1191 if (MDOC_BLOCK
== n
->type
&& n
->prev
)
1199 termp_rv_pre(DECL_ARGS
)
1201 const struct mdoc_node
*nn
;
1204 term_word(p
, "The");
1206 for (nn
= n
->child
; nn
; nn
= nn
->next
) {
1207 term_fontpush(p
, TERMFONT_BOLD
);
1208 term_word(p
, nn
->string
);
1210 p
->flags
|= TERMP_NOSPACE
;
1211 if (nn
->next
&& NULL
== nn
->next
->next
)
1212 term_word(p
, "(), and");
1214 term_word(p
, "(),");
1220 term_word(p
, "functions return");
1222 term_word(p
, "function returns");
1224 term_word(p
, "the value 0 if successful; otherwise the value "
1225 "-1 is returned and the global variable");
1227 term_fontpush(p
, TERMFONT_UNDER
);
1228 term_word(p
, "errno");
1231 term_word(p
, "is set to indicate the error.");
1239 termp_ex_pre(DECL_ARGS
)
1241 const struct mdoc_node
*nn
;
1243 term_word(p
, "The");
1245 for (nn
= n
->child
; nn
; nn
= nn
->next
) {
1246 term_fontpush(p
, TERMFONT_BOLD
);
1247 term_word(p
, nn
->string
);
1249 p
->flags
|= TERMP_NOSPACE
;
1250 if (nn
->next
&& NULL
== nn
->next
->next
)
1251 term_word(p
, ", and");
1255 p
->flags
&= ~TERMP_NOSPACE
;
1259 term_word(p
, "utilities exit");
1261 term_word(p
, "utility exits");
1263 term_word(p
, "0 on success, and >0 if an error occurs.");
1271 termp_nd_pre(DECL_ARGS
)
1274 if (MDOC_BODY
!= n
->type
)
1277 #if defined(__OpenBSD__) || defined(__linux__)
1278 term_word(p
, "\\(en");
1280 term_word(p
, "\\(em");
1288 termp_bl_pre(DECL_ARGS
)
1291 return(MDOC_HEAD
!= n
->type
);
1297 termp_bl_post(DECL_ARGS
)
1300 if (MDOC_BLOCK
== n
->type
)
1307 termp_op_post(DECL_ARGS
)
1310 if (MDOC_BODY
!= n
->type
)
1312 p
->flags
|= TERMP_NOSPACE
;
1313 term_word(p
, "\\(rB");
1319 termp_xr_pre(DECL_ARGS
)
1321 const struct mdoc_node
*nn
;
1323 if (NULL
== n
->child
)
1326 assert(MDOC_TEXT
== n
->child
->type
);
1329 term_word(p
, nn
->string
);
1330 if (NULL
== (nn
= nn
->next
))
1332 p
->flags
|= TERMP_NOSPACE
;
1334 p
->flags
|= TERMP_NOSPACE
;
1335 term_word(p
, nn
->string
);
1336 p
->flags
|= TERMP_NOSPACE
;
1344 termp_vt_pre(DECL_ARGS
)
1347 if (MDOC_ELEM
== n
->type
)
1348 return(termp_under_pre(p
, pair
, m
, n
));
1349 else if (MDOC_HEAD
== n
->type
)
1351 else if (MDOC_BLOCK
== n
->type
)
1354 return(termp_under_pre(p
, pair
, m
, n
));
1360 termp_vt_post(DECL_ARGS
)
1363 if (MDOC_BLOCK
!= n
->type
)
1365 if (n
->next
&& MDOC_Vt
== n
->next
->tok
)
1374 termp_bold_pre(DECL_ARGS
)
1377 term_fontpush(p
, TERMFONT_BOLD
);
1384 termp_fd_post(DECL_ARGS
)
1387 if (n
->sec
!= SEC_SYNOPSIS
|| ! (MDOC_LINE
& n
->flags
))
1391 if (n
->next
&& MDOC_Fd
!= n
->next
->tok
)
1398 termp_sh_pre(DECL_ARGS
)
1401 /* No vspace between consecutive `Sh' calls. */
1405 if (n
->prev
&& MDOC_Sh
== n
->prev
->tok
)
1406 if (NULL
== n
->prev
->body
->child
)
1411 term_fontpush(p
, TERMFONT_BOLD
);
1425 termp_sh_post(DECL_ARGS
)
1444 termp_op_pre(DECL_ARGS
)
1449 term_word(p
, "\\(lB");
1450 p
->flags
|= TERMP_NOSPACE
;
1461 termp_bt_pre(DECL_ARGS
)
1464 term_word(p
, "is currently in beta test.");
1471 termp_lb_post(DECL_ARGS
)
1474 if (SEC_LIBRARY
== n
->sec
&& MDOC_LINE
& n
->flags
)
1481 termp_ud_pre(DECL_ARGS
)
1484 term_word(p
, "currently under development.");
1491 termp_d1_pre(DECL_ARGS
)
1494 if (MDOC_BLOCK
!= n
->type
)
1497 p
->offset
+= (INDENT
+ 1);
1504 termp_d1_post(DECL_ARGS
)
1507 if (MDOC_BLOCK
!= n
->type
)
1515 termp_aq_pre(DECL_ARGS
)
1518 if (MDOC_BODY
!= n
->type
)
1520 term_word(p
, "\\(la");
1521 p
->flags
|= TERMP_NOSPACE
;
1528 termp_aq_post(DECL_ARGS
)
1531 if (MDOC_BODY
!= n
->type
)
1533 p
->flags
|= TERMP_NOSPACE
;
1534 term_word(p
, "\\(ra");
1540 termp_ft_pre(DECL_ARGS
)
1543 if (SEC_SYNOPSIS
== n
->sec
&& MDOC_LINE
& n
->flags
)
1544 if (n
->prev
&& MDOC_Fo
== n
->prev
->tok
)
1547 term_fontpush(p
, TERMFONT_UNDER
);
1554 termp_ft_post(DECL_ARGS
)
1557 if (SEC_SYNOPSIS
== n
->sec
&& MDOC_LINE
& n
->flags
)
1564 termp_fn_pre(DECL_ARGS
)
1566 const struct mdoc_node
*nn
;
1568 term_fontpush(p
, TERMFONT_BOLD
);
1569 term_word(p
, n
->child
->string
);
1572 p
->flags
|= TERMP_NOSPACE
;
1575 for (nn
= n
->child
->next
; nn
; nn
= nn
->next
) {
1576 term_fontpush(p
, TERMFONT_UNDER
);
1577 term_word(p
, nn
->string
);
1586 if (SEC_SYNOPSIS
== n
->sec
)
1595 termp_fn_post(DECL_ARGS
)
1598 if (n
->sec
== SEC_SYNOPSIS
&& n
->next
&& MDOC_LINE
& n
->flags
)
1605 termp_fa_pre(DECL_ARGS
)
1607 const struct mdoc_node
*nn
;
1609 if (n
->parent
->tok
!= MDOC_Fo
) {
1610 term_fontpush(p
, TERMFONT_UNDER
);
1614 for (nn
= n
->child
; nn
; nn
= nn
->next
) {
1615 term_fontpush(p
, TERMFONT_UNDER
);
1616 term_word(p
, nn
->string
);
1623 if (n
->child
&& n
->next
&& n
->next
->tok
== MDOC_Fa
)
1632 termp_bd_pre(DECL_ARGS
)
1636 const struct mdoc_node
*nn
;
1638 if (MDOC_BLOCK
== n
->type
) {
1639 print_bvspace(p
, n
, n
);
1641 } else if (MDOC_HEAD
== n
->type
)
1646 type
= arg_disptype(nn
);
1649 if (-1 != (i
= arg_getattr(MDOC_Offset
, nn
)))
1650 p
->offset
+= a2offs(&nn
->args
->argv
[i
]);
1653 * If -ragged or -filled are specified, the block does nothing
1654 * but change the indentation. If -unfilled or -literal are
1655 * specified, text is printed exactly as entered in the display:
1656 * for macro lines, a newline is appended to the line. Blank
1657 * lines are allowed.
1660 if (MDOC_Literal
!= type
&& MDOC_Unfilled
!= type
)
1664 rmax
= p
->maxrmargin
;
1665 p
->rmargin
= p
->maxrmargin
= TERM_MAXMARGIN
;
1667 for (nn
= n
->child
; nn
; nn
= nn
->next
) {
1668 p
->flags
|= TERMP_NOSPACE
;
1669 print_mdoc_node(p
, pair
, m
, nn
);
1670 if (NULL
== nn
->next
)
1672 if (nn
->prev
&& nn
->prev
->line
< nn
->line
)
1674 else if (NULL
== nn
->prev
)
1679 p
->maxrmargin
= rmax
;
1686 termp_bd_post(DECL_ARGS
)
1691 if (MDOC_BODY
!= n
->type
)
1694 type
= arg_disptype(n
->parent
);
1698 rmax
= p
->maxrmargin
;
1700 if (MDOC_Literal
== type
|| MDOC_Unfilled
== type
)
1701 p
->rmargin
= p
->maxrmargin
= TERM_MAXMARGIN
;
1703 p
->flags
|= TERMP_NOSPACE
;
1707 p
->maxrmargin
= rmax
;
1713 termp_qq_pre(DECL_ARGS
)
1716 if (MDOC_BODY
!= n
->type
)
1719 p
->flags
|= TERMP_NOSPACE
;
1726 termp_qq_post(DECL_ARGS
)
1729 if (MDOC_BODY
!= n
->type
)
1731 p
->flags
|= TERMP_NOSPACE
;
1738 termp_bx_post(DECL_ARGS
)
1742 p
->flags
|= TERMP_NOSPACE
;
1743 term_word(p
, "BSD");
1749 termp_xx_pre(DECL_ARGS
)
1785 termp_sq_pre(DECL_ARGS
)
1788 if (MDOC_BODY
!= n
->type
)
1790 term_word(p
, "\\(oq");
1791 p
->flags
|= TERMP_NOSPACE
;
1798 termp_sq_post(DECL_ARGS
)
1801 if (MDOC_BODY
!= n
->type
)
1803 p
->flags
|= TERMP_NOSPACE
;
1804 term_word(p
, "\\(aq");
1810 termp_pf_pre(DECL_ARGS
)
1813 p
->flags
|= TERMP_IGNDELIM
;
1820 termp_pf_post(DECL_ARGS
)
1823 p
->flags
&= ~TERMP_IGNDELIM
;
1824 p
->flags
|= TERMP_NOSPACE
;
1830 termp_ss_pre(DECL_ARGS
)
1840 term_fontpush(p
, TERMFONT_BOLD
);
1841 p
->offset
= HALFINDENT
;
1853 termp_ss_post(DECL_ARGS
)
1856 if (MDOC_HEAD
== n
->type
)
1863 termp_cd_pre(DECL_ARGS
)
1866 term_fontpush(p
, TERMFONT_BOLD
);
1874 termp_in_pre(DECL_ARGS
)
1877 term_fontpush(p
, TERMFONT_BOLD
);
1878 if (SEC_SYNOPSIS
== n
->sec
)
1879 term_word(p
, "#include");
1882 p
->flags
|= TERMP_NOSPACE
;
1889 termp_in_post(DECL_ARGS
)
1892 term_fontpush(p
, TERMFONT_BOLD
);
1893 p
->flags
|= TERMP_NOSPACE
;
1897 if (SEC_SYNOPSIS
!= n
->sec
&& ! (MDOC_LINE
& n
->flags
))
1902 * XXX Not entirely correct. If `.In foo bar' is specified in
1903 * the SYNOPSIS section, then it produces a single break after
1904 * the <foo>; mandoc asserts a vertical space. Since this
1905 * construction is rarely used, I think it's fine.
1907 if (n
->next
&& MDOC_In
!= n
->next
->tok
)
1914 termp_sp_pre(DECL_ARGS
)
1920 len
= n
->child
? a2height(n
->child
) : 1;
1932 for (i
= 0; i
< len
; i
++)
1941 termp_brq_pre(DECL_ARGS
)
1944 if (MDOC_BODY
!= n
->type
)
1946 term_word(p
, "\\(lC");
1947 p
->flags
|= TERMP_NOSPACE
;
1954 termp_brq_post(DECL_ARGS
)
1957 if (MDOC_BODY
!= n
->type
)
1959 p
->flags
|= TERMP_NOSPACE
;
1960 term_word(p
, "\\(rC");
1966 termp_bq_pre(DECL_ARGS
)
1969 if (MDOC_BODY
!= n
->type
)
1971 term_word(p
, "\\(lB");
1972 p
->flags
|= TERMP_NOSPACE
;
1979 termp_bq_post(DECL_ARGS
)
1982 if (MDOC_BODY
!= n
->type
)
1984 p
->flags
|= TERMP_NOSPACE
;
1985 term_word(p
, "\\(rB");
1991 termp_pq_pre(DECL_ARGS
)
1994 if (MDOC_BODY
!= n
->type
)
1996 term_word(p
, "\\&(");
1997 p
->flags
|= TERMP_NOSPACE
;
2004 termp_pq_post(DECL_ARGS
)
2007 if (MDOC_BODY
!= n
->type
)
2015 termp_fo_pre(DECL_ARGS
)
2017 const struct mdoc_node
*nn
;
2019 if (MDOC_BODY
== n
->type
) {
2020 p
->flags
|= TERMP_NOSPACE
;
2022 p
->flags
|= TERMP_NOSPACE
;
2024 } else if (MDOC_HEAD
!= n
->type
)
2027 term_fontpush(p
, TERMFONT_BOLD
);
2028 for (nn
= n
->child
; nn
; nn
= nn
->next
) {
2029 assert(MDOC_TEXT
== nn
->type
);
2030 term_word(p
, nn
->string
);
2040 termp_fo_post(DECL_ARGS
)
2043 if (MDOC_BODY
!= n
->type
)
2045 p
->flags
|= TERMP_NOSPACE
;
2047 p
->flags
|= TERMP_NOSPACE
;
2055 termp_bf_pre(DECL_ARGS
)
2057 const struct mdoc_node
*nn
;
2059 if (MDOC_HEAD
== n
->type
)
2061 else if (MDOC_BLOCK
!= n
->type
)
2064 if (NULL
== (nn
= n
->head
->child
)) {
2065 if (arg_hasattr(MDOC_Emphasis
, n
))
2066 term_fontpush(p
, TERMFONT_UNDER
);
2067 else if (arg_hasattr(MDOC_Symbolic
, n
))
2068 term_fontpush(p
, TERMFONT_BOLD
);
2070 term_fontpush(p
, TERMFONT_NONE
);
2075 assert(MDOC_TEXT
== nn
->type
);
2076 if (0 == strcmp("Em", nn
->string
))
2077 term_fontpush(p
, TERMFONT_UNDER
);
2078 else if (0 == strcmp("Sy", nn
->string
))
2079 term_fontpush(p
, TERMFONT_BOLD
);
2081 term_fontpush(p
, TERMFONT_NONE
);
2089 termp_sm_pre(DECL_ARGS
)
2092 assert(n
->child
&& MDOC_TEXT
== n
->child
->type
);
2093 if (0 == strcmp("on", n
->child
->string
))
2094 p
->flags
&= ~TERMP_NONOSPACE
;
2096 p
->flags
|= TERMP_NONOSPACE
;
2104 termp_ap_pre(DECL_ARGS
)
2107 p
->flags
|= TERMP_NOSPACE
;
2108 term_word(p
, "\\(aq");
2109 p
->flags
|= TERMP_NOSPACE
;
2116 termp____post(DECL_ARGS
)
2121 p
->flags
|= TERMP_NOSPACE
;
2122 term_word(p
, n
->next
? "," : ".");
2128 termp_li_pre(DECL_ARGS
)
2131 term_fontpush(p
, TERMFONT_NONE
);
2138 termp_lk_pre(DECL_ARGS
)
2140 const struct mdoc_node
*nn
;
2142 term_fontpush(p
, TERMFONT_UNDER
);
2145 if (NULL
== nn
->next
)
2148 term_word(p
, nn
->string
);
2151 p
->flags
|= TERMP_NOSPACE
;
2154 term_fontpush(p
, TERMFONT_BOLD
);
2155 for (nn
= nn
->next
; nn
; nn
= nn
->next
)
2156 term_word(p
, nn
->string
);
2165 termp_under_pre(DECL_ARGS
)
2168 term_fontpush(p
, TERMFONT_UNDER
);