]>
git.cameronkatri.com Git - mandoc.git/blob - mdoc_term.c
1 /* $Id: mdoc_term.c,v 1.100 2009/10/31 06:50:25 kristaps Exp $ */
3 * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@kth.se>
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #include <sys/types.h>
35 struct termpair
*ppair
;
40 #define DECL_ARGS struct termp *p, \
41 struct termpair *pair, \
42 const struct mdoc_meta *m, \
43 const struct mdoc_node *n
46 int (*pre
)(DECL_ARGS
);
47 void (*post
)(DECL_ARGS
);
50 static size_t a2width(const struct mdoc_argv
*, int);
51 static size_t a2height(const struct mdoc_node
*);
52 static size_t a2offs(const struct mdoc_argv
*);
54 static int arg_hasattr(int, const struct mdoc_node
*);
55 static int arg_getattrs(const int *, int *, size_t,
56 const struct mdoc_node
*);
57 static int arg_getattr(int, const struct mdoc_node
*);
58 static int arg_listtype(const struct mdoc_node
*);
59 static void print_bvspace(struct termp
*,
60 const struct mdoc_node
*,
61 const struct mdoc_node
*);
62 static void print_node(DECL_ARGS
);
63 static void print_head(DECL_ARGS
);
64 static void print_body(DECL_ARGS
);
65 static void print_foot(DECL_ARGS
);
68 extern size_t strlcpy(char *, const char *, size_t);
69 extern size_t strlcat(char *, const char *, size_t);
72 static void termp____post(DECL_ARGS
);
73 static void termp_an_post(DECL_ARGS
);
74 static void termp_aq_post(DECL_ARGS
);
75 static void termp_bd_post(DECL_ARGS
);
76 static void termp_bl_post(DECL_ARGS
);
77 static void termp_bq_post(DECL_ARGS
);
78 static void termp_brq_post(DECL_ARGS
);
79 static void termp_bx_post(DECL_ARGS
);
80 static void termp_d1_post(DECL_ARGS
);
81 static void termp_dq_post(DECL_ARGS
);
82 static void termp_fd_post(DECL_ARGS
);
83 static void termp_fn_post(DECL_ARGS
);
84 static void termp_fo_post(DECL_ARGS
);
85 static void termp_ft_post(DECL_ARGS
);
86 static void termp_in_post(DECL_ARGS
);
87 static void termp_it_post(DECL_ARGS
);
88 static void termp_lb_post(DECL_ARGS
);
89 static void termp_op_post(DECL_ARGS
);
90 static void termp_pf_post(DECL_ARGS
);
91 static void termp_pq_post(DECL_ARGS
);
92 static void termp_qq_post(DECL_ARGS
);
93 static void termp_sh_post(DECL_ARGS
);
94 static void termp_sq_post(DECL_ARGS
);
95 static void termp_ss_post(DECL_ARGS
);
96 static void termp_vt_post(DECL_ARGS
);
98 static int termp__t_pre(DECL_ARGS
);
99 static int termp_an_pre(DECL_ARGS
);
100 static int termp_ap_pre(DECL_ARGS
);
101 static int termp_aq_pre(DECL_ARGS
);
102 static int termp_bd_pre(DECL_ARGS
);
103 static int termp_bf_pre(DECL_ARGS
);
104 static int termp_bold_pre(DECL_ARGS
);
105 static int termp_bq_pre(DECL_ARGS
);
106 static int termp_brq_pre(DECL_ARGS
);
107 static int termp_bt_pre(DECL_ARGS
);
108 static int termp_cd_pre(DECL_ARGS
);
109 static int termp_d1_pre(DECL_ARGS
);
110 static int termp_dq_pre(DECL_ARGS
);
111 static int termp_ex_pre(DECL_ARGS
);
112 static int termp_fa_pre(DECL_ARGS
);
113 static int termp_fl_pre(DECL_ARGS
);
114 static int termp_fn_pre(DECL_ARGS
);
115 static int termp_fo_pre(DECL_ARGS
);
116 static int termp_ft_pre(DECL_ARGS
);
117 static int termp_in_pre(DECL_ARGS
);
118 static int termp_it_pre(DECL_ARGS
);
119 static int termp_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_xr_pre(DECL_ARGS
);
137 static int termp_xx_pre(DECL_ARGS
);
139 static const struct termact termacts
[MDOC_MAX
] = {
140 { termp_ap_pre
, NULL
}, /* Ap */
141 { NULL
, NULL
}, /* Dd */
142 { NULL
, NULL
}, /* Dt */
143 { NULL
, NULL
}, /* Os */
144 { termp_sh_pre
, termp_sh_post
}, /* Sh */
145 { termp_ss_pre
, termp_ss_post
}, /* Ss */
146 { termp_sp_pre
, NULL
}, /* Pp */
147 { termp_d1_pre
, termp_d1_post
}, /* D1 */
148 { termp_d1_pre
, termp_d1_post
}, /* Dl */
149 { termp_bd_pre
, termp_bd_post
}, /* Bd */
150 { NULL
, NULL
}, /* Ed */
151 { NULL
, termp_bl_post
}, /* Bl */
152 { NULL
, NULL
}, /* El */
153 { termp_it_pre
, termp_it_post
}, /* It */
154 { NULL
, NULL
}, /* Ad */
155 { termp_an_pre
, termp_an_post
}, /* An */
156 { termp_under_pre
, NULL
}, /* Ar */
157 { termp_cd_pre
, NULL
}, /* Cd */
158 { termp_bold_pre
, NULL
}, /* Cm */
159 { NULL
, NULL
}, /* Dv */
160 { NULL
, NULL
}, /* Er */
161 { NULL
, NULL
}, /* Ev */
162 { termp_ex_pre
, NULL
}, /* Ex */
163 { termp_fa_pre
, NULL
}, /* Fa */
164 { termp_bold_pre
, termp_fd_post
}, /* Fd */
165 { termp_fl_pre
, NULL
}, /* Fl */
166 { termp_fn_pre
, termp_fn_post
}, /* Fn */
167 { termp_ft_pre
, termp_ft_post
}, /* Ft */
168 { termp_bold_pre
, NULL
}, /* Ic */
169 { termp_in_pre
, termp_in_post
}, /* In */
170 { NULL
, NULL
}, /* Li */
171 { termp_nd_pre
, NULL
}, /* Nd */
172 { termp_nm_pre
, NULL
}, /* Nm */
173 { termp_op_pre
, termp_op_post
}, /* Op */
174 { NULL
, NULL
}, /* Ot */
175 { termp_under_pre
, NULL
}, /* Pa */
176 { termp_rv_pre
, NULL
}, /* Rv */
177 { NULL
, NULL
}, /* St */
178 { termp_under_pre
, NULL
}, /* Va */
179 { termp_under_pre
, termp_vt_post
}, /* Vt */
180 { termp_xr_pre
, NULL
}, /* Xr */
181 { NULL
, termp____post
}, /* %A */
182 { termp_under_pre
, termp____post
}, /* %B */
183 { NULL
, termp____post
}, /* %D */
184 { termp_under_pre
, termp____post
}, /* %I */
185 { termp_under_pre
, termp____post
}, /* %J */
186 { NULL
, termp____post
}, /* %N */
187 { NULL
, termp____post
}, /* %O */
188 { NULL
, termp____post
}, /* %P */
189 { NULL
, termp____post
}, /* %R */
190 { termp__t_pre
, termp____post
}, /* %T */
191 { NULL
, termp____post
}, /* %V */
192 { NULL
, NULL
}, /* Ac */
193 { termp_aq_pre
, termp_aq_post
}, /* Ao */
194 { termp_aq_pre
, termp_aq_post
}, /* Aq */
195 { NULL
, NULL
}, /* At */
196 { NULL
, NULL
}, /* Bc */
197 { termp_bf_pre
, NULL
}, /* Bf */
198 { termp_bq_pre
, termp_bq_post
}, /* Bo */
199 { termp_bq_pre
, termp_bq_post
}, /* Bq */
200 { termp_xx_pre
, NULL
}, /* Bsx */
201 { NULL
, termp_bx_post
}, /* Bx */
202 { NULL
, NULL
}, /* Db */
203 { NULL
, NULL
}, /* Dc */
204 { termp_dq_pre
, termp_dq_post
}, /* Do */
205 { termp_dq_pre
, termp_dq_post
}, /* Dq */
206 { NULL
, NULL
}, /* Ec */
207 { NULL
, NULL
}, /* Ef */
208 { termp_under_pre
, NULL
}, /* Em */
209 { NULL
, NULL
}, /* Eo */
210 { termp_xx_pre
, NULL
}, /* Fx */
211 { termp_bold_pre
, NULL
}, /* Ms */ /* FIXME: convert to symbol? */
212 { NULL
, NULL
}, /* No */
213 { termp_ns_pre
, NULL
}, /* Ns */
214 { termp_xx_pre
, NULL
}, /* Nx */
215 { termp_xx_pre
, NULL
}, /* Ox */
216 { NULL
, NULL
}, /* Pc */
217 { termp_pf_pre
, termp_pf_post
}, /* Pf */
218 { termp_pq_pre
, termp_pq_post
}, /* Po */
219 { termp_pq_pre
, termp_pq_post
}, /* Pq */
220 { NULL
, NULL
}, /* Qc */
221 { termp_sq_pre
, termp_sq_post
}, /* Ql */
222 { termp_qq_pre
, termp_qq_post
}, /* Qo */
223 { termp_qq_pre
, termp_qq_post
}, /* Qq */
224 { NULL
, NULL
}, /* Re */
225 { termp_rs_pre
, NULL
}, /* Rs */
226 { NULL
, NULL
}, /* Sc */
227 { termp_sq_pre
, termp_sq_post
}, /* So */
228 { termp_sq_pre
, termp_sq_post
}, /* Sq */
229 { termp_sm_pre
, NULL
}, /* Sm */
230 { termp_under_pre
, NULL
}, /* Sx */
231 { termp_bold_pre
, NULL
}, /* Sy */
232 { NULL
, NULL
}, /* Tn */
233 { termp_xx_pre
, NULL
}, /* Ux */
234 { NULL
, NULL
}, /* Xc */
235 { NULL
, NULL
}, /* Xo */
236 { termp_fo_pre
, termp_fo_post
}, /* Fo */
237 { NULL
, NULL
}, /* Fc */
238 { termp_op_pre
, termp_op_post
}, /* Oo */
239 { NULL
, NULL
}, /* Oc */
240 { NULL
, NULL
}, /* Bk */
241 { NULL
, NULL
}, /* Ek */
242 { termp_bt_pre
, NULL
}, /* Bt */
243 { NULL
, NULL
}, /* Hf */
244 { NULL
, NULL
}, /* Fr */
245 { termp_ud_pre
, NULL
}, /* Ud */
246 { NULL
, termp_lb_post
}, /* Lb */
247 { termp_sp_pre
, NULL
}, /* Lp */
248 { termp_lk_pre
, NULL
}, /* Lk */
249 { termp_under_pre
, NULL
}, /* Mt */
250 { termp_brq_pre
, termp_brq_post
}, /* Brq */
251 { termp_brq_pre
, termp_brq_post
}, /* Bro */
252 { NULL
, NULL
}, /* Brc */
253 { NULL
, termp____post
}, /* %C */
254 { NULL
, NULL
}, /* Es */ /* TODO */
255 { NULL
, NULL
}, /* En */ /* TODO */
256 { termp_xx_pre
, NULL
}, /* Dx */
257 { NULL
, termp____post
}, /* %Q */
258 { termp_sp_pre
, NULL
}, /* br */
259 { termp_sp_pre
, NULL
}, /* sp */
260 { termp_under_pre
, termp____post
}, /* %U */
265 terminal_mdoc(void *arg
, const struct mdoc
*mdoc
)
267 const struct mdoc_node
*n
;
268 const struct mdoc_meta
*m
;
271 p
= (struct termp
*)arg
;
273 if (NULL
== p
->symtab
)
275 case (TERMENC_ASCII
):
276 p
->symtab
= chars_init(CHARS_ASCII
);
286 print_head(p
, NULL
, m
, n
);
288 print_body(p
, NULL
, m
, n
->child
);
289 print_foot(p
, NULL
, m
, n
);
294 print_body(DECL_ARGS
)
297 print_node(p
, pair
, m
, n
);
299 print_body(p
, pair
, m
, n
->next
);
305 print_node(DECL_ARGS
)
307 int chld
, bold
, under
;
308 struct termpair npair
;
309 size_t offset
, rmargin
;
313 rmargin
= p
->rmargin
;
317 memset(&npair
, 0, sizeof(struct termpair
));
320 if (MDOC_TEXT
!= n
->type
) {
321 if (termacts
[n
->tok
].pre
)
322 chld
= (*termacts
[n
->tok
].pre
)(p
, &npair
, m
, n
);
324 term_word(p
, n
->string
);
325 if (chld
&& n
->child
)
326 print_body(p
, &npair
, m
, n
->child
);
329 * XXX - if bold/under were to span scopes, this wouldn't be
330 * possible, but because decoration is always in-scope, we can
331 * get away with this.
337 if (MDOC_TEXT
!= n
->type
)
338 if (termacts
[n
->tok
].post
)
339 (*termacts
[n
->tok
].post
)(p
, &npair
, m
, n
);
342 p
->rmargin
= rmargin
;
348 print_foot(DECL_ARGS
)
350 char buf
[DATESIZ
], os
[BUFSIZ
];
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
;
393 /* FIXME: put in utility library. */
396 print_head(DECL_ARGS
)
398 char buf
[BUFSIZ
], title
[BUFSIZ
];
400 p
->rmargin
= p
->maxrmargin
;
404 * The header is strange. It has three components, which are
405 * really two with the first duplicated. It goes like this:
407 * IDENTIFIER TITLE IDENTIFIER
409 * The IDENTIFIER is NAME(SECTION), which is the command-name
410 * (if given, or "unknown" if not) followed by the manual page
411 * section. These are given in `Dt'. The TITLE is a free-form
412 * string depending on the manual volume. If not specified, it
413 * switches on the manual section.
417 strlcpy(buf
, m
->vol
, BUFSIZ
);
420 strlcat(buf
, " (", BUFSIZ
);
421 strlcat(buf
, m
->arch
, BUFSIZ
);
422 strlcat(buf
, ")", BUFSIZ
);
425 snprintf(title
, BUFSIZ
, "%s(%d)", m
->title
, m
->msec
);
428 p
->rmargin
= (p
->maxrmargin
- strlen(buf
) + 1) / 2;
429 p
->flags
|= TERMP_NOBREAK
| TERMP_NOSPACE
;
434 p
->offset
= p
->rmargin
;
435 p
->rmargin
= p
->maxrmargin
- strlen(title
);
436 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
441 p
->offset
= p
->rmargin
;
442 p
->rmargin
= p
->maxrmargin
;
443 p
->flags
&= ~TERMP_NOBREAK
;
444 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
450 p
->rmargin
= p
->maxrmargin
;
451 p
->flags
&= ~TERMP_NOSPACE
;
456 a2height(const struct mdoc_node
*n
)
460 assert(MDOC_TEXT
== n
->type
);
462 if ( ! a2roffsu(n
->string
, &su
, SCALE_VS
))
463 SCALE_VS_INIT(&su
, strlen(n
->string
));
465 return(term_vspan(&su
));
470 a2width(const struct mdoc_argv
*arg
, int pos
)
474 assert(arg
->value
[pos
]);
475 if ( ! a2roffsu(arg
->value
[pos
], &su
, SCALE_MAX
))
476 SCALE_HS_INIT(&su
, strlen(arg
->value
[pos
]));
479 return(term_hspan(&su
) + 2);
484 arg_listtype(const struct mdoc_node
*n
)
488 assert(MDOC_BLOCK
== n
->type
);
490 len
= (int)(n
->args
? n
->args
->argc
: 0);
492 for (i
= 0; i
< len
; i
++)
493 switch (n
->args
->argv
[i
].arg
) {
515 return(n
->args
->argv
[i
].arg
);
525 a2offs(const struct mdoc_argv
*arg
)
529 if ('\0' == arg
->value
[0][0])
531 else if (0 == strcmp(arg
->value
[0], "left"))
533 else if (0 == strcmp(arg
->value
[0], "indent"))
535 else if (0 == strcmp(arg
->value
[0], "indent-two"))
536 return((INDENT
+ 1) * 2);
537 else if ( ! a2roffsu(arg
->value
[0], &su
, SCALE_MAX
))
538 SCALE_HS_INIT(&su
, strlen(arg
->value
[0]));
540 return(term_hspan(&su
));
545 arg_hasattr(int arg
, const struct mdoc_node
*n
)
548 return(-1 != arg_getattr(arg
, n
));
553 arg_getattr(int v
, const struct mdoc_node
*n
)
557 return(arg_getattrs(&v
, &val
, 1, n
) ? val
: -1);
562 arg_getattrs(const int *keys
, int *vals
,
563 size_t sz
, const struct mdoc_node
*n
)
570 for (k
= i
= 0; i
< (int)n
->args
->argc
; i
++)
571 for (j
= 0; j
< (int)sz
; j
++)
572 if (n
->args
->argv
[i
].arg
== keys
[j
]) {
581 print_bvspace(struct termp
*p
,
582 const struct mdoc_node
*bl
,
583 const struct mdoc_node
*n
)
585 const struct mdoc_node
*nn
;
588 if (arg_hasattr(MDOC_Compact
, bl
))
591 /* Do not vspace directly after Ss/Sh. */
593 for (nn
= n
; nn
; nn
= nn
->parent
) {
594 if (MDOC_BLOCK
!= nn
->type
)
596 if (MDOC_Ss
== nn
->tok
)
598 if (MDOC_Sh
== nn
->tok
)
600 if (NULL
== nn
->prev
)
605 /* A `-column' does not assert vspace within the list. */
607 if (MDOC_Bl
== bl
->tok
&& arg_hasattr(MDOC_Column
, bl
))
608 if (n
->prev
&& MDOC_It
== n
->prev
->tok
)
611 /* A `-diag' without body does not vspace. */
613 if (MDOC_Bl
== bl
->tok
&& arg_hasattr(MDOC_Diag
, bl
))
614 if (n
->prev
&& MDOC_It
== n
->prev
->tok
) {
615 assert(n
->prev
->body
);
616 if (NULL
== n
->prev
->body
->child
)
626 termp_dq_pre(DECL_ARGS
)
629 if (MDOC_BODY
!= n
->type
)
632 term_word(p
, "\\(lq");
633 p
->flags
|= TERMP_NOSPACE
;
640 termp_dq_post(DECL_ARGS
)
643 if (MDOC_BODY
!= n
->type
)
646 p
->flags
|= TERMP_NOSPACE
;
647 term_word(p
, "\\(rq");
653 termp_it_pre(DECL_ARGS
)
655 const struct mdoc_node
*bl
, *nn
;
657 int i
, type
, keys
[3], vals
[3];
658 size_t width
, offset
;
660 if (MDOC_BLOCK
== n
->type
) {
661 print_bvspace(p
, n
->parent
->parent
, n
);
665 bl
= n
->parent
->parent
->parent
;
667 /* Save parent attributes. */
669 pair
->flag
= p
->flags
;
671 /* Get list width and offset. */
673 keys
[0] = MDOC_Width
;
674 keys
[1] = MDOC_Offset
;
675 keys
[2] = MDOC_Column
;
677 vals
[0] = vals
[1] = vals
[2] = -1;
681 (void)arg_getattrs(keys
, vals
, 3, bl
);
683 type
= arg_listtype(bl
);
686 /* Calculate real width and offset. */
690 if (MDOC_BODY
== n
->type
)
693 * Work around groff's column handling. The offset is
694 * equal to the sum of all widths leading to the current
695 * column (plus the -offset value). If this column
696 * exceeds the stated number of columns, the width is
697 * set as 0, else it's the stated column width (later
698 * the 0 will be adjusted to default 10 or, if in the
699 * last column case, set to stretch to the margin).
701 for (i
= 0, nn
= n
->prev
; nn
&&
702 i
< (int)bl
->args
->argv
[vals
[2]].sz
;
705 (&bl
->args
->argv
[vals
[2]], i
);
707 /* Whether exceeds maximum column. */
708 if (i
< (int)bl
->args
->argv
[vals
[2]].sz
)
709 width
= a2width(&bl
->args
->argv
[vals
[2]], i
);
714 offset
+= a2offs(&bl
->args
->argv
[vals
[1]]);
718 width
= a2width(&bl
->args
->argv
[vals
[0]], 0);
720 offset
+= a2offs(&bl
->args
->argv
[vals
[1]]);
725 * List-type can override the width in the case of fixed-head
726 * values (bullet, dash/hyphen, enum). Tags need a non-zero
758 * Whitespace control. Inset bodies need an initial space,
759 * while diagonal bodies need two.
762 p
->flags
|= TERMP_NOSPACE
;
766 if (MDOC_BODY
== n
->type
)
767 term_word(p
, "\\ \\ ");
770 if (MDOC_BODY
== n
->type
)
777 p
->flags
|= TERMP_NOSPACE
;
781 if (MDOC_HEAD
== n
->type
)
789 * Pad and break control. This is the tricker part. Lists with
790 * set right-margins for the head get TERMP_NOBREAK because, if
791 * they overrun the margin, they wrap to the new margin.
792 * Correspondingly, the body for these types don't left-pad, as
793 * the head will pad out to to the right.
804 if (MDOC_HEAD
== n
->type
)
805 p
->flags
|= TERMP_NOBREAK
;
807 p
->flags
|= TERMP_NOLPAD
;
810 if (MDOC_HEAD
== n
->type
)
811 p
->flags
|= TERMP_NOBREAK
;
813 p
->flags
|= TERMP_NOLPAD
;
815 if (MDOC_HEAD
!= n
->type
)
819 * This is ugly. If `-hang' is specified and the body
820 * is a `Bl' or `Bd', then we want basically to nullify
821 * the "overstep" effect in term_flushln() and treat
822 * this as a `-ohang' list instead.
824 if (n
->next
->child
&&
825 (MDOC_Bl
== n
->next
->child
->tok
||
826 MDOC_Bd
== n
->next
->child
->tok
)) {
827 p
->flags
&= ~TERMP_NOBREAK
;
828 p
->flags
&= ~TERMP_NOLPAD
;
830 p
->flags
|= TERMP_HANG
;
833 if (MDOC_HEAD
== n
->type
)
834 p
->flags
|= TERMP_NOBREAK
| TERMP_TWOSPACE
;
836 p
->flags
|= TERMP_NOLPAD
;
838 if (MDOC_HEAD
!= n
->type
)
840 if (NULL
== n
->next
|| NULL
== n
->next
->child
)
841 p
->flags
|= TERMP_DANGLE
;
844 if (MDOC_HEAD
== n
->type
) {
846 if (MDOC_BODY
== n
->next
->type
)
847 p
->flags
&= ~TERMP_NOBREAK
;
849 p
->flags
|= TERMP_NOBREAK
;
851 p
->flags
|= TERMP_NOLPAD
;
855 if (MDOC_HEAD
== n
->type
)
856 p
->flags
|= TERMP_NOBREAK
;
863 * Margin control. Set-head-width lists have their right
864 * margins shortened. The body for these lists has the offset
865 * necessarily lengthened. Everybody gets the offset.
873 * Same stipulation as above, regarding `-hang'. We
874 * don't want to recalculate rmargin and offsets when
875 * using `Bd' or `Bl' within `-hang' overstep lists.
877 if (MDOC_HEAD
== n
->type
&& n
->next
->child
&&
878 (MDOC_Bl
== n
->next
->child
->tok
||
879 MDOC_Bd
== n
->next
->child
->tok
))
892 if (MDOC_HEAD
== n
->type
)
893 p
->rmargin
= p
->offset
+ width
;
899 p
->rmargin
= p
->offset
+ width
;
901 * XXX - this behaviour is not documented: the
902 * right-most column is filled to the right margin.
904 if (MDOC_HEAD
== n
->type
&&
905 MDOC_BODY
== n
->next
->type
)
906 p
->rmargin
= p
->maxrmargin
;
913 * The dash, hyphen, bullet and enum lists all have a special
914 * HEAD character (temporarily bold, in some cases).
917 if (MDOC_HEAD
== n
->type
)
921 term_word(p
, "\\[bu]");
928 term_word(p
, "\\(hy");
932 (pair
->ppair
->ppair
->count
)++;
933 (void)snprintf(buf
, sizeof(buf
), "%d.",
934 pair
->ppair
->ppair
->count
);
942 * If we're not going to process our children, indicate so here.
955 if (MDOC_HEAD
== n
->type
)
959 if (MDOC_BODY
== n
->type
)
972 termp_it_post(DECL_ARGS
)
976 if (MDOC_BODY
!= n
->type
&& MDOC_HEAD
!= n
->type
)
979 type
= arg_listtype(n
->parent
->parent
->parent
);
988 if (MDOC_BODY
== n
->type
)
992 if (MDOC_HEAD
== n
->type
)
1000 p
->flags
= pair
->flag
;
1006 termp_nm_pre(DECL_ARGS
)
1009 if (SEC_SYNOPSIS
== n
->sec
)
1012 if (NULL
== n
->child
)
1013 term_word(p
, m
->name
);
1020 termp_fl_pre(DECL_ARGS
)
1024 term_word(p
, "\\-");
1025 p
->flags
|= TERMP_NOSPACE
;
1032 termp_an_pre(DECL_ARGS
)
1035 if (NULL
== n
->child
)
1039 * If not in the AUTHORS section, `An -split' will cause
1040 * newlines to occur before the author name. If in the AUTHORS
1041 * section, by default, the first `An' invocation is nosplit,
1042 * then all subsequent ones, regardless of whether interspersed
1043 * with other macros/text, are split. -split, in this case,
1044 * will override the condition of the implied first -nosplit.
1047 if (n
->sec
== SEC_AUTHORS
) {
1048 if ( ! (TERMP_ANPREC
& p
->flags
)) {
1049 if (TERMP_SPLIT
& p
->flags
)
1053 if (TERMP_NOSPLIT
& p
->flags
)
1059 if (TERMP_SPLIT
& p
->flags
)
1068 termp_an_post(DECL_ARGS
)
1072 if (SEC_AUTHORS
== n
->sec
)
1073 p
->flags
|= TERMP_ANPREC
;
1077 if (arg_getattr(MDOC_Split
, n
) > -1) {
1078 p
->flags
&= ~TERMP_NOSPLIT
;
1079 p
->flags
|= TERMP_SPLIT
;
1081 p
->flags
&= ~TERMP_SPLIT
;
1082 p
->flags
|= TERMP_NOSPLIT
;
1090 termp_ns_pre(DECL_ARGS
)
1093 p
->flags
|= TERMP_NOSPACE
;
1100 termp_rs_pre(DECL_ARGS
)
1103 if (SEC_SEE_ALSO
!= n
->sec
)
1105 if (MDOC_BLOCK
== n
->type
&& n
->prev
)
1113 termp_rv_pre(DECL_ARGS
)
1115 const struct mdoc_node
*nn
;
1118 term_word(p
, "The");
1120 for (nn
= n
->child
; nn
; nn
= nn
->next
) {
1122 term_word(p
, nn
->string
);
1124 p
->flags
|= TERMP_NOSPACE
;
1125 if (nn
->next
&& NULL
== nn
->next
->next
)
1126 term_word(p
, "(), and");
1128 term_word(p
, "(),");
1134 term_word(p
, "functions return");
1136 term_word(p
, "function returns");
1138 term_word(p
, "the value 0 if successful; otherwise the value "
1139 "-1 is returned and the global variable");
1142 term_word(p
, "errno");
1145 term_word(p
, "is set to indicate the error.");
1153 termp_ex_pre(DECL_ARGS
)
1155 const struct mdoc_node
*nn
;
1157 term_word(p
, "The");
1159 for (nn
= n
->child
; nn
; nn
= nn
->next
) {
1161 term_word(p
, nn
->string
);
1163 p
->flags
|= TERMP_NOSPACE
;
1164 if (nn
->next
&& NULL
== nn
->next
->next
)
1165 term_word(p
, ", and");
1169 p
->flags
&= ~TERMP_NOSPACE
;
1173 term_word(p
, "utilities exit");
1175 term_word(p
, "utility exits");
1177 term_word(p
, "0 on success, and >0 if an error occurs.");
1185 termp_nd_pre(DECL_ARGS
)
1188 if (MDOC_BODY
!= n
->type
)
1191 #if defined(__OpenBSD__) || defined(__linux__)
1192 term_word(p
, "\\(en");
1194 term_word(p
, "\\(em");
1202 termp_bl_post(DECL_ARGS
)
1205 if (MDOC_BLOCK
== n
->type
)
1212 termp_op_post(DECL_ARGS
)
1215 if (MDOC_BODY
!= n
->type
)
1217 p
->flags
|= TERMP_NOSPACE
;
1218 term_word(p
, "\\(rB");
1224 termp_xr_pre(DECL_ARGS
)
1226 const struct mdoc_node
*nn
;
1228 assert(n
->child
&& MDOC_TEXT
== n
->child
->type
);
1231 term_word(p
, nn
->string
);
1232 if (NULL
== (nn
= nn
->next
))
1234 p
->flags
|= TERMP_NOSPACE
;
1236 p
->flags
|= TERMP_NOSPACE
;
1237 term_word(p
, nn
->string
);
1238 p
->flags
|= TERMP_NOSPACE
;
1247 termp_vt_post(DECL_ARGS
)
1250 if (n
->sec
!= SEC_SYNOPSIS
)
1252 if (n
->next
&& MDOC_Vt
== n
->next
->tok
)
1261 termp_bold_pre(DECL_ARGS
)
1271 termp_fd_post(DECL_ARGS
)
1274 if (n
->sec
!= SEC_SYNOPSIS
)
1278 if (n
->next
&& MDOC_Fd
!= n
->next
->tok
)
1285 termp_sh_pre(DECL_ARGS
)
1288 /* No vspace between consecutive `Sh' calls. */
1292 if (n
->prev
&& MDOC_Sh
== n
->prev
->tok
)
1293 if (NULL
== n
->prev
->body
->child
)
1312 termp_sh_post(DECL_ARGS
)
1331 termp_op_pre(DECL_ARGS
)
1336 term_word(p
, "\\(lB");
1337 p
->flags
|= TERMP_NOSPACE
;
1348 termp_bt_pre(DECL_ARGS
)
1351 term_word(p
, "is currently in beta test.");
1358 termp_lb_post(DECL_ARGS
)
1361 if (SEC_LIBRARY
== n
->sec
)
1368 termp_ud_pre(DECL_ARGS
)
1371 term_word(p
, "currently under development.");
1378 termp_d1_pre(DECL_ARGS
)
1381 if (MDOC_BLOCK
!= n
->type
)
1384 p
->offset
+= (INDENT
+ 1);
1391 termp_d1_post(DECL_ARGS
)
1394 if (MDOC_BLOCK
!= n
->type
)
1402 termp_aq_pre(DECL_ARGS
)
1405 if (MDOC_BODY
!= n
->type
)
1407 term_word(p
, "\\(la");
1408 p
->flags
|= TERMP_NOSPACE
;
1415 termp_aq_post(DECL_ARGS
)
1418 if (MDOC_BODY
!= n
->type
)
1420 p
->flags
|= TERMP_NOSPACE
;
1421 term_word(p
, "\\(ra");
1427 termp_ft_pre(DECL_ARGS
)
1430 if (SEC_SYNOPSIS
== n
->sec
)
1431 if (n
->prev
&& MDOC_Fo
== n
->prev
->tok
)
1440 termp_ft_post(DECL_ARGS
)
1443 if (SEC_SYNOPSIS
== n
->sec
)
1450 termp_fn_pre(DECL_ARGS
)
1452 const struct mdoc_node
*nn
;
1455 term_word(p
, n
->child
->string
);
1458 p
->flags
|= TERMP_NOSPACE
;
1461 for (nn
= n
->child
->next
; nn
; nn
= nn
->next
) {
1463 term_word(p
, nn
->string
);
1471 if (SEC_SYNOPSIS
== n
->sec
)
1480 termp_fn_post(DECL_ARGS
)
1483 if (n
->sec
== SEC_SYNOPSIS
&& n
->next
)
1490 termp_fa_pre(DECL_ARGS
)
1492 const struct mdoc_node
*nn
;
1494 if (n
->parent
->tok
!= MDOC_Fo
) {
1499 for (nn
= n
->child
; nn
; nn
= nn
->next
) {
1501 term_word(p
, nn
->string
);
1507 if (n
->child
&& n
->next
&& n
->next
->tok
== MDOC_Fa
)
1516 termp_bd_pre(DECL_ARGS
)
1519 const struct mdoc_node
*nn
;
1521 if (MDOC_BLOCK
== n
->type
) {
1522 print_bvspace(p
, n
, n
);
1524 } else if (MDOC_BODY
!= n
->type
)
1529 for (type
= -1, i
= 0; i
< (int)nn
->args
->argc
; i
++) {
1530 switch (nn
->args
->argv
[i
].arg
) {
1531 case (MDOC_Centred
):
1537 case (MDOC_Unfilled
):
1539 case (MDOC_Literal
):
1540 type
= nn
->args
->argv
[i
].arg
;
1543 p
->offset
+= a2offs(&nn
->args
->argv
[i
]);
1551 * If -ragged or -filled are specified, the block does nothing
1552 * but change the indentation. If -unfilled or -literal are
1553 * specified, text is printed exactly as entered in the display:
1554 * for macro lines, a newline is appended to the line. Blank
1555 * lines are allowed.
1559 if (MDOC_Literal
!= type
&& MDOC_Unfilled
!= type
)
1562 for (nn
= n
->child
; nn
; nn
= nn
->next
) {
1563 p
->flags
|= TERMP_NOSPACE
;
1564 print_node(p
, pair
, m
, nn
);
1565 if (NULL
== nn
->next
)
1567 if (nn
->prev
&& nn
->prev
->line
< nn
->line
)
1569 else if (NULL
== nn
->prev
)
1579 termp_bd_post(DECL_ARGS
)
1582 if (MDOC_BODY
!= n
->type
)
1584 p
->flags
|= TERMP_NOSPACE
;
1591 termp_qq_pre(DECL_ARGS
)
1594 if (MDOC_BODY
!= n
->type
)
1597 p
->flags
|= TERMP_NOSPACE
;
1604 termp_qq_post(DECL_ARGS
)
1607 if (MDOC_BODY
!= n
->type
)
1609 p
->flags
|= TERMP_NOSPACE
;
1616 termp_bx_post(DECL_ARGS
)
1620 p
->flags
|= TERMP_NOSPACE
;
1621 term_word(p
, "BSD");
1627 termp_xx_pre(DECL_ARGS
)
1663 termp_sq_pre(DECL_ARGS
)
1666 if (MDOC_BODY
!= n
->type
)
1668 term_word(p
, "\\(oq");
1669 p
->flags
|= TERMP_NOSPACE
;
1676 termp_sq_post(DECL_ARGS
)
1679 if (MDOC_BODY
!= n
->type
)
1681 p
->flags
|= TERMP_NOSPACE
;
1682 term_word(p
, "\\(aq");
1688 termp_pf_pre(DECL_ARGS
)
1691 p
->flags
|= TERMP_IGNDELIM
;
1698 termp_pf_post(DECL_ARGS
)
1701 p
->flags
&= ~TERMP_IGNDELIM
;
1702 p
->flags
|= TERMP_NOSPACE
;
1708 termp_ss_pre(DECL_ARGS
)
1719 p
->offset
= HALFINDENT
;
1731 termp_ss_post(DECL_ARGS
)
1734 if (MDOC_HEAD
== n
->type
)
1741 termp_cd_pre(DECL_ARGS
)
1752 termp_in_pre(DECL_ARGS
)
1756 if (SEC_SYNOPSIS
== n
->sec
)
1757 term_word(p
, "#include");
1760 p
->flags
|= TERMP_NOSPACE
;
1767 termp_in_post(DECL_ARGS
)
1771 p
->flags
|= TERMP_NOSPACE
;
1775 if (SEC_SYNOPSIS
!= n
->sec
)
1780 * XXX Not entirely correct. If `.In foo bar' is specified in
1781 * the SYNOPSIS section, then it produces a single break after
1782 * the <foo>; mandoc asserts a vertical space. Since this
1783 * construction is rarely used, I think it's fine.
1785 if (n
->next
&& MDOC_In
!= n
->next
->tok
)
1792 termp_sp_pre(DECL_ARGS
)
1798 len
= n
->child
? a2height(n
->child
) : 1;
1810 for (i
= 0; i
< len
; i
++)
1819 termp_brq_pre(DECL_ARGS
)
1822 if (MDOC_BODY
!= n
->type
)
1824 term_word(p
, "\\(lC");
1825 p
->flags
|= TERMP_NOSPACE
;
1832 termp_brq_post(DECL_ARGS
)
1835 if (MDOC_BODY
!= n
->type
)
1837 p
->flags
|= TERMP_NOSPACE
;
1838 term_word(p
, "\\(rC");
1844 termp_bq_pre(DECL_ARGS
)
1847 if (MDOC_BODY
!= n
->type
)
1849 term_word(p
, "\\(lB");
1850 p
->flags
|= TERMP_NOSPACE
;
1857 termp_bq_post(DECL_ARGS
)
1860 if (MDOC_BODY
!= n
->type
)
1862 p
->flags
|= TERMP_NOSPACE
;
1863 term_word(p
, "\\(rB");
1869 termp_pq_pre(DECL_ARGS
)
1872 if (MDOC_BODY
!= n
->type
)
1874 term_word(p
, "\\&(");
1875 p
->flags
|= TERMP_NOSPACE
;
1882 termp_pq_post(DECL_ARGS
)
1885 if (MDOC_BODY
!= n
->type
)
1893 termp_fo_pre(DECL_ARGS
)
1895 const struct mdoc_node
*nn
;
1897 if (MDOC_BODY
== n
->type
) {
1898 p
->flags
|= TERMP_NOSPACE
;
1900 p
->flags
|= TERMP_NOSPACE
;
1902 } else if (MDOC_HEAD
!= n
->type
)
1906 for (nn
= n
->child
; nn
; nn
= nn
->next
) {
1907 assert(MDOC_TEXT
== nn
->type
);
1908 term_word(p
, nn
->string
);
1918 termp_fo_post(DECL_ARGS
)
1921 if (MDOC_BODY
!= n
->type
)
1923 p
->flags
|= TERMP_NOSPACE
;
1925 p
->flags
|= TERMP_NOSPACE
;
1933 termp_bf_pre(DECL_ARGS
)
1935 const struct mdoc_node
*nn
;
1937 if (MDOC_HEAD
== n
->type
)
1939 else if (MDOC_BLOCK
!= n
->type
)
1942 if (NULL
== (nn
= n
->head
->child
)) {
1943 if (arg_hasattr(MDOC_Emphasis
, n
))
1945 else if (arg_hasattr(MDOC_Symbolic
, n
))
1951 assert(MDOC_TEXT
== nn
->type
);
1952 if (0 == strcmp("Em", nn
->string
))
1954 else if (0 == strcmp("Sy", nn
->string
))
1963 termp_sm_pre(DECL_ARGS
)
1966 assert(n
->child
&& MDOC_TEXT
== n
->child
->type
);
1967 if (0 == strcmp("on", n
->child
->string
)) {
1968 p
->flags
&= ~TERMP_NONOSPACE
;
1969 p
->flags
&= ~TERMP_NOSPACE
;
1971 p
->flags
|= TERMP_NONOSPACE
;
1979 termp_ap_pre(DECL_ARGS
)
1982 p
->flags
|= TERMP_NOSPACE
;
1983 term_word(p
, "\\(aq");
1984 p
->flags
|= TERMP_NOSPACE
;
1991 termp____post(DECL_ARGS
)
1996 p
->flags
|= TERMP_NOSPACE
;
1999 term_word(p
, "\\(rq");
2000 p
->flags
|= TERMP_NOSPACE
;
2005 term_word(p
, n
->next
? "," : ".");
2011 termp_lk_pre(DECL_ARGS
)
2013 const struct mdoc_node
*nn
;
2018 if (NULL
== nn
->next
)
2021 term_word(p
, nn
->string
);
2024 p
->flags
|= TERMP_NOSPACE
;
2028 for (nn
= nn
->next
; nn
; nn
= nn
->next
)
2029 term_word(p
, nn
->string
);
2038 termp_under_pre(DECL_ARGS
)
2048 termp__t_pre(DECL_ARGS
)
2051 term_word(p
, "\\(lq");
2052 p
->flags
|= TERMP_NOSPACE
;