1 /* $Id: mdoc_term.c,v 1.112 2010/03/29 19:28:04 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.
17 #include <sys/types.h>
35 struct termpair
*ppair
;
39 #define DECL_ARGS struct termp *p, \
40 struct termpair *pair, \
41 const struct mdoc_meta *m, \
42 const struct mdoc_node *n
45 int (*pre
)(DECL_ARGS
);
46 void (*post
)(DECL_ARGS
);
49 static size_t a2width(const struct mdoc_argv
*, int);
50 static size_t a2height(const struct mdoc_node
*);
51 static size_t a2offs(const struct mdoc_argv
*);
53 static int arg_hasattr(int, const struct mdoc_node
*);
54 static int arg_getattrs(const int *, int *, size_t,
55 const struct mdoc_node
*);
56 static int arg_getattr(int, const struct mdoc_node
*);
57 static int arg_listtype(const struct mdoc_node
*);
58 static void print_bvspace(struct termp
*,
59 const struct mdoc_node
*,
60 const struct mdoc_node
*);
61 static void print_mdoc_node(DECL_ARGS
);
62 static void print_mdoc_head(DECL_ARGS
);
63 static void print_mdoc_nodelist(DECL_ARGS
);
64 static void print_foot(DECL_ARGS
);
66 static void termp____post(DECL_ARGS
);
67 static void termp_an_post(DECL_ARGS
);
68 static void termp_aq_post(DECL_ARGS
);
69 static void termp_bd_post(DECL_ARGS
);
70 static void termp_bl_post(DECL_ARGS
);
71 static void termp_bq_post(DECL_ARGS
);
72 static void termp_brq_post(DECL_ARGS
);
73 static void termp_bx_post(DECL_ARGS
);
74 static void termp_d1_post(DECL_ARGS
);
75 static void termp_dq_post(DECL_ARGS
);
76 static void termp_fd_post(DECL_ARGS
);
77 static void termp_fn_post(DECL_ARGS
);
78 static void termp_fo_post(DECL_ARGS
);
79 static void termp_ft_post(DECL_ARGS
);
80 static void termp_in_post(DECL_ARGS
);
81 static void termp_it_post(DECL_ARGS
);
82 static void termp_lb_post(DECL_ARGS
);
83 static void termp_op_post(DECL_ARGS
);
84 static void termp_pf_post(DECL_ARGS
);
85 static void termp_pq_post(DECL_ARGS
);
86 static void termp_qq_post(DECL_ARGS
);
87 static void termp_sh_post(DECL_ARGS
);
88 static void termp_sq_post(DECL_ARGS
);
89 static void termp_ss_post(DECL_ARGS
);
90 static void termp_vt_post(DECL_ARGS
);
92 static int termp__t_pre(DECL_ARGS
);
93 static int termp_an_pre(DECL_ARGS
);
94 static int termp_ap_pre(DECL_ARGS
);
95 static int termp_aq_pre(DECL_ARGS
);
96 static int termp_bd_pre(DECL_ARGS
);
97 static int termp_bf_pre(DECL_ARGS
);
98 static int termp_bold_pre(DECL_ARGS
);
99 static int termp_bq_pre(DECL_ARGS
);
100 static int termp_brq_pre(DECL_ARGS
);
101 static int termp_bt_pre(DECL_ARGS
);
102 static int termp_cd_pre(DECL_ARGS
);
103 static int termp_d1_pre(DECL_ARGS
);
104 static int termp_dq_pre(DECL_ARGS
);
105 static int termp_ex_pre(DECL_ARGS
);
106 static int termp_fa_pre(DECL_ARGS
);
107 static int termp_fl_pre(DECL_ARGS
);
108 static int termp_fn_pre(DECL_ARGS
);
109 static int termp_fo_pre(DECL_ARGS
);
110 static int termp_ft_pre(DECL_ARGS
);
111 static int termp_in_pre(DECL_ARGS
);
112 static int termp_it_pre(DECL_ARGS
);
113 static int termp_li_pre(DECL_ARGS
);
114 static int termp_lk_pre(DECL_ARGS
);
115 static int termp_nd_pre(DECL_ARGS
);
116 static int termp_nm_pre(DECL_ARGS
);
117 static int termp_ns_pre(DECL_ARGS
);
118 static int termp_op_pre(DECL_ARGS
);
119 static int termp_pf_pre(DECL_ARGS
);
120 static int termp_pq_pre(DECL_ARGS
);
121 static int termp_qq_pre(DECL_ARGS
);
122 static int termp_rs_pre(DECL_ARGS
);
123 static int termp_rv_pre(DECL_ARGS
);
124 static int termp_sh_pre(DECL_ARGS
);
125 static int termp_sm_pre(DECL_ARGS
);
126 static int termp_sp_pre(DECL_ARGS
);
127 static int termp_sq_pre(DECL_ARGS
);
128 static int termp_ss_pre(DECL_ARGS
);
129 static int termp_under_pre(DECL_ARGS
);
130 static int termp_ud_pre(DECL_ARGS
);
131 static int termp_vt_pre(DECL_ARGS
);
132 static int termp_xr_pre(DECL_ARGS
);
133 static int termp_xx_pre(DECL_ARGS
);
135 static const struct termact termacts
[MDOC_MAX
] = {
136 { termp_ap_pre
, NULL
}, /* Ap */
137 { NULL
, NULL
}, /* Dd */
138 { NULL
, NULL
}, /* Dt */
139 { NULL
, NULL
}, /* Os */
140 { termp_sh_pre
, termp_sh_post
}, /* Sh */
141 { termp_ss_pre
, termp_ss_post
}, /* Ss */
142 { termp_sp_pre
, NULL
}, /* Pp */
143 { termp_d1_pre
, termp_d1_post
}, /* D1 */
144 { termp_d1_pre
, termp_d1_post
}, /* Dl */
145 { termp_bd_pre
, termp_bd_post
}, /* Bd */
146 { NULL
, NULL
}, /* Ed */
147 { NULL
, termp_bl_post
}, /* Bl */
148 { NULL
, NULL
}, /* El */
149 { termp_it_pre
, termp_it_post
}, /* It */
150 { NULL
, NULL
}, /* Ad */
151 { termp_an_pre
, termp_an_post
}, /* An */
152 { termp_under_pre
, NULL
}, /* Ar */
153 { termp_cd_pre
, NULL
}, /* Cd */
154 { termp_bold_pre
, NULL
}, /* Cm */
155 { NULL
, NULL
}, /* Dv */
156 { NULL
, NULL
}, /* Er */
157 { NULL
, NULL
}, /* Ev */
158 { termp_ex_pre
, NULL
}, /* Ex */
159 { termp_fa_pre
, NULL
}, /* Fa */
160 { termp_bold_pre
, termp_fd_post
}, /* Fd */
161 { termp_fl_pre
, NULL
}, /* Fl */
162 { termp_fn_pre
, termp_fn_post
}, /* Fn */
163 { termp_ft_pre
, termp_ft_post
}, /* Ft */
164 { termp_bold_pre
, NULL
}, /* Ic */
165 { termp_in_pre
, termp_in_post
}, /* In */
166 { termp_li_pre
, NULL
}, /* Li */
167 { termp_nd_pre
, NULL
}, /* Nd */
168 { termp_nm_pre
, NULL
}, /* Nm */
169 { termp_op_pre
, termp_op_post
}, /* Op */
170 { NULL
, NULL
}, /* Ot */
171 { termp_under_pre
, NULL
}, /* Pa */
172 { termp_rv_pre
, NULL
}, /* Rv */
173 { NULL
, NULL
}, /* St */
174 { termp_under_pre
, NULL
}, /* Va */
175 { termp_vt_pre
, termp_vt_post
}, /* Vt */
176 { termp_xr_pre
, NULL
}, /* Xr */
177 { NULL
, termp____post
}, /* %A */
178 { termp_under_pre
, termp____post
}, /* %B */
179 { NULL
, termp____post
}, /* %D */
180 { termp_under_pre
, termp____post
}, /* %I */
181 { termp_under_pre
, termp____post
}, /* %J */
182 { NULL
, termp____post
}, /* %N */
183 { NULL
, termp____post
}, /* %O */
184 { NULL
, termp____post
}, /* %P */
185 { NULL
, termp____post
}, /* %R */
186 { termp__t_pre
, termp____post
}, /* %T */
187 { NULL
, termp____post
}, /* %V */
188 { NULL
, NULL
}, /* Ac */
189 { termp_aq_pre
, termp_aq_post
}, /* Ao */
190 { termp_aq_pre
, termp_aq_post
}, /* Aq */
191 { NULL
, NULL
}, /* At */
192 { NULL
, NULL
}, /* Bc */
193 { termp_bf_pre
, NULL
}, /* Bf */
194 { termp_bq_pre
, termp_bq_post
}, /* Bo */
195 { termp_bq_pre
, termp_bq_post
}, /* Bq */
196 { termp_xx_pre
, NULL
}, /* Bsx */
197 { NULL
, termp_bx_post
}, /* Bx */
198 { NULL
, NULL
}, /* Db */
199 { NULL
, NULL
}, /* Dc */
200 { termp_dq_pre
, termp_dq_post
}, /* Do */
201 { termp_dq_pre
, termp_dq_post
}, /* Dq */
202 { NULL
, NULL
}, /* Ec */ /* FIXME: no space */
203 { NULL
, NULL
}, /* Ef */
204 { termp_under_pre
, NULL
}, /* Em */
205 { NULL
, NULL
}, /* Eo */
206 { termp_xx_pre
, NULL
}, /* Fx */
207 { termp_bold_pre
, NULL
}, /* Ms */ /* FIXME: convert to symbol? */
208 { NULL
, NULL
}, /* No */
209 { termp_ns_pre
, NULL
}, /* Ns */
210 { termp_xx_pre
, NULL
}, /* Nx */
211 { termp_xx_pre
, NULL
}, /* Ox */
212 { NULL
, NULL
}, /* Pc */
213 { termp_pf_pre
, termp_pf_post
}, /* Pf */
214 { termp_pq_pre
, termp_pq_post
}, /* Po */
215 { termp_pq_pre
, termp_pq_post
}, /* Pq */
216 { NULL
, NULL
}, /* Qc */
217 { termp_sq_pre
, termp_sq_post
}, /* Ql */
218 { termp_qq_pre
, termp_qq_post
}, /* Qo */
219 { termp_qq_pre
, termp_qq_post
}, /* Qq */
220 { NULL
, NULL
}, /* Re */
221 { termp_rs_pre
, NULL
}, /* Rs */
222 { NULL
, NULL
}, /* Sc */
223 { termp_sq_pre
, termp_sq_post
}, /* So */
224 { termp_sq_pre
, termp_sq_post
}, /* Sq */
225 { termp_sm_pre
, NULL
}, /* Sm */
226 { termp_under_pre
, NULL
}, /* Sx */
227 { termp_bold_pre
, NULL
}, /* Sy */
228 { NULL
, NULL
}, /* Tn */
229 { termp_xx_pre
, NULL
}, /* Ux */
230 { NULL
, NULL
}, /* Xc */
231 { NULL
, NULL
}, /* Xo */
232 { termp_fo_pre
, termp_fo_post
}, /* Fo */
233 { NULL
, NULL
}, /* Fc */
234 { termp_op_pre
, termp_op_post
}, /* Oo */
235 { NULL
, NULL
}, /* Oc */
236 { NULL
, NULL
}, /* Bk */
237 { NULL
, NULL
}, /* Ek */
238 { termp_bt_pre
, NULL
}, /* Bt */
239 { NULL
, NULL
}, /* Hf */
240 { NULL
, NULL
}, /* Fr */
241 { termp_ud_pre
, NULL
}, /* Ud */
242 { NULL
, termp_lb_post
}, /* Lb */
243 { termp_sp_pre
, NULL
}, /* Lp */
244 { termp_lk_pre
, NULL
}, /* Lk */
245 { termp_under_pre
, NULL
}, /* Mt */
246 { termp_brq_pre
, termp_brq_post
}, /* Brq */
247 { termp_brq_pre
, termp_brq_post
}, /* Bro */
248 { NULL
, NULL
}, /* Brc */
249 { NULL
, termp____post
}, /* %C */
250 { NULL
, NULL
}, /* Es */ /* TODO */
251 { NULL
, NULL
}, /* En */ /* TODO */
252 { termp_xx_pre
, NULL
}, /* Dx */
253 { NULL
, termp____post
}, /* %Q */
254 { termp_sp_pre
, NULL
}, /* br */
255 { termp_sp_pre
, NULL
}, /* sp */
256 { termp_under_pre
, termp____post
}, /* %U */
261 terminal_mdoc(void *arg
, const struct mdoc
*mdoc
)
263 const struct mdoc_node
*n
;
264 const struct mdoc_meta
*m
;
267 p
= (struct termp
*)arg
;
272 if (NULL
== p
->symtab
)
274 case (TERMENC_ASCII
):
275 p
->symtab
= chars_init(CHARS_ASCII
);
285 print_mdoc_head(p
, NULL
, m
, n
);
287 print_mdoc_nodelist(p
, NULL
, m
, n
->child
);
288 print_foot(p
, NULL
, m
, n
);
293 print_mdoc_nodelist(DECL_ARGS
)
296 print_mdoc_node(p
, pair
, m
, n
);
298 print_mdoc_nodelist(p
, pair
, m
, n
->next
);
304 print_mdoc_node(DECL_ARGS
)
308 struct termpair npair
;
309 size_t offset
, rmargin
;
313 rmargin
= p
->rmargin
;
314 font
= term_fontq(p
);
316 memset(&npair
, 0, sizeof(struct termpair
));
319 if (MDOC_TEXT
!= n
->type
) {
320 if (termacts
[n
->tok
].pre
)
321 chld
= (*termacts
[n
->tok
].pre
)(p
, &npair
, m
, n
);
323 term_word(p
, n
->string
);
325 if (chld
&& n
->child
)
326 print_mdoc_nodelist(p
, &npair
, m
, n
->child
);
328 term_fontpopq(p
, font
);
330 if (MDOC_TEXT
!= n
->type
)
331 if (termacts
[n
->tok
].post
)
332 (*termacts
[n
->tok
].post
)(p
, &npair
, m
, n
);
335 p
->rmargin
= rmargin
;
341 print_foot(DECL_ARGS
)
343 char buf
[DATESIZ
], os
[BUFSIZ
];
345 term_fontrepl(p
, TERMFONT_NONE
);
348 * Output the footer in new-groff style, that is, three columns
349 * with the middle being the manual date and flanking columns
350 * being the operating system:
355 time2a(m
->date
, buf
, DATESIZ
);
356 strlcpy(os
, m
->os
, BUFSIZ
);
361 p
->rmargin
= (p
->maxrmargin
- strlen(buf
) + 1) / 2;
362 p
->flags
|= TERMP_NOSPACE
| TERMP_NOBREAK
;
367 p
->offset
= p
->rmargin
;
368 p
->rmargin
= p
->maxrmargin
- strlen(os
);
369 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
374 p
->offset
= p
->rmargin
;
375 p
->rmargin
= p
->maxrmargin
;
376 p
->flags
&= ~TERMP_NOBREAK
;
377 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
383 p
->rmargin
= p
->maxrmargin
;
390 print_mdoc_head(DECL_ARGS
)
392 char buf
[BUFSIZ
], title
[BUFSIZ
];
394 p
->rmargin
= p
->maxrmargin
;
398 * The header is strange. It has three components, which are
399 * really two with the first duplicated. It goes like this:
401 * IDENTIFIER TITLE IDENTIFIER
403 * The IDENTIFIER is NAME(SECTION), which is the command-name
404 * (if given, or "unknown" if not) followed by the manual page
405 * section. These are given in `Dt'. The TITLE is a free-form
406 * string depending on the manual volume. If not specified, it
407 * switches on the manual section.
411 strlcpy(buf
, m
->vol
, BUFSIZ
);
414 strlcat(buf
, " (", BUFSIZ
);
415 strlcat(buf
, m
->arch
, BUFSIZ
);
416 strlcat(buf
, ")", BUFSIZ
);
419 snprintf(title
, BUFSIZ
, "%s(%d)", m
->title
, m
->msec
);
422 p
->rmargin
= (p
->maxrmargin
- strlen(buf
) + 1) / 2;
423 p
->flags
|= TERMP_NOBREAK
| TERMP_NOSPACE
;
428 p
->offset
= p
->rmargin
;
429 p
->rmargin
= p
->maxrmargin
- strlen(title
);
430 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
435 p
->offset
= p
->rmargin
;
436 p
->rmargin
= p
->maxrmargin
;
437 p
->flags
&= ~TERMP_NOBREAK
;
438 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
444 p
->rmargin
= p
->maxrmargin
;
445 p
->flags
&= ~TERMP_NOSPACE
;
450 a2height(const struct mdoc_node
*n
)
454 assert(MDOC_TEXT
== n
->type
);
456 if ( ! a2roffsu(n
->string
, &su
, SCALE_VS
))
457 SCALE_VS_INIT(&su
, strlen(n
->string
));
459 return(term_vspan(&su
));
464 a2width(const struct mdoc_argv
*arg
, int pos
)
468 assert(arg
->value
[pos
]);
469 if ( ! a2roffsu(arg
->value
[pos
], &su
, SCALE_MAX
))
470 SCALE_HS_INIT(&su
, strlen(arg
->value
[pos
]));
472 return(term_hspan(&su
));
477 arg_listtype(const struct mdoc_node
*n
)
481 assert(MDOC_BLOCK
== n
->type
);
483 len
= (int)(n
->args
? n
->args
->argc
: 0);
485 for (i
= 0; i
< len
; i
++)
486 switch (n
->args
->argv
[i
].arg
) {
508 return(n
->args
->argv
[i
].arg
);
518 a2offs(const struct mdoc_argv
*arg
)
522 if ('\0' == arg
->value
[0][0])
524 else if (0 == strcmp(arg
->value
[0], "left"))
526 else if (0 == strcmp(arg
->value
[0], "indent"))
528 else if (0 == strcmp(arg
->value
[0], "indent-two"))
529 return((INDENT
+ 1) * 2);
530 else if ( ! a2roffsu(arg
->value
[0], &su
, SCALE_MAX
))
531 SCALE_HS_INIT(&su
, strlen(arg
->value
[0]));
533 return(term_hspan(&su
));
538 * Return 1 if an argument has a particular argument value or 0 if it
539 * does not. See arg_getattr().
542 arg_hasattr(int arg
, const struct mdoc_node
*n
)
545 return(-1 != arg_getattr(arg
, n
));
550 * Get the index of an argument in a node's argument list or -1 if it
551 * does not exist. See arg_getattrs().
554 arg_getattr(int v
, const struct mdoc_node
*n
)
558 return(arg_getattrs(&v
, &val
, 1, n
) ? val
: -1);
563 * Walk through the argument list for a node and fill an array "vals"
564 * with the positions of the argument structures listed in "keys".
565 * Return the number of elements that were written into "vals", which
569 arg_getattrs(const int *keys
, int *vals
,
570 size_t sz
, const struct mdoc_node
*n
)
577 for (k
= i
= 0; i
< (int)n
->args
->argc
; i
++)
578 for (j
= 0; j
< (int)sz
; j
++)
579 if (n
->args
->argv
[i
].arg
== keys
[j
]) {
588 * Determine how much space to print out before block elements of `It'
589 * (and thus `Bl') and `Bd'. And then go ahead and print that space,
593 print_bvspace(struct termp
*p
,
594 const struct mdoc_node
*bl
,
595 const struct mdoc_node
*n
)
597 const struct mdoc_node
*nn
;
600 if (arg_hasattr(MDOC_Compact
, bl
))
603 /* Do not vspace directly after Ss/Sh. */
605 for (nn
= n
; nn
; nn
= nn
->parent
) {
606 if (MDOC_BLOCK
!= nn
->type
)
608 if (MDOC_Ss
== nn
->tok
)
610 if (MDOC_Sh
== nn
->tok
)
612 if (NULL
== nn
->prev
)
617 /* A `-column' does not assert vspace within the list. */
619 if (MDOC_Bl
== bl
->tok
&& arg_hasattr(MDOC_Column
, bl
))
620 if (n
->prev
&& MDOC_It
== n
->prev
->tok
)
623 /* A `-diag' without body does not vspace. */
625 if (MDOC_Bl
== bl
->tok
&& arg_hasattr(MDOC_Diag
, bl
))
626 if (n
->prev
&& MDOC_It
== n
->prev
->tok
) {
627 assert(n
->prev
->body
);
628 if (NULL
== n
->prev
->body
->child
)
638 termp_dq_pre(DECL_ARGS
)
641 if (MDOC_BODY
!= n
->type
)
644 term_word(p
, "\\(lq");
645 p
->flags
|= TERMP_NOSPACE
;
652 termp_dq_post(DECL_ARGS
)
655 if (MDOC_BODY
!= n
->type
)
658 p
->flags
|= TERMP_NOSPACE
;
659 term_word(p
, "\\(rq");
665 termp_it_pre(DECL_ARGS
)
667 const struct mdoc_node
*bl
, *nn
;
669 int i
, type
, keys
[3], vals
[3];
670 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
= arg_listtype(bl
);
693 * First calculate width and offset. This is pretty easy unless
694 * we're a -column list, in which case all prior columns must
701 offset
= a2offs(&bl
->args
->argv
[vals
[1]]);
705 if (MDOC_BODY
== n
->type
)
708 * Imitate groff's column handling:
709 * - For each earlier column, add its width.
710 * - For less than 5 columns, add four more blanks per
712 * - For exactly 5 columns, add three more blank per
714 * - For more than 5 columns, add only one column.
716 ncols
= bl
->args
->argv
[vals
[2]].sz
;
718 dcol
= ncols
< 5 ? 4 : ncols
== 5 ? 3 : 1;
720 for (i
= 0, nn
= n
->prev
;
721 nn
&& i
< (int)ncols
;
723 offset
+= dcol
+ a2width
724 (&bl
->args
->argv
[vals
[2]], i
);
728 * When exceeding the declared number of columns, leave
729 * the remaining widths at 0. This will later be
730 * adjusted to the default width of 10, or, for the last
731 * column, stretched to the right margin.
737 * Use the declared column widths, extended as explained
738 * in the preceding paragraph.
740 width
= a2width(&bl
->args
->argv
[vals
[2]], i
) + dcol
;
747 * Note: buffer the width by 2, which is groff's magic
748 * number for buffering single arguments. See the above
749 * handling for column for how this changes.
751 width
= a2width(&bl
->args
->argv
[vals
[0]], 0) + 2;
756 * List-type can override the width in the case of fixed-head
757 * values (bullet, dash/hyphen, enum). Tags need a non-zero
789 * Whitespace control. Inset bodies need an initial space,
790 * while diagonal bodies need two.
793 p
->flags
|= TERMP_NOSPACE
;
797 if (MDOC_BODY
== n
->type
)
798 term_word(p
, "\\ \\ ");
801 if (MDOC_BODY
== n
->type
)
808 p
->flags
|= TERMP_NOSPACE
;
812 if (MDOC_HEAD
== n
->type
)
813 term_fontpush(p
, TERMFONT_BOLD
);
820 * Pad and break control. This is the tricky part. These flags
821 * are documented in term_flushln() in term.c. Note that we're
822 * going to unset all of these flags in termp_it_post() when we
834 if (MDOC_HEAD
== n
->type
)
835 p
->flags
|= TERMP_NOBREAK
;
837 p
->flags
|= TERMP_NOLPAD
;
840 if (MDOC_HEAD
== n
->type
)
841 p
->flags
|= TERMP_NOBREAK
;
843 p
->flags
|= TERMP_NOLPAD
;
845 if (MDOC_HEAD
!= n
->type
)
849 * This is ugly. If `-hang' is specified and the body
850 * is a `Bl' or `Bd', then we want basically to nullify
851 * the "overstep" effect in term_flushln() and treat
852 * this as a `-ohang' list instead.
854 if (n
->next
->child
&&
855 (MDOC_Bl
== n
->next
->child
->tok
||
856 MDOC_Bd
== n
->next
->child
->tok
)) {
857 p
->flags
&= ~TERMP_NOBREAK
;
858 p
->flags
&= ~TERMP_NOLPAD
;
860 p
->flags
|= TERMP_HANG
;
863 if (MDOC_HEAD
== n
->type
)
864 p
->flags
|= TERMP_NOBREAK
| TERMP_TWOSPACE
;
866 p
->flags
|= TERMP_NOLPAD
;
868 if (MDOC_HEAD
!= n
->type
)
870 if (NULL
== n
->next
|| NULL
== n
->next
->child
)
871 p
->flags
|= TERMP_DANGLE
;
874 if (MDOC_HEAD
== n
->type
) {
876 if (MDOC_BODY
== n
->next
->type
)
877 p
->flags
&= ~TERMP_NOBREAK
;
879 p
->flags
|= TERMP_NOBREAK
;
881 p
->flags
|= TERMP_NOLPAD
;
885 if (MDOC_HEAD
== n
->type
)
886 p
->flags
|= TERMP_NOBREAK
;
893 * Margin control. Set-head-width lists have their right
894 * margins shortened. The body for these lists has the offset
895 * necessarily lengthened. Everybody gets the offset.
903 * Same stipulation as above, regarding `-hang'. We
904 * don't want to recalculate rmargin and offsets when
905 * using `Bd' or `Bl' within `-hang' overstep lists.
907 if (MDOC_HEAD
== n
->type
&& n
->next
->child
&&
908 (MDOC_Bl
== n
->next
->child
->tok
||
909 MDOC_Bd
== n
->next
->child
->tok
))
922 if (MDOC_HEAD
== n
->type
)
923 p
->rmargin
= p
->offset
+ width
;
929 p
->rmargin
= p
->offset
+ width
;
931 * XXX - this behaviour is not documented: the
932 * right-most column is filled to the right margin.
934 if (MDOC_HEAD
== n
->type
&&
935 MDOC_BODY
== n
->next
->type
&&
936 p
->rmargin
< p
->maxrmargin
)
937 p
->rmargin
= p
->maxrmargin
;
944 * The dash, hyphen, bullet and enum lists all have a special
945 * HEAD character (temporarily bold, in some cases).
948 if (MDOC_HEAD
== n
->type
)
951 term_fontpush(p
, TERMFONT_BOLD
);
952 term_word(p
, "\\[bu]");
958 term_fontpush(p
, TERMFONT_BOLD
);
959 term_word(p
, "\\(hy");
963 (pair
->ppair
->ppair
->count
)++;
964 snprintf(buf
, sizeof(buf
), "%d.",
965 pair
->ppair
->ppair
->count
);
973 * If we're not going to process our children, indicate so here.
986 if (MDOC_HEAD
== n
->type
)
990 if (MDOC_BODY
== n
->type
)
1003 termp_it_post(DECL_ARGS
)
1007 if (MDOC_BLOCK
== n
->type
)
1010 type
= arg_listtype(n
->parent
->parent
->parent
);
1019 if (MDOC_BODY
== n
->type
)
1023 if (MDOC_HEAD
== n
->type
)
1032 * Now that our output is flushed, we can reset our tags. Since
1033 * only `It' sets these flags, we're free to assume that nobody
1034 * has munged them in the meanwhile.
1037 p
->flags
&= ~TERMP_DANGLE
;
1038 p
->flags
&= ~TERMP_NOBREAK
;
1039 p
->flags
&= ~TERMP_TWOSPACE
;
1040 p
->flags
&= ~TERMP_NOLPAD
;
1041 p
->flags
&= ~TERMP_HANG
;
1047 termp_nm_pre(DECL_ARGS
)
1050 if (SEC_SYNOPSIS
== n
->sec
)
1053 term_fontpush(p
, TERMFONT_BOLD
);
1055 if (NULL
== n
->child
)
1056 term_word(p
, m
->name
);
1063 termp_fl_pre(DECL_ARGS
)
1066 term_fontpush(p
, TERMFONT_BOLD
);
1067 term_word(p
, "\\-");
1069 /* A blank `Fl' should incur a subsequent space. */
1072 p
->flags
|= TERMP_NOSPACE
;
1080 termp_an_pre(DECL_ARGS
)
1083 if (NULL
== n
->child
)
1087 * If not in the AUTHORS section, `An -split' will cause
1088 * newlines to occur before the author name. If in the AUTHORS
1089 * section, by default, the first `An' invocation is nosplit,
1090 * then all subsequent ones, regardless of whether interspersed
1091 * with other macros/text, are split. -split, in this case,
1092 * will override the condition of the implied first -nosplit.
1095 if (n
->sec
== SEC_AUTHORS
) {
1096 if ( ! (TERMP_ANPREC
& p
->flags
)) {
1097 if (TERMP_SPLIT
& p
->flags
)
1101 if (TERMP_NOSPLIT
& p
->flags
)
1107 if (TERMP_SPLIT
& p
->flags
)
1116 termp_an_post(DECL_ARGS
)
1120 if (SEC_AUTHORS
== n
->sec
)
1121 p
->flags
|= TERMP_ANPREC
;
1125 if (arg_getattr(MDOC_Split
, n
) > -1) {
1126 p
->flags
&= ~TERMP_NOSPLIT
;
1127 p
->flags
|= TERMP_SPLIT
;
1129 p
->flags
&= ~TERMP_SPLIT
;
1130 p
->flags
|= TERMP_NOSPLIT
;
1138 termp_ns_pre(DECL_ARGS
)
1141 p
->flags
|= TERMP_NOSPACE
;
1148 termp_rs_pre(DECL_ARGS
)
1151 if (SEC_SEE_ALSO
!= n
->sec
)
1153 if (MDOC_BLOCK
== n
->type
&& n
->prev
)
1161 termp_rv_pre(DECL_ARGS
)
1163 const struct mdoc_node
*nn
;
1166 term_word(p
, "The");
1168 for (nn
= n
->child
; nn
; nn
= nn
->next
) {
1169 term_fontpush(p
, TERMFONT_BOLD
);
1170 term_word(p
, nn
->string
);
1172 p
->flags
|= TERMP_NOSPACE
;
1173 if (nn
->next
&& NULL
== nn
->next
->next
)
1174 term_word(p
, "(), and");
1176 term_word(p
, "(),");
1182 term_word(p
, "functions return");
1184 term_word(p
, "function returns");
1186 term_word(p
, "the value 0 if successful; otherwise the value "
1187 "-1 is returned and the global variable");
1189 term_fontpush(p
, TERMFONT_UNDER
);
1190 term_word(p
, "errno");
1193 term_word(p
, "is set to indicate the error.");
1201 termp_ex_pre(DECL_ARGS
)
1203 const struct mdoc_node
*nn
;
1205 term_word(p
, "The");
1207 for (nn
= n
->child
; nn
; nn
= nn
->next
) {
1208 term_fontpush(p
, TERMFONT_BOLD
);
1209 term_word(p
, nn
->string
);
1211 p
->flags
|= TERMP_NOSPACE
;
1212 if (nn
->next
&& NULL
== nn
->next
->next
)
1213 term_word(p
, ", and");
1217 p
->flags
&= ~TERMP_NOSPACE
;
1221 term_word(p
, "utilities exit");
1223 term_word(p
, "utility exits");
1225 term_word(p
, "0 on success, and >0 if an error occurs.");
1233 termp_nd_pre(DECL_ARGS
)
1236 if (MDOC_BODY
!= n
->type
)
1239 #if defined(__OpenBSD__) || defined(__linux__)
1240 term_word(p
, "\\(en");
1242 term_word(p
, "\\(em");
1250 termp_bl_post(DECL_ARGS
)
1253 if (MDOC_BLOCK
== n
->type
)
1260 termp_op_post(DECL_ARGS
)
1263 if (MDOC_BODY
!= n
->type
)
1265 p
->flags
|= TERMP_NOSPACE
;
1266 term_word(p
, "\\(rB");
1272 termp_xr_pre(DECL_ARGS
)
1274 const struct mdoc_node
*nn
;
1276 assert(n
->child
&& MDOC_TEXT
== n
->child
->type
);
1279 term_word(p
, nn
->string
);
1280 if (NULL
== (nn
= nn
->next
))
1282 p
->flags
|= TERMP_NOSPACE
;
1284 p
->flags
|= TERMP_NOSPACE
;
1285 term_word(p
, nn
->string
);
1286 p
->flags
|= TERMP_NOSPACE
;
1294 termp_vt_pre(DECL_ARGS
)
1297 if (MDOC_ELEM
== n
->type
)
1298 return(termp_under_pre(p
, pair
, m
, n
));
1299 else if (MDOC_HEAD
== n
->type
)
1301 else if (MDOC_BLOCK
== n
->type
)
1304 return(termp_under_pre(p
, pair
, m
, n
));
1310 termp_vt_post(DECL_ARGS
)
1313 if (MDOC_BLOCK
!= n
->type
)
1315 if (n
->next
&& MDOC_Vt
== n
->next
->tok
)
1324 termp_bold_pre(DECL_ARGS
)
1327 term_fontpush(p
, TERMFONT_BOLD
);
1334 termp_fd_post(DECL_ARGS
)
1337 if (n
->sec
!= SEC_SYNOPSIS
)
1341 if (n
->next
&& MDOC_Fd
!= n
->next
->tok
)
1348 termp_sh_pre(DECL_ARGS
)
1351 /* No vspace between consecutive `Sh' calls. */
1355 if (n
->prev
&& MDOC_Sh
== n
->prev
->tok
)
1356 if (NULL
== n
->prev
->body
->child
)
1361 term_fontpush(p
, TERMFONT_BOLD
);
1375 termp_sh_post(DECL_ARGS
)
1394 termp_op_pre(DECL_ARGS
)
1399 term_word(p
, "\\(lB");
1400 p
->flags
|= TERMP_NOSPACE
;
1411 termp_bt_pre(DECL_ARGS
)
1414 term_word(p
, "is currently in beta test.");
1421 termp_lb_post(DECL_ARGS
)
1424 if (SEC_LIBRARY
== n
->sec
)
1431 termp_ud_pre(DECL_ARGS
)
1434 term_word(p
, "currently under development.");
1441 termp_d1_pre(DECL_ARGS
)
1444 if (MDOC_BLOCK
!= n
->type
)
1447 p
->offset
+= (INDENT
+ 1);
1454 termp_d1_post(DECL_ARGS
)
1457 if (MDOC_BLOCK
!= n
->type
)
1465 termp_aq_pre(DECL_ARGS
)
1468 if (MDOC_BODY
!= n
->type
)
1470 term_word(p
, "\\(la");
1471 p
->flags
|= TERMP_NOSPACE
;
1478 termp_aq_post(DECL_ARGS
)
1481 if (MDOC_BODY
!= n
->type
)
1483 p
->flags
|= TERMP_NOSPACE
;
1484 term_word(p
, "\\(ra");
1490 termp_ft_pre(DECL_ARGS
)
1493 if (SEC_SYNOPSIS
== n
->sec
)
1494 if (n
->prev
&& MDOC_Fo
== n
->prev
->tok
)
1497 term_fontpush(p
, TERMFONT_UNDER
);
1504 termp_ft_post(DECL_ARGS
)
1507 if (SEC_SYNOPSIS
== n
->sec
)
1514 termp_fn_pre(DECL_ARGS
)
1516 const struct mdoc_node
*nn
;
1518 term_fontpush(p
, TERMFONT_BOLD
);
1519 term_word(p
, n
->child
->string
);
1522 p
->flags
|= TERMP_NOSPACE
;
1525 for (nn
= n
->child
->next
; nn
; nn
= nn
->next
) {
1526 term_fontpush(p
, TERMFONT_UNDER
);
1527 term_word(p
, nn
->string
);
1536 if (SEC_SYNOPSIS
== n
->sec
)
1545 termp_fn_post(DECL_ARGS
)
1548 if (n
->sec
== SEC_SYNOPSIS
&& n
->next
)
1555 termp_fa_pre(DECL_ARGS
)
1557 const struct mdoc_node
*nn
;
1559 if (n
->parent
->tok
!= MDOC_Fo
) {
1560 term_fontpush(p
, TERMFONT_UNDER
);
1564 for (nn
= n
->child
; nn
; nn
= nn
->next
) {
1565 term_fontpush(p
, TERMFONT_UNDER
);
1566 term_word(p
, nn
->string
);
1573 if (n
->child
&& n
->next
&& n
->next
->tok
== MDOC_Fa
)
1582 termp_bd_pre(DECL_ARGS
)
1585 const struct mdoc_node
*nn
;
1587 if (MDOC_BLOCK
== n
->type
) {
1588 print_bvspace(p
, n
, n
);
1590 } else if (MDOC_BODY
!= n
->type
)
1595 for (type
= -1, i
= 0; i
< (int)nn
->args
->argc
; i
++) {
1596 switch (nn
->args
->argv
[i
].arg
) {
1597 case (MDOC_Centred
):
1603 case (MDOC_Unfilled
):
1605 case (MDOC_Literal
):
1606 type
= nn
->args
->argv
[i
].arg
;
1609 p
->offset
+= a2offs(&nn
->args
->argv
[i
]);
1617 * If -ragged or -filled are specified, the block does nothing
1618 * but change the indentation. If -unfilled or -literal are
1619 * specified, text is printed exactly as entered in the display:
1620 * for macro lines, a newline is appended to the line. Blank
1621 * lines are allowed.
1625 if (MDOC_Literal
!= type
&& MDOC_Unfilled
!= type
)
1628 for (nn
= n
->child
; nn
; nn
= nn
->next
) {
1629 p
->flags
|= TERMP_NOSPACE
;
1630 print_mdoc_node(p
, pair
, m
, nn
);
1631 if (NULL
== nn
->next
)
1633 if (nn
->prev
&& nn
->prev
->line
< nn
->line
)
1635 else if (NULL
== nn
->prev
)
1645 termp_bd_post(DECL_ARGS
)
1648 if (MDOC_BODY
!= n
->type
)
1650 p
->flags
|= TERMP_NOSPACE
;
1657 termp_qq_pre(DECL_ARGS
)
1660 if (MDOC_BODY
!= n
->type
)
1663 p
->flags
|= TERMP_NOSPACE
;
1670 termp_qq_post(DECL_ARGS
)
1673 if (MDOC_BODY
!= n
->type
)
1675 p
->flags
|= TERMP_NOSPACE
;
1682 termp_bx_post(DECL_ARGS
)
1686 p
->flags
|= TERMP_NOSPACE
;
1687 term_word(p
, "BSD");
1693 termp_xx_pre(DECL_ARGS
)
1729 termp_sq_pre(DECL_ARGS
)
1732 if (MDOC_BODY
!= n
->type
)
1734 term_word(p
, "\\(oq");
1735 p
->flags
|= TERMP_NOSPACE
;
1742 termp_sq_post(DECL_ARGS
)
1745 if (MDOC_BODY
!= n
->type
)
1747 p
->flags
|= TERMP_NOSPACE
;
1748 term_word(p
, "\\(aq");
1754 termp_pf_pre(DECL_ARGS
)
1757 p
->flags
|= TERMP_IGNDELIM
;
1764 termp_pf_post(DECL_ARGS
)
1767 p
->flags
&= ~TERMP_IGNDELIM
;
1768 p
->flags
|= TERMP_NOSPACE
;
1774 termp_ss_pre(DECL_ARGS
)
1784 term_fontpush(p
, TERMFONT_BOLD
);
1785 p
->offset
= HALFINDENT
;
1797 termp_ss_post(DECL_ARGS
)
1800 if (MDOC_HEAD
== n
->type
)
1807 termp_cd_pre(DECL_ARGS
)
1810 term_fontpush(p
, TERMFONT_BOLD
);
1818 termp_in_pre(DECL_ARGS
)
1821 term_fontpush(p
, TERMFONT_BOLD
);
1822 if (SEC_SYNOPSIS
== n
->sec
)
1823 term_word(p
, "#include");
1826 p
->flags
|= TERMP_NOSPACE
;
1833 termp_in_post(DECL_ARGS
)
1836 term_fontpush(p
, TERMFONT_BOLD
);
1837 p
->flags
|= TERMP_NOSPACE
;
1841 if (SEC_SYNOPSIS
!= n
->sec
)
1846 * XXX Not entirely correct. If `.In foo bar' is specified in
1847 * the SYNOPSIS section, then it produces a single break after
1848 * the <foo>; mandoc asserts a vertical space. Since this
1849 * construction is rarely used, I think it's fine.
1851 if (n
->next
&& MDOC_In
!= n
->next
->tok
)
1858 termp_sp_pre(DECL_ARGS
)
1864 len
= n
->child
? a2height(n
->child
) : 1;
1876 for (i
= 0; i
< len
; i
++)
1885 termp_brq_pre(DECL_ARGS
)
1888 if (MDOC_BODY
!= n
->type
)
1890 term_word(p
, "\\(lC");
1891 p
->flags
|= TERMP_NOSPACE
;
1898 termp_brq_post(DECL_ARGS
)
1901 if (MDOC_BODY
!= n
->type
)
1903 p
->flags
|= TERMP_NOSPACE
;
1904 term_word(p
, "\\(rC");
1910 termp_bq_pre(DECL_ARGS
)
1913 if (MDOC_BODY
!= n
->type
)
1915 term_word(p
, "\\(lB");
1916 p
->flags
|= TERMP_NOSPACE
;
1923 termp_bq_post(DECL_ARGS
)
1926 if (MDOC_BODY
!= n
->type
)
1928 p
->flags
|= TERMP_NOSPACE
;
1929 term_word(p
, "\\(rB");
1935 termp_pq_pre(DECL_ARGS
)
1938 if (MDOC_BODY
!= n
->type
)
1940 term_word(p
, "\\&(");
1941 p
->flags
|= TERMP_NOSPACE
;
1948 termp_pq_post(DECL_ARGS
)
1951 if (MDOC_BODY
!= n
->type
)
1959 termp_fo_pre(DECL_ARGS
)
1961 const struct mdoc_node
*nn
;
1963 if (MDOC_BODY
== n
->type
) {
1964 p
->flags
|= TERMP_NOSPACE
;
1966 p
->flags
|= TERMP_NOSPACE
;
1968 } else if (MDOC_HEAD
!= n
->type
)
1971 term_fontpush(p
, TERMFONT_BOLD
);
1972 for (nn
= n
->child
; nn
; nn
= nn
->next
) {
1973 assert(MDOC_TEXT
== nn
->type
);
1974 term_word(p
, nn
->string
);
1984 termp_fo_post(DECL_ARGS
)
1987 if (MDOC_BODY
!= n
->type
)
1989 p
->flags
|= TERMP_NOSPACE
;
1991 p
->flags
|= TERMP_NOSPACE
;
1999 termp_bf_pre(DECL_ARGS
)
2001 const struct mdoc_node
*nn
;
2003 if (MDOC_HEAD
== n
->type
)
2005 else if (MDOC_BLOCK
!= n
->type
)
2008 if (NULL
== (nn
= n
->head
->child
)) {
2009 if (arg_hasattr(MDOC_Emphasis
, n
))
2010 term_fontpush(p
, TERMFONT_UNDER
);
2011 else if (arg_hasattr(MDOC_Symbolic
, n
))
2012 term_fontpush(p
, TERMFONT_BOLD
);
2014 term_fontpush(p
, TERMFONT_NONE
);
2019 assert(MDOC_TEXT
== nn
->type
);
2020 if (0 == strcmp("Em", nn
->string
))
2021 term_fontpush(p
, TERMFONT_UNDER
);
2022 else if (0 == strcmp("Sy", nn
->string
))
2023 term_fontpush(p
, TERMFONT_BOLD
);
2025 term_fontpush(p
, TERMFONT_NONE
);
2033 termp_sm_pre(DECL_ARGS
)
2036 assert(n
->child
&& MDOC_TEXT
== n
->child
->type
);
2037 if (0 == strcmp("on", n
->child
->string
))
2038 p
->flags
&= ~TERMP_NONOSPACE
;
2040 p
->flags
|= TERMP_NONOSPACE
;
2048 termp_ap_pre(DECL_ARGS
)
2051 p
->flags
|= TERMP_NOSPACE
;
2052 term_word(p
, "\\(aq");
2053 p
->flags
|= TERMP_NOSPACE
;
2060 termp____post(DECL_ARGS
)
2065 p
->flags
|= TERMP_NOSPACE
;
2068 term_word(p
, "\\(rq");
2069 p
->flags
|= TERMP_NOSPACE
;
2074 term_word(p
, n
->next
? "," : ".");
2080 termp_li_pre(DECL_ARGS
)
2083 term_fontpush(p
, TERMFONT_NONE
);
2090 termp_lk_pre(DECL_ARGS
)
2092 const struct mdoc_node
*nn
;
2094 term_fontpush(p
, TERMFONT_UNDER
);
2097 if (NULL
== nn
->next
)
2100 term_word(p
, nn
->string
);
2103 p
->flags
|= TERMP_NOSPACE
;
2106 term_fontpush(p
, TERMFONT_BOLD
);
2107 for (nn
= nn
->next
; nn
; nn
= nn
->next
)
2108 term_word(p
, nn
->string
);
2117 termp_under_pre(DECL_ARGS
)
2120 term_fontpush(p
, TERMFONT_UNDER
);
2127 termp__t_pre(DECL_ARGS
)
2130 term_word(p
, "\\(lq");
2131 p
->flags
|= TERMP_NOSPACE
;