]>
git.cameronkatri.com Git - mandoc.git/blob - mdoc_term.c
1 /* $Id: mdoc_term.c,v 1.140 2010/06/04 22:26:13 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(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 */
263 { NULL
, NULL
}, /* Ta */
268 terminal_mdoc(void *arg
, const struct mdoc
*mdoc
)
270 const struct mdoc_node
*n
;
271 const struct mdoc_meta
*m
;
274 p
= (struct termp
*)arg
;
277 p
->maxrmargin
= p
->defrmargin
;
280 if (NULL
== p
->symtab
)
282 case (TERMENC_ASCII
):
283 p
->symtab
= chars_init(CHARS_ASCII
);
293 print_mdoc_head(p
, NULL
, m
, n
);
295 print_mdoc_nodelist(p
, NULL
, m
, n
->child
);
296 print_foot(p
, NULL
, m
, n
);
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
;
352 print_foot(DECL_ARGS
)
354 char buf
[DATESIZ
], os
[BUFSIZ
];
356 term_fontrepl(p
, TERMFONT_NONE
);
359 * Output the footer in new-groff style, that is, three columns
360 * with the middle being the manual date and flanking columns
361 * being the operating system:
366 time2a(m
->date
, buf
, DATESIZ
);
367 strlcpy(os
, m
->os
, BUFSIZ
);
372 p
->rmargin
= (p
->maxrmargin
- strlen(buf
) + 1) / 2;
373 p
->flags
|= TERMP_NOSPACE
| TERMP_NOBREAK
;
378 p
->offset
= p
->rmargin
;
379 p
->rmargin
= p
->maxrmargin
- strlen(os
);
380 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
385 p
->offset
= p
->rmargin
;
386 p
->rmargin
= p
->maxrmargin
;
387 p
->flags
&= ~TERMP_NOBREAK
;
388 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
394 p
->rmargin
= p
->maxrmargin
;
401 print_mdoc_head(DECL_ARGS
)
403 char buf
[BUFSIZ
], title
[BUFSIZ
];
405 p
->rmargin
= p
->maxrmargin
;
409 * The header is strange. It has three components, which are
410 * really two with the first duplicated. It goes like this:
412 * IDENTIFIER TITLE IDENTIFIER
414 * The IDENTIFIER is NAME(SECTION), which is the command-name
415 * (if given, or "unknown" if not) followed by the manual page
416 * section. These are given in `Dt'. The TITLE is a free-form
417 * string depending on the manual volume. If not specified, it
418 * switches on the manual section.
422 strlcpy(buf
, m
->vol
, BUFSIZ
);
425 strlcat(buf
, " (", BUFSIZ
);
426 strlcat(buf
, m
->arch
, BUFSIZ
);
427 strlcat(buf
, ")", BUFSIZ
);
430 snprintf(title
, BUFSIZ
, "%s(%s)", m
->title
, m
->msec
);
433 p
->rmargin
= (p
->maxrmargin
- strlen(buf
) + 1) / 2;
434 p
->flags
|= TERMP_NOBREAK
| TERMP_NOSPACE
;
439 p
->offset
= p
->rmargin
;
440 p
->rmargin
= p
->maxrmargin
- strlen(title
);
441 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
446 p
->offset
= p
->rmargin
;
447 p
->rmargin
= p
->maxrmargin
;
448 p
->flags
&= ~TERMP_NOBREAK
;
449 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
455 p
->rmargin
= p
->maxrmargin
;
456 p
->flags
&= ~TERMP_NOSPACE
;
461 a2height(const struct mdoc_node
*n
)
465 assert(MDOC_TEXT
== n
->type
);
467 if ( ! a2roffsu(n
->string
, &su
, SCALE_VS
))
468 SCALE_VS_INIT(&su
, strlen(n
->string
));
470 return(term_vspan(&su
));
475 a2width(const struct mdoc_argv
*arg
, int pos
)
479 assert(arg
->value
[pos
]);
480 if ( ! a2roffsu(arg
->value
[pos
], &su
, SCALE_MAX
))
481 SCALE_HS_INIT(&su
, strlen(arg
->value
[pos
]));
483 return(term_hspan(&su
));
488 arg_disptype(const struct mdoc_node
*n
)
492 assert(MDOC_BLOCK
== n
->type
);
494 len
= (int)(n
->args
? n
->args
->argc
: 0);
496 for (i
= 0; i
< len
; i
++)
497 switch (n
->args
->argv
[i
].arg
) {
504 case (MDOC_Unfilled
):
507 return(n
->args
->argv
[i
].arg
);
517 a2offs(const struct mdoc_argv
*arg
)
521 if ('\0' == arg
->value
[0][0])
523 else if (0 == strcmp(arg
->value
[0], "left"))
525 else if (0 == strcmp(arg
->value
[0], "indent"))
527 else if (0 == strcmp(arg
->value
[0], "indent-two"))
528 return((INDENT
+ 1) * 2);
529 else if ( ! a2roffsu(arg
->value
[0], &su
, SCALE_MAX
))
530 SCALE_HS_INIT(&su
, strlen(arg
->value
[0]));
532 return(term_hspan(&su
));
537 * Return 1 if an argument has a particular argument value or 0 if it
538 * does not. See arg_getattr().
541 arg_hasattr(int arg
, const struct mdoc_node
*n
)
544 return(-1 != arg_getattr(arg
, n
));
549 * Get the index of an argument in a node's argument list or -1 if it
550 * does not exist. See arg_getattrs().
553 arg_getattr(int v
, const struct mdoc_node
*n
)
557 return(arg_getattrs(&v
, &val
, 1, n
) ? val
: -1);
562 * Walk through the argument list for a node and fill an array "vals"
563 * with the positions of the argument structures listed in "keys".
564 * Return the number of elements that were written into "vals", which
568 arg_getattrs(const int *keys
, int *vals
,
569 size_t sz
, const struct mdoc_node
*n
)
576 for (k
= i
= 0; i
< (int)n
->args
->argc
; i
++)
577 for (j
= 0; j
< (int)sz
; j
++)
578 if (n
->args
->argv
[i
].arg
== keys
[j
]) {
587 * Determine how much space to print out before block elements of `It'
588 * (and thus `Bl') and `Bd'. And then go ahead and print that space,
592 print_bvspace(struct termp
*p
,
593 const struct mdoc_node
*bl
,
594 const struct mdoc_node
*n
)
596 const struct mdoc_node
*nn
;
599 if (arg_hasattr(MDOC_Compact
, bl
))
602 /* Do not vspace directly after Ss/Sh. */
604 for (nn
= n
; nn
; nn
= nn
->parent
) {
605 if (MDOC_BLOCK
!= nn
->type
)
607 if (MDOC_Ss
== nn
->tok
)
609 if (MDOC_Sh
== nn
->tok
)
611 if (NULL
== nn
->prev
)
616 /* A `-column' does not assert vspace within the list. */
618 if (MDOC_Bl
== bl
->tok
&& LIST_column
== bl
->data
.list
)
619 if (n
->prev
&& MDOC_It
== n
->prev
->tok
)
622 /* A `-diag' without body does not vspace. */
624 if (MDOC_Bl
== bl
->tok
&& LIST_diag
== bl
->data
.list
)
625 if (n
->prev
&& MDOC_It
== n
->prev
->tok
) {
626 assert(n
->prev
->body
);
627 if (NULL
== n
->prev
->body
->child
)
637 termp_dq_pre(DECL_ARGS
)
640 if (MDOC_BODY
!= n
->type
)
643 term_word(p
, "\\(lq");
644 p
->flags
|= TERMP_NOSPACE
;
651 termp_dq_post(DECL_ARGS
)
654 if (MDOC_BODY
!= n
->type
)
657 p
->flags
|= TERMP_NOSPACE
;
658 term_word(p
, "\\(rq");
664 termp_it_pre(DECL_ARGS
)
666 const struct mdoc_node
*bl
, *nn
;
668 int i
, keys
[3], vals
[3];
669 size_t width
, offset
, ncols
, dcol
;
672 if (MDOC_BLOCK
== n
->type
) {
673 print_bvspace(p
, n
->parent
->parent
, n
);
677 bl
= n
->parent
->parent
->parent
;
679 /* Get list width, offset, and list type from argument list. */
681 keys
[0] = MDOC_Width
;
682 keys
[1] = MDOC_Offset
;
683 keys
[2] = MDOC_Column
;
685 vals
[0] = vals
[1] = vals
[2] = -1;
687 arg_getattrs(keys
, vals
, 3, bl
);
689 type
= bl
->data
.list
;
692 * First calculate width and offset. This is pretty easy unless
693 * we're a -column list, in which case all prior columns must
700 offset
= a2offs(&bl
->args
->argv
[vals
[1]]);
704 if (MDOC_HEAD
== n
->type
)
707 * Imitate groff's column handling:
708 * - For each earlier column, add its width.
709 * - For less than 5 columns, add four more blanks per
711 * - For exactly 5 columns, add three more blank per
713 * - For more than 5 columns, add only one column.
715 ncols
= bl
->args
->argv
[vals
[2]].sz
;
717 dcol
= ncols
< 5 ? 4 : ncols
== 5 ? 3 : 1;
720 * Calculate the offset by applying all prior MDOC_BODY,
721 * so we stop at the MDOC_HEAD (NULL == nn->prev).
724 for (i
= 0, nn
= n
->prev
;
725 nn
->prev
&& i
< (int)ncols
;
727 offset
+= dcol
+ a2width
728 (&bl
->args
->argv
[vals
[2]], i
);
732 * When exceeding the declared number of columns, leave
733 * the remaining widths at 0. This will later be
734 * adjusted to the default width of 10, or, for the last
735 * column, stretched to the right margin.
741 * Use the declared column widths, extended as explained
742 * in the preceding paragraph.
744 width
= a2width(&bl
->args
->argv
[vals
[2]], i
) + dcol
;
751 * Note: buffer the width by 2, which is groff's magic
752 * number for buffering single arguments. See the above
753 * handling for column for how this changes.
755 width
= a2width(&bl
->args
->argv
[vals
[0]], 0) + 2;
760 * List-type can override the width in the case of fixed-head
761 * values (bullet, dash/hyphen, enum). Tags need a non-zero
793 * Whitespace control. Inset bodies need an initial space,
794 * while diagonal bodies need two.
797 p
->flags
|= TERMP_NOSPACE
;
801 if (MDOC_BODY
== n
->type
)
802 term_word(p
, "\\ \\ ");
805 if (MDOC_BODY
== n
->type
)
812 p
->flags
|= TERMP_NOSPACE
;
816 if (MDOC_HEAD
== n
->type
)
817 term_fontpush(p
, TERMFONT_BOLD
);
824 * Pad and break control. This is the tricky part. These flags
825 * are documented in term_flushln() in term.c. Note that we're
826 * going to unset all of these flags in termp_it_post() when we
838 if (MDOC_HEAD
== n
->type
)
839 p
->flags
|= TERMP_NOBREAK
;
841 p
->flags
|= TERMP_NOLPAD
;
844 if (MDOC_HEAD
== n
->type
)
845 p
->flags
|= TERMP_NOBREAK
;
847 p
->flags
|= TERMP_NOLPAD
;
849 if (MDOC_HEAD
!= n
->type
)
853 * This is ugly. If `-hang' is specified and the body
854 * is a `Bl' or `Bd', then we want basically to nullify
855 * the "overstep" effect in term_flushln() and treat
856 * this as a `-ohang' list instead.
858 if (n
->next
->child
&&
859 (MDOC_Bl
== n
->next
->child
->tok
||
860 MDOC_Bd
== n
->next
->child
->tok
)) {
861 p
->flags
&= ~TERMP_NOBREAK
;
862 p
->flags
&= ~TERMP_NOLPAD
;
864 p
->flags
|= TERMP_HANG
;
867 if (MDOC_HEAD
== n
->type
)
868 p
->flags
|= TERMP_NOBREAK
| TERMP_TWOSPACE
;
870 p
->flags
|= TERMP_NOLPAD
;
872 if (MDOC_HEAD
!= n
->type
)
874 if (NULL
== n
->next
|| NULL
== n
->next
->child
)
875 p
->flags
|= TERMP_DANGLE
;
878 if (MDOC_HEAD
== n
->type
)
882 p
->flags
&= ~TERMP_NOBREAK
;
884 p
->flags
|= TERMP_NOBREAK
;
887 if (MDOC_BODY
== n
->prev
->type
)
888 p
->flags
|= TERMP_NOLPAD
;
892 if (MDOC_HEAD
== n
->type
)
893 p
->flags
|= TERMP_NOBREAK
;
900 * Margin control. Set-head-width lists have their right
901 * margins shortened. The body for these lists has the offset
902 * necessarily lengthened. Everybody gets the offset.
910 * Same stipulation as above, regarding `-hang'. We
911 * don't want to recalculate rmargin and offsets when
912 * using `Bd' or `Bl' within `-hang' overstep lists.
914 if (MDOC_HEAD
== n
->type
&& n
->next
->child
&&
915 (MDOC_Bl
== n
->next
->child
->tok
||
916 MDOC_Bd
== n
->next
->child
->tok
))
929 if (MDOC_HEAD
== n
->type
)
930 p
->rmargin
= p
->offset
+ width
;
936 p
->rmargin
= p
->offset
+ width
;
938 * XXX - this behaviour is not documented: the
939 * right-most column is filled to the right margin.
941 if (MDOC_HEAD
== n
->type
)
943 if (NULL
== n
->next
&& p
->rmargin
< p
->maxrmargin
)
944 p
->rmargin
= p
->maxrmargin
;
951 * The dash, hyphen, bullet and enum lists all have a special
952 * HEAD character (temporarily bold, in some cases).
955 if (MDOC_HEAD
== n
->type
)
958 term_fontpush(p
, TERMFONT_BOLD
);
959 term_word(p
, "\\[bu]");
965 term_fontpush(p
, TERMFONT_BOLD
);
966 term_word(p
, "\\(hy");
970 (pair
->ppair
->ppair
->count
)++;
971 snprintf(buf
, sizeof(buf
), "%d.",
972 pair
->ppair
->ppair
->count
);
980 * If we're not going to process our children, indicate so here.
993 if (MDOC_HEAD
== n
->type
)
997 if (MDOC_HEAD
== n
->type
)
1010 termp_it_post(DECL_ARGS
)
1012 enum mdoc_list type
;
1014 if (MDOC_BLOCK
== n
->type
)
1017 type
= n
->parent
->parent
->parent
->data
.list
;
1025 if (MDOC_BODY
== n
->type
)
1029 if (MDOC_BODY
== n
->type
)
1038 * Now that our output is flushed, we can reset our tags. Since
1039 * only `It' sets these flags, we're free to assume that nobody
1040 * has munged them in the meanwhile.
1043 p
->flags
&= ~TERMP_DANGLE
;
1044 p
->flags
&= ~TERMP_NOBREAK
;
1045 p
->flags
&= ~TERMP_TWOSPACE
;
1046 p
->flags
&= ~TERMP_NOLPAD
;
1047 p
->flags
&= ~TERMP_HANG
;
1053 termp_nm_pre(DECL_ARGS
)
1056 if (NULL
== n
->child
&& NULL
== m
->name
)
1059 if (SEC_SYNOPSIS
== n
->sec
&& MDOC_LINE
& n
->flags
)
1062 term_fontpush(p
, TERMFONT_BOLD
);
1064 if (NULL
== n
->child
)
1065 term_word(p
, m
->name
);
1073 termp_fl_pre(DECL_ARGS
)
1076 term_fontpush(p
, TERMFONT_BOLD
);
1077 term_word(p
, "\\-");
1080 p
->flags
|= TERMP_NOSPACE
;
1081 else if (n
->next
&& n
->next
->line
== n
->line
)
1082 p
->flags
|= TERMP_NOSPACE
;
1090 termp_an_pre(DECL_ARGS
)
1093 if (NULL
== n
->child
)
1097 * If not in the AUTHORS section, `An -split' will cause
1098 * newlines to occur before the author name. If in the AUTHORS
1099 * section, by default, the first `An' invocation is nosplit,
1100 * then all subsequent ones, regardless of whether interspersed
1101 * with other macros/text, are split. -split, in this case,
1102 * will override the condition of the implied first -nosplit.
1105 if (n
->sec
== SEC_AUTHORS
) {
1106 if ( ! (TERMP_ANPREC
& p
->flags
)) {
1107 if (TERMP_SPLIT
& p
->flags
)
1111 if (TERMP_NOSPLIT
& p
->flags
)
1117 if (TERMP_SPLIT
& p
->flags
)
1126 termp_an_post(DECL_ARGS
)
1130 if (SEC_AUTHORS
== n
->sec
)
1131 p
->flags
|= TERMP_ANPREC
;
1135 if (arg_hasattr(MDOC_Split
, n
)) {
1136 p
->flags
&= ~TERMP_NOSPLIT
;
1137 p
->flags
|= TERMP_SPLIT
;
1139 p
->flags
&= ~TERMP_SPLIT
;
1140 p
->flags
|= TERMP_NOSPLIT
;
1148 termp_ns_pre(DECL_ARGS
)
1151 p
->flags
|= TERMP_NOSPACE
;
1158 termp_rs_pre(DECL_ARGS
)
1161 if (SEC_SEE_ALSO
!= n
->sec
)
1163 if (MDOC_BLOCK
== n
->type
&& n
->prev
)
1171 termp_rv_pre(DECL_ARGS
)
1173 const struct mdoc_node
*nn
;
1176 term_word(p
, "The");
1178 for (nn
= n
->child
; nn
; nn
= nn
->next
) {
1179 term_fontpush(p
, TERMFONT_BOLD
);
1180 term_word(p
, nn
->string
);
1182 p
->flags
|= TERMP_NOSPACE
;
1183 if (nn
->next
&& NULL
== nn
->next
->next
)
1184 term_word(p
, "(), and");
1186 term_word(p
, "(),");
1191 if (n
->child
&& n
->child
->next
)
1192 term_word(p
, "functions return");
1194 term_word(p
, "function returns");
1196 term_word(p
, "the value 0 if successful; otherwise the value "
1197 "-1 is returned and the global variable");
1199 term_fontpush(p
, TERMFONT_UNDER
);
1200 term_word(p
, "errno");
1203 term_word(p
, "is set to indicate the error.");
1204 p
->flags
|= TERMP_SENTENCE
;
1212 termp_ex_pre(DECL_ARGS
)
1214 const struct mdoc_node
*nn
;
1216 term_word(p
, "The");
1218 for (nn
= n
->child
; nn
; nn
= nn
->next
) {
1219 term_fontpush(p
, TERMFONT_BOLD
);
1220 term_word(p
, nn
->string
);
1222 p
->flags
|= TERMP_NOSPACE
;
1223 if (nn
->next
&& NULL
== nn
->next
->next
)
1224 term_word(p
, ", and");
1228 p
->flags
&= ~TERMP_NOSPACE
;
1231 if (n
->child
&& n
->child
->next
)
1232 term_word(p
, "utilities exit");
1234 term_word(p
, "utility exits");
1236 term_word(p
, "0 on success, and >0 if an error occurs.");
1237 p
->flags
|= TERMP_SENTENCE
;
1245 termp_nd_pre(DECL_ARGS
)
1248 if (MDOC_BODY
!= n
->type
)
1251 #if defined(__OpenBSD__) || defined(__linux__)
1252 term_word(p
, "\\(en");
1254 term_word(p
, "\\(em");
1262 termp_bl_pre(DECL_ARGS
)
1265 return(MDOC_HEAD
!= n
->type
);
1271 termp_bl_post(DECL_ARGS
)
1274 if (MDOC_BLOCK
== n
->type
)
1281 termp_op_post(DECL_ARGS
)
1284 if (MDOC_BODY
!= n
->type
)
1286 p
->flags
|= TERMP_NOSPACE
;
1287 term_word(p
, "\\(rB");
1293 termp_xr_pre(DECL_ARGS
)
1295 const struct mdoc_node
*nn
;
1297 if (NULL
== n
->child
)
1300 assert(MDOC_TEXT
== n
->child
->type
);
1303 term_word(p
, nn
->string
);
1304 if (NULL
== (nn
= nn
->next
))
1306 p
->flags
|= TERMP_NOSPACE
;
1308 p
->flags
|= TERMP_NOSPACE
;
1309 term_word(p
, nn
->string
);
1310 p
->flags
|= TERMP_NOSPACE
;
1318 termp_vt_pre(DECL_ARGS
)
1321 if (MDOC_ELEM
== n
->type
)
1322 return(termp_under_pre(p
, pair
, m
, n
));
1323 else if (MDOC_HEAD
== n
->type
)
1325 else if (MDOC_BLOCK
== n
->type
)
1328 return(termp_under_pre(p
, pair
, m
, n
));
1334 termp_vt_post(DECL_ARGS
)
1337 if (MDOC_BLOCK
!= n
->type
)
1339 if (n
->next
&& MDOC_Vt
== n
->next
->tok
)
1348 termp_bold_pre(DECL_ARGS
)
1351 term_fontpush(p
, TERMFONT_BOLD
);
1358 termp_fd_post(DECL_ARGS
)
1367 termp_sh_pre(DECL_ARGS
)
1370 /* No vspace between consecutive `Sh' calls. */
1374 if (n
->prev
&& MDOC_Sh
== n
->prev
->tok
)
1375 if (NULL
== n
->prev
->body
->child
)
1380 term_fontpush(p
, TERMFONT_BOLD
);
1394 termp_sh_post(DECL_ARGS
)
1413 termp_op_pre(DECL_ARGS
)
1418 term_word(p
, "\\(lB");
1419 p
->flags
|= TERMP_NOSPACE
;
1430 termp_bt_pre(DECL_ARGS
)
1433 term_word(p
, "is currently in beta test.");
1434 p
->flags
|= TERMP_SENTENCE
;
1441 termp_lb_post(DECL_ARGS
)
1444 if (SEC_LIBRARY
== n
->sec
&& MDOC_LINE
& n
->flags
)
1451 termp_ud_pre(DECL_ARGS
)
1454 term_word(p
, "currently under development.");
1455 p
->flags
|= TERMP_SENTENCE
;
1462 termp_d1_pre(DECL_ARGS
)
1465 if (MDOC_BLOCK
!= n
->type
)
1468 p
->offset
+= (INDENT
+ 1);
1475 termp_d1_post(DECL_ARGS
)
1478 if (MDOC_BLOCK
!= n
->type
)
1486 termp_aq_pre(DECL_ARGS
)
1489 if (MDOC_BODY
!= n
->type
)
1491 term_word(p
, "\\(la");
1492 p
->flags
|= TERMP_NOSPACE
;
1499 termp_aq_post(DECL_ARGS
)
1502 if (MDOC_BODY
!= n
->type
)
1504 p
->flags
|= TERMP_NOSPACE
;
1505 term_word(p
, "\\(ra");
1511 termp_ft_pre(DECL_ARGS
)
1514 if (SEC_SYNOPSIS
== n
->sec
&& MDOC_LINE
& n
->flags
)
1515 if (n
->prev
&& MDOC_Fo
== n
->prev
->tok
)
1518 term_fontpush(p
, TERMFONT_UNDER
);
1525 termp_ft_post(DECL_ARGS
)
1528 if (SEC_SYNOPSIS
== n
->sec
&& MDOC_LINE
& n
->flags
)
1535 termp_fn_pre(DECL_ARGS
)
1537 const struct mdoc_node
*nn
;
1539 /* NB: MDOC_LINE has no effect on this macro! */
1540 if (SEC_SYNOPSIS
== n
->sec
) {
1541 if (n
->prev
&& MDOC_Ft
== n
->prev
->tok
)
1547 term_fontpush(p
, TERMFONT_BOLD
);
1548 term_word(p
, n
->child
->string
);
1551 p
->flags
|= TERMP_NOSPACE
;
1554 for (nn
= n
->child
->next
; nn
; nn
= nn
->next
) {
1555 term_fontpush(p
, TERMFONT_UNDER
);
1556 term_word(p
, nn
->string
);
1565 if (SEC_SYNOPSIS
== n
->sec
)
1574 termp_fn_post(DECL_ARGS
)
1577 /* NB: MDOC_LINE has no effect on this macro! */
1578 if (SEC_SYNOPSIS
== n
->sec
)
1585 termp_fa_pre(DECL_ARGS
)
1587 const struct mdoc_node
*nn
;
1589 if (n
->parent
->tok
!= MDOC_Fo
) {
1590 term_fontpush(p
, TERMFONT_UNDER
);
1594 for (nn
= n
->child
; nn
; nn
= nn
->next
) {
1595 term_fontpush(p
, TERMFONT_UNDER
);
1596 term_word(p
, nn
->string
);
1603 if (n
->child
&& n
->next
&& n
->next
->tok
== MDOC_Fa
)
1612 termp_bd_pre(DECL_ARGS
)
1617 const struct mdoc_node
*nn
;
1619 if (MDOC_BLOCK
== n
->type
) {
1620 print_bvspace(p
, n
, n
);
1622 } else if (MDOC_HEAD
== n
->type
)
1627 type
= arg_disptype(nn
);
1630 if (-1 != (i
= arg_getattr(MDOC_Offset
, nn
)))
1631 p
->offset
+= a2offs(&nn
->args
->argv
[i
]);
1634 * If -ragged or -filled are specified, the block does nothing
1635 * but change the indentation. If -unfilled or -literal are
1636 * specified, text is printed exactly as entered in the display:
1637 * for macro lines, a newline is appended to the line. Blank
1638 * lines are allowed.
1641 if (MDOC_Literal
!= type
&& MDOC_Unfilled
!= type
)
1644 tabwidth
= p
->tabwidth
;
1647 rmax
= p
->maxrmargin
;
1648 p
->rmargin
= p
->maxrmargin
= TERM_MAXMARGIN
;
1650 for (nn
= n
->child
; nn
; nn
= nn
->next
) {
1651 p
->flags
|= TERMP_NOSPACE
;
1652 print_mdoc_node(p
, pair
, m
, nn
);
1653 if (NULL
== nn
->prev
||
1654 nn
->prev
->line
< nn
->line
||
1658 p
->tabwidth
= tabwidth
;
1661 p
->maxrmargin
= rmax
;
1668 termp_bd_post(DECL_ARGS
)
1673 if (MDOC_BODY
!= n
->type
)
1676 type
= arg_disptype(n
->parent
);
1680 rmax
= p
->maxrmargin
;
1682 if (MDOC_Literal
== type
|| MDOC_Unfilled
== type
)
1683 p
->rmargin
= p
->maxrmargin
= TERM_MAXMARGIN
;
1685 p
->flags
|= TERMP_NOSPACE
;
1689 p
->maxrmargin
= rmax
;
1695 termp_qq_pre(DECL_ARGS
)
1698 if (MDOC_BODY
!= n
->type
)
1701 p
->flags
|= TERMP_NOSPACE
;
1708 termp_qq_post(DECL_ARGS
)
1711 if (MDOC_BODY
!= n
->type
)
1713 p
->flags
|= TERMP_NOSPACE
;
1720 termp_bx_post(DECL_ARGS
)
1724 p
->flags
|= TERMP_NOSPACE
;
1725 term_word(p
, "BSD");
1731 termp_xx_pre(DECL_ARGS
)
1767 termp_sq_pre(DECL_ARGS
)
1770 if (MDOC_BODY
!= n
->type
)
1772 term_word(p
, "\\(oq");
1773 p
->flags
|= TERMP_NOSPACE
;
1780 termp_sq_post(DECL_ARGS
)
1783 if (MDOC_BODY
!= n
->type
)
1785 p
->flags
|= TERMP_NOSPACE
;
1786 term_word(p
, "\\(aq");
1792 termp_pf_pre(DECL_ARGS
)
1795 p
->flags
|= TERMP_IGNDELIM
;
1802 termp_pf_post(DECL_ARGS
)
1805 p
->flags
&= ~TERMP_IGNDELIM
;
1806 p
->flags
|= TERMP_NOSPACE
;
1812 termp_ss_pre(DECL_ARGS
)
1822 term_fontpush(p
, TERMFONT_BOLD
);
1823 p
->offset
= HALFINDENT
;
1835 termp_ss_post(DECL_ARGS
)
1838 if (MDOC_HEAD
== n
->type
)
1845 termp_cd_pre(DECL_ARGS
)
1848 term_fontpush(p
, TERMFONT_BOLD
);
1856 termp_in_pre(DECL_ARGS
)
1859 if (SEC_SYNOPSIS
== n
->sec
&& MDOC_LINE
& n
->flags
) {
1860 term_fontpush(p
, TERMFONT_BOLD
);
1861 term_word(p
, "#include");
1865 term_fontpush(p
, TERMFONT_UNDER
);
1868 p
->flags
|= TERMP_NOSPACE
;
1875 termp_in_post(DECL_ARGS
)
1878 if (SEC_SYNOPSIS
== n
->sec
&& MDOC_LINE
& n
->flags
)
1879 term_fontpush(p
, TERMFONT_BOLD
);
1881 p
->flags
|= TERMP_NOSPACE
;
1884 if (SEC_SYNOPSIS
== n
->sec
&& MDOC_LINE
& n
->flags
) {
1893 termp_sp_pre(DECL_ARGS
)
1899 len
= n
->child
? a2height(n
->child
) : 1;
1911 for (i
= 0; i
< len
; i
++)
1920 termp_brq_pre(DECL_ARGS
)
1923 if (MDOC_BODY
!= n
->type
)
1925 term_word(p
, "\\(lC");
1926 p
->flags
|= TERMP_NOSPACE
;
1933 termp_brq_post(DECL_ARGS
)
1936 if (MDOC_BODY
!= n
->type
)
1938 p
->flags
|= TERMP_NOSPACE
;
1939 term_word(p
, "\\(rC");
1945 termp_bq_pre(DECL_ARGS
)
1948 if (MDOC_BODY
!= n
->type
)
1950 term_word(p
, "\\(lB");
1951 p
->flags
|= TERMP_NOSPACE
;
1958 termp_bq_post(DECL_ARGS
)
1961 if (MDOC_BODY
!= n
->type
)
1963 p
->flags
|= TERMP_NOSPACE
;
1964 term_word(p
, "\\(rB");
1970 termp_pq_pre(DECL_ARGS
)
1973 if (MDOC_BODY
!= n
->type
)
1975 term_word(p
, "\\&(");
1976 p
->flags
|= TERMP_NOSPACE
;
1983 termp_pq_post(DECL_ARGS
)
1986 if (MDOC_BODY
!= n
->type
)
1994 termp_fo_pre(DECL_ARGS
)
1996 const struct mdoc_node
*nn
;
1998 if (MDOC_BODY
== n
->type
) {
1999 p
->flags
|= TERMP_NOSPACE
;
2001 p
->flags
|= TERMP_NOSPACE
;
2003 } else if (MDOC_HEAD
!= n
->type
)
2006 term_fontpush(p
, TERMFONT_BOLD
);
2007 for (nn
= n
->child
; nn
; nn
= nn
->next
) {
2008 assert(MDOC_TEXT
== nn
->type
);
2009 term_word(p
, nn
->string
);
2019 termp_fo_post(DECL_ARGS
)
2022 if (MDOC_BODY
!= n
->type
)
2024 p
->flags
|= TERMP_NOSPACE
;
2026 p
->flags
|= TERMP_NOSPACE
;
2034 termp_bf_pre(DECL_ARGS
)
2036 const struct mdoc_node
*nn
;
2038 if (MDOC_HEAD
== n
->type
)
2040 else if (MDOC_BLOCK
!= n
->type
)
2043 if (NULL
== (nn
= n
->head
->child
)) {
2044 if (arg_hasattr(MDOC_Emphasis
, n
))
2045 term_fontpush(p
, TERMFONT_UNDER
);
2046 else if (arg_hasattr(MDOC_Symbolic
, n
))
2047 term_fontpush(p
, TERMFONT_BOLD
);
2049 term_fontpush(p
, TERMFONT_NONE
);
2054 assert(MDOC_TEXT
== nn
->type
);
2055 if (0 == strcmp("Em", nn
->string
))
2056 term_fontpush(p
, TERMFONT_UNDER
);
2057 else if (0 == strcmp("Sy", nn
->string
))
2058 term_fontpush(p
, TERMFONT_BOLD
);
2060 term_fontpush(p
, TERMFONT_NONE
);
2068 termp_sm_pre(DECL_ARGS
)
2071 assert(n
->child
&& MDOC_TEXT
== n
->child
->type
);
2072 if (0 == strcmp("on", n
->child
->string
))
2073 p
->flags
&= ~TERMP_NONOSPACE
;
2075 p
->flags
|= TERMP_NONOSPACE
;
2083 termp_ap_pre(DECL_ARGS
)
2086 p
->flags
|= TERMP_NOSPACE
;
2087 term_word(p
, "\\(aq");
2088 p
->flags
|= TERMP_NOSPACE
;
2095 termp____post(DECL_ARGS
)
2100 p
->flags
|= TERMP_NOSPACE
;
2101 term_word(p
, n
->next
? "," : ".");
2107 termp_li_pre(DECL_ARGS
)
2110 term_fontpush(p
, TERMFONT_NONE
);
2117 termp_lk_pre(DECL_ARGS
)
2119 const struct mdoc_node
*nn
;
2121 term_fontpush(p
, TERMFONT_UNDER
);
2124 if (NULL
== nn
->next
)
2127 term_word(p
, nn
->string
);
2130 p
->flags
|= TERMP_NOSPACE
;
2133 term_fontpush(p
, TERMFONT_BOLD
);
2134 for (nn
= nn
->next
; nn
; nn
= nn
->next
)
2135 term_word(p
, nn
->string
);
2144 termp_under_pre(DECL_ARGS
)
2147 term_fontpush(p
, TERMFONT_UNDER
);