]>
git.cameronkatri.com Git - mandoc.git/blob - mdoc_term.c
1 /* $Id: mdoc_term.c,v 1.22 2009/07/07 11:47:17 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: macro arguments can be escaped. */
30 /* FIXME: support more offset/width tokens. */
33 #define TTYPE_CMD_FLAG 1
34 #define TTYPE_CMD_ARG 2
35 #define TTYPE_SECTION 3
36 #define TTYPE_FUNC_DECL 4
37 #define TTYPE_VAR_DECL 5
38 #define TTYPE_FUNC_TYPE 6
39 #define TTYPE_FUNC_NAME 7
40 #define TTYPE_FUNC_ARG 8
42 #define TTYPE_SSECTION 10
45 #define TTYPE_CONFIG 13
47 #define TTYPE_INCLUDE 15
49 #define TTYPE_SYMBOL 17
51 #define TTYPE_LINK_ANCHOR 19
52 #define TTYPE_LINK_TEXT 20
53 #define TTYPE_REF_JOURNAL 21
57 const int ttypes
[TTYPE_NMAX
] = {
58 TERMP_BOLD
, /* TTYPE_PROG */
59 TERMP_BOLD
, /* TTYPE_CMD_FLAG */
60 TERMP_UNDER
, /* TTYPE_CMD_ARG */
61 TERMP_BOLD
, /* TTYPE_SECTION */
62 TERMP_BOLD
, /* TTYPE_FUNC_DECL */
63 TERMP_UNDER
, /* TTYPE_VAR_DECL */
64 TERMP_UNDER
, /* TTYPE_FUNC_TYPE */
65 TERMP_BOLD
, /* TTYPE_FUNC_NAME */
66 TERMP_UNDER
, /* TTYPE_FUNC_ARG */
67 TERMP_UNDER
, /* TTYPE_LINK */
68 TERMP_BOLD
, /* TTYPE_SSECTION */
69 TERMP_UNDER
, /* TTYPE_FILE */
70 TERMP_UNDER
, /* TTYPE_EMPH */
71 TERMP_BOLD
, /* TTYPE_CONFIG */
72 TERMP_BOLD
, /* TTYPE_CMD */
73 TERMP_BOLD
, /* TTYPE_INCLUDE */
74 TERMP_BOLD
, /* TTYPE_SYMB */
75 TERMP_BOLD
, /* TTYPE_SYMBOL */
76 TERMP_BOLD
, /* TTYPE_DIAG */
77 TERMP_UNDER
, /* TTYPE_LINK_ANCHOR */
78 TERMP_BOLD
, /* TTYPE_LINK_TEXT */
79 TERMP_UNDER
, /* TTYPE_REF_JOURNAL */
80 TERMP_BOLD
/* TTYPE_LIST */
83 /* XXX - clean this up. */
86 struct termpair
*ppair
;
88 #define TERMPAIR_FLAG (1 << 0)
89 int flag
; /* Whether being used. */
90 size_t offset
; /* Left margin. */
91 size_t rmargin
; /* Right margin. */
92 int count
; /* Enum count. */
95 #define TERMPAIR_SETFLAG(termp, p, fl) \
97 assert(! (TERMPAIR_FLAG & (p)->type)); \
98 (termp)->flags |= (fl); \
100 (p)->type |= TERMPAIR_FLAG; \
101 } while ( /* CONSTCOND */ 0)
104 struct termp *p, struct termpair *pair, \
105 const struct mdoc_meta *meta, \
106 const struct mdoc_node *node
108 #define DECL_PRE(name) \
109 static int name##_pre(DECL_ARGS)
110 #define DECL_POST(name) \
111 static void name##_post(DECL_ARGS)
112 #define DECL_PREPOST(name) \
116 DECL_PREPOST(termp__t
);
117 DECL_PREPOST(termp_aq
);
118 DECL_PREPOST(termp_bd
);
119 DECL_PREPOST(termp_bq
);
120 DECL_PREPOST(termp_brq
);
121 DECL_PREPOST(termp_d1
);
122 DECL_PREPOST(termp_dq
);
123 DECL_PREPOST(termp_fd
);
124 DECL_PREPOST(termp_fn
);
125 DECL_PREPOST(termp_fo
);
126 DECL_PREPOST(termp_ft
);
127 DECL_PREPOST(termp_in
);
128 DECL_PREPOST(termp_it
);
129 DECL_PREPOST(termp_lb
);
130 DECL_PREPOST(termp_op
);
131 DECL_PREPOST(termp_pf
);
132 DECL_PREPOST(termp_pq
);
133 DECL_PREPOST(termp_qq
);
134 DECL_PREPOST(termp_sh
);
135 DECL_PREPOST(termp_ss
);
136 DECL_PREPOST(termp_sq
);
137 DECL_PREPOST(termp_vt
);
181 int (*pre
)(DECL_ARGS
);
182 void (*post
)(DECL_ARGS
);
185 static const struct termact termacts
[MDOC_MAX
] = {
186 { termp_ap_pre
, NULL
}, /* Ap */
187 { NULL
, NULL
}, /* Dd */
188 { NULL
, NULL
}, /* Dt */
189 { NULL
, NULL
}, /* Os */
190 { termp_sh_pre
, termp_sh_post
}, /* Sh */
191 { termp_ss_pre
, termp_ss_post
}, /* Ss */
192 { termp_pp_pre
, NULL
}, /* Pp */
193 { termp_d1_pre
, termp_d1_post
}, /* D1 */
194 { termp_d1_pre
, termp_d1_post
}, /* Dl */
195 { termp_bd_pre
, termp_bd_post
}, /* Bd */
196 { NULL
, NULL
}, /* Ed */
197 { NULL
, termp_bl_post
}, /* Bl */
198 { NULL
, NULL
}, /* El */
199 { termp_it_pre
, termp_it_post
}, /* It */
200 { NULL
, NULL
}, /* Ad */
201 { NULL
, NULL
}, /* An */
202 { termp_ar_pre
, NULL
}, /* Ar */
203 { termp_cd_pre
, NULL
}, /* Cd */
204 { termp_cm_pre
, NULL
}, /* Cm */
205 { NULL
, NULL
}, /* Dv */
206 { NULL
, NULL
}, /* Er */
207 { NULL
, NULL
}, /* Ev */
208 { termp_ex_pre
, NULL
}, /* Ex */
209 { termp_fa_pre
, NULL
}, /* Fa */
210 { termp_fd_pre
, termp_fd_post
}, /* Fd */
211 { termp_fl_pre
, NULL
}, /* Fl */
212 { termp_fn_pre
, termp_fn_post
}, /* Fn */
213 { termp_ft_pre
, termp_ft_post
}, /* Ft */
214 { termp_ic_pre
, NULL
}, /* Ic */
215 { termp_in_pre
, termp_in_post
}, /* In */
216 { NULL
, NULL
}, /* Li */
217 { termp_nd_pre
, NULL
}, /* Nd */
218 { termp_nm_pre
, NULL
}, /* Nm */
219 { termp_op_pre
, termp_op_post
}, /* Op */
220 { NULL
, NULL
}, /* Ot */
221 { termp_pa_pre
, NULL
}, /* Pa */
222 { termp_rv_pre
, NULL
}, /* Rv */
223 { termp_st_pre
, NULL
}, /* St */
224 { termp_va_pre
, NULL
}, /* Va */
225 { termp_vt_pre
, termp_vt_post
}, /* Vt */
226 { termp_xr_pre
, NULL
}, /* Xr */
227 { NULL
, termp____post
}, /* %A */
228 { NULL
, termp____post
}, /* %B */
229 { NULL
, termp____post
}, /* %D */
230 { NULL
, termp____post
}, /* %I */
231 { termp__j_pre
, termp____post
}, /* %J */
232 { NULL
, termp____post
}, /* %N */
233 { NULL
, termp____post
}, /* %O */
234 { NULL
, termp____post
}, /* %P */
235 { NULL
, termp____post
}, /* %R */
236 { termp__t_pre
, termp__t_post
}, /* %T */
237 { NULL
, termp____post
}, /* %V */
238 { NULL
, NULL
}, /* Ac */
239 { termp_aq_pre
, termp_aq_post
}, /* Ao */
240 { termp_aq_pre
, termp_aq_post
}, /* Aq */
241 { termp_at_pre
, NULL
}, /* At */
242 { NULL
, NULL
}, /* Bc */
243 { termp_bf_pre
, NULL
}, /* Bf */
244 { termp_bq_pre
, termp_bq_post
}, /* Bo */
245 { termp_bq_pre
, termp_bq_post
}, /* Bq */
246 { termp_bsx_pre
, NULL
}, /* Bsx */
247 { NULL
, termp_bx_post
}, /* Bx */
248 { NULL
, NULL
}, /* Db */
249 { NULL
, NULL
}, /* Dc */
250 { termp_dq_pre
, termp_dq_post
}, /* Do */
251 { termp_dq_pre
, termp_dq_post
}, /* Dq */
252 { NULL
, NULL
}, /* Ec */
253 { NULL
, NULL
}, /* Ef */
254 { termp_em_pre
, NULL
}, /* Em */
255 { NULL
, NULL
}, /* Eo */
256 { termp_fx_pre
, NULL
}, /* Fx */
257 { termp_ms_pre
, NULL
}, /* Ms */
258 { NULL
, NULL
}, /* No */
259 { termp_ns_pre
, NULL
}, /* Ns */
260 { termp_nx_pre
, NULL
}, /* Nx */
261 { termp_ox_pre
, NULL
}, /* Ox */
262 { NULL
, NULL
}, /* Pc */
263 { termp_pf_pre
, termp_pf_post
}, /* Pf */
264 { termp_pq_pre
, termp_pq_post
}, /* Po */
265 { termp_pq_pre
, termp_pq_post
}, /* Pq */
266 { NULL
, NULL
}, /* Qc */
267 { termp_sq_pre
, termp_sq_post
}, /* Ql */
268 { termp_qq_pre
, termp_qq_post
}, /* Qo */
269 { termp_qq_pre
, termp_qq_post
}, /* Qq */
270 { NULL
, NULL
}, /* Re */
271 { termp_rs_pre
, NULL
}, /* Rs */
272 { NULL
, NULL
}, /* Sc */
273 { termp_sq_pre
, termp_sq_post
}, /* So */
274 { termp_sq_pre
, termp_sq_post
}, /* Sq */
275 { termp_sm_pre
, NULL
}, /* Sm */
276 { termp_sx_pre
, NULL
}, /* Sx */
277 { termp_sy_pre
, NULL
}, /* Sy */
278 { NULL
, NULL
}, /* Tn */
279 { termp_ux_pre
, NULL
}, /* Ux */
280 { NULL
, NULL
}, /* Xc */
281 { NULL
, NULL
}, /* Xo */
282 { termp_fo_pre
, termp_fo_post
}, /* Fo */
283 { NULL
, NULL
}, /* Fc */
284 { termp_op_pre
, termp_op_post
}, /* Oo */
285 { NULL
, NULL
}, /* Oc */
286 { NULL
, NULL
}, /* Bk */
287 { NULL
, NULL
}, /* Ek */
288 { termp_bt_pre
, NULL
}, /* Bt */
289 { NULL
, NULL
}, /* Hf */
290 { NULL
, NULL
}, /* Fr */
291 { termp_ud_pre
, NULL
}, /* Ud */
292 { termp_lb_pre
, termp_lb_post
}, /* Lb */
293 { termp_pp_pre
, NULL
}, /* Lp */
294 { termp_lk_pre
, NULL
}, /* Lk */
295 { termp_mt_pre
, NULL
}, /* Mt */
296 { termp_brq_pre
, termp_brq_post
}, /* Brq */
297 { termp_brq_pre
, termp_brq_post
}, /* Bro */
298 { NULL
, NULL
}, /* Brc */
299 { NULL
, NULL
}, /* %C */
300 { NULL
, NULL
}, /* Es */
301 { NULL
, NULL
}, /* En */
302 { termp_dx_pre
, NULL
}, /* Dx */
303 { NULL
, NULL
}, /* %Q */
307 extern size_t strlcpy(char *, const char *, size_t);
308 extern size_t strlcat(char *, const char *, size_t);
311 static int arg_hasattr(int, const struct mdoc_node
*);
312 static int arg_getattrs(const int *, int *, size_t,
313 const struct mdoc_node
*);
314 static int arg_getattr(int, const struct mdoc_node
*);
315 static size_t arg_offset(const struct mdoc_argv
*);
316 static size_t arg_width(const struct mdoc_argv
*, int);
317 static int arg_listtype(const struct mdoc_node
*);
318 static int fmt_block_vspace(struct termp
*,
319 const struct mdoc_node
*,
320 const struct mdoc_node
*);
321 static void print_node(DECL_ARGS
);
322 static void print_head(struct termp
*,
323 const struct mdoc_meta
*);
324 static void print_body(DECL_ARGS
);
325 static void print_foot(struct termp
*,
326 const struct mdoc_meta
*);
330 mdoc_run(struct termp
*p
, const struct mdoc
*m
)
333 * Main output function. When this is called, assume that the
334 * tree is properly formed.
337 print_head(p
, mdoc_meta(m
));
338 assert(mdoc_node(m
));
339 assert(MDOC_ROOT
== mdoc_node(m
)->type
);
340 if (mdoc_node(m
)->child
)
341 print_body(p
, NULL
, mdoc_meta(m
), mdoc_node(m
)->child
);
342 print_foot(p
, mdoc_meta(m
));
348 print_body(DECL_ARGS
)
351 print_node(p
, pair
, meta
, node
);
354 print_body(p
, pair
, meta
, node
->next
);
359 print_node(DECL_ARGS
)
362 struct termpair npair
;
364 /* Pre-processing. */
369 npair
.offset
= npair
.rmargin
= 0;
373 if (MDOC_TEXT
!= node
->type
) {
374 if (termacts
[node
->tok
].pre
)
375 if ( ! (*termacts
[node
->tok
].pre
)(p
, &npair
, meta
, node
))
377 } else /* MDOC_TEXT == node->type */
378 term_word(p
, node
->string
);
382 if (TERMPAIR_FLAG
& npair
.type
)
383 p
->flags
|= npair
.flag
;
385 if (dochild
&& node
->child
)
386 print_body(p
, &npair
, meta
, node
->child
);
388 if (TERMPAIR_FLAG
& npair
.type
)
389 p
->flags
&= ~npair
.flag
;
391 /* Post-processing. */
393 if (MDOC_TEXT
!= node
->type
)
394 if (termacts
[node
->tok
].post
)
395 (*termacts
[node
->tok
].post
)(p
, &npair
, meta
, node
);
400 print_foot(struct termp
*p
, const struct mdoc_meta
*meta
)
406 * Output the footer in new-groff style, that is, three columns
407 * with the middle being the manual date and flanking columns
408 * being the operating system:
413 if (NULL
== (buf
= malloc(p
->rmargin
)))
415 if (NULL
== (os
= malloc(p
->rmargin
)))
418 tm
= localtime(&meta
->date
);
420 if (0 == strftime(buf
, p
->rmargin
, "%B %d, %Y", tm
))
423 (void)strlcpy(os
, meta
->os
, p
->rmargin
);
428 p
->rmargin
= (p
->maxrmargin
- strlen(buf
) + 1) / 2;
429 p
->flags
|= TERMP_NOSPACE
| TERMP_NOBREAK
;
434 p
->offset
= p
->rmargin
;
435 p
->rmargin
= p
->maxrmargin
- strlen(os
);
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
;
459 print_head(struct termp
*p
, const struct mdoc_meta
*meta
)
463 p
->rmargin
= p
->maxrmargin
;
466 if (NULL
== (buf
= malloc(p
->rmargin
)))
468 if (NULL
== (title
= malloc(p
->rmargin
)))
472 * The header is strange. It has three components, which are
473 * really two with the first duplicated. It goes like this:
475 * IDENTIFIER TITLE IDENTIFIER
477 * The IDENTIFIER is NAME(SECTION), which is the command-name
478 * (if given, or "unknown" if not) followed by the manual page
479 * section. These are given in `Dt'. The TITLE is a free-form
480 * string depending on the manual volume. If not specified, it
481 * switches on the manual section.
485 (void)strlcpy(buf
, meta
->vol
, p
->rmargin
);
488 (void)strlcat(buf
, " (", p
->rmargin
);
489 (void)strlcat(buf
, meta
->arch
, p
->rmargin
);
490 (void)strlcat(buf
, ")", p
->rmargin
);
493 (void)snprintf(title
, p
->rmargin
, "%s(%d)",
494 meta
->title
, meta
->msec
);
497 p
->rmargin
= (p
->maxrmargin
- strlen(buf
) + 1) / 2;
498 p
->flags
|= TERMP_NOBREAK
| TERMP_NOSPACE
;
503 p
->offset
= p
->rmargin
;
504 p
->rmargin
= p
->maxrmargin
- strlen(title
);
505 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
510 p
->offset
= p
->rmargin
;
511 p
->rmargin
= p
->maxrmargin
;
512 p
->flags
&= ~TERMP_NOBREAK
;
513 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
519 p
->rmargin
= p
->maxrmargin
;
520 p
->flags
&= ~TERMP_NOSPACE
;
528 arg_width(const struct mdoc_argv
*arg
, int pos
)
533 assert(pos
< (int)arg
->sz
&& pos
>= 0);
534 assert(arg
->value
[pos
]);
536 if (0 == (len
= (int)strlen(arg
->value
[pos
])))
539 for (i
= 0; i
< len
- 1; i
++)
540 if ( ! isdigit((u_char
)arg
->value
[pos
][i
]))
544 if ('n' == arg
->value
[pos
][len
- 1] ||
545 'm' == arg
->value
[pos
][len
- 1]) {
546 v
= (size_t)atoi(arg
->value
[pos
]);
551 return(strlen(arg
->value
[pos
]) + 2);
556 arg_listtype(const struct mdoc_node
*n
)
560 assert(MDOC_BLOCK
== n
->type
);
562 len
= (int)(n
->args
? n
->args
->argc
: 0);
564 for (i
= 0; i
< len
; i
++)
565 switch (n
->args
->argv
[i
].arg
) {
585 return(n
->args
->argv
[i
].arg
);
590 /* FIXME: mandated by parser. */
592 errx(1, "list type not supported");
598 arg_offset(const struct mdoc_argv
*arg
)
602 if (0 == strcmp(*arg
->value
, "left"))
604 if (0 == strcmp(*arg
->value
, "indent"))
606 if (0 == strcmp(*arg
->value
, "indent-two"))
607 return((INDENT
+ 1) * 2);
609 /* FIXME: needs to support field-widths (10n, etc.). */
611 return(strlen(*arg
->value
));
616 arg_hasattr(int arg
, const struct mdoc_node
*n
)
619 return(-1 != arg_getattr(arg
, n
));
624 arg_getattr(int v
, const struct mdoc_node
*n
)
628 return(arg_getattrs(&v
, &val
, 1, n
) ? val
: -1);
633 arg_getattrs(const int *keys
, int *vals
,
634 size_t sz
, const struct mdoc_node
*n
)
641 for (k
= i
= 0; i
< (int)n
->args
->argc
; i
++)
642 for (j
= 0; j
< (int)sz
; j
++)
643 if (n
->args
->argv
[i
].arg
== keys
[j
]) {
653 fmt_block_vspace(struct termp
*p
,
654 const struct mdoc_node
*bl
,
655 const struct mdoc_node
*node
)
657 const struct mdoc_node
*n
;
661 if (arg_hasattr(MDOC_Compact
, bl
))
664 for (n
= node
; n
; n
= n
->parent
) {
665 if (MDOC_BLOCK
!= n
->type
)
667 if (MDOC_Ss
== n
->tok
)
669 if (MDOC_Sh
== n
->tok
)
683 termp_dq_pre(DECL_ARGS
)
686 if (MDOC_BODY
!= node
->type
)
689 term_word(p
, "\\(lq");
690 p
->flags
|= TERMP_NOSPACE
;
697 termp_dq_post(DECL_ARGS
)
700 if (MDOC_BODY
!= node
->type
)
703 p
->flags
|= TERMP_NOSPACE
;
704 term_word(p
, "\\(rq");
710 termp_it_pre(DECL_ARGS
)
712 const struct mdoc_node
*bl
, *n
;
714 int i
, type
, keys
[3], vals
[3], sv
;
715 size_t width
, offset
;
717 if (MDOC_BLOCK
== node
->type
)
718 return(fmt_block_vspace(p
, node
->parent
->parent
, node
));
720 bl
= node
->parent
->parent
->parent
;
722 /* Save parent attributes. */
724 pair
->offset
= p
->offset
;
725 pair
->rmargin
= p
->rmargin
;
726 pair
->flag
= p
->flags
;
728 /* Get list width and offset. */
730 keys
[0] = MDOC_Width
;
731 keys
[1] = MDOC_Offset
;
732 keys
[2] = MDOC_Column
;
734 vals
[0] = vals
[1] = vals
[2] = -1;
738 (void)arg_getattrs(keys
, vals
, 3, bl
);
740 type
= arg_listtype(bl
);
742 /* Calculate real width and offset. */
746 if (MDOC_BODY
== node
->type
)
748 for (i
= 0, n
= node
->prev
; n
; n
= n
->prev
, i
++)
750 (&bl
->args
->argv
[vals
[2]], i
);
751 assert(i
< (int)bl
->args
->argv
[vals
[2]].sz
);
752 width
= arg_width(&bl
->args
->argv
[vals
[2]], i
);
754 offset
+= arg_offset(&bl
->args
->argv
[vals
[1]]);
758 width
= arg_width(&bl
->args
->argv
[vals
[0]], 0);
760 offset
+= arg_offset(&bl
->args
->argv
[vals
[1]]);
765 * List-type can override the width in the case of fixed-head
766 * values (bullet, dash/hyphen, enum). Tags need a non-zero
792 * Whitespace control. Inset bodies need an initial space,
793 * while diagonal bodies need two.
801 if (MDOC_BODY
== node
->type
)
802 p
->flags
&= ~TERMP_NOSPACE
;
804 p
->flags
|= TERMP_NOSPACE
;
807 p
->flags
|= TERMP_NOSPACE
;
812 * Style flags. Diagnostic heads need TTYPE_DIAG.
817 if (MDOC_HEAD
== node
->type
)
818 p
->flags
|= ttypes
[TTYPE_DIAG
];
825 * Pad and break control. This is the tricker part. Lists with
826 * set right-margins for the head get TERMP_NOBREAK because, if
827 * they overrun the margin, they wrap to the new margin.
828 * Correspondingly, the body for these types don't left-pad, as
829 * the head will pad out to to the right.
842 if (MDOC_HEAD
== node
->type
)
843 p
->flags
|= TERMP_NOBREAK
;
845 p
->flags
|= TERMP_NOLPAD
;
846 if (MDOC_HEAD
== node
->type
&& MDOC_Tag
== type
)
847 if (NULL
== node
->next
||
848 NULL
== node
->next
->child
)
849 p
->flags
|= TERMP_NONOBREAK
;
852 if (MDOC_HEAD
== node
->type
) {
854 if (MDOC_BODY
== node
->next
->type
)
855 p
->flags
&= ~TERMP_NOBREAK
;
857 p
->flags
|= TERMP_NOBREAK
;
859 p
->flags
|= TERMP_NOLPAD
;
863 if (MDOC_HEAD
== node
->type
)
864 p
->flags
|= TERMP_NOBREAK
;
871 * Margin control. Set-head-width lists have their right
872 * margins shortened. The body for these lists has the offset
873 * necessarily lengthened. Everybody gets the offset.
888 if (MDOC_HEAD
== node
->type
)
889 p
->rmargin
= p
->offset
+ width
;
894 p
->rmargin
= p
->offset
+ width
;
901 * The dash, hyphen, bullet and enum lists all have a special
902 * HEAD character (temporarily bold, in some cases).
906 if (MDOC_HEAD
== node
->type
)
909 p
->flags
|= TERMP_BOLD
;
910 term_word(p
, "\\[bu]");
915 p
->flags
|= TERMP_BOLD
;
919 (pair
->ppair
->ppair
->count
)++;
920 (void)snprintf(buf
, sizeof(buf
), "%d.",
921 pair
->ppair
->ppair
->count
);
928 p
->flags
= sv
; /* Restore saved flags. */
931 * If we're not going to process our children, indicate so here.
944 if (MDOC_HEAD
== node
->type
)
948 if (MDOC_BODY
== node
->type
)
961 termp_it_post(DECL_ARGS
)
965 if (MDOC_BODY
!= node
->type
&& MDOC_HEAD
!= node
->type
)
968 type
= arg_listtype(node
->parent
->parent
->parent
);
976 if (MDOC_BODY
== node
->type
)
980 if (MDOC_HEAD
== node
->type
)
988 p
->offset
= pair
->offset
;
989 p
->rmargin
= pair
->rmargin
;
990 p
->flags
= pair
->flag
;
996 termp_nm_pre(DECL_ARGS
)
999 if (SEC_SYNOPSIS
== node
->sec
)
1002 TERMPAIR_SETFLAG(p
, pair
, ttypes
[TTYPE_PROG
]);
1003 if (NULL
== node
->child
)
1004 term_word(p
, meta
->name
);
1012 termp_fl_pre(DECL_ARGS
)
1015 TERMPAIR_SETFLAG(p
, pair
, ttypes
[TTYPE_CMD_FLAG
]);
1016 term_word(p
, "\\-");
1017 p
->flags
|= TERMP_NOSPACE
;
1024 termp_ar_pre(DECL_ARGS
)
1027 TERMPAIR_SETFLAG(p
, pair
, ttypes
[TTYPE_CMD_ARG
]);
1034 termp_ns_pre(DECL_ARGS
)
1037 p
->flags
|= TERMP_NOSPACE
;
1044 termp_pp_pre(DECL_ARGS
)
1054 termp_st_pre(DECL_ARGS
)
1058 if (node
->child
&& (cp
= mdoc_a2st(node
->child
->string
)))
1066 termp_rs_pre(DECL_ARGS
)
1069 if (MDOC_BLOCK
== node
->type
&& node
->prev
)
1077 termp_rv_pre(DECL_ARGS
)
1081 /* FIXME: mandated by parser. */
1083 if (-1 == (i
= arg_getattr(MDOC_Std
, node
)))
1084 errx(1, "expected -std argument");
1085 if (1 != node
->args
->argv
[i
].sz
)
1086 errx(1, "expected -std argument");
1089 term_word(p
, "The");
1091 p
->flags
|= ttypes
[TTYPE_FUNC_NAME
];
1092 term_word(p
, *node
->args
->argv
[i
].value
);
1093 p
->flags
&= ~ttypes
[TTYPE_FUNC_NAME
];
1094 p
->flags
|= TERMP_NOSPACE
;
1096 term_word(p
, "() function returns the value 0 if successful;");
1097 term_word(p
, "otherwise the value -1 is returned and the");
1098 term_word(p
, "global variable");
1100 p
->flags
|= ttypes
[TTYPE_VAR_DECL
];
1101 term_word(p
, "errno");
1102 p
->flags
&= ~ttypes
[TTYPE_VAR_DECL
];
1104 term_word(p
, "is set to indicate the error.");
1112 termp_ex_pre(DECL_ARGS
)
1116 /* FIXME: mandated by parser? */
1118 if (-1 == (i
= arg_getattr(MDOC_Std
, node
)))
1119 errx(1, "expected -std argument");
1120 if (1 != node
->args
->argv
[i
].sz
)
1121 errx(1, "expected -std argument");
1123 term_word(p
, "The");
1124 p
->flags
|= ttypes
[TTYPE_PROG
];
1125 term_word(p
, *node
->args
->argv
[i
].value
);
1126 p
->flags
&= ~ttypes
[TTYPE_PROG
];
1127 term_word(p
, "utility exits 0 on success, and >0 if an error occurs.");
1135 termp_nd_pre(DECL_ARGS
)
1138 term_word(p
, "\\-");
1145 termp_bl_post(DECL_ARGS
)
1148 if (MDOC_BLOCK
== node
->type
)
1155 termp_op_post(DECL_ARGS
)
1158 if (MDOC_BODY
!= node
->type
)
1160 p
->flags
|= TERMP_NOSPACE
;
1161 term_word(p
, "\\(rB");
1167 termp_xr_pre(DECL_ARGS
)
1169 const struct mdoc_node
*n
;
1171 assert(node
->child
&& MDOC_TEXT
== node
->child
->type
);
1174 term_word(p
, n
->string
);
1175 if (NULL
== (n
= n
->next
))
1177 p
->flags
|= TERMP_NOSPACE
;
1179 p
->flags
|= TERMP_NOSPACE
;
1180 term_word(p
, n
->string
);
1181 p
->flags
|= TERMP_NOSPACE
;
1189 termp_vt_pre(DECL_ARGS
)
1192 /* FIXME: this can be "type name". */
1193 TERMPAIR_SETFLAG(p
, pair
, ttypes
[TTYPE_VAR_DECL
]);
1200 termp_vt_post(DECL_ARGS
)
1203 if (node
->sec
== SEC_SYNOPSIS
)
1210 termp_fd_pre(DECL_ARGS
)
1214 * FIXME: this naming is bad. This value is used, in general,
1215 * for the #include header or other preprocessor statement.
1217 TERMPAIR_SETFLAG(p
, pair
, ttypes
[TTYPE_FUNC_DECL
]);
1224 termp_fd_post(DECL_ARGS
)
1227 if (node
->sec
!= SEC_SYNOPSIS
)
1230 if (node
->next
&& MDOC_Fd
!= node
->next
->tok
)
1237 termp_sh_pre(DECL_ARGS
)
1240 switch (node
->type
) {
1243 TERMPAIR_SETFLAG(p
, pair
, ttypes
[TTYPE_SECTION
]);
1257 termp_sh_post(DECL_ARGS
)
1260 switch (node
->type
) {
1276 termp_op_pre(DECL_ARGS
)
1279 switch (node
->type
) {
1281 term_word(p
, "\\(lB");
1282 p
->flags
|= TERMP_NOSPACE
;
1293 termp_bt_pre(DECL_ARGS
)
1296 term_word(p
, "is currently in beta test.");
1303 termp_lb_pre(DECL_ARGS
)
1307 assert(node
->child
&& MDOC_TEXT
== node
->child
->type
);
1308 lb
= mdoc_a2lib(node
->child
->string
);
1313 term_word(p
, "library");
1320 termp_lb_post(DECL_ARGS
)
1329 termp_ud_pre(DECL_ARGS
)
1332 term_word(p
, "currently under development.");
1339 termp_d1_pre(DECL_ARGS
)
1342 if (MDOC_BLOCK
!= node
->type
)
1345 pair
->offset
= INDENT
+ 1;
1346 p
->offset
+= pair
->offset
;
1353 termp_d1_post(DECL_ARGS
)
1356 if (MDOC_BLOCK
!= node
->type
)
1359 p
->offset
-= pair
->offset
;
1365 termp_aq_pre(DECL_ARGS
)
1368 if (MDOC_BODY
!= node
->type
)
1370 term_word(p
, "\\(la");
1371 p
->flags
|= TERMP_NOSPACE
;
1378 termp_aq_post(DECL_ARGS
)
1381 if (MDOC_BODY
!= node
->type
)
1383 p
->flags
|= TERMP_NOSPACE
;
1384 term_word(p
, "\\(ra");
1390 termp_ft_pre(DECL_ARGS
)
1393 if (SEC_SYNOPSIS
== node
->sec
)
1394 if (node
->prev
&& MDOC_Fo
== node
->prev
->tok
)
1396 TERMPAIR_SETFLAG(p
, pair
, ttypes
[TTYPE_FUNC_TYPE
]);
1403 termp_ft_post(DECL_ARGS
)
1406 if (SEC_SYNOPSIS
== node
->sec
)
1413 termp_fn_pre(DECL_ARGS
)
1415 const struct mdoc_node
*n
;
1417 assert(node
->child
&& MDOC_TEXT
== node
->child
->type
);
1419 /* FIXME: can be "type funcname" "type varname"... */
1421 p
->flags
|= ttypes
[TTYPE_FUNC_NAME
];
1422 term_word(p
, node
->child
->string
);
1423 p
->flags
&= ~ttypes
[TTYPE_FUNC_NAME
];
1425 p
->flags
|= TERMP_NOSPACE
;
1428 for (n
= node
->child
->next
; n
; n
= n
->next
) {
1429 p
->flags
|= ttypes
[TTYPE_FUNC_ARG
];
1430 term_word(p
, n
->string
);
1431 p
->flags
&= ~ttypes
[TTYPE_FUNC_ARG
];
1438 if (SEC_SYNOPSIS
== node
->sec
)
1447 termp_fn_post(DECL_ARGS
)
1450 if (node
->sec
== SEC_SYNOPSIS
&& node
->next
)
1458 termp_sx_pre(DECL_ARGS
)
1461 TERMPAIR_SETFLAG(p
, pair
, ttypes
[TTYPE_LINK
]);
1468 termp_fa_pre(DECL_ARGS
)
1470 struct mdoc_node
*n
;
1472 if (node
->parent
->tok
!= MDOC_Fo
) {
1473 TERMPAIR_SETFLAG(p
, pair
, ttypes
[TTYPE_FUNC_ARG
]);
1477 for (n
= node
->child
; n
; n
= n
->next
) {
1478 p
->flags
|= ttypes
[TTYPE_FUNC_ARG
];
1479 term_word(p
, n
->string
);
1480 p
->flags
&= ~ttypes
[TTYPE_FUNC_ARG
];
1485 if (node
->child
&& node
->next
&& node
->next
->tok
== MDOC_Fa
)
1494 termp_va_pre(DECL_ARGS
)
1497 TERMPAIR_SETFLAG(p
, pair
, ttypes
[TTYPE_VAR_DECL
]);
1504 termp_bd_pre(DECL_ARGS
)
1509 * This is fairly tricky due primarily to crappy documentation.
1510 * If -ragged or -filled are specified, the block does nothing
1511 * but change the indentation.
1513 * If, on the other hand, -unfilled or -literal are specified,
1514 * then the game changes. Text is printed exactly as entered in
1515 * the display: if a macro line, a newline is appended to the
1516 * line. Blank lines are allowed.
1519 if (MDOC_BLOCK
== node
->type
)
1520 return(fmt_block_vspace(p
, node
, node
));
1521 else if (MDOC_BODY
!= node
->type
)
1524 /* FIXME: display type should be mandated by parser. */
1526 if (NULL
== node
->parent
->args
)
1527 errx(1, "missing display type");
1529 pair
->offset
= p
->offset
;
1531 for (type
= -1, i
= 0;
1532 i
< (int)node
->parent
->args
->argc
; i
++) {
1533 switch (node
->parent
->args
->argv
[i
].arg
) {
1538 case (MDOC_Unfilled
):
1540 case (MDOC_Literal
):
1541 type
= node
->parent
->args
->argv
[i
].arg
;
1542 i
= (int)node
->parent
->args
->argc
;
1549 if (NULL
== node
->parent
->args
)
1550 errx(1, "missing display type");
1552 i
= arg_getattr(MDOC_Offset
, node
->parent
);
1554 if (1 != node
->parent
->args
->argv
[i
].sz
)
1555 errx(1, "expected single value");
1556 p
->offset
+= arg_offset(&node
->parent
->args
->argv
[i
]);
1560 case (MDOC_Literal
):
1562 case (MDOC_Unfilled
):
1569 * Tricky. Iterate through all children. If we're on a
1570 * different parse line, append a newline and then the contents.
1574 p
->flags
|= TERMP_LITERAL
;
1575 ln
= node
->child
? node
->child
->line
: 0;
1577 for (node
= node
->child
; node
; node
= node
->next
) {
1578 if (ln
< node
->line
) {
1580 p
->flags
|= TERMP_NOSPACE
;
1583 print_node(p
, pair
, meta
, node
);
1592 termp_bd_post(DECL_ARGS
)
1595 if (MDOC_BODY
!= node
->type
)
1599 p
->flags
&= ~TERMP_LITERAL
;
1600 p
->offset
= pair
->offset
;
1601 p
->flags
|= TERMP_NOSPACE
;
1607 termp_qq_pre(DECL_ARGS
)
1610 if (MDOC_BODY
!= node
->type
)
1613 p
->flags
|= TERMP_NOSPACE
;
1620 termp_qq_post(DECL_ARGS
)
1623 if (MDOC_BODY
!= node
->type
)
1625 p
->flags
|= TERMP_NOSPACE
;
1632 termp_bsx_pre(DECL_ARGS
)
1635 term_word(p
, "BSDI BSD/OS");
1642 termp_bx_post(DECL_ARGS
)
1646 p
->flags
|= TERMP_NOSPACE
;
1647 term_word(p
, "BSD");
1653 termp_ox_pre(DECL_ARGS
)
1656 term_word(p
, "OpenBSD");
1663 termp_dx_pre(DECL_ARGS
)
1666 term_word(p
, "DragonFly");
1673 termp_ux_pre(DECL_ARGS
)
1676 term_word(p
, "UNIX");
1683 termp_fx_pre(DECL_ARGS
)
1686 term_word(p
, "FreeBSD");
1693 termp_nx_pre(DECL_ARGS
)
1696 term_word(p
, "NetBSD");
1703 termp_sq_pre(DECL_ARGS
)
1706 if (MDOC_BODY
!= node
->type
)
1708 term_word(p
, "\\(oq");
1709 p
->flags
|= TERMP_NOSPACE
;
1716 termp_sq_post(DECL_ARGS
)
1719 if (MDOC_BODY
!= node
->type
)
1721 p
->flags
|= TERMP_NOSPACE
;
1722 term_word(p
, "\\(aq");
1728 termp_pf_pre(DECL_ARGS
)
1731 p
->flags
|= TERMP_IGNDELIM
;
1738 termp_pf_post(DECL_ARGS
)
1741 p
->flags
&= ~TERMP_IGNDELIM
;
1742 p
->flags
|= TERMP_NOSPACE
;
1748 termp_ss_pre(DECL_ARGS
)
1751 switch (node
->type
) {
1758 TERMPAIR_SETFLAG(p
, pair
, ttypes
[TTYPE_SSECTION
]);
1759 p
->offset
= HALFINDENT
;
1771 termp_ss_post(DECL_ARGS
)
1774 switch (node
->type
) {
1787 termp_pa_pre(DECL_ARGS
)
1790 TERMPAIR_SETFLAG(p
, pair
, ttypes
[TTYPE_FILE
]);
1797 termp_em_pre(DECL_ARGS
)
1800 TERMPAIR_SETFLAG(p
, pair
, ttypes
[TTYPE_EMPH
]);
1807 termp_cd_pre(DECL_ARGS
)
1810 TERMPAIR_SETFLAG(p
, pair
, ttypes
[TTYPE_CONFIG
]);
1818 termp_cm_pre(DECL_ARGS
)
1821 TERMPAIR_SETFLAG(p
, pair
, ttypes
[TTYPE_CMD_FLAG
]);
1828 termp_ic_pre(DECL_ARGS
)
1831 TERMPAIR_SETFLAG(p
, pair
, ttypes
[TTYPE_CMD
]);
1838 termp_in_pre(DECL_ARGS
)
1841 /* XXX This conforms to new-groff style. */
1842 TERMPAIR_SETFLAG(p
, pair
, ttypes
[TTYPE_INCLUDE
]);
1844 if (SEC_SYNOPSIS
== node
->sec
)
1845 term_word(p
, "#include");
1848 p
->flags
|= TERMP_NOSPACE
;
1855 termp_in_post(DECL_ARGS
)
1858 p
->flags
|= TERMP_NOSPACE
;
1861 if (SEC_SYNOPSIS
!= node
->sec
)
1866 * XXX Not entirely correct. If `.In foo bar' is specified in
1867 * the SYNOPSIS section, then it produces a single break after
1868 * the <foo>; mandoc asserts a vertical space. Since this
1869 * construction is rarely used, I think it's fine.
1871 if (node
->next
&& MDOC_In
!= node
->next
->tok
)
1878 termp_at_pre(DECL_ARGS
)
1885 att
= mdoc_a2att(node
->child
->string
);
1896 termp_brq_pre(DECL_ARGS
)
1899 if (MDOC_BODY
!= node
->type
)
1901 term_word(p
, "\\(lC");
1902 p
->flags
|= TERMP_NOSPACE
;
1909 termp_brq_post(DECL_ARGS
)
1912 if (MDOC_BODY
!= node
->type
)
1914 p
->flags
|= TERMP_NOSPACE
;
1915 term_word(p
, "\\(rC");
1921 termp_bq_pre(DECL_ARGS
)
1924 if (MDOC_BODY
!= node
->type
)
1926 term_word(p
, "\\(lB");
1927 p
->flags
|= TERMP_NOSPACE
;
1934 termp_bq_post(DECL_ARGS
)
1937 if (MDOC_BODY
!= node
->type
)
1939 p
->flags
|= TERMP_NOSPACE
;
1940 term_word(p
, "\\(rB");
1946 termp_pq_pre(DECL_ARGS
)
1949 if (MDOC_BODY
!= node
->type
)
1951 term_word(p
, "\\&(");
1952 p
->flags
|= TERMP_NOSPACE
;
1959 termp_pq_post(DECL_ARGS
)
1962 if (MDOC_BODY
!= node
->type
)
1970 termp_fo_pre(DECL_ARGS
)
1972 const struct mdoc_node
*n
;
1974 if (MDOC_BODY
== node
->type
) {
1976 p
->flags
|= TERMP_NOSPACE
;
1978 } else if (MDOC_HEAD
!= node
->type
)
1981 /* XXX - groff shows only first parameter */
1983 p
->flags
|= ttypes
[TTYPE_FUNC_NAME
];
1984 for (n
= node
->child
; n
; n
= n
->next
) {
1985 assert(MDOC_TEXT
== n
->type
);
1986 term_word(p
, n
->string
);
1988 p
->flags
&= ~ttypes
[TTYPE_FUNC_NAME
];
1996 termp_fo_post(DECL_ARGS
)
1999 if (MDOC_BODY
!= node
->type
)
2001 p
->flags
|= TERMP_NOSPACE
;
2003 p
->flags
|= TERMP_NOSPACE
;
2011 termp_bf_pre(DECL_ARGS
)
2013 const struct mdoc_node
*n
;
2015 if (MDOC_HEAD
== node
->type
) {
2017 } else if (MDOC_BLOCK
!= node
->type
)
2020 if (NULL
== (n
= node
->head
->child
)) {
2021 if (arg_hasattr(MDOC_Emphasis
, node
))
2022 TERMPAIR_SETFLAG(p
, pair
, ttypes
[TTYPE_EMPH
]);
2023 else if (arg_hasattr(MDOC_Symbolic
, node
))
2024 TERMPAIR_SETFLAG(p
, pair
, ttypes
[TTYPE_SYMB
]);
2029 assert(MDOC_TEXT
== n
->type
);
2030 if (0 == strcmp("Em", n
->string
))
2031 TERMPAIR_SETFLAG(p
, pair
, ttypes
[TTYPE_EMPH
]);
2032 else if (0 == strcmp("Sy", n
->string
))
2033 TERMPAIR_SETFLAG(p
, pair
, ttypes
[TTYPE_EMPH
]);
2041 termp_sy_pre(DECL_ARGS
)
2044 TERMPAIR_SETFLAG(p
, pair
, ttypes
[TTYPE_SYMB
]);
2051 termp_ms_pre(DECL_ARGS
)
2054 TERMPAIR_SETFLAG(p
, pair
, ttypes
[TTYPE_SYMBOL
]);
2062 termp_sm_pre(DECL_ARGS
)
2065 assert(node
->child
&& MDOC_TEXT
== node
->child
->type
);
2066 if (0 == strcmp("on", node
->child
->string
)) {
2067 p
->flags
&= ~TERMP_NONOSPACE
;
2068 p
->flags
&= ~TERMP_NOSPACE
;
2070 p
->flags
|= TERMP_NONOSPACE
;
2078 termp_ap_pre(DECL_ARGS
)
2081 p
->flags
|= TERMP_NOSPACE
;
2082 term_word(p
, "\\(aq");
2083 p
->flags
|= TERMP_NOSPACE
;
2090 termp__j_pre(DECL_ARGS
)
2093 TERMPAIR_SETFLAG(p
, pair
, ttypes
[TTYPE_REF_JOURNAL
]);
2100 termp__t_pre(DECL_ARGS
)
2104 p
->flags
|= TERMP_NOSPACE
;
2111 termp__t_post(DECL_ARGS
)
2114 p
->flags
|= TERMP_NOSPACE
;
2116 termp____post(p
, pair
, meta
, node
);
2122 termp____post(DECL_ARGS
)
2125 p
->flags
|= TERMP_NOSPACE
;
2126 term_word(p
, node
->next
? "," : ".");
2132 termp_lk_pre(DECL_ARGS
)
2134 const struct mdoc_node
*n
;
2136 assert(node
->child
);
2139 if (NULL
== n
->next
) {
2140 TERMPAIR_SETFLAG(p
, pair
, ttypes
[TTYPE_LINK_ANCHOR
]);
2144 p
->flags
|= ttypes
[TTYPE_LINK_ANCHOR
];
2145 term_word(p
, n
->string
);
2146 p
->flags
|= TERMP_NOSPACE
;
2148 p
->flags
&= ~ttypes
[TTYPE_LINK_ANCHOR
];
2150 p
->flags
|= ttypes
[TTYPE_LINK_TEXT
];
2151 for (n
= n
->next
; n
; n
= n
->next
)
2152 term_word(p
, n
->string
);
2154 p
->flags
&= ~ttypes
[TTYPE_LINK_TEXT
];
2161 termp_mt_pre(DECL_ARGS
)
2164 TERMPAIR_SETFLAG(p
, pair
, ttypes
[TTYPE_LINK_ANCHOR
]);