]>
git.cameronkatri.com Git - mandoc.git/blob - mdoc_term.c
1 /* $Id: mdoc_term.c,v 1.78 2009/09/22 16:10:52 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>
29 /* FIXME: check HANG lists: they seem to be broken... :
33 * .It Fl f Ns Ar option...
34 * Override default compiler behaviour. See
35 * .Sx Compiler Options
37 * Override default compiler behaviour. See
38 * .Sx Compiler Options
40 * Override default compiler behaviour. See
41 * .Sx Compiler Options
43 * Override default compiler behaviour. See
44 * .Sx Compiler Options
47 * .It a sasd fasd as afsd sfad sfds sadfs sd sfd ssfad asfd
48 * Override default compiler behaviour. See
49 * .Sx Compiler Options
51 * Override default compiler behaviour. See
52 * .Sx Compiler Options
54 * Override default compiler behaviour. See
55 * .Sx Compiler Options
57 * Override default compiler behaviour. See
58 * .Sx Compiler Options
68 struct termpair
*ppair
;
73 #define DECL_ARGS struct termp *p, \
74 struct termpair *pair, \
75 const struct mdoc_meta *meta, \
76 const struct mdoc_node *node
79 int (*pre
)(DECL_ARGS
);
80 void (*post
)(DECL_ARGS
);
83 static void termp____post(DECL_ARGS
);
84 static void termp_an_post(DECL_ARGS
);
85 static void termp_aq_post(DECL_ARGS
);
86 static void termp_bd_post(DECL_ARGS
);
87 static void termp_bl_post(DECL_ARGS
);
88 static void termp_bq_post(DECL_ARGS
);
89 static void termp_brq_post(DECL_ARGS
);
90 static void termp_bx_post(DECL_ARGS
);
91 static void termp_d1_post(DECL_ARGS
);
92 static void termp_dq_post(DECL_ARGS
);
93 static void termp_fd_post(DECL_ARGS
);
94 static void termp_fn_post(DECL_ARGS
);
95 static void termp_fo_post(DECL_ARGS
);
96 static void termp_ft_post(DECL_ARGS
);
97 static void termp_in_post(DECL_ARGS
);
98 static void termp_it_post(DECL_ARGS
);
99 static void termp_lb_post(DECL_ARGS
);
100 static void termp_op_post(DECL_ARGS
);
101 static void termp_pf_post(DECL_ARGS
);
102 static void termp_pq_post(DECL_ARGS
);
103 static void termp_qq_post(DECL_ARGS
);
104 static void termp_sh_post(DECL_ARGS
);
105 static void termp_sq_post(DECL_ARGS
);
106 static void termp_ss_post(DECL_ARGS
);
107 static void termp_vt_post(DECL_ARGS
);
109 static int termp_an_pre(DECL_ARGS
);
110 static int termp_ap_pre(DECL_ARGS
);
111 static int termp_aq_pre(DECL_ARGS
);
112 static int termp_bd_pre(DECL_ARGS
);
113 static int termp_bf_pre(DECL_ARGS
);
114 static int termp_bold_pre(DECL_ARGS
);
115 static int termp_bq_pre(DECL_ARGS
);
116 static int termp_brq_pre(DECL_ARGS
);
117 static int termp_bt_pre(DECL_ARGS
);
118 static int termp_cd_pre(DECL_ARGS
);
119 static int termp_d1_pre(DECL_ARGS
);
120 static int termp_dq_pre(DECL_ARGS
);
121 static int termp_ex_pre(DECL_ARGS
);
122 static int termp_fa_pre(DECL_ARGS
);
123 static int termp_fl_pre(DECL_ARGS
);
124 static int termp_fn_pre(DECL_ARGS
);
125 static int termp_fo_pre(DECL_ARGS
);
126 static int termp_ft_pre(DECL_ARGS
);
127 static int termp_in_pre(DECL_ARGS
);
128 static int termp_it_pre(DECL_ARGS
);
129 static int termp_lk_pre(DECL_ARGS
);
130 static int termp_nd_pre(DECL_ARGS
);
131 static int termp_nm_pre(DECL_ARGS
);
132 static int termp_ns_pre(DECL_ARGS
);
133 static int termp_op_pre(DECL_ARGS
);
134 static int termp_pf_pre(DECL_ARGS
);
135 static int termp_pq_pre(DECL_ARGS
);
136 static int termp_qq_pre(DECL_ARGS
);
137 static int termp_rs_pre(DECL_ARGS
);
138 static int termp_rv_pre(DECL_ARGS
);
139 static int termp_sh_pre(DECL_ARGS
);
140 static int termp_sm_pre(DECL_ARGS
);
141 static int termp_sp_pre(DECL_ARGS
);
142 static int termp_sq_pre(DECL_ARGS
);
143 static int termp_ss_pre(DECL_ARGS
);
144 static int termp_under_pre(DECL_ARGS
);
145 static int termp_ud_pre(DECL_ARGS
);
146 static int termp_xr_pre(DECL_ARGS
);
147 static int termp_xx_pre(DECL_ARGS
);
149 static const struct termact termacts
[MDOC_MAX
] = {
150 { termp_ap_pre
, NULL
}, /* Ap */
151 { NULL
, NULL
}, /* Dd */
152 { NULL
, NULL
}, /* Dt */
153 { NULL
, NULL
}, /* Os */
154 { termp_sh_pre
, termp_sh_post
}, /* Sh */
155 { termp_ss_pre
, termp_ss_post
}, /* Ss */
156 { termp_sp_pre
, NULL
}, /* Pp */
157 { termp_d1_pre
, termp_d1_post
}, /* D1 */
158 { termp_d1_pre
, termp_d1_post
}, /* Dl */
159 { termp_bd_pre
, termp_bd_post
}, /* Bd */
160 { NULL
, NULL
}, /* Ed */
161 { NULL
, termp_bl_post
}, /* Bl */
162 { NULL
, NULL
}, /* El */
163 { termp_it_pre
, termp_it_post
}, /* It */
164 { NULL
, NULL
}, /* Ad */
165 { termp_an_pre
, termp_an_post
}, /* An */
166 { termp_under_pre
, NULL
}, /* Ar */
167 { termp_cd_pre
, NULL
}, /* Cd */
168 { termp_bold_pre
, NULL
}, /* Cm */
169 { NULL
, NULL
}, /* Dv */
170 { NULL
, NULL
}, /* Er */
171 { NULL
, NULL
}, /* Ev */
172 { termp_ex_pre
, NULL
}, /* Ex */
173 { termp_fa_pre
, NULL
}, /* Fa */
174 { termp_bold_pre
, termp_fd_post
}, /* Fd */
175 { termp_fl_pre
, NULL
}, /* Fl */
176 { termp_fn_pre
, termp_fn_post
}, /* Fn */
177 { termp_ft_pre
, termp_ft_post
}, /* Ft */
178 { termp_bold_pre
, NULL
}, /* Ic */
179 { termp_in_pre
, termp_in_post
}, /* In */
180 { NULL
, NULL
}, /* Li */
181 { termp_nd_pre
, NULL
}, /* Nd */
182 { termp_nm_pre
, NULL
}, /* Nm */
183 { termp_op_pre
, termp_op_post
}, /* Op */
184 { NULL
, NULL
}, /* Ot */
185 { termp_under_pre
, NULL
}, /* Pa */
186 { termp_rv_pre
, NULL
}, /* Rv */
187 { NULL
, NULL
}, /* St */
188 { termp_under_pre
, NULL
}, /* Va */
189 { termp_under_pre
, termp_vt_post
}, /* Vt */
190 { termp_xr_pre
, NULL
}, /* Xr */
191 { NULL
, termp____post
}, /* %A */
192 { NULL
, termp____post
}, /* %B */
193 { NULL
, termp____post
}, /* %D */
194 { NULL
, termp____post
}, /* %I */
195 { termp_under_pre
, termp____post
}, /* %J */
196 { NULL
, termp____post
}, /* %N */
197 { NULL
, termp____post
}, /* %O */
198 { NULL
, termp____post
}, /* %P */
199 { NULL
, termp____post
}, /* %R */
200 { termp_under_pre
, termp____post
}, /* %T */
201 { NULL
, termp____post
}, /* %V */
202 { NULL
, NULL
}, /* Ac */
203 { termp_aq_pre
, termp_aq_post
}, /* Ao */
204 { termp_aq_pre
, termp_aq_post
}, /* Aq */
205 { NULL
, NULL
}, /* At */
206 { NULL
, NULL
}, /* Bc */
207 { termp_bf_pre
, NULL
}, /* Bf */
208 { termp_bq_pre
, termp_bq_post
}, /* Bo */
209 { termp_bq_pre
, termp_bq_post
}, /* Bq */
210 { termp_xx_pre
, NULL
}, /* Bsx */
211 { NULL
, termp_bx_post
}, /* Bx */
212 { NULL
, NULL
}, /* Db */
213 { NULL
, NULL
}, /* Dc */
214 { termp_dq_pre
, termp_dq_post
}, /* Do */
215 { termp_dq_pre
, termp_dq_post
}, /* Dq */
216 { NULL
, NULL
}, /* Ec */
217 { NULL
, NULL
}, /* Ef */
218 { termp_under_pre
, NULL
}, /* Em */
219 { NULL
, NULL
}, /* Eo */
220 { termp_xx_pre
, NULL
}, /* Fx */
221 { termp_bold_pre
, NULL
}, /* Ms */
222 { NULL
, NULL
}, /* No */
223 { termp_ns_pre
, NULL
}, /* Ns */
224 { termp_xx_pre
, NULL
}, /* Nx */
225 { termp_xx_pre
, NULL
}, /* Ox */
226 { NULL
, NULL
}, /* Pc */
227 { termp_pf_pre
, termp_pf_post
}, /* Pf */
228 { termp_pq_pre
, termp_pq_post
}, /* Po */
229 { termp_pq_pre
, termp_pq_post
}, /* Pq */
230 { NULL
, NULL
}, /* Qc */
231 { termp_sq_pre
, termp_sq_post
}, /* Ql */
232 { termp_qq_pre
, termp_qq_post
}, /* Qo */
233 { termp_qq_pre
, termp_qq_post
}, /* Qq */
234 { NULL
, NULL
}, /* Re */
235 { termp_rs_pre
, NULL
}, /* Rs */
236 { NULL
, NULL
}, /* Sc */
237 { termp_sq_pre
, termp_sq_post
}, /* So */
238 { termp_sq_pre
, termp_sq_post
}, /* Sq */
239 { termp_sm_pre
, NULL
}, /* Sm */
240 { termp_under_pre
, NULL
}, /* Sx */
241 { termp_bold_pre
, NULL
}, /* Sy */
242 { NULL
, NULL
}, /* Tn */
243 { termp_xx_pre
, NULL
}, /* Ux */
244 { NULL
, NULL
}, /* Xc */
245 { NULL
, NULL
}, /* Xo */
246 { termp_fo_pre
, termp_fo_post
}, /* Fo */
247 { NULL
, NULL
}, /* Fc */
248 { termp_op_pre
, termp_op_post
}, /* Oo */
249 { NULL
, NULL
}, /* Oc */
250 { NULL
, NULL
}, /* Bk */
251 { NULL
, NULL
}, /* Ek */
252 { termp_bt_pre
, NULL
}, /* Bt */
253 { NULL
, NULL
}, /* Hf */
254 { NULL
, NULL
}, /* Fr */
255 { termp_ud_pre
, NULL
}, /* Ud */
256 { NULL
, termp_lb_post
}, /* Lb */
257 { termp_sp_pre
, NULL
}, /* Lp */
258 { termp_lk_pre
, NULL
}, /* Lk */
259 { termp_under_pre
, NULL
}, /* Mt */
260 { termp_brq_pre
, termp_brq_post
}, /* Brq */
261 { termp_brq_pre
, termp_brq_post
}, /* Bro */
262 { NULL
, NULL
}, /* Brc */
263 { NULL
, NULL
}, /* %C */
264 { NULL
, NULL
}, /* Es */
265 { NULL
, NULL
}, /* En */
266 { termp_xx_pre
, NULL
}, /* Dx */
267 { NULL
, NULL
}, /* %Q */
268 { termp_sp_pre
, NULL
}, /* br */
269 { termp_sp_pre
, NULL
}, /* sp */
273 extern size_t strlcpy(char *, const char *, size_t);
274 extern size_t strlcat(char *, const char *, size_t);
277 static int arg_hasattr(int, const struct mdoc_node
*);
278 static int arg_getattrs(const int *, int *, size_t,
279 const struct mdoc_node
*);
280 static int arg_getattr(int, const struct mdoc_node
*);
281 static size_t arg_offset(const struct mdoc_argv
*);
282 static size_t arg_width(const struct mdoc_argv
*, int);
283 static int arg_listtype(const struct mdoc_node
*);
284 static void fmt_block_vspace(struct termp
*,
285 const struct mdoc_node
*,
286 const struct mdoc_node
*);
287 static void print_node(DECL_ARGS
);
288 static void print_head(DECL_ARGS
);
289 static void print_body(DECL_ARGS
);
290 static void print_foot(DECL_ARGS
);
294 mdoc_run(struct termp
*p
, const struct mdoc
*mdoc
)
296 const struct mdoc_node
*n
;
297 const struct mdoc_meta
*m
;
302 print_head(p
, NULL
, m
, n
);
304 print_body(p
, NULL
, m
, n
->child
);
305 print_foot(p
, NULL
, m
, n
);
310 print_body(DECL_ARGS
)
313 print_node(p
, pair
, meta
, node
);
315 print_body(p
, pair
, meta
, node
->next
);
321 print_node(DECL_ARGS
)
323 int chld
, bold
, under
;
324 struct termpair npair
;
325 size_t offset
, rmargin
;
329 rmargin
= p
->rmargin
;
333 bzero(&npair
, sizeof(struct termpair
));
336 if (MDOC_TEXT
!= node
->type
) {
337 if (termacts
[node
->tok
].pre
)
338 chld
= (*termacts
[node
->tok
].pre
)
339 (p
, &npair
, meta
, node
);
341 term_word(p
, node
->string
);
343 if (chld
&& node
->child
)
344 print_body(p
, &npair
, meta
, node
->child
);
347 * XXX - if bold/under were to span scopes, this wouldn't be
348 * possible, but because decoration is always in-scope, we can
349 * get away with this.
355 if (MDOC_TEXT
!= node
->type
)
356 if (termacts
[node
->tok
].post
)
357 (*termacts
[node
->tok
].post
)
358 (p
, &npair
, meta
, node
);
361 p
->rmargin
= rmargin
;
367 print_foot(DECL_ARGS
)
373 * Output the footer in new-groff style, that is, three columns
374 * with the middle being the manual date and flanking columns
375 * being the operating system:
380 if (NULL
== (buf
= malloc(p
->rmargin
)))
381 err(EXIT_FAILURE
, "malloc");
382 if (NULL
== (os
= malloc(p
->rmargin
)))
383 err(EXIT_FAILURE
, "malloc");
385 tm
= localtime(&meta
->date
);
387 if (0 == strftime(buf
, p
->rmargin
, "%B %e, %Y", tm
))
388 err(EXIT_FAILURE
, "strftime");
390 (void)strlcpy(os
, meta
->os
, p
->rmargin
);
395 p
->rmargin
= (p
->maxrmargin
- strlen(buf
) + 1) / 2;
396 p
->flags
|= TERMP_NOSPACE
| TERMP_NOBREAK
;
401 p
->offset
= p
->rmargin
;
402 p
->rmargin
= p
->maxrmargin
- strlen(os
);
403 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
408 p
->offset
= p
->rmargin
;
409 p
->rmargin
= p
->maxrmargin
;
410 p
->flags
&= ~TERMP_NOBREAK
;
411 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
417 p
->rmargin
= p
->maxrmargin
;
425 /* FIXME: put in utility library. */
428 print_head(DECL_ARGS
)
432 p
->rmargin
= p
->maxrmargin
;
435 if (NULL
== (buf
= malloc(p
->rmargin
)))
436 err(EXIT_FAILURE
, "malloc");
437 if (NULL
== (title
= malloc(p
->rmargin
)))
438 err(EXIT_FAILURE
, "malloc");
441 * The header is strange. It has three components, which are
442 * really two with the first duplicated. It goes like this:
444 * IDENTIFIER TITLE IDENTIFIER
446 * The IDENTIFIER is NAME(SECTION), which is the command-name
447 * (if given, or "unknown" if not) followed by the manual page
448 * section. These are given in `Dt'. The TITLE is a free-form
449 * string depending on the manual volume. If not specified, it
450 * switches on the manual section.
454 (void)strlcpy(buf
, meta
->vol
, p
->rmargin
);
457 (void)strlcat(buf
, " (", p
->rmargin
);
458 (void)strlcat(buf
, meta
->arch
, p
->rmargin
);
459 (void)strlcat(buf
, ")", p
->rmargin
);
462 snprintf(title
, p
->rmargin
, "%s(%d)", meta
->title
, meta
->msec
);
465 p
->rmargin
= (p
->maxrmargin
- strlen(buf
) + 1) / 2;
466 p
->flags
|= TERMP_NOBREAK
| TERMP_NOSPACE
;
471 p
->offset
= p
->rmargin
;
472 p
->rmargin
= p
->maxrmargin
- strlen(title
);
473 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
478 p
->offset
= p
->rmargin
;
479 p
->rmargin
= p
->maxrmargin
;
480 p
->flags
&= ~TERMP_NOBREAK
;
481 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
487 p
->rmargin
= p
->maxrmargin
;
488 p
->flags
&= ~TERMP_NOSPACE
;
495 /* FIXME: put in utility file for front-ends. */
497 arg_width(const struct mdoc_argv
*arg
, int pos
)
502 assert(pos
< (int)arg
->sz
&& pos
>= 0);
503 assert(arg
->value
[pos
]);
507 if (0 == (len
= (int)strlen(p
)))
510 for (i
= 0; i
< len
- 1; i
++)
511 if ( ! isdigit((u_char
)p
[i
]))
515 if ('n' == p
[len
- 1] || 'm' == p
[len
- 1])
516 return((size_t)atoi(p
) + 2);
518 return((size_t)len
+ 2);
522 /* FIXME: put in utility file for front-ends. */
524 arg_listtype(const struct mdoc_node
*n
)
528 assert(MDOC_BLOCK
== n
->type
);
530 len
= (int)(n
->args
? n
->args
->argc
: 0);
532 for (i
= 0; i
< len
; i
++)
533 switch (n
->args
->argv
[i
].arg
) {
555 return(n
->args
->argv
[i
].arg
);
564 /* FIXME: put in utility file for front-ends. */
566 arg_offset(const struct mdoc_argv
*arg
)
574 if (0 == strcmp(p
, "left"))
576 if (0 == strcmp(p
, "indent"))
578 if (0 == strcmp(p
, "indent-two"))
579 return((INDENT
+ 1) * 2);
581 if (0 == (len
= (int)strlen(p
)))
584 for (i
= 0; i
< len
- 1; i
++)
585 if ( ! isdigit((u_char
)p
[i
]))
589 if ('n' == p
[len
- 1] || 'm' == p
[len
- 1])
590 return((size_t)atoi(p
));
597 arg_hasattr(int arg
, const struct mdoc_node
*n
)
600 return(-1 != arg_getattr(arg
, n
));
605 arg_getattr(int v
, const struct mdoc_node
*n
)
609 return(arg_getattrs(&v
, &val
, 1, n
) ? val
: -1);
614 arg_getattrs(const int *keys
, int *vals
,
615 size_t sz
, const struct mdoc_node
*n
)
622 for (k
= i
= 0; i
< (int)n
->args
->argc
; i
++)
623 for (j
= 0; j
< (int)sz
; j
++)
624 if (n
->args
->argv
[i
].arg
== keys
[j
]) {
634 fmt_block_vspace(struct termp
*p
,
635 const struct mdoc_node
*bl
,
636 const struct mdoc_node
*node
)
638 const struct mdoc_node
*n
;
642 if (MDOC_Bl
== bl
->tok
&& arg_hasattr(MDOC_Compact
, bl
))
647 * Search through our prior nodes. If we follow a `Ss' or `Sh',
651 for (n
= node
; n
; n
= n
->parent
) {
652 if (MDOC_BLOCK
!= n
->type
)
654 if (MDOC_Ss
== n
->tok
)
656 if (MDOC_Sh
== n
->tok
)
664 * XXX - not documented: a `-column' does not ever assert vspace
668 if (MDOC_Bl
== bl
->tok
&& arg_hasattr(MDOC_Column
, bl
))
669 if (node
->prev
&& MDOC_It
== node
->prev
->tok
)
673 * XXX - not documented: a `-diag' without a body does not
674 * assert a vspace prior to the next element.
676 if (MDOC_Bl
== bl
->tok
&& arg_hasattr(MDOC_Diag
, bl
))
677 if (node
->prev
&& MDOC_It
== node
->prev
->tok
) {
678 assert(node
->prev
->body
);
679 if (NULL
== node
->prev
->body
->child
)
689 termp_dq_pre(DECL_ARGS
)
692 if (MDOC_BODY
!= node
->type
)
695 term_word(p
, "\\(lq");
696 p
->flags
|= TERMP_NOSPACE
;
703 termp_dq_post(DECL_ARGS
)
706 if (MDOC_BODY
!= node
->type
)
709 p
->flags
|= TERMP_NOSPACE
;
710 term_word(p
, "\\(rq");
716 termp_it_pre(DECL_ARGS
)
718 const struct mdoc_node
*bl
, *n
;
720 int i
, type
, keys
[3], vals
[3];
721 size_t width
, offset
;
723 if (MDOC_BLOCK
== node
->type
) {
724 fmt_block_vspace(p
, node
->parent
->parent
, node
);
728 bl
= node
->parent
->parent
->parent
;
730 /* Save parent attributes. */
732 pair
->flag
= p
->flags
;
734 /* Get list width and offset. */
736 keys
[0] = MDOC_Width
;
737 keys
[1] = MDOC_Offset
;
738 keys
[2] = MDOC_Column
;
740 vals
[0] = vals
[1] = vals
[2] = -1;
744 (void)arg_getattrs(keys
, vals
, 3, bl
);
746 type
= arg_listtype(bl
);
749 /* Calculate real width and offset. */
753 if (MDOC_BODY
== node
->type
)
756 * Work around groff's column handling. The offset is
757 * equal to the sum of all widths leading to the current
758 * column (plus the -offset value). If this column
759 * exceeds the stated number of columns, the width is
760 * set as 0, else it's the stated column width (later
761 * the 0 will be adjusted to default 10 or, if in the
762 * last column case, set to stretch to the margin).
764 for (i
= 0, n
= node
->prev
; n
&&
765 i
< (int)bl
->args
->argv
[vals
[2]].sz
;
768 (&bl
->args
->argv
[vals
[2]], i
);
770 /* Whether exceeds maximum column. */
771 if (i
< (int)bl
->args
->argv
[vals
[2]].sz
)
772 width
= arg_width(&bl
->args
->argv
[vals
[2]], i
);
777 offset
+= arg_offset(&bl
->args
->argv
[vals
[1]]);
781 width
= arg_width(&bl
->args
->argv
[vals
[0]], 0);
783 offset
+= arg_offset(&bl
->args
->argv
[vals
[1]]);
788 * List-type can override the width in the case of fixed-head
789 * values (bullet, dash/hyphen, enum). Tags need a non-zero
821 * Whitespace control. Inset bodies need an initial space,
822 * while diagonal bodies need two.
825 p
->flags
|= TERMP_NOSPACE
;
829 if (MDOC_BODY
== node
->type
)
830 term_word(p
, "\\ \\ ");
833 if (MDOC_BODY
== node
->type
)
840 p
->flags
|= TERMP_NOSPACE
;
844 if (MDOC_HEAD
== node
->type
)
852 * Pad and break control. This is the tricker part. Lists with
853 * set right-margins for the head get TERMP_NOBREAK because, if
854 * they overrun the margin, they wrap to the new margin.
855 * Correspondingly, the body for these types don't left-pad, as
856 * the head will pad out to to the right.
867 if (MDOC_HEAD
== node
->type
)
868 p
->flags
|= TERMP_NOBREAK
;
870 p
->flags
|= TERMP_NOLPAD
;
873 if (MDOC_HEAD
== node
->type
)
874 p
->flags
|= TERMP_NOBREAK
;
876 p
->flags
|= TERMP_NOLPAD
;
878 if (MDOC_HEAD
!= node
->type
)
882 * This is ugly. If `-hang' is specified and the body
883 * is a `Bl' or `Bd', then we want basically to nullify
884 * the "overstep" effect in term_flushln() and treat
885 * this as a `-ohang' list instead.
887 if (node
->next
->child
&&
888 (MDOC_Bl
== node
->next
->child
->tok
||
889 MDOC_Bd
== node
->next
->child
->tok
)) {
890 p
->flags
&= ~TERMP_NOBREAK
;
891 p
->flags
&= ~TERMP_NOLPAD
;
893 p
->flags
|= TERMP_HANG
;
896 if (MDOC_HEAD
== node
->type
)
897 p
->flags
|= TERMP_NOBREAK
| TERMP_TWOSPACE
;
899 p
->flags
|= TERMP_NOLPAD
;
901 if (MDOC_HEAD
!= node
->type
)
903 if (NULL
== node
->next
|| NULL
== node
->next
->child
)
904 p
->flags
|= TERMP_DANGLE
;
907 if (MDOC_HEAD
== node
->type
) {
909 if (MDOC_BODY
== node
->next
->type
)
910 p
->flags
&= ~TERMP_NOBREAK
;
912 p
->flags
|= TERMP_NOBREAK
;
914 p
->flags
|= TERMP_NOLPAD
;
918 if (MDOC_HEAD
== node
->type
)
919 p
->flags
|= TERMP_NOBREAK
;
926 * Margin control. Set-head-width lists have their right
927 * margins shortened. The body for these lists has the offset
928 * necessarily lengthened. Everybody gets the offset.
936 * Same stipulation as above, regarding `-hang'. We
937 * don't want to recalculate rmargin and offsets when
938 * using `Bd' or `Bl' within `-hang' overstep lists.
940 if (MDOC_HEAD
== node
->type
&& node
->next
->child
&&
941 (MDOC_Bl
== node
->next
->child
->tok
||
942 MDOC_Bd
== node
->next
->child
->tok
))
955 if (MDOC_HEAD
== node
->type
)
956 p
->rmargin
= p
->offset
+ width
;
962 p
->rmargin
= p
->offset
+ width
;
964 * XXX - this behaviour is not documented: the
965 * right-most column is filled to the right margin.
967 if (MDOC_HEAD
== node
->type
&&
968 MDOC_BODY
== node
->next
->type
)
969 p
->rmargin
= p
->maxrmargin
;
976 * The dash, hyphen, bullet and enum lists all have a special
977 * HEAD character (temporarily bold, in some cases).
980 if (MDOC_HEAD
== node
->type
)
984 term_word(p
, "\\[bu]");
991 term_word(p
, "\\(hy");
995 (pair
->ppair
->ppair
->count
)++;
996 (void)snprintf(buf
, sizeof(buf
), "%d.",
997 pair
->ppair
->ppair
->count
);
1005 * If we're not going to process our children, indicate so here.
1018 if (MDOC_HEAD
== node
->type
)
1022 if (MDOC_BODY
== node
->type
)
1035 termp_it_post(DECL_ARGS
)
1039 if (MDOC_BODY
!= node
->type
&& MDOC_HEAD
!= node
->type
)
1042 type
= arg_listtype(node
->parent
->parent
->parent
);
1051 if (MDOC_BODY
== node
->type
)
1055 if (MDOC_HEAD
== node
->type
)
1063 p
->flags
= pair
->flag
;
1069 termp_nm_pre(DECL_ARGS
)
1072 if (SEC_SYNOPSIS
== node
->sec
)
1075 if (NULL
== node
->child
)
1076 term_word(p
, meta
->name
);
1083 termp_fl_pre(DECL_ARGS
)
1087 term_word(p
, "\\-");
1088 p
->flags
|= TERMP_NOSPACE
;
1095 termp_an_pre(DECL_ARGS
)
1098 if (NULL
== node
->child
)
1102 * XXX: this is poorly documented. If not in the AUTHORS
1103 * section, `An -split' will cause newlines to occur before the
1104 * author name. If in the AUTHORS section, by default, the
1105 * first `An' invocation is nosplit, then all subsequent ones,
1106 * regardless of whether interspersed with other macros/text,
1107 * are split. -split, in this case, will override the condition
1108 * of the implied first -nosplit.
1111 if (node
->sec
== SEC_AUTHORS
) {
1112 if ( ! (TERMP_ANPREC
& p
->flags
)) {
1113 if (TERMP_SPLIT
& p
->flags
)
1117 if (TERMP_NOSPLIT
& p
->flags
)
1123 if (TERMP_SPLIT
& p
->flags
)
1132 termp_an_post(DECL_ARGS
)
1136 if (SEC_AUTHORS
== node
->sec
)
1137 p
->flags
|= TERMP_ANPREC
;
1141 if (arg_getattr(MDOC_Split
, node
) > -1) {
1142 p
->flags
&= ~TERMP_NOSPLIT
;
1143 p
->flags
|= TERMP_SPLIT
;
1145 p
->flags
&= ~TERMP_SPLIT
;
1146 p
->flags
|= TERMP_NOSPLIT
;
1154 termp_ns_pre(DECL_ARGS
)
1157 p
->flags
|= TERMP_NOSPACE
;
1164 termp_rs_pre(DECL_ARGS
)
1167 if (MDOC_BLOCK
== node
->type
&& node
->prev
)
1175 termp_rv_pre(DECL_ARGS
)
1177 const struct mdoc_node
*nn
;
1180 term_word(p
, "The");
1184 for ( ; nn
; nn
= nn
->next
) {
1186 term_word(p
, nn
->string
);
1188 p
->flags
|= TERMP_NOSPACE
;
1189 if (nn
->next
&& NULL
== nn
->next
->next
)
1190 term_word(p
, "(), and");
1192 term_word(p
, "(),");
1197 if (node
->child
->next
)
1198 term_word(p
, "functions return");
1200 term_word(p
, "function returns");
1202 term_word(p
, "the value 0 if successful; otherwise the value "
1203 "-1 is returned and the global variable");
1206 term_word(p
, "errno");
1209 term_word(p
, "is set to indicate the error.");
1217 termp_ex_pre(DECL_ARGS
)
1219 const struct mdoc_node
*nn
;
1221 term_word(p
, "The");
1225 for ( ; nn
; nn
= nn
->next
) {
1227 term_word(p
, nn
->string
);
1229 p
->flags
|= TERMP_NOSPACE
;
1230 if (nn
->next
&& NULL
== nn
->next
->next
)
1231 term_word(p
, ", and");
1235 p
->flags
&= ~TERMP_NOSPACE
;
1238 if (node
->child
->next
)
1239 term_word(p
, "utilities exit");
1241 term_word(p
, "utility exits");
1243 term_word(p
, "0 on success, and >0 if an error occurs.");
1251 termp_nd_pre(DECL_ARGS
)
1254 if (MDOC_BODY
!= node
->type
)
1257 #if defined(__OpenBSD__) || defined(__linux__)
1258 term_word(p
, "\\(en");
1260 term_word(p
, "\\(em");
1268 termp_bl_post(DECL_ARGS
)
1271 if (MDOC_BLOCK
== node
->type
)
1278 termp_op_post(DECL_ARGS
)
1281 if (MDOC_BODY
!= node
->type
)
1283 p
->flags
|= TERMP_NOSPACE
;
1284 term_word(p
, "\\(rB");
1290 termp_xr_pre(DECL_ARGS
)
1292 const struct mdoc_node
*n
;
1294 assert(node
->child
&& MDOC_TEXT
== node
->child
->type
);
1297 term_word(p
, n
->string
);
1298 if (NULL
== (n
= n
->next
))
1300 p
->flags
|= TERMP_NOSPACE
;
1302 p
->flags
|= TERMP_NOSPACE
;
1303 term_word(p
, n
->string
);
1304 p
->flags
|= TERMP_NOSPACE
;
1312 termp_vt_post(DECL_ARGS
)
1315 if (node
->sec
!= SEC_SYNOPSIS
)
1317 if (node
->next
&& MDOC_Vt
== node
->next
->tok
)
1319 else if (node
->next
)
1326 termp_bold_pre(DECL_ARGS
)
1336 termp_fd_post(DECL_ARGS
)
1339 if (node
->sec
!= SEC_SYNOPSIS
)
1343 if (node
->next
&& MDOC_Fd
!= node
->next
->tok
)
1350 termp_sh_pre(DECL_ARGS
)
1353 * XXX: undocumented: using two `Sh' macros in sequence has no
1354 * vspace between calls, only a newline.
1356 switch (node
->type
) {
1358 if (node
->prev
&& MDOC_Sh
== node
->prev
->tok
)
1359 if (NULL
== node
->prev
->body
->child
)
1378 termp_sh_post(DECL_ARGS
)
1381 switch (node
->type
) {
1397 termp_op_pre(DECL_ARGS
)
1400 switch (node
->type
) {
1402 term_word(p
, "\\(lB");
1403 p
->flags
|= TERMP_NOSPACE
;
1414 termp_bt_pre(DECL_ARGS
)
1417 term_word(p
, "is currently in beta test.");
1424 termp_lb_post(DECL_ARGS
)
1433 termp_ud_pre(DECL_ARGS
)
1436 term_word(p
, "currently under development.");
1443 termp_d1_pre(DECL_ARGS
)
1446 if (MDOC_BLOCK
!= node
->type
)
1449 p
->offset
+= (INDENT
+ 1);
1456 termp_d1_post(DECL_ARGS
)
1459 if (MDOC_BLOCK
!= node
->type
)
1467 termp_aq_pre(DECL_ARGS
)
1470 if (MDOC_BODY
!= node
->type
)
1472 term_word(p
, "\\(la");
1473 p
->flags
|= TERMP_NOSPACE
;
1480 termp_aq_post(DECL_ARGS
)
1483 if (MDOC_BODY
!= node
->type
)
1485 p
->flags
|= TERMP_NOSPACE
;
1486 term_word(p
, "\\(ra");
1492 termp_ft_pre(DECL_ARGS
)
1495 if (SEC_SYNOPSIS
== node
->sec
)
1496 if (node
->prev
&& MDOC_Fo
== node
->prev
->tok
)
1505 termp_ft_post(DECL_ARGS
)
1508 if (SEC_SYNOPSIS
== node
->sec
)
1515 termp_fn_pre(DECL_ARGS
)
1517 const struct mdoc_node
*n
;
1519 assert(node
->child
&& MDOC_TEXT
== node
->child
->type
);
1521 /* FIXME: can be "type funcname" "type varname"... */
1524 term_word(p
, node
->child
->string
);
1527 p
->flags
|= TERMP_NOSPACE
;
1530 for (n
= node
->child
->next
; n
; n
= n
->next
) {
1532 term_word(p
, n
->string
);
1540 if (SEC_SYNOPSIS
== node
->sec
)
1549 termp_fn_post(DECL_ARGS
)
1552 if (node
->sec
== SEC_SYNOPSIS
&& node
->next
)
1559 termp_fa_pre(DECL_ARGS
)
1561 struct mdoc_node
*n
;
1563 if (node
->parent
->tok
!= MDOC_Fo
) {
1568 for (n
= node
->child
; n
; n
= n
->next
) {
1570 term_word(p
, n
->string
);
1576 if (node
->child
&& node
->next
&& node
->next
->tok
== MDOC_Fa
)
1585 termp_bd_pre(DECL_ARGS
)
1590 * This is fairly tricky due primarily to crappy documentation.
1591 * If -ragged or -filled are specified, the block does nothing
1592 * but change the indentation.
1594 * If, on the other hand, -unfilled or -literal are specified,
1595 * then the game changes. Text is printed exactly as entered in
1596 * the display: if a macro line, a newline is appended to the
1597 * line. Blank lines are allowed.
1600 if (MDOC_BLOCK
== node
->type
) {
1601 fmt_block_vspace(p
, node
, node
);
1603 } else if (MDOC_BODY
!= node
->type
)
1606 assert(node
->parent
->args
);
1608 for (type
= -1, i
= 0; -1 == type
&&
1609 i
< (int)node
->parent
->args
->argc
; i
++) {
1610 switch (node
->parent
->args
->argv
[i
].arg
) {
1615 case (MDOC_Unfilled
):
1617 case (MDOC_Literal
):
1618 type
= node
->parent
->args
->argv
[i
].arg
;
1627 i
= arg_getattr(MDOC_Offset
, node
->parent
);
1629 p
->offset
+= arg_offset(&node
->parent
->args
->argv
[i
]);
1632 case (MDOC_Literal
):
1634 case (MDOC_Unfilled
):
1640 for (node
= node
->child
; node
; node
= node
->next
) {
1641 p
->flags
|= TERMP_NOSPACE
;
1642 print_node(p
, pair
, meta
, node
);
1653 termp_bd_post(DECL_ARGS
)
1656 if (MDOC_BODY
!= node
->type
)
1658 p
->flags
|= TERMP_NOSPACE
;
1665 termp_qq_pre(DECL_ARGS
)
1668 if (MDOC_BODY
!= node
->type
)
1671 p
->flags
|= TERMP_NOSPACE
;
1678 termp_qq_post(DECL_ARGS
)
1681 if (MDOC_BODY
!= node
->type
)
1683 p
->flags
|= TERMP_NOSPACE
;
1690 termp_bx_post(DECL_ARGS
)
1694 p
->flags
|= TERMP_NOSPACE
;
1695 term_word(p
, "BSD");
1701 termp_xx_pre(DECL_ARGS
)
1706 switch (node
->tok
) {
1711 pp
= "DragonFlyBSD";
1737 termp_sq_pre(DECL_ARGS
)
1740 if (MDOC_BODY
!= node
->type
)
1742 term_word(p
, "\\(oq");
1743 p
->flags
|= TERMP_NOSPACE
;
1750 termp_sq_post(DECL_ARGS
)
1753 if (MDOC_BODY
!= node
->type
)
1755 p
->flags
|= TERMP_NOSPACE
;
1756 term_word(p
, "\\(aq");
1762 termp_pf_pre(DECL_ARGS
)
1765 p
->flags
|= TERMP_IGNDELIM
;
1772 termp_pf_post(DECL_ARGS
)
1775 p
->flags
&= ~TERMP_IGNDELIM
;
1776 p
->flags
|= TERMP_NOSPACE
;
1782 termp_ss_pre(DECL_ARGS
)
1785 switch (node
->type
) {
1793 p
->offset
= HALFINDENT
;
1805 termp_ss_post(DECL_ARGS
)
1808 if (MDOC_HEAD
== node
->type
)
1815 termp_cd_pre(DECL_ARGS
)
1826 termp_in_pre(DECL_ARGS
)
1830 if (SEC_SYNOPSIS
== node
->sec
)
1831 term_word(p
, "#include");
1834 p
->flags
|= TERMP_NOSPACE
;
1841 termp_in_post(DECL_ARGS
)
1848 if (SEC_SYNOPSIS
!= node
->sec
)
1853 * XXX Not entirely correct. If `.In foo bar' is specified in
1854 * the SYNOPSIS section, then it produces a single break after
1855 * the <foo>; mandoc asserts a vertical space. Since this
1856 * construction is rarely used, I think it's fine.
1858 if (node
->next
&& MDOC_In
!= node
->next
->tok
)
1865 termp_sp_pre(DECL_ARGS
)
1869 switch (node
->tok
) {
1871 len
= node
->child
? atoi(node
->child
->string
) : 1;
1883 for (i
= 0; i
< len
; i
++)
1892 termp_brq_pre(DECL_ARGS
)
1895 if (MDOC_BODY
!= node
->type
)
1897 term_word(p
, "\\(lC");
1898 p
->flags
|= TERMP_NOSPACE
;
1905 termp_brq_post(DECL_ARGS
)
1908 if (MDOC_BODY
!= node
->type
)
1910 p
->flags
|= TERMP_NOSPACE
;
1911 term_word(p
, "\\(rC");
1917 termp_bq_pre(DECL_ARGS
)
1920 if (MDOC_BODY
!= node
->type
)
1922 term_word(p
, "\\(lB");
1923 p
->flags
|= TERMP_NOSPACE
;
1930 termp_bq_post(DECL_ARGS
)
1933 if (MDOC_BODY
!= node
->type
)
1935 p
->flags
|= TERMP_NOSPACE
;
1936 term_word(p
, "\\(rB");
1942 termp_pq_pre(DECL_ARGS
)
1945 if (MDOC_BODY
!= node
->type
)
1947 term_word(p
, "\\&(");
1948 p
->flags
|= TERMP_NOSPACE
;
1955 termp_pq_post(DECL_ARGS
)
1958 if (MDOC_BODY
!= node
->type
)
1966 termp_fo_pre(DECL_ARGS
)
1968 const struct mdoc_node
*n
;
1970 if (MDOC_BODY
== node
->type
) {
1971 p
->flags
|= TERMP_NOSPACE
;
1973 p
->flags
|= TERMP_NOSPACE
;
1975 } else if (MDOC_HEAD
!= node
->type
)
1979 for (n
= node
->child
; n
; n
= n
->next
) {
1980 assert(MDOC_TEXT
== n
->type
);
1981 term_word(p
, n
->string
);
1991 termp_fo_post(DECL_ARGS
)
1994 if (MDOC_BODY
!= node
->type
)
1996 p
->flags
|= TERMP_NOSPACE
;
1998 p
->flags
|= TERMP_NOSPACE
;
2006 termp_bf_pre(DECL_ARGS
)
2008 const struct mdoc_node
*n
;
2010 if (MDOC_HEAD
== node
->type
)
2012 else if (MDOC_BLOCK
!= node
->type
)
2015 if (NULL
== (n
= node
->head
->child
)) {
2016 if (arg_hasattr(MDOC_Emphasis
, node
))
2018 else if (arg_hasattr(MDOC_Symbolic
, node
))
2024 assert(MDOC_TEXT
== n
->type
);
2025 if (0 == strcmp("Em", n
->string
))
2027 else if (0 == strcmp("Sy", n
->string
))
2036 termp_sm_pre(DECL_ARGS
)
2039 assert(node
->child
&& MDOC_TEXT
== node
->child
->type
);
2040 if (0 == strcmp("on", node
->child
->string
)) {
2041 p
->flags
&= ~TERMP_NONOSPACE
;
2042 p
->flags
&= ~TERMP_NOSPACE
;
2044 p
->flags
|= TERMP_NONOSPACE
;
2052 termp_ap_pre(DECL_ARGS
)
2055 p
->flags
|= TERMP_NOSPACE
;
2056 term_word(p
, "\\(aq");
2057 p
->flags
|= TERMP_NOSPACE
;
2064 termp____post(DECL_ARGS
)
2067 p
->flags
|= TERMP_NOSPACE
;
2068 term_word(p
, node
->next
? "," : ".");
2074 termp_lk_pre(DECL_ARGS
)
2076 const struct mdoc_node
*n
;
2078 assert(node
->child
);
2081 if (NULL
== n
->next
) {
2087 term_word(p
, n
->string
);
2088 p
->flags
|= TERMP_NOSPACE
;
2093 for (n
= n
->next
; n
; n
= n
->next
)
2094 term_word(p
, n
->string
);
2103 termp_under_pre(DECL_ARGS
)