]>
git.cameronkatri.com Git - mandoc.git/blob - mdoc_term.c
1 /* $Id: mdoc_term.c,v 1.87 2009/10/07 12:20:20 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 *m, \
76 const struct mdoc_node *n
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__t_pre(DECL_ARGS
);
110 static int termp_an_pre(DECL_ARGS
);
111 static int termp_ap_pre(DECL_ARGS
);
112 static int termp_aq_pre(DECL_ARGS
);
113 static int termp_bd_pre(DECL_ARGS
);
114 static int termp_bf_pre(DECL_ARGS
);
115 static int termp_bold_pre(DECL_ARGS
);
116 static int termp_bq_pre(DECL_ARGS
);
117 static int termp_brq_pre(DECL_ARGS
);
118 static int termp_bt_pre(DECL_ARGS
);
119 static int termp_cd_pre(DECL_ARGS
);
120 static int termp_d1_pre(DECL_ARGS
);
121 static int termp_dq_pre(DECL_ARGS
);
122 static int termp_ex_pre(DECL_ARGS
);
123 static int termp_fa_pre(DECL_ARGS
);
124 static int termp_fl_pre(DECL_ARGS
);
125 static int termp_fn_pre(DECL_ARGS
);
126 static int termp_fo_pre(DECL_ARGS
);
127 static int termp_ft_pre(DECL_ARGS
);
128 static int termp_in_pre(DECL_ARGS
);
129 static int termp_it_pre(DECL_ARGS
);
130 static int termp_lk_pre(DECL_ARGS
);
131 static int termp_nd_pre(DECL_ARGS
);
132 static int termp_nm_pre(DECL_ARGS
);
133 static int termp_ns_pre(DECL_ARGS
);
134 static int termp_op_pre(DECL_ARGS
);
135 static int termp_pf_pre(DECL_ARGS
);
136 static int termp_pq_pre(DECL_ARGS
);
137 static int termp_qq_pre(DECL_ARGS
);
138 static int termp_rs_pre(DECL_ARGS
);
139 static int termp_rv_pre(DECL_ARGS
);
140 static int termp_sh_pre(DECL_ARGS
);
141 static int termp_sm_pre(DECL_ARGS
);
142 static int termp_sp_pre(DECL_ARGS
);
143 static int termp_sq_pre(DECL_ARGS
);
144 static int termp_ss_pre(DECL_ARGS
);
145 static int termp_under_pre(DECL_ARGS
);
146 static int termp_ud_pre(DECL_ARGS
);
147 static int termp_xr_pre(DECL_ARGS
);
148 static int termp_xx_pre(DECL_ARGS
);
150 static const struct termact termacts
[MDOC_MAX
] = {
151 { termp_ap_pre
, NULL
}, /* Ap */
152 { NULL
, NULL
}, /* Dd */
153 { NULL
, NULL
}, /* Dt */
154 { NULL
, NULL
}, /* Os */
155 { termp_sh_pre
, termp_sh_post
}, /* Sh */
156 { termp_ss_pre
, termp_ss_post
}, /* Ss */
157 { termp_sp_pre
, NULL
}, /* Pp */
158 { termp_d1_pre
, termp_d1_post
}, /* D1 */
159 { termp_d1_pre
, termp_d1_post
}, /* Dl */
160 { termp_bd_pre
, termp_bd_post
}, /* Bd */
161 { NULL
, NULL
}, /* Ed */
162 { NULL
, termp_bl_post
}, /* Bl */
163 { NULL
, NULL
}, /* El */
164 { termp_it_pre
, termp_it_post
}, /* It */
165 { NULL
, NULL
}, /* Ad */
166 { termp_an_pre
, termp_an_post
}, /* An */
167 { termp_under_pre
, NULL
}, /* Ar */
168 { termp_cd_pre
, NULL
}, /* Cd */
169 { termp_bold_pre
, NULL
}, /* Cm */
170 { NULL
, NULL
}, /* Dv */
171 { NULL
, NULL
}, /* Er */
172 { NULL
, NULL
}, /* Ev */
173 { termp_ex_pre
, NULL
}, /* Ex */
174 { termp_fa_pre
, NULL
}, /* Fa */
175 { termp_bold_pre
, termp_fd_post
}, /* Fd */
176 { termp_fl_pre
, NULL
}, /* Fl */
177 { termp_fn_pre
, termp_fn_post
}, /* Fn */
178 { termp_ft_pre
, termp_ft_post
}, /* Ft */
179 { termp_bold_pre
, NULL
}, /* Ic */
180 { termp_in_pre
, termp_in_post
}, /* In */
181 { NULL
, NULL
}, /* Li */
182 { termp_nd_pre
, NULL
}, /* Nd */
183 { termp_nm_pre
, NULL
}, /* Nm */
184 { termp_op_pre
, termp_op_post
}, /* Op */
185 { NULL
, NULL
}, /* Ot */
186 { termp_under_pre
, NULL
}, /* Pa */
187 { termp_rv_pre
, NULL
}, /* Rv */
188 { NULL
, NULL
}, /* St */
189 { termp_under_pre
, NULL
}, /* Va */
190 { termp_under_pre
, termp_vt_post
}, /* Vt */
191 { termp_xr_pre
, NULL
}, /* Xr */
192 { NULL
, termp____post
}, /* %A */
193 { termp_under_pre
, termp____post
}, /* %B */
194 { NULL
, termp____post
}, /* %D */
195 { termp_under_pre
, termp____post
}, /* %I */
196 { termp_under_pre
, termp____post
}, /* %J */
197 { NULL
, termp____post
}, /* %N */
198 { NULL
, termp____post
}, /* %O */
199 { NULL
, termp____post
}, /* %P */
200 { NULL
, termp____post
}, /* %R */
201 { termp__t_pre
, termp____post
}, /* %T */
202 { NULL
, termp____post
}, /* %V */
203 { NULL
, NULL
}, /* Ac */
204 { termp_aq_pre
, termp_aq_post
}, /* Ao */
205 { termp_aq_pre
, termp_aq_post
}, /* Aq */
206 { NULL
, NULL
}, /* At */
207 { NULL
, NULL
}, /* Bc */
208 { termp_bf_pre
, NULL
}, /* Bf */
209 { termp_bq_pre
, termp_bq_post
}, /* Bo */
210 { termp_bq_pre
, termp_bq_post
}, /* Bq */
211 { termp_xx_pre
, NULL
}, /* Bsx */
212 { NULL
, termp_bx_post
}, /* Bx */
213 { NULL
, NULL
}, /* Db */
214 { NULL
, NULL
}, /* Dc */
215 { termp_dq_pre
, termp_dq_post
}, /* Do */
216 { termp_dq_pre
, termp_dq_post
}, /* Dq */
217 { NULL
, NULL
}, /* Ec */
218 { NULL
, NULL
}, /* Ef */
219 { termp_under_pre
, NULL
}, /* Em */
220 { NULL
, NULL
}, /* Eo */
221 { termp_xx_pre
, NULL
}, /* Fx */
222 { termp_bold_pre
, NULL
}, /* Ms */ /* FIXME: convert to symbol? */
223 { NULL
, NULL
}, /* No */
224 { termp_ns_pre
, NULL
}, /* Ns */
225 { termp_xx_pre
, NULL
}, /* Nx */
226 { termp_xx_pre
, NULL
}, /* Ox */
227 { NULL
, NULL
}, /* Pc */
228 { termp_pf_pre
, termp_pf_post
}, /* Pf */
229 { termp_pq_pre
, termp_pq_post
}, /* Po */
230 { termp_pq_pre
, termp_pq_post
}, /* Pq */
231 { NULL
, NULL
}, /* Qc */
232 { termp_sq_pre
, termp_sq_post
}, /* Ql */
233 { termp_qq_pre
, termp_qq_post
}, /* Qo */
234 { termp_qq_pre
, termp_qq_post
}, /* Qq */
235 { NULL
, NULL
}, /* Re */
236 { termp_rs_pre
, NULL
}, /* Rs */
237 { NULL
, NULL
}, /* Sc */
238 { termp_sq_pre
, termp_sq_post
}, /* So */
239 { termp_sq_pre
, termp_sq_post
}, /* Sq */
240 { termp_sm_pre
, NULL
}, /* Sm */
241 { termp_under_pre
, NULL
}, /* Sx */
242 { termp_bold_pre
, NULL
}, /* Sy */
243 { NULL
, NULL
}, /* Tn */
244 { termp_xx_pre
, NULL
}, /* Ux */
245 { NULL
, NULL
}, /* Xc */
246 { NULL
, NULL
}, /* Xo */
247 { termp_fo_pre
, termp_fo_post
}, /* Fo */
248 { NULL
, NULL
}, /* Fc */
249 { termp_op_pre
, termp_op_post
}, /* Oo */
250 { NULL
, NULL
}, /* Oc */
251 { NULL
, NULL
}, /* Bk */
252 { NULL
, NULL
}, /* Ek */
253 { termp_bt_pre
, NULL
}, /* Bt */
254 { NULL
, NULL
}, /* Hf */
255 { NULL
, NULL
}, /* Fr */
256 { termp_ud_pre
, NULL
}, /* Ud */
257 { NULL
, termp_lb_post
}, /* Lb */
258 { termp_sp_pre
, NULL
}, /* Lp */
259 { termp_lk_pre
, NULL
}, /* Lk */
260 { termp_under_pre
, NULL
}, /* Mt */
261 { termp_brq_pre
, termp_brq_post
}, /* Brq */
262 { termp_brq_pre
, termp_brq_post
}, /* Bro */
263 { NULL
, NULL
}, /* Brc */
264 { NULL
, termp____post
}, /* %C */
265 { NULL
, NULL
}, /* Es */ /* TODO */
266 { NULL
, NULL
}, /* En */ /* TODO */
267 { termp_xx_pre
, NULL
}, /* Dx */
268 { NULL
, termp____post
}, /* %Q */
269 { termp_sp_pre
, NULL
}, /* br */
270 { termp_sp_pre
, NULL
}, /* sp */
274 extern size_t strlcpy(char *, const char *, size_t);
275 extern size_t strlcat(char *, const char *, size_t);
278 static int arg_hasattr(int, const struct mdoc_node
*);
279 static int arg_getattrs(const int *, int *, size_t,
280 const struct mdoc_node
*);
281 static int arg_getattr(int, const struct mdoc_node
*);
282 static size_t arg_offset(const struct mdoc_argv
*);
283 static size_t arg_width(const struct mdoc_argv
*, int);
284 static int arg_listtype(const struct mdoc_node
*);
285 static void fmt_block_vspace(struct termp
*,
286 const struct mdoc_node
*,
287 const struct mdoc_node
*);
288 static void print_node(DECL_ARGS
);
289 static void print_head(DECL_ARGS
);
290 static void print_body(DECL_ARGS
);
291 static void print_foot(DECL_ARGS
);
295 mdoc_run(struct termp
*p
, const struct mdoc
*mdoc
)
297 const struct mdoc_node
*n
;
298 const struct mdoc_meta
*m
;
303 print_head(p
, NULL
, m
, n
);
305 print_body(p
, NULL
, m
, n
->child
);
306 print_foot(p
, NULL
, m
, n
);
311 print_body(DECL_ARGS
)
314 print_node(p
, pair
, m
, n
);
316 print_body(p
, pair
, m
, n
->next
);
322 print_node(DECL_ARGS
)
324 int chld
, bold
, under
;
325 struct termpair npair
;
326 size_t offset
, rmargin
;
330 rmargin
= p
->rmargin
;
334 bzero(&npair
, sizeof(struct termpair
));
337 if (MDOC_TEXT
!= n
->type
) {
338 if (termacts
[n
->tok
].pre
)
339 chld
= (*termacts
[n
->tok
].pre
)(p
, &npair
, m
, n
);
341 term_word(p
, n
->string
);
342 if (chld
&& n
->child
)
343 print_body(p
, &npair
, m
, n
->child
);
346 * XXX - if bold/under were to span scopes, this wouldn't be
347 * possible, but because decoration is always in-scope, we can
348 * get away with this.
354 if (MDOC_TEXT
!= n
->type
)
355 if (termacts
[n
->tok
].post
)
356 (*termacts
[n
->tok
].post
)(p
, &npair
, m
, n
);
359 p
->rmargin
= rmargin
;
365 print_foot(DECL_ARGS
)
371 * Output the footer in new-groff style, that is, three columns
372 * with the middle being the manual date and flanking columns
373 * being the operating system:
378 if (NULL
== (buf
= malloc(p
->rmargin
)))
379 err(EXIT_FAILURE
, "malloc");
380 if (NULL
== (os
= malloc(p
->rmargin
)))
381 err(EXIT_FAILURE
, "malloc");
383 tm
= localtime(&m
->date
);
385 if (0 == strftime(buf
, p
->rmargin
, "%B %e, %Y", tm
))
386 err(EXIT_FAILURE
, "strftime");
388 (void)strlcpy(os
, m
->os
, p
->rmargin
);
393 p
->rmargin
= (p
->maxrmargin
- strlen(buf
) + 1) / 2;
394 p
->flags
|= TERMP_NOSPACE
| TERMP_NOBREAK
;
399 p
->offset
= p
->rmargin
;
400 p
->rmargin
= p
->maxrmargin
- strlen(os
);
401 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
406 p
->offset
= p
->rmargin
;
407 p
->rmargin
= p
->maxrmargin
;
408 p
->flags
&= ~TERMP_NOBREAK
;
409 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
415 p
->rmargin
= p
->maxrmargin
;
423 /* FIXME: put in utility library. */
426 print_head(DECL_ARGS
)
430 p
->rmargin
= p
->maxrmargin
;
433 if (NULL
== (buf
= malloc(p
->rmargin
)))
434 err(EXIT_FAILURE
, "malloc");
435 if (NULL
== (title
= malloc(p
->rmargin
)))
436 err(EXIT_FAILURE
, "malloc");
439 * The header is strange. It has three components, which are
440 * really two with the first duplicated. It goes like this:
442 * IDENTIFIER TITLE IDENTIFIER
444 * The IDENTIFIER is NAME(SECTION), which is the command-name
445 * (if given, or "unknown" if not) followed by the manual page
446 * section. These are given in `Dt'. The TITLE is a free-form
447 * string depending on the manual volume. If not specified, it
448 * switches on the manual section.
452 (void)strlcpy(buf
, m
->vol
, p
->rmargin
);
455 (void)strlcat(buf
, " (", p
->rmargin
);
456 (void)strlcat(buf
, m
->arch
, p
->rmargin
);
457 (void)strlcat(buf
, ")", p
->rmargin
);
460 snprintf(title
, p
->rmargin
, "%s(%d)", m
->title
, m
->msec
);
463 p
->rmargin
= (p
->maxrmargin
- strlen(buf
) + 1) / 2;
464 p
->flags
|= TERMP_NOBREAK
| TERMP_NOSPACE
;
469 p
->offset
= p
->rmargin
;
470 p
->rmargin
= p
->maxrmargin
- strlen(title
);
471 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
476 p
->offset
= p
->rmargin
;
477 p
->rmargin
= p
->maxrmargin
;
478 p
->flags
&= ~TERMP_NOBREAK
;
479 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
485 p
->rmargin
= p
->maxrmargin
;
486 p
->flags
&= ~TERMP_NOSPACE
;
493 /* FIXME: put in utility file for front-ends. */
495 arg_width(const struct mdoc_argv
*arg
, int pos
)
500 assert(pos
< (int)arg
->sz
&& pos
>= 0);
501 assert(arg
->value
[pos
]);
505 if (0 == (len
= (int)strlen(p
)))
508 for (i
= 0; i
< len
- 1; i
++)
509 if ( ! isdigit((u_char
)p
[i
]))
513 if ('n' == p
[len
- 1] || 'm' == p
[len
- 1])
514 return((size_t)atoi(p
) + 2);
516 return((size_t)len
+ 2);
520 /* FIXME: put in utility file for front-ends. */
522 arg_listtype(const struct mdoc_node
*n
)
526 assert(MDOC_BLOCK
== n
->type
);
528 len
= (int)(n
->args
? n
->args
->argc
: 0);
530 for (i
= 0; i
< len
; i
++)
531 switch (n
->args
->argv
[i
].arg
) {
553 return(n
->args
->argv
[i
].arg
);
562 /* FIXME: put in utility file for front-ends. */
564 arg_offset(const struct mdoc_argv
*arg
)
572 if (0 == strcmp(p
, "left"))
574 if (0 == strcmp(p
, "indent"))
576 if (0 == strcmp(p
, "indent-two"))
577 return((INDENT
+ 1) * 2);
579 if (0 == (len
= (int)strlen(p
)))
582 for (i
= 0; i
< len
- 1; i
++)
583 if ( ! isdigit((u_char
)p
[i
]))
587 if ('n' == p
[len
- 1] || 'm' == p
[len
- 1])
588 return((size_t)atoi(p
));
595 arg_hasattr(int arg
, const struct mdoc_node
*n
)
598 return(-1 != arg_getattr(arg
, n
));
603 arg_getattr(int v
, const struct mdoc_node
*n
)
607 return(arg_getattrs(&v
, &val
, 1, n
) ? val
: -1);
612 arg_getattrs(const int *keys
, int *vals
,
613 size_t sz
, const struct mdoc_node
*n
)
620 for (k
= i
= 0; i
< (int)n
->args
->argc
; i
++)
621 for (j
= 0; j
< (int)sz
; j
++)
622 if (n
->args
->argv
[i
].arg
== keys
[j
]) {
632 fmt_block_vspace(struct termp
*p
,
633 const struct mdoc_node
*bl
,
634 const struct mdoc_node
*n
)
636 const struct mdoc_node
*nn
;
639 if (arg_hasattr(MDOC_Compact
, bl
))
642 /* Do not vspace directly after Ss/Sh. */
644 for (nn
= n
; nn
; nn
= nn
->parent
) {
645 if (MDOC_BLOCK
!= nn
->type
)
647 if (MDOC_Ss
== nn
->tok
)
649 if (MDOC_Sh
== nn
->tok
)
651 if (NULL
== nn
->prev
)
656 /* A `-column' does not assert vspace within the list. */
658 if (MDOC_Bl
== bl
->tok
&& arg_hasattr(MDOC_Column
, bl
))
659 if (n
->prev
&& MDOC_It
== n
->prev
->tok
)
662 /* A `-diag' without body does not vspace. */
664 if (MDOC_Bl
== bl
->tok
&& arg_hasattr(MDOC_Diag
, bl
))
665 if (n
->prev
&& MDOC_It
== n
->prev
->tok
) {
666 assert(n
->prev
->body
);
667 if (NULL
== n
->prev
->body
->child
)
677 termp_dq_pre(DECL_ARGS
)
680 if (MDOC_BODY
!= n
->type
)
683 term_word(p
, "\\(lq");
684 p
->flags
|= TERMP_NOSPACE
;
691 termp_dq_post(DECL_ARGS
)
694 if (MDOC_BODY
!= n
->type
)
697 p
->flags
|= TERMP_NOSPACE
;
698 term_word(p
, "\\(rq");
704 termp_it_pre(DECL_ARGS
)
706 const struct mdoc_node
*bl
, *nn
;
708 int i
, type
, keys
[3], vals
[3];
709 size_t width
, offset
;
711 if (MDOC_BLOCK
== n
->type
) {
712 fmt_block_vspace(p
, n
->parent
->parent
, n
);
716 bl
= n
->parent
->parent
->parent
;
718 /* Save parent attributes. */
720 pair
->flag
= p
->flags
;
722 /* Get list width and offset. */
724 keys
[0] = MDOC_Width
;
725 keys
[1] = MDOC_Offset
;
726 keys
[2] = MDOC_Column
;
728 vals
[0] = vals
[1] = vals
[2] = -1;
732 (void)arg_getattrs(keys
, vals
, 3, bl
);
734 type
= arg_listtype(bl
);
737 /* Calculate real width and offset. */
741 if (MDOC_BODY
== n
->type
)
744 * Work around groff's column handling. The offset is
745 * equal to the sum of all widths leading to the current
746 * column (plus the -offset value). If this column
747 * exceeds the stated number of columns, the width is
748 * set as 0, else it's the stated column width (later
749 * the 0 will be adjusted to default 10 or, if in the
750 * last column case, set to stretch to the margin).
752 for (i
= 0, nn
= n
->prev
; nn
&&
753 i
< (int)bl
->args
->argv
[vals
[2]].sz
;
756 (&bl
->args
->argv
[vals
[2]], i
);
758 /* Whether exceeds maximum column. */
759 if (i
< (int)bl
->args
->argv
[vals
[2]].sz
)
760 width
= arg_width(&bl
->args
->argv
[vals
[2]], i
);
765 offset
+= arg_offset(&bl
->args
->argv
[vals
[1]]);
769 width
= arg_width(&bl
->args
->argv
[vals
[0]], 0);
771 offset
+= arg_offset(&bl
->args
->argv
[vals
[1]]);
776 * List-type can override the width in the case of fixed-head
777 * values (bullet, dash/hyphen, enum). Tags need a non-zero
809 * Whitespace control. Inset bodies need an initial space,
810 * while diagonal bodies need two.
813 p
->flags
|= TERMP_NOSPACE
;
817 if (MDOC_BODY
== n
->type
)
818 term_word(p
, "\\ \\ ");
821 if (MDOC_BODY
== n
->type
)
828 p
->flags
|= TERMP_NOSPACE
;
832 if (MDOC_HEAD
== n
->type
)
840 * Pad and break control. This is the tricker part. Lists with
841 * set right-margins for the head get TERMP_NOBREAK because, if
842 * they overrun the margin, they wrap to the new margin.
843 * Correspondingly, the body for these types don't left-pad, as
844 * the head will pad out to to the right.
855 if (MDOC_HEAD
== n
->type
)
856 p
->flags
|= TERMP_NOBREAK
;
858 p
->flags
|= TERMP_NOLPAD
;
861 if (MDOC_HEAD
== n
->type
)
862 p
->flags
|= TERMP_NOBREAK
;
864 p
->flags
|= TERMP_NOLPAD
;
866 if (MDOC_HEAD
!= n
->type
)
870 * This is ugly. If `-hang' is specified and the body
871 * is a `Bl' or `Bd', then we want basically to nullify
872 * the "overstep" effect in term_flushln() and treat
873 * this as a `-ohang' list instead.
875 if (n
->next
->child
&&
876 (MDOC_Bl
== n
->next
->child
->tok
||
877 MDOC_Bd
== n
->next
->child
->tok
)) {
878 p
->flags
&= ~TERMP_NOBREAK
;
879 p
->flags
&= ~TERMP_NOLPAD
;
881 p
->flags
|= TERMP_HANG
;
884 if (MDOC_HEAD
== n
->type
)
885 p
->flags
|= TERMP_NOBREAK
| TERMP_TWOSPACE
;
887 p
->flags
|= TERMP_NOLPAD
;
889 if (MDOC_HEAD
!= n
->type
)
891 if (NULL
== n
->next
|| NULL
== n
->next
->child
)
892 p
->flags
|= TERMP_DANGLE
;
895 if (MDOC_HEAD
== n
->type
) {
897 if (MDOC_BODY
== n
->next
->type
)
898 p
->flags
&= ~TERMP_NOBREAK
;
900 p
->flags
|= TERMP_NOBREAK
;
902 p
->flags
|= TERMP_NOLPAD
;
906 if (MDOC_HEAD
== n
->type
)
907 p
->flags
|= TERMP_NOBREAK
;
914 * Margin control. Set-head-width lists have their right
915 * margins shortened. The body for these lists has the offset
916 * necessarily lengthened. Everybody gets the offset.
924 * Same stipulation as above, regarding `-hang'. We
925 * don't want to recalculate rmargin and offsets when
926 * using `Bd' or `Bl' within `-hang' overstep lists.
928 if (MDOC_HEAD
== n
->type
&& n
->next
->child
&&
929 (MDOC_Bl
== n
->next
->child
->tok
||
930 MDOC_Bd
== n
->next
->child
->tok
))
943 if (MDOC_HEAD
== n
->type
)
944 p
->rmargin
= p
->offset
+ width
;
950 p
->rmargin
= p
->offset
+ width
;
952 * XXX - this behaviour is not documented: the
953 * right-most column is filled to the right margin.
955 if (MDOC_HEAD
== n
->type
&&
956 MDOC_BODY
== n
->next
->type
)
957 p
->rmargin
= p
->maxrmargin
;
964 * The dash, hyphen, bullet and enum lists all have a special
965 * HEAD character (temporarily bold, in some cases).
968 if (MDOC_HEAD
== n
->type
)
972 term_word(p
, "\\[bu]");
979 term_word(p
, "\\(hy");
983 (pair
->ppair
->ppair
->count
)++;
984 (void)snprintf(buf
, sizeof(buf
), "%d.",
985 pair
->ppair
->ppair
->count
);
993 * If we're not going to process our children, indicate so here.
1006 if (MDOC_HEAD
== n
->type
)
1010 if (MDOC_BODY
== n
->type
)
1023 termp_it_post(DECL_ARGS
)
1027 if (MDOC_BODY
!= n
->type
&& MDOC_HEAD
!= n
->type
)
1030 type
= arg_listtype(n
->parent
->parent
->parent
);
1039 if (MDOC_BODY
== n
->type
)
1043 if (MDOC_HEAD
== n
->type
)
1051 p
->flags
= pair
->flag
;
1057 termp_nm_pre(DECL_ARGS
)
1060 if (SEC_SYNOPSIS
== n
->sec
)
1063 if (NULL
== n
->child
)
1064 term_word(p
, m
->name
);
1071 termp_fl_pre(DECL_ARGS
)
1075 term_word(p
, "\\-");
1076 p
->flags
|= TERMP_NOSPACE
;
1083 termp_an_pre(DECL_ARGS
)
1086 if (NULL
== n
->child
)
1090 * If not in the AUTHORS section, `An -split' will cause
1091 * newlines to occur before the author name. If in the AUTHORS
1092 * section, by default, the first `An' invocation is nosplit,
1093 * then all subsequent ones, regardless of whether interspersed
1094 * with other macros/text, are split. -split, in this case,
1095 * will override the condition of the implied first -nosplit.
1098 if (n
->sec
== SEC_AUTHORS
) {
1099 if ( ! (TERMP_ANPREC
& p
->flags
)) {
1100 if (TERMP_SPLIT
& p
->flags
)
1104 if (TERMP_NOSPLIT
& p
->flags
)
1110 if (TERMP_SPLIT
& p
->flags
)
1119 termp_an_post(DECL_ARGS
)
1123 if (SEC_AUTHORS
== n
->sec
)
1124 p
->flags
|= TERMP_ANPREC
;
1128 if (arg_getattr(MDOC_Split
, n
) > -1) {
1129 p
->flags
&= ~TERMP_NOSPLIT
;
1130 p
->flags
|= TERMP_SPLIT
;
1132 p
->flags
&= ~TERMP_SPLIT
;
1133 p
->flags
|= TERMP_NOSPLIT
;
1141 termp_ns_pre(DECL_ARGS
)
1144 p
->flags
|= TERMP_NOSPACE
;
1151 termp_rs_pre(DECL_ARGS
)
1154 if (SEC_SEE_ALSO
!= n
->sec
)
1156 if (MDOC_BLOCK
== n
->type
&& n
->prev
)
1164 termp_rv_pre(DECL_ARGS
)
1166 const struct mdoc_node
*nn
;
1169 term_word(p
, "The");
1171 for (nn
= n
->child
; nn
; nn
= nn
->next
) {
1173 term_word(p
, nn
->string
);
1175 p
->flags
|= TERMP_NOSPACE
;
1176 if (nn
->next
&& NULL
== nn
->next
->next
)
1177 term_word(p
, "(), and");
1179 term_word(p
, "(),");
1185 term_word(p
, "functions return");
1187 term_word(p
, "function returns");
1189 term_word(p
, "the value 0 if successful; otherwise the value "
1190 "-1 is returned and the global variable");
1193 term_word(p
, "errno");
1196 term_word(p
, "is set to indicate the error.");
1204 termp_ex_pre(DECL_ARGS
)
1206 const struct mdoc_node
*nn
;
1208 term_word(p
, "The");
1210 for (nn
= n
->child
; nn
; nn
= nn
->next
) {
1212 term_word(p
, nn
->string
);
1214 p
->flags
|= TERMP_NOSPACE
;
1215 if (nn
->next
&& NULL
== nn
->next
->next
)
1216 term_word(p
, ", and");
1220 p
->flags
&= ~TERMP_NOSPACE
;
1224 term_word(p
, "utilities exit");
1226 term_word(p
, "utility exits");
1228 term_word(p
, "0 on success, and >0 if an error occurs.");
1236 termp_nd_pre(DECL_ARGS
)
1239 if (MDOC_BODY
!= n
->type
)
1242 #if defined(__OpenBSD__) || defined(__linux__)
1243 term_word(p
, "\\(en");
1245 term_word(p
, "\\(em");
1253 termp_bl_post(DECL_ARGS
)
1256 if (MDOC_BLOCK
== n
->type
)
1263 termp_op_post(DECL_ARGS
)
1266 if (MDOC_BODY
!= n
->type
)
1268 p
->flags
|= TERMP_NOSPACE
;
1269 term_word(p
, "\\(rB");
1275 termp_xr_pre(DECL_ARGS
)
1277 const struct mdoc_node
*nn
;
1279 assert(n
->child
&& MDOC_TEXT
== n
->child
->type
);
1282 term_word(p
, nn
->string
);
1283 if (NULL
== (nn
= nn
->next
))
1285 p
->flags
|= TERMP_NOSPACE
;
1287 p
->flags
|= TERMP_NOSPACE
;
1288 term_word(p
, nn
->string
);
1289 p
->flags
|= TERMP_NOSPACE
;
1298 termp_vt_post(DECL_ARGS
)
1301 if (n
->sec
!= SEC_SYNOPSIS
)
1303 if (n
->next
&& MDOC_Vt
== n
->next
->tok
)
1312 termp_bold_pre(DECL_ARGS
)
1322 termp_fd_post(DECL_ARGS
)
1325 if (n
->sec
!= SEC_SYNOPSIS
)
1329 if (n
->next
&& MDOC_Fd
!= n
->next
->tok
)
1336 termp_sh_pre(DECL_ARGS
)
1339 /* No vspace between consecutive `Sh' calls. */
1343 if (n
->prev
&& MDOC_Sh
== n
->prev
->tok
)
1344 if (NULL
== n
->prev
->body
->child
)
1363 termp_sh_post(DECL_ARGS
)
1382 termp_op_pre(DECL_ARGS
)
1387 term_word(p
, "\\(lB");
1388 p
->flags
|= TERMP_NOSPACE
;
1399 termp_bt_pre(DECL_ARGS
)
1402 term_word(p
, "is currently in beta test.");
1409 termp_lb_post(DECL_ARGS
)
1412 if (SEC_LIBRARY
== n
->sec
)
1419 termp_ud_pre(DECL_ARGS
)
1422 term_word(p
, "currently under development.");
1429 termp_d1_pre(DECL_ARGS
)
1432 if (MDOC_BLOCK
!= n
->type
)
1435 p
->offset
+= (INDENT
+ 1);
1442 termp_d1_post(DECL_ARGS
)
1445 if (MDOC_BLOCK
!= n
->type
)
1453 termp_aq_pre(DECL_ARGS
)
1456 if (MDOC_BODY
!= n
->type
)
1458 term_word(p
, "\\(la");
1459 p
->flags
|= TERMP_NOSPACE
;
1466 termp_aq_post(DECL_ARGS
)
1469 if (MDOC_BODY
!= n
->type
)
1471 p
->flags
|= TERMP_NOSPACE
;
1472 term_word(p
, "\\(ra");
1478 termp_ft_pre(DECL_ARGS
)
1481 if (SEC_SYNOPSIS
== n
->sec
)
1482 if (n
->prev
&& MDOC_Fo
== n
->prev
->tok
)
1491 termp_ft_post(DECL_ARGS
)
1494 if (SEC_SYNOPSIS
== n
->sec
)
1501 termp_fn_pre(DECL_ARGS
)
1503 const struct mdoc_node
*nn
;
1506 term_word(p
, n
->child
->string
);
1509 p
->flags
|= TERMP_NOSPACE
;
1512 for (nn
= n
->child
->next
; nn
; nn
= nn
->next
) {
1514 term_word(p
, nn
->string
);
1522 if (SEC_SYNOPSIS
== n
->sec
)
1531 termp_fn_post(DECL_ARGS
)
1534 if (n
->sec
== SEC_SYNOPSIS
&& n
->next
)
1541 termp_fa_pre(DECL_ARGS
)
1543 const struct mdoc_node
*nn
;
1545 if (n
->parent
->tok
!= MDOC_Fo
) {
1550 for (nn
= n
->child
; nn
; nn
= nn
->next
) {
1552 term_word(p
, nn
->string
);
1558 if (n
->child
&& n
->next
&& n
->next
->tok
== MDOC_Fa
)
1567 termp_bd_pre(DECL_ARGS
)
1570 const struct mdoc_node
*nn
;
1572 if (MDOC_BLOCK
== n
->type
) {
1573 fmt_block_vspace(p
, n
, n
);
1575 } else if (MDOC_BODY
!= n
->type
)
1580 for (type
= -1, i
= 0; i
< (int)nn
->args
->argc
; i
++) {
1581 switch (nn
->args
->argv
[i
].arg
) {
1586 case (MDOC_Unfilled
):
1588 case (MDOC_Literal
):
1589 type
= nn
->args
->argv
[i
].arg
;
1592 p
->offset
+= arg_offset(&nn
->args
->argv
[i
]);
1600 * If -ragged or -filled are specified, the block does nothing
1601 * but change the indentation. If -unfilled or -literal are
1602 * specified, text is printed exactly as entered in the display:
1603 * for macro lines, a newline is appended to the line. Blank
1604 * lines are allowed.
1608 if (MDOC_Literal
!= type
&& MDOC_Unfilled
!= type
)
1611 for (nn
= n
->child
; nn
; nn
= nn
->next
) {
1612 p
->flags
|= TERMP_NOSPACE
;
1613 print_node(p
, pair
, m
, nn
);
1614 if (NULL
== nn
->next
)
1616 if (nn
->prev
&& nn
->prev
->line
< nn
->line
)
1618 else if (NULL
== nn
->prev
)
1628 termp_bd_post(DECL_ARGS
)
1631 if (MDOC_BODY
!= n
->type
)
1633 p
->flags
|= TERMP_NOSPACE
;
1640 termp_qq_pre(DECL_ARGS
)
1643 if (MDOC_BODY
!= n
->type
)
1646 p
->flags
|= TERMP_NOSPACE
;
1653 termp_qq_post(DECL_ARGS
)
1656 if (MDOC_BODY
!= n
->type
)
1658 p
->flags
|= TERMP_NOSPACE
;
1665 termp_bx_post(DECL_ARGS
)
1669 p
->flags
|= TERMP_NOSPACE
;
1670 term_word(p
, "BSD");
1676 termp_xx_pre(DECL_ARGS
)
1686 pp
= "DragonFlyBSD";
1712 termp_sq_pre(DECL_ARGS
)
1715 if (MDOC_BODY
!= n
->type
)
1717 term_word(p
, "\\(oq");
1718 p
->flags
|= TERMP_NOSPACE
;
1725 termp_sq_post(DECL_ARGS
)
1728 if (MDOC_BODY
!= n
->type
)
1730 p
->flags
|= TERMP_NOSPACE
;
1731 term_word(p
, "\\(aq");
1737 termp_pf_pre(DECL_ARGS
)
1740 p
->flags
|= TERMP_IGNDELIM
;
1747 termp_pf_post(DECL_ARGS
)
1750 p
->flags
&= ~TERMP_IGNDELIM
;
1751 p
->flags
|= TERMP_NOSPACE
;
1757 termp_ss_pre(DECL_ARGS
)
1768 p
->offset
= HALFINDENT
;
1780 termp_ss_post(DECL_ARGS
)
1783 if (MDOC_HEAD
== n
->type
)
1790 termp_cd_pre(DECL_ARGS
)
1801 termp_in_pre(DECL_ARGS
)
1805 if (SEC_SYNOPSIS
== n
->sec
)
1806 term_word(p
, "#include");
1809 p
->flags
|= TERMP_NOSPACE
;
1816 termp_in_post(DECL_ARGS
)
1820 p
->flags
|= TERMP_NOSPACE
;
1824 if (SEC_SYNOPSIS
!= n
->sec
)
1829 * XXX Not entirely correct. If `.In foo bar' is specified in
1830 * the SYNOPSIS section, then it produces a single break after
1831 * the <foo>; mandoc asserts a vertical space. Since this
1832 * construction is rarely used, I think it's fine.
1834 if (n
->next
&& MDOC_In
!= n
->next
->tok
)
1841 termp_sp_pre(DECL_ARGS
)
1847 len
= n
->child
? atoi(n
->child
->string
) : 1;
1859 for (i
= 0; i
< len
; i
++)
1868 termp_brq_pre(DECL_ARGS
)
1871 if (MDOC_BODY
!= n
->type
)
1873 term_word(p
, "\\(lC");
1874 p
->flags
|= TERMP_NOSPACE
;
1881 termp_brq_post(DECL_ARGS
)
1884 if (MDOC_BODY
!= n
->type
)
1886 p
->flags
|= TERMP_NOSPACE
;
1887 term_word(p
, "\\(rC");
1893 termp_bq_pre(DECL_ARGS
)
1896 if (MDOC_BODY
!= n
->type
)
1898 term_word(p
, "\\(lB");
1899 p
->flags
|= TERMP_NOSPACE
;
1906 termp_bq_post(DECL_ARGS
)
1909 if (MDOC_BODY
!= n
->type
)
1911 p
->flags
|= TERMP_NOSPACE
;
1912 term_word(p
, "\\(rB");
1918 termp_pq_pre(DECL_ARGS
)
1921 if (MDOC_BODY
!= n
->type
)
1923 term_word(p
, "\\&(");
1924 p
->flags
|= TERMP_NOSPACE
;
1931 termp_pq_post(DECL_ARGS
)
1934 if (MDOC_BODY
!= n
->type
)
1942 termp_fo_pre(DECL_ARGS
)
1944 const struct mdoc_node
*nn
;
1946 if (MDOC_BODY
== n
->type
) {
1947 p
->flags
|= TERMP_NOSPACE
;
1949 p
->flags
|= TERMP_NOSPACE
;
1951 } else if (MDOC_HEAD
!= n
->type
)
1955 for (nn
= n
->child
; nn
; nn
= nn
->next
) {
1956 assert(MDOC_TEXT
== nn
->type
);
1957 term_word(p
, nn
->string
);
1967 termp_fo_post(DECL_ARGS
)
1970 if (MDOC_BODY
!= n
->type
)
1972 p
->flags
|= TERMP_NOSPACE
;
1974 p
->flags
|= TERMP_NOSPACE
;
1982 termp_bf_pre(DECL_ARGS
)
1984 const struct mdoc_node
*nn
;
1986 if (MDOC_HEAD
== n
->type
)
1988 else if (MDOC_BLOCK
!= n
->type
)
1991 if (NULL
== (nn
= n
->head
->child
)) {
1992 if (arg_hasattr(MDOC_Emphasis
, n
))
1994 else if (arg_hasattr(MDOC_Symbolic
, n
))
2000 assert(MDOC_TEXT
== nn
->type
);
2001 if (0 == strcmp("Em", nn
->string
))
2003 else if (0 == strcmp("Sy", nn
->string
))
2012 termp_sm_pre(DECL_ARGS
)
2015 assert(n
->child
&& MDOC_TEXT
== n
->child
->type
);
2016 if (0 == strcmp("on", n
->child
->string
)) {
2017 p
->flags
&= ~TERMP_NONOSPACE
;
2018 p
->flags
&= ~TERMP_NOSPACE
;
2020 p
->flags
|= TERMP_NONOSPACE
;
2028 termp_ap_pre(DECL_ARGS
)
2031 p
->flags
|= TERMP_NOSPACE
;
2032 term_word(p
, "\\(aq");
2033 p
->flags
|= TERMP_NOSPACE
;
2040 termp____post(DECL_ARGS
)
2043 p
->flags
|= TERMP_NOSPACE
;
2046 term_word(p
, "\\(rq");
2047 p
->flags
|= TERMP_NOSPACE
;
2052 term_word(p
, n
->next
? "," : ".");
2058 termp_lk_pre(DECL_ARGS
)
2060 const struct mdoc_node
*nn
;
2062 if (NULL
== (nn
= n
->child
->next
)) {
2068 term_word(p
, nn
->string
);
2069 p
->flags
|= TERMP_NOSPACE
;
2074 for (nn
= nn
->next
; nn
; nn
= nn
->next
)
2075 term_word(p
, nn
->string
);
2084 termp_under_pre(DECL_ARGS
)
2094 termp__t_pre(DECL_ARGS
)
2097 term_word(p
, "\\(lq");
2098 p
->flags
|= TERMP_NOSPACE
;