]>
git.cameronkatri.com Git - mandoc.git/blob - mdoc_term.c
1 /* $Id: mdoc_term.c,v 1.116 2010/04/06 16:27:53 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>
39 struct termpair
*ppair
;
43 #define DECL_ARGS struct termp *p, \
44 struct termpair *pair, \
45 const struct mdoc_meta *m, \
46 const struct mdoc_node *n
49 int (*pre
)(DECL_ARGS
);
50 void (*post
)(DECL_ARGS
);
53 static size_t a2width(const struct mdoc_argv
*, int);
54 static size_t a2height(const struct mdoc_node
*);
55 static size_t a2offs(const struct mdoc_argv
*);
57 static int arg_hasattr(int, const struct mdoc_node
*);
58 static int arg_getattrs(const int *, int *, size_t,
59 const struct mdoc_node
*);
60 static int arg_getattr(int, const struct mdoc_node
*);
61 static int arg_listtype(const struct mdoc_node
*);
62 static void print_bvspace(struct termp
*,
63 const struct mdoc_node
*,
64 const struct mdoc_node
*);
65 static void print_mdoc_node(DECL_ARGS
);
66 static void print_mdoc_head(DECL_ARGS
);
67 static void print_mdoc_nodelist(DECL_ARGS
);
68 static void print_foot(DECL_ARGS
);
70 static void termp____post(DECL_ARGS
);
71 static void termp_an_post(DECL_ARGS
);
72 static void termp_aq_post(DECL_ARGS
);
73 static void termp_bd_post(DECL_ARGS
);
74 static void termp_bl_post(DECL_ARGS
);
75 static void termp_bq_post(DECL_ARGS
);
76 static void termp_brq_post(DECL_ARGS
);
77 static void termp_bx_post(DECL_ARGS
);
78 static void termp_d1_post(DECL_ARGS
);
79 static void termp_dq_post(DECL_ARGS
);
80 static void termp_fd_post(DECL_ARGS
);
81 static void termp_fn_post(DECL_ARGS
);
82 static void termp_fo_post(DECL_ARGS
);
83 static void termp_ft_post(DECL_ARGS
);
84 static void termp_in_post(DECL_ARGS
);
85 static void termp_it_post(DECL_ARGS
);
86 static void termp_lb_post(DECL_ARGS
);
87 static void termp_op_post(DECL_ARGS
);
88 static void termp_pf_post(DECL_ARGS
);
89 static void termp_pq_post(DECL_ARGS
);
90 static void termp_qq_post(DECL_ARGS
);
91 static void termp_sh_post(DECL_ARGS
);
92 static void termp_sq_post(DECL_ARGS
);
93 static void termp_ss_post(DECL_ARGS
);
94 static void termp_vt_post(DECL_ARGS
);
96 static int termp__t_pre(DECL_ARGS
);
97 static int termp_an_pre(DECL_ARGS
);
98 static int termp_ap_pre(DECL_ARGS
);
99 static int termp_aq_pre(DECL_ARGS
);
100 static int termp_bd_pre(DECL_ARGS
);
101 static int termp_bf_pre(DECL_ARGS
);
102 static int termp_bl_pre(DECL_ARGS
);
103 static int termp_bold_pre(DECL_ARGS
);
104 static int termp_bq_pre(DECL_ARGS
);
105 static int termp_brq_pre(DECL_ARGS
);
106 static int termp_bt_pre(DECL_ARGS
);
107 static int termp_cd_pre(DECL_ARGS
);
108 static int termp_d1_pre(DECL_ARGS
);
109 static int termp_dq_pre(DECL_ARGS
);
110 static int termp_ex_pre(DECL_ARGS
);
111 static int termp_fa_pre(DECL_ARGS
);
112 static int termp_fl_pre(DECL_ARGS
);
113 static int termp_fn_pre(DECL_ARGS
);
114 static int termp_fo_pre(DECL_ARGS
);
115 static int termp_ft_pre(DECL_ARGS
);
116 static int termp_in_pre(DECL_ARGS
);
117 static int termp_it_pre(DECL_ARGS
);
118 static int termp_li_pre(DECL_ARGS
);
119 static int termp_lk_pre(DECL_ARGS
);
120 static int termp_nd_pre(DECL_ARGS
);
121 static int termp_nm_pre(DECL_ARGS
);
122 static int termp_ns_pre(DECL_ARGS
);
123 static int termp_op_pre(DECL_ARGS
);
124 static int termp_pf_pre(DECL_ARGS
);
125 static int termp_pq_pre(DECL_ARGS
);
126 static int termp_qq_pre(DECL_ARGS
);
127 static int termp_rs_pre(DECL_ARGS
);
128 static int termp_rv_pre(DECL_ARGS
);
129 static int termp_sh_pre(DECL_ARGS
);
130 static int termp_sm_pre(DECL_ARGS
);
131 static int termp_sp_pre(DECL_ARGS
);
132 static int termp_sq_pre(DECL_ARGS
);
133 static int termp_ss_pre(DECL_ARGS
);
134 static int termp_under_pre(DECL_ARGS
);
135 static int termp_ud_pre(DECL_ARGS
);
136 static int termp_vt_pre(DECL_ARGS
);
137 static int termp_xr_pre(DECL_ARGS
);
138 static int termp_xx_pre(DECL_ARGS
);
140 static const struct termact termacts
[MDOC_MAX
] = {
141 { termp_ap_pre
, NULL
}, /* Ap */
142 { NULL
, NULL
}, /* Dd */
143 { NULL
, NULL
}, /* Dt */
144 { NULL
, NULL
}, /* Os */
145 { termp_sh_pre
, termp_sh_post
}, /* Sh */
146 { termp_ss_pre
, termp_ss_post
}, /* Ss */
147 { termp_sp_pre
, NULL
}, /* Pp */
148 { termp_d1_pre
, termp_d1_post
}, /* D1 */
149 { termp_d1_pre
, termp_d1_post
}, /* Dl */
150 { termp_bd_pre
, termp_bd_post
}, /* Bd */
151 { NULL
, NULL
}, /* Ed */
152 { termp_bl_pre
, termp_bl_post
}, /* Bl */
153 { NULL
, NULL
}, /* El */
154 { termp_it_pre
, termp_it_post
}, /* It */
155 { NULL
, NULL
}, /* Ad */
156 { termp_an_pre
, termp_an_post
}, /* An */
157 { termp_under_pre
, NULL
}, /* Ar */
158 { termp_cd_pre
, NULL
}, /* Cd */
159 { termp_bold_pre
, NULL
}, /* Cm */
160 { NULL
, NULL
}, /* Dv */
161 { NULL
, NULL
}, /* Er */
162 { NULL
, NULL
}, /* Ev */
163 { termp_ex_pre
, NULL
}, /* Ex */
164 { termp_fa_pre
, NULL
}, /* Fa */
165 { termp_bold_pre
, termp_fd_post
}, /* Fd */
166 { termp_fl_pre
, NULL
}, /* Fl */
167 { termp_fn_pre
, termp_fn_post
}, /* Fn */
168 { termp_ft_pre
, termp_ft_post
}, /* Ft */
169 { termp_bold_pre
, NULL
}, /* Ic */
170 { termp_in_pre
, termp_in_post
}, /* In */
171 { termp_li_pre
, NULL
}, /* Li */
172 { termp_nd_pre
, NULL
}, /* Nd */
173 { termp_nm_pre
, NULL
}, /* Nm */
174 { termp_op_pre
, termp_op_post
}, /* Op */
175 { NULL
, NULL
}, /* Ot */
176 { termp_under_pre
, NULL
}, /* Pa */
177 { termp_rv_pre
, NULL
}, /* Rv */
178 { NULL
, NULL
}, /* St */
179 { termp_under_pre
, NULL
}, /* Va */
180 { termp_vt_pre
, termp_vt_post
}, /* Vt */
181 { termp_xr_pre
, NULL
}, /* Xr */
182 { NULL
, termp____post
}, /* %A */
183 { termp_under_pre
, termp____post
}, /* %B */
184 { NULL
, termp____post
}, /* %D */
185 { termp_under_pre
, termp____post
}, /* %I */
186 { termp_under_pre
, termp____post
}, /* %J */
187 { NULL
, termp____post
}, /* %N */
188 { NULL
, termp____post
}, /* %O */
189 { NULL
, termp____post
}, /* %P */
190 { NULL
, termp____post
}, /* %R */
191 { termp__t_pre
, termp____post
}, /* %T */
192 { NULL
, termp____post
}, /* %V */
193 { NULL
, NULL
}, /* Ac */
194 { termp_aq_pre
, termp_aq_post
}, /* Ao */
195 { termp_aq_pre
, termp_aq_post
}, /* Aq */
196 { NULL
, NULL
}, /* At */
197 { NULL
, NULL
}, /* Bc */
198 { termp_bf_pre
, NULL
}, /* Bf */
199 { termp_bq_pre
, termp_bq_post
}, /* Bo */
200 { termp_bq_pre
, termp_bq_post
}, /* Bq */
201 { termp_xx_pre
, NULL
}, /* Bsx */
202 { NULL
, termp_bx_post
}, /* Bx */
203 { NULL
, NULL
}, /* Db */
204 { NULL
, NULL
}, /* Dc */
205 { termp_dq_pre
, termp_dq_post
}, /* Do */
206 { termp_dq_pre
, termp_dq_post
}, /* Dq */
207 { NULL
, NULL
}, /* Ec */ /* FIXME: no space */
208 { NULL
, NULL
}, /* Ef */
209 { termp_under_pre
, NULL
}, /* Em */
210 { NULL
, NULL
}, /* Eo */
211 { termp_xx_pre
, NULL
}, /* Fx */
212 { termp_bold_pre
, NULL
}, /* Ms */ /* FIXME: convert to symbol? */
213 { NULL
, NULL
}, /* No */
214 { termp_ns_pre
, NULL
}, /* Ns */
215 { termp_xx_pre
, NULL
}, /* Nx */
216 { termp_xx_pre
, NULL
}, /* Ox */
217 { NULL
, NULL
}, /* Pc */
218 { termp_pf_pre
, termp_pf_post
}, /* Pf */
219 { termp_pq_pre
, termp_pq_post
}, /* Po */
220 { termp_pq_pre
, termp_pq_post
}, /* Pq */
221 { NULL
, NULL
}, /* Qc */
222 { termp_sq_pre
, termp_sq_post
}, /* Ql */
223 { termp_qq_pre
, termp_qq_post
}, /* Qo */
224 { termp_qq_pre
, termp_qq_post
}, /* Qq */
225 { NULL
, NULL
}, /* Re */
226 { termp_rs_pre
, NULL
}, /* Rs */
227 { NULL
, NULL
}, /* Sc */
228 { termp_sq_pre
, termp_sq_post
}, /* So */
229 { termp_sq_pre
, termp_sq_post
}, /* Sq */
230 { termp_sm_pre
, NULL
}, /* Sm */
231 { termp_under_pre
, NULL
}, /* Sx */
232 { termp_bold_pre
, NULL
}, /* Sy */
233 { NULL
, NULL
}, /* Tn */
234 { termp_xx_pre
, NULL
}, /* Ux */
235 { NULL
, NULL
}, /* Xc */
236 { NULL
, NULL
}, /* Xo */
237 { termp_fo_pre
, termp_fo_post
}, /* Fo */
238 { NULL
, NULL
}, /* Fc */
239 { termp_op_pre
, termp_op_post
}, /* Oo */
240 { NULL
, NULL
}, /* Oc */
241 { NULL
, NULL
}, /* Bk */
242 { NULL
, NULL
}, /* Ek */
243 { termp_bt_pre
, NULL
}, /* Bt */
244 { NULL
, NULL
}, /* Hf */
245 { NULL
, NULL
}, /* Fr */
246 { termp_ud_pre
, NULL
}, /* Ud */
247 { NULL
, termp_lb_post
}, /* Lb */
248 { termp_sp_pre
, NULL
}, /* Lp */
249 { termp_lk_pre
, NULL
}, /* Lk */
250 { termp_under_pre
, NULL
}, /* Mt */
251 { termp_brq_pre
, termp_brq_post
}, /* Brq */
252 { termp_brq_pre
, termp_brq_post
}, /* Bro */
253 { NULL
, NULL
}, /* Brc */
254 { NULL
, termp____post
}, /* %C */
255 { NULL
, NULL
}, /* Es */ /* TODO */
256 { NULL
, NULL
}, /* En */ /* TODO */
257 { termp_xx_pre
, NULL
}, /* Dx */
258 { NULL
, termp____post
}, /* %Q */
259 { termp_sp_pre
, NULL
}, /* br */
260 { termp_sp_pre
, NULL
}, /* sp */
261 { termp_under_pre
, termp____post
}, /* %U */
266 terminal_mdoc(void *arg
, const struct mdoc
*mdoc
)
268 const struct mdoc_node
*n
;
269 const struct mdoc_meta
*m
;
272 p
= (struct termp
*)arg
;
277 if (NULL
== p
->symtab
)
279 case (TERMENC_ASCII
):
280 p
->symtab
= chars_init(CHARS_ASCII
);
290 print_mdoc_head(p
, NULL
, m
, n
);
292 print_mdoc_nodelist(p
, NULL
, m
, n
->child
);
293 print_foot(p
, NULL
, m
, n
);
298 print_mdoc_nodelist(DECL_ARGS
)
301 print_mdoc_node(p
, pair
, m
, n
);
303 print_mdoc_nodelist(p
, pair
, m
, n
->next
);
309 print_mdoc_node(DECL_ARGS
)
313 struct termpair npair
;
314 size_t offset
, rmargin
;
318 rmargin
= p
->rmargin
;
319 font
= term_fontq(p
);
321 memset(&npair
, 0, sizeof(struct termpair
));
324 if (MDOC_TEXT
!= n
->type
) {
325 if (termacts
[n
->tok
].pre
)
326 chld
= (*termacts
[n
->tok
].pre
)(p
, &npair
, m
, n
);
328 term_word(p
, n
->string
);
330 if (chld
&& n
->child
)
331 print_mdoc_nodelist(p
, &npair
, m
, n
->child
);
333 term_fontpopq(p
, font
);
335 if (MDOC_TEXT
!= n
->type
)
336 if (termacts
[n
->tok
].post
)
337 (*termacts
[n
->tok
].post
)(p
, &npair
, m
, n
);
340 p
->rmargin
= rmargin
;
346 print_foot(DECL_ARGS
)
348 char buf
[DATESIZ
], os
[BUFSIZ
];
350 term_fontrepl(p
, TERMFONT_NONE
);
353 * Output the footer in new-groff style, that is, three columns
354 * with the middle being the manual date and flanking columns
355 * being the operating system:
360 time2a(m
->date
, buf
, DATESIZ
);
361 strlcpy(os
, m
->os
, BUFSIZ
);
366 p
->rmargin
= (p
->maxrmargin
- strlen(buf
) + 1) / 2;
367 p
->flags
|= TERMP_NOSPACE
| TERMP_NOBREAK
;
372 p
->offset
= p
->rmargin
;
373 p
->rmargin
= p
->maxrmargin
- strlen(os
);
374 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
379 p
->offset
= p
->rmargin
;
380 p
->rmargin
= p
->maxrmargin
;
381 p
->flags
&= ~TERMP_NOBREAK
;
382 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
388 p
->rmargin
= p
->maxrmargin
;
395 print_mdoc_head(DECL_ARGS
)
397 char buf
[BUFSIZ
], title
[BUFSIZ
];
399 p
->rmargin
= p
->maxrmargin
;
403 * The header is strange. It has three components, which are
404 * really two with the first duplicated. It goes like this:
406 * IDENTIFIER TITLE IDENTIFIER
408 * The IDENTIFIER is NAME(SECTION), which is the command-name
409 * (if given, or "unknown" if not) followed by the manual page
410 * section. These are given in `Dt'. The TITLE is a free-form
411 * string depending on the manual volume. If not specified, it
412 * switches on the manual section.
416 strlcpy(buf
, m
->vol
, BUFSIZ
);
419 strlcat(buf
, " (", BUFSIZ
);
420 strlcat(buf
, m
->arch
, BUFSIZ
);
421 strlcat(buf
, ")", BUFSIZ
);
424 snprintf(title
, BUFSIZ
, "%s(%d)", m
->title
, m
->msec
);
427 p
->rmargin
= (p
->maxrmargin
- strlen(buf
) + 1) / 2;
428 p
->flags
|= TERMP_NOBREAK
| TERMP_NOSPACE
;
433 p
->offset
= p
->rmargin
;
434 p
->rmargin
= p
->maxrmargin
- strlen(title
);
435 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
440 p
->offset
= p
->rmargin
;
441 p
->rmargin
= p
->maxrmargin
;
442 p
->flags
&= ~TERMP_NOBREAK
;
443 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
449 p
->rmargin
= p
->maxrmargin
;
450 p
->flags
&= ~TERMP_NOSPACE
;
455 a2height(const struct mdoc_node
*n
)
459 assert(MDOC_TEXT
== n
->type
);
461 if ( ! a2roffsu(n
->string
, &su
, SCALE_VS
))
462 SCALE_VS_INIT(&su
, strlen(n
->string
));
464 return(term_vspan(&su
));
469 a2width(const struct mdoc_argv
*arg
, int pos
)
473 assert(arg
->value
[pos
]);
474 if ( ! a2roffsu(arg
->value
[pos
], &su
, SCALE_MAX
))
475 SCALE_HS_INIT(&su
, strlen(arg
->value
[pos
]));
477 return(term_hspan(&su
));
482 arg_listtype(const struct mdoc_node
*n
)
486 assert(MDOC_BLOCK
== n
->type
);
488 len
= (int)(n
->args
? n
->args
->argc
: 0);
490 for (i
= 0; i
< len
; i
++)
491 switch (n
->args
->argv
[i
].arg
) {
513 return(n
->args
->argv
[i
].arg
);
523 a2offs(const struct mdoc_argv
*arg
)
527 if ('\0' == arg
->value
[0][0])
529 else if (0 == strcmp(arg
->value
[0], "left"))
531 else if (0 == strcmp(arg
->value
[0], "indent"))
533 else if (0 == strcmp(arg
->value
[0], "indent-two"))
534 return((INDENT
+ 1) * 2);
535 else if ( ! a2roffsu(arg
->value
[0], &su
, SCALE_MAX
))
536 SCALE_HS_INIT(&su
, strlen(arg
->value
[0]));
538 return(term_hspan(&su
));
543 * Return 1 if an argument has a particular argument value or 0 if it
544 * does not. See arg_getattr().
547 arg_hasattr(int arg
, const struct mdoc_node
*n
)
550 return(-1 != arg_getattr(arg
, n
));
555 * Get the index of an argument in a node's argument list or -1 if it
556 * does not exist. See arg_getattrs().
559 arg_getattr(int v
, const struct mdoc_node
*n
)
563 return(arg_getattrs(&v
, &val
, 1, n
) ? val
: -1);
568 * Walk through the argument list for a node and fill an array "vals"
569 * with the positions of the argument structures listed in "keys".
570 * Return the number of elements that were written into "vals", which
574 arg_getattrs(const int *keys
, int *vals
,
575 size_t sz
, const struct mdoc_node
*n
)
582 for (k
= i
= 0; i
< (int)n
->args
->argc
; i
++)
583 for (j
= 0; j
< (int)sz
; j
++)
584 if (n
->args
->argv
[i
].arg
== keys
[j
]) {
593 * Determine how much space to print out before block elements of `It'
594 * (and thus `Bl') and `Bd'. And then go ahead and print that space,
598 print_bvspace(struct termp
*p
,
599 const struct mdoc_node
*bl
,
600 const struct mdoc_node
*n
)
602 const struct mdoc_node
*nn
;
605 if (arg_hasattr(MDOC_Compact
, bl
))
608 /* Do not vspace directly after Ss/Sh. */
610 for (nn
= n
; nn
; nn
= nn
->parent
) {
611 if (MDOC_BLOCK
!= nn
->type
)
613 if (MDOC_Ss
== nn
->tok
)
615 if (MDOC_Sh
== nn
->tok
)
617 if (NULL
== nn
->prev
)
622 /* A `-column' does not assert vspace within the list. */
624 if (MDOC_Bl
== bl
->tok
&& arg_hasattr(MDOC_Column
, bl
))
625 if (n
->prev
&& MDOC_It
== n
->prev
->tok
)
628 /* A `-diag' without body does not vspace. */
630 if (MDOC_Bl
== bl
->tok
&& arg_hasattr(MDOC_Diag
, bl
))
631 if (n
->prev
&& MDOC_It
== n
->prev
->tok
) {
632 assert(n
->prev
->body
);
633 if (NULL
== n
->prev
->body
->child
)
643 termp_dq_pre(DECL_ARGS
)
646 if (MDOC_BODY
!= n
->type
)
649 term_word(p
, "\\(lq");
650 p
->flags
|= TERMP_NOSPACE
;
657 termp_dq_post(DECL_ARGS
)
660 if (MDOC_BODY
!= n
->type
)
663 p
->flags
|= TERMP_NOSPACE
;
664 term_word(p
, "\\(rq");
670 termp_it_pre(DECL_ARGS
)
672 const struct mdoc_node
*bl
, *nn
;
674 int i
, type
, keys
[3], vals
[3];
675 size_t width
, offset
, ncols
, dcol
;
677 if (MDOC_BLOCK
== n
->type
) {
678 print_bvspace(p
, n
->parent
->parent
, n
);
682 bl
= n
->parent
->parent
->parent
;
684 /* Get list width, offset, and list type from argument list. */
686 keys
[0] = MDOC_Width
;
687 keys
[1] = MDOC_Offset
;
688 keys
[2] = MDOC_Column
;
690 vals
[0] = vals
[1] = vals
[2] = -1;
692 arg_getattrs(keys
, vals
, 3, bl
);
694 type
= arg_listtype(bl
);
698 * First calculate width and offset. This is pretty easy unless
699 * we're a -column list, in which case all prior columns must
706 offset
= a2offs(&bl
->args
->argv
[vals
[1]]);
710 if (MDOC_BODY
== n
->type
)
713 * Imitate groff's column handling:
714 * - For each earlier column, add its width.
715 * - For less than 5 columns, add four more blanks per
717 * - For exactly 5 columns, add three more blank per
719 * - For more than 5 columns, add only one column.
721 ncols
= bl
->args
->argv
[vals
[2]].sz
;
723 dcol
= ncols
< 5 ? 4 : ncols
== 5 ? 3 : 1;
725 for (i
= 0, nn
= n
->prev
;
726 nn
&& i
< (int)ncols
;
728 offset
+= dcol
+ a2width
729 (&bl
->args
->argv
[vals
[2]], i
);
733 * When exceeding the declared number of columns, leave
734 * the remaining widths at 0. This will later be
735 * adjusted to the default width of 10, or, for the last
736 * column, stretched to the right margin.
742 * Use the declared column widths, extended as explained
743 * in the preceding paragraph.
745 width
= a2width(&bl
->args
->argv
[vals
[2]], i
) + dcol
;
752 * Note: buffer the width by 2, which is groff's magic
753 * number for buffering single arguments. See the above
754 * handling for column for how this changes.
756 width
= a2width(&bl
->args
->argv
[vals
[0]], 0) + 2;
761 * List-type can override the width in the case of fixed-head
762 * values (bullet, dash/hyphen, enum). Tags need a non-zero
794 * Whitespace control. Inset bodies need an initial space,
795 * while diagonal bodies need two.
798 p
->flags
|= TERMP_NOSPACE
;
802 if (MDOC_BODY
== n
->type
)
803 term_word(p
, "\\ \\ ");
806 if (MDOC_BODY
== n
->type
)
813 p
->flags
|= TERMP_NOSPACE
;
817 if (MDOC_HEAD
== n
->type
)
818 term_fontpush(p
, TERMFONT_BOLD
);
825 * Pad and break control. This is the tricky part. These flags
826 * are documented in term_flushln() in term.c. Note that we're
827 * going to unset all of these flags in termp_it_post() when we
839 if (MDOC_HEAD
== n
->type
)
840 p
->flags
|= TERMP_NOBREAK
;
842 p
->flags
|= TERMP_NOLPAD
;
845 if (MDOC_HEAD
== n
->type
)
846 p
->flags
|= TERMP_NOBREAK
;
848 p
->flags
|= TERMP_NOLPAD
;
850 if (MDOC_HEAD
!= n
->type
)
854 * This is ugly. If `-hang' is specified and the body
855 * is a `Bl' or `Bd', then we want basically to nullify
856 * the "overstep" effect in term_flushln() and treat
857 * this as a `-ohang' list instead.
859 if (n
->next
->child
&&
860 (MDOC_Bl
== n
->next
->child
->tok
||
861 MDOC_Bd
== n
->next
->child
->tok
)) {
862 p
->flags
&= ~TERMP_NOBREAK
;
863 p
->flags
&= ~TERMP_NOLPAD
;
865 p
->flags
|= TERMP_HANG
;
868 if (MDOC_HEAD
== n
->type
)
869 p
->flags
|= TERMP_NOBREAK
| TERMP_TWOSPACE
;
871 p
->flags
|= TERMP_NOLPAD
;
873 if (MDOC_HEAD
!= n
->type
)
875 if (NULL
== n
->next
|| NULL
== n
->next
->child
)
876 p
->flags
|= TERMP_DANGLE
;
879 if (MDOC_HEAD
== n
->type
) {
881 if (MDOC_BODY
== n
->next
->type
)
882 p
->flags
&= ~TERMP_NOBREAK
;
884 p
->flags
|= TERMP_NOBREAK
;
886 p
->flags
|= TERMP_NOLPAD
;
890 if (MDOC_HEAD
== n
->type
)
891 p
->flags
|= TERMP_NOBREAK
;
898 * Margin control. Set-head-width lists have their right
899 * margins shortened. The body for these lists has the offset
900 * necessarily lengthened. Everybody gets the offset.
908 * Same stipulation as above, regarding `-hang'. We
909 * don't want to recalculate rmargin and offsets when
910 * using `Bd' or `Bl' within `-hang' overstep lists.
912 if (MDOC_HEAD
== n
->type
&& n
->next
->child
&&
913 (MDOC_Bl
== n
->next
->child
->tok
||
914 MDOC_Bd
== n
->next
->child
->tok
))
927 if (MDOC_HEAD
== n
->type
)
928 p
->rmargin
= p
->offset
+ width
;
934 p
->rmargin
= p
->offset
+ width
;
936 * XXX - this behaviour is not documented: the
937 * right-most column is filled to the right margin.
939 if (MDOC_HEAD
== n
->type
&&
940 MDOC_BODY
== n
->next
->type
&&
941 p
->rmargin
< p
->maxrmargin
)
942 p
->rmargin
= p
->maxrmargin
;
949 * The dash, hyphen, bullet and enum lists all have a special
950 * HEAD character (temporarily bold, in some cases).
953 if (MDOC_HEAD
== n
->type
)
956 term_fontpush(p
, TERMFONT_BOLD
);
957 term_word(p
, "\\[bu]");
963 term_fontpush(p
, TERMFONT_BOLD
);
964 term_word(p
, "\\(hy");
968 (pair
->ppair
->ppair
->count
)++;
969 snprintf(buf
, sizeof(buf
), "%d.",
970 pair
->ppair
->ppair
->count
);
978 * If we're not going to process our children, indicate so here.
991 if (MDOC_HEAD
== n
->type
)
995 if (MDOC_BODY
== n
->type
)
1008 termp_it_post(DECL_ARGS
)
1012 if (MDOC_BLOCK
== n
->type
)
1015 type
= arg_listtype(n
->parent
->parent
->parent
);
1024 if (MDOC_BODY
== n
->type
)
1028 if (MDOC_HEAD
== n
->type
)
1037 * Now that our output is flushed, we can reset our tags. Since
1038 * only `It' sets these flags, we're free to assume that nobody
1039 * has munged them in the meanwhile.
1042 p
->flags
&= ~TERMP_DANGLE
;
1043 p
->flags
&= ~TERMP_NOBREAK
;
1044 p
->flags
&= ~TERMP_TWOSPACE
;
1045 p
->flags
&= ~TERMP_NOLPAD
;
1046 p
->flags
&= ~TERMP_HANG
;
1052 termp_nm_pre(DECL_ARGS
)
1055 if (SEC_SYNOPSIS
== n
->sec
)
1058 term_fontpush(p
, TERMFONT_BOLD
);
1060 if (NULL
== n
->child
)
1061 term_word(p
, m
->name
);
1068 termp_fl_pre(DECL_ARGS
)
1071 term_fontpush(p
, TERMFONT_BOLD
);
1072 term_word(p
, "\\-");
1075 p
->flags
|= TERMP_NOSPACE
;
1076 else if (n
->next
&& n
->next
->line
== n
->line
)
1077 p
->flags
|= TERMP_NOSPACE
;
1085 termp_an_pre(DECL_ARGS
)
1088 if (NULL
== n
->child
)
1092 * If not in the AUTHORS section, `An -split' will cause
1093 * newlines to occur before the author name. If in the AUTHORS
1094 * section, by default, the first `An' invocation is nosplit,
1095 * then all subsequent ones, regardless of whether interspersed
1096 * with other macros/text, are split. -split, in this case,
1097 * will override the condition of the implied first -nosplit.
1100 if (n
->sec
== SEC_AUTHORS
) {
1101 if ( ! (TERMP_ANPREC
& p
->flags
)) {
1102 if (TERMP_SPLIT
& p
->flags
)
1106 if (TERMP_NOSPLIT
& p
->flags
)
1112 if (TERMP_SPLIT
& p
->flags
)
1121 termp_an_post(DECL_ARGS
)
1125 if (SEC_AUTHORS
== n
->sec
)
1126 p
->flags
|= TERMP_ANPREC
;
1130 if (arg_getattr(MDOC_Split
, n
) > -1) {
1131 p
->flags
&= ~TERMP_NOSPLIT
;
1132 p
->flags
|= TERMP_SPLIT
;
1134 p
->flags
&= ~TERMP_SPLIT
;
1135 p
->flags
|= TERMP_NOSPLIT
;
1143 termp_ns_pre(DECL_ARGS
)
1146 p
->flags
|= TERMP_NOSPACE
;
1153 termp_rs_pre(DECL_ARGS
)
1156 if (SEC_SEE_ALSO
!= n
->sec
)
1158 if (MDOC_BLOCK
== n
->type
&& n
->prev
)
1166 termp_rv_pre(DECL_ARGS
)
1168 const struct mdoc_node
*nn
;
1171 term_word(p
, "The");
1173 for (nn
= n
->child
; nn
; nn
= nn
->next
) {
1174 term_fontpush(p
, TERMFONT_BOLD
);
1175 term_word(p
, nn
->string
);
1177 p
->flags
|= TERMP_NOSPACE
;
1178 if (nn
->next
&& NULL
== nn
->next
->next
)
1179 term_word(p
, "(), and");
1181 term_word(p
, "(),");
1187 term_word(p
, "functions return");
1189 term_word(p
, "function returns");
1191 term_word(p
, "the value 0 if successful; otherwise the value "
1192 "-1 is returned and the global variable");
1194 term_fontpush(p
, TERMFONT_UNDER
);
1195 term_word(p
, "errno");
1198 term_word(p
, "is set to indicate the error.");
1206 termp_ex_pre(DECL_ARGS
)
1208 const struct mdoc_node
*nn
;
1210 term_word(p
, "The");
1212 for (nn
= n
->child
; nn
; nn
= nn
->next
) {
1213 term_fontpush(p
, TERMFONT_BOLD
);
1214 term_word(p
, nn
->string
);
1216 p
->flags
|= TERMP_NOSPACE
;
1217 if (nn
->next
&& NULL
== nn
->next
->next
)
1218 term_word(p
, ", and");
1222 p
->flags
&= ~TERMP_NOSPACE
;
1226 term_word(p
, "utilities exit");
1228 term_word(p
, "utility exits");
1230 term_word(p
, "0 on success, and >0 if an error occurs.");
1238 termp_nd_pre(DECL_ARGS
)
1241 if (MDOC_BODY
!= n
->type
)
1244 #if defined(__OpenBSD__) || defined(__linux__)
1245 term_word(p
, "\\(en");
1247 term_word(p
, "\\(em");
1255 termp_bl_pre(DECL_ARGS
)
1258 return(MDOC_HEAD
!= n
->type
);
1264 termp_bl_post(DECL_ARGS
)
1267 if (MDOC_BLOCK
== n
->type
)
1274 termp_op_post(DECL_ARGS
)
1277 if (MDOC_BODY
!= n
->type
)
1279 p
->flags
|= TERMP_NOSPACE
;
1280 term_word(p
, "\\(rB");
1286 termp_xr_pre(DECL_ARGS
)
1288 const struct mdoc_node
*nn
;
1290 if (NULL
== n
->child
)
1293 assert(MDOC_TEXT
== n
->child
->type
);
1296 term_word(p
, nn
->string
);
1297 if (NULL
== (nn
= nn
->next
))
1299 p
->flags
|= TERMP_NOSPACE
;
1301 p
->flags
|= TERMP_NOSPACE
;
1302 term_word(p
, nn
->string
);
1303 p
->flags
|= TERMP_NOSPACE
;
1311 termp_vt_pre(DECL_ARGS
)
1314 if (MDOC_ELEM
== n
->type
)
1315 return(termp_under_pre(p
, pair
, m
, n
));
1316 else if (MDOC_HEAD
== n
->type
)
1318 else if (MDOC_BLOCK
== n
->type
)
1321 return(termp_under_pre(p
, pair
, m
, n
));
1327 termp_vt_post(DECL_ARGS
)
1330 if (MDOC_BLOCK
!= n
->type
)
1332 if (n
->next
&& MDOC_Vt
== n
->next
->tok
)
1341 termp_bold_pre(DECL_ARGS
)
1344 term_fontpush(p
, TERMFONT_BOLD
);
1351 termp_fd_post(DECL_ARGS
)
1354 if (n
->sec
!= SEC_SYNOPSIS
)
1358 if (n
->next
&& MDOC_Fd
!= n
->next
->tok
)
1365 termp_sh_pre(DECL_ARGS
)
1368 /* No vspace between consecutive `Sh' calls. */
1372 if (n
->prev
&& MDOC_Sh
== n
->prev
->tok
)
1373 if (NULL
== n
->prev
->body
->child
)
1378 term_fontpush(p
, TERMFONT_BOLD
);
1392 termp_sh_post(DECL_ARGS
)
1411 termp_op_pre(DECL_ARGS
)
1416 term_word(p
, "\\(lB");
1417 p
->flags
|= TERMP_NOSPACE
;
1428 termp_bt_pre(DECL_ARGS
)
1431 term_word(p
, "is currently in beta test.");
1438 termp_lb_post(DECL_ARGS
)
1441 if (SEC_LIBRARY
== n
->sec
)
1448 termp_ud_pre(DECL_ARGS
)
1451 term_word(p
, "currently under development.");
1458 termp_d1_pre(DECL_ARGS
)
1461 if (MDOC_BLOCK
!= n
->type
)
1464 p
->offset
+= (INDENT
+ 1);
1471 termp_d1_post(DECL_ARGS
)
1474 if (MDOC_BLOCK
!= n
->type
)
1482 termp_aq_pre(DECL_ARGS
)
1485 if (MDOC_BODY
!= n
->type
)
1487 term_word(p
, "\\(la");
1488 p
->flags
|= TERMP_NOSPACE
;
1495 termp_aq_post(DECL_ARGS
)
1498 if (MDOC_BODY
!= n
->type
)
1500 p
->flags
|= TERMP_NOSPACE
;
1501 term_word(p
, "\\(ra");
1507 termp_ft_pre(DECL_ARGS
)
1510 if (SEC_SYNOPSIS
== n
->sec
)
1511 if (n
->prev
&& MDOC_Fo
== n
->prev
->tok
)
1514 term_fontpush(p
, TERMFONT_UNDER
);
1521 termp_ft_post(DECL_ARGS
)
1524 if (SEC_SYNOPSIS
== n
->sec
)
1531 termp_fn_pre(DECL_ARGS
)
1533 const struct mdoc_node
*nn
;
1535 term_fontpush(p
, TERMFONT_BOLD
);
1536 term_word(p
, n
->child
->string
);
1539 p
->flags
|= TERMP_NOSPACE
;
1542 for (nn
= n
->child
->next
; nn
; nn
= nn
->next
) {
1543 term_fontpush(p
, TERMFONT_UNDER
);
1544 term_word(p
, nn
->string
);
1553 if (SEC_SYNOPSIS
== n
->sec
)
1562 termp_fn_post(DECL_ARGS
)
1565 if (n
->sec
== SEC_SYNOPSIS
&& n
->next
)
1572 termp_fa_pre(DECL_ARGS
)
1574 const struct mdoc_node
*nn
;
1576 if (n
->parent
->tok
!= MDOC_Fo
) {
1577 term_fontpush(p
, TERMFONT_UNDER
);
1581 for (nn
= n
->child
; nn
; nn
= nn
->next
) {
1582 term_fontpush(p
, TERMFONT_UNDER
);
1583 term_word(p
, nn
->string
);
1590 if (n
->child
&& n
->next
&& n
->next
->tok
== MDOC_Fa
)
1599 termp_bd_pre(DECL_ARGS
)
1602 const struct mdoc_node
*nn
;
1604 if (MDOC_BLOCK
== n
->type
) {
1605 print_bvspace(p
, n
, n
);
1607 } else if (MDOC_HEAD
== n
->type
)
1612 for (type
= -1, i
= 0; i
< (int)nn
->args
->argc
; i
++) {
1613 switch (nn
->args
->argv
[i
].arg
) {
1614 case (MDOC_Centred
):
1620 case (MDOC_Unfilled
):
1622 case (MDOC_Literal
):
1623 type
= nn
->args
->argv
[i
].arg
;
1626 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.
1642 if (MDOC_Literal
!= type
&& MDOC_Unfilled
!= type
)
1645 for (nn
= n
->child
; nn
; nn
= nn
->next
) {
1646 p
->flags
|= TERMP_NOSPACE
;
1647 print_mdoc_node(p
, pair
, m
, nn
);
1648 if (NULL
== nn
->next
)
1650 if (nn
->prev
&& nn
->prev
->line
< nn
->line
)
1652 else if (NULL
== nn
->prev
)
1662 termp_bd_post(DECL_ARGS
)
1665 if (MDOC_BODY
!= n
->type
)
1667 p
->flags
|= TERMP_NOSPACE
;
1674 termp_qq_pre(DECL_ARGS
)
1677 if (MDOC_BODY
!= n
->type
)
1680 p
->flags
|= TERMP_NOSPACE
;
1687 termp_qq_post(DECL_ARGS
)
1690 if (MDOC_BODY
!= n
->type
)
1692 p
->flags
|= TERMP_NOSPACE
;
1699 termp_bx_post(DECL_ARGS
)
1703 p
->flags
|= TERMP_NOSPACE
;
1704 term_word(p
, "BSD");
1710 termp_xx_pre(DECL_ARGS
)
1746 termp_sq_pre(DECL_ARGS
)
1749 if (MDOC_BODY
!= n
->type
)
1751 term_word(p
, "\\(oq");
1752 p
->flags
|= TERMP_NOSPACE
;
1759 termp_sq_post(DECL_ARGS
)
1762 if (MDOC_BODY
!= n
->type
)
1764 p
->flags
|= TERMP_NOSPACE
;
1765 term_word(p
, "\\(aq");
1771 termp_pf_pre(DECL_ARGS
)
1774 p
->flags
|= TERMP_IGNDELIM
;
1781 termp_pf_post(DECL_ARGS
)
1784 p
->flags
&= ~TERMP_IGNDELIM
;
1785 p
->flags
|= TERMP_NOSPACE
;
1791 termp_ss_pre(DECL_ARGS
)
1801 term_fontpush(p
, TERMFONT_BOLD
);
1802 p
->offset
= HALFINDENT
;
1814 termp_ss_post(DECL_ARGS
)
1817 if (MDOC_HEAD
== n
->type
)
1824 termp_cd_pre(DECL_ARGS
)
1827 term_fontpush(p
, TERMFONT_BOLD
);
1835 termp_in_pre(DECL_ARGS
)
1838 term_fontpush(p
, TERMFONT_BOLD
);
1839 if (SEC_SYNOPSIS
== n
->sec
)
1840 term_word(p
, "#include");
1843 p
->flags
|= TERMP_NOSPACE
;
1850 termp_in_post(DECL_ARGS
)
1853 term_fontpush(p
, TERMFONT_BOLD
);
1854 p
->flags
|= TERMP_NOSPACE
;
1858 if (SEC_SYNOPSIS
!= n
->sec
)
1863 * XXX Not entirely correct. If `.In foo bar' is specified in
1864 * the SYNOPSIS section, then it produces a single break after
1865 * the <foo>; mandoc asserts a vertical space. Since this
1866 * construction is rarely used, I think it's fine.
1868 if (n
->next
&& MDOC_In
!= n
->next
->tok
)
1875 termp_sp_pre(DECL_ARGS
)
1881 len
= n
->child
? a2height(n
->child
) : 1;
1893 for (i
= 0; i
< len
; i
++)
1902 termp_brq_pre(DECL_ARGS
)
1905 if (MDOC_BODY
!= n
->type
)
1907 term_word(p
, "\\(lC");
1908 p
->flags
|= TERMP_NOSPACE
;
1915 termp_brq_post(DECL_ARGS
)
1918 if (MDOC_BODY
!= n
->type
)
1920 p
->flags
|= TERMP_NOSPACE
;
1921 term_word(p
, "\\(rC");
1927 termp_bq_pre(DECL_ARGS
)
1930 if (MDOC_BODY
!= n
->type
)
1932 term_word(p
, "\\(lB");
1933 p
->flags
|= TERMP_NOSPACE
;
1940 termp_bq_post(DECL_ARGS
)
1943 if (MDOC_BODY
!= n
->type
)
1945 p
->flags
|= TERMP_NOSPACE
;
1946 term_word(p
, "\\(rB");
1952 termp_pq_pre(DECL_ARGS
)
1955 if (MDOC_BODY
!= n
->type
)
1957 term_word(p
, "\\&(");
1958 p
->flags
|= TERMP_NOSPACE
;
1965 termp_pq_post(DECL_ARGS
)
1968 if (MDOC_BODY
!= n
->type
)
1976 termp_fo_pre(DECL_ARGS
)
1978 const struct mdoc_node
*nn
;
1980 if (MDOC_BODY
== n
->type
) {
1981 p
->flags
|= TERMP_NOSPACE
;
1983 p
->flags
|= TERMP_NOSPACE
;
1985 } else if (MDOC_HEAD
!= n
->type
)
1988 term_fontpush(p
, TERMFONT_BOLD
);
1989 for (nn
= n
->child
; nn
; nn
= nn
->next
) {
1990 assert(MDOC_TEXT
== nn
->type
);
1991 term_word(p
, nn
->string
);
2001 termp_fo_post(DECL_ARGS
)
2004 if (MDOC_BODY
!= n
->type
)
2006 p
->flags
|= TERMP_NOSPACE
;
2008 p
->flags
|= TERMP_NOSPACE
;
2016 termp_bf_pre(DECL_ARGS
)
2018 const struct mdoc_node
*nn
;
2020 if (MDOC_HEAD
== n
->type
)
2022 else if (MDOC_BLOCK
!= n
->type
)
2025 if (NULL
== (nn
= n
->head
->child
)) {
2026 if (arg_hasattr(MDOC_Emphasis
, n
))
2027 term_fontpush(p
, TERMFONT_UNDER
);
2028 else if (arg_hasattr(MDOC_Symbolic
, n
))
2029 term_fontpush(p
, TERMFONT_BOLD
);
2031 term_fontpush(p
, TERMFONT_NONE
);
2036 assert(MDOC_TEXT
== nn
->type
);
2037 if (0 == strcmp("Em", nn
->string
))
2038 term_fontpush(p
, TERMFONT_UNDER
);
2039 else if (0 == strcmp("Sy", nn
->string
))
2040 term_fontpush(p
, TERMFONT_BOLD
);
2042 term_fontpush(p
, TERMFONT_NONE
);
2050 termp_sm_pre(DECL_ARGS
)
2053 assert(n
->child
&& MDOC_TEXT
== n
->child
->type
);
2054 if (0 == strcmp("on", n
->child
->string
))
2055 p
->flags
&= ~TERMP_NONOSPACE
;
2057 p
->flags
|= TERMP_NONOSPACE
;
2065 termp_ap_pre(DECL_ARGS
)
2068 p
->flags
|= TERMP_NOSPACE
;
2069 term_word(p
, "\\(aq");
2070 p
->flags
|= TERMP_NOSPACE
;
2077 termp____post(DECL_ARGS
)
2082 p
->flags
|= TERMP_NOSPACE
;
2085 term_word(p
, "\\(rq");
2086 p
->flags
|= TERMP_NOSPACE
;
2091 term_word(p
, n
->next
? "," : ".");
2097 termp_li_pre(DECL_ARGS
)
2100 term_fontpush(p
, TERMFONT_NONE
);
2107 termp_lk_pre(DECL_ARGS
)
2109 const struct mdoc_node
*nn
;
2111 term_fontpush(p
, TERMFONT_UNDER
);
2114 if (NULL
== nn
->next
)
2117 term_word(p
, nn
->string
);
2120 p
->flags
|= TERMP_NOSPACE
;
2123 term_fontpush(p
, TERMFONT_BOLD
);
2124 for (nn
= nn
->next
; nn
; nn
= nn
->next
)
2125 term_word(p
, nn
->string
);
2134 termp_under_pre(DECL_ARGS
)
2137 term_fontpush(p
, TERMFONT_UNDER
);
2144 termp__t_pre(DECL_ARGS
)
2147 term_word(p
, "\\(lq");
2148 p
->flags
|= TERMP_NOSPACE
;