]>
git.cameronkatri.com Git - mandoc.git/blob - mdoc_term.c
1 /* $Id: mdoc_term.c,v 1.28 2009/07/12 17:25:07 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 */
84 * This is used to preserve a style of value across a macro, instead of
85 * losing it while the body is processed.
88 struct termpair
*ppair
;
89 int flag
; /* Whether being used. */
90 size_t offset
; /* Left margin. */
91 size_t rmargin
; /* Right margin. */
92 int count
; /* Enum count. */
96 struct termp *p, struct termpair *pair, \
97 const struct mdoc_meta *meta, \
98 const struct mdoc_node *node
100 #define DECL_PRE(name) \
101 static int name##_pre(DECL_ARGS)
102 #define DECL_POST(name) \
103 static void name##_post(DECL_ARGS)
104 #define DECL_PREPOST(name) \
108 DECL_PREPOST(termp__t
);
109 DECL_PREPOST(termp_aq
);
110 DECL_PREPOST(termp_bd
);
111 DECL_PREPOST(termp_bq
);
112 DECL_PREPOST(termp_brq
);
113 DECL_PREPOST(termp_d1
);
114 DECL_PREPOST(termp_dq
);
115 DECL_PREPOST(termp_fd
);
116 DECL_PREPOST(termp_fn
);
117 DECL_PREPOST(termp_fo
);
118 DECL_PREPOST(termp_ft
);
119 DECL_PREPOST(termp_in
);
120 DECL_PREPOST(termp_it
);
121 DECL_PREPOST(termp_lb
);
122 DECL_PREPOST(termp_op
);
123 DECL_PREPOST(termp_pf
);
124 DECL_PREPOST(termp_pq
);
125 DECL_PREPOST(termp_qq
);
126 DECL_PREPOST(termp_sh
);
127 DECL_PREPOST(termp_ss
);
128 DECL_PREPOST(termp_sq
);
129 DECL_PREPOST(termp_vt
);
168 int (*pre
)(DECL_ARGS
);
169 void (*post
)(DECL_ARGS
);
172 static const struct termact termacts
[MDOC_MAX
] = {
173 { termp_ap_pre
, NULL
}, /* Ap */
174 { NULL
, NULL
}, /* Dd */
175 { NULL
, NULL
}, /* Dt */
176 { NULL
, NULL
}, /* Os */
177 { termp_sh_pre
, termp_sh_post
}, /* Sh */
178 { termp_ss_pre
, termp_ss_post
}, /* Ss */
179 { termp_pp_pre
, NULL
}, /* Pp */
180 { termp_d1_pre
, termp_d1_post
}, /* D1 */
181 { termp_d1_pre
, termp_d1_post
}, /* Dl */
182 { termp_bd_pre
, termp_bd_post
}, /* Bd */
183 { NULL
, NULL
}, /* Ed */
184 { NULL
, termp_bl_post
}, /* Bl */
185 { NULL
, NULL
}, /* El */
186 { termp_it_pre
, termp_it_post
}, /* It */
187 { NULL
, NULL
}, /* Ad */
188 { NULL
, NULL
}, /* An */
189 { termp_ar_pre
, NULL
}, /* Ar */
190 { termp_cd_pre
, NULL
}, /* Cd */
191 { termp_cm_pre
, NULL
}, /* Cm */
192 { NULL
, NULL
}, /* Dv */
193 { NULL
, NULL
}, /* Er */
194 { NULL
, NULL
}, /* Ev */
195 { termp_ex_pre
, NULL
}, /* Ex */
196 { termp_fa_pre
, NULL
}, /* Fa */
197 { termp_fd_pre
, termp_fd_post
}, /* Fd */
198 { termp_fl_pre
, NULL
}, /* Fl */
199 { termp_fn_pre
, termp_fn_post
}, /* Fn */
200 { termp_ft_pre
, termp_ft_post
}, /* Ft */
201 { termp_ic_pre
, NULL
}, /* Ic */
202 { termp_in_pre
, termp_in_post
}, /* In */
203 { NULL
, NULL
}, /* Li */
204 { termp_nd_pre
, NULL
}, /* Nd */
205 { termp_nm_pre
, NULL
}, /* Nm */
206 { termp_op_pre
, termp_op_post
}, /* Op */
207 { NULL
, NULL
}, /* Ot */
208 { termp_pa_pre
, NULL
}, /* Pa */
209 { termp_rv_pre
, NULL
}, /* Rv */
210 { termp_st_pre
, NULL
}, /* St */
211 { termp_va_pre
, NULL
}, /* Va */
212 { termp_vt_pre
, termp_vt_post
}, /* Vt */
213 { termp_xr_pre
, NULL
}, /* Xr */
214 { NULL
, termp____post
}, /* %A */
215 { NULL
, termp____post
}, /* %B */
216 { NULL
, termp____post
}, /* %D */
217 { NULL
, termp____post
}, /* %I */
218 { termp__j_pre
, termp____post
}, /* %J */
219 { NULL
, termp____post
}, /* %N */
220 { NULL
, termp____post
}, /* %O */
221 { NULL
, termp____post
}, /* %P */
222 { NULL
, termp____post
}, /* %R */
223 { termp__t_pre
, termp__t_post
}, /* %T */
224 { NULL
, termp____post
}, /* %V */
225 { NULL
, NULL
}, /* Ac */
226 { termp_aq_pre
, termp_aq_post
}, /* Ao */
227 { termp_aq_pre
, termp_aq_post
}, /* Aq */
228 { termp_at_pre
, NULL
}, /* At */
229 { NULL
, NULL
}, /* Bc */
230 { termp_bf_pre
, NULL
}, /* Bf */
231 { termp_bq_pre
, termp_bq_post
}, /* Bo */
232 { termp_bq_pre
, termp_bq_post
}, /* Bq */
233 { termp_xx_pre
, NULL
}, /* Bsx */
234 { NULL
, termp_bx_post
}, /* Bx */
235 { NULL
, NULL
}, /* Db */
236 { NULL
, NULL
}, /* Dc */
237 { termp_dq_pre
, termp_dq_post
}, /* Do */
238 { termp_dq_pre
, termp_dq_post
}, /* Dq */
239 { NULL
, NULL
}, /* Ec */
240 { NULL
, NULL
}, /* Ef */
241 { termp_em_pre
, NULL
}, /* Em */
242 { NULL
, NULL
}, /* Eo */
243 { termp_xx_pre
, NULL
}, /* Fx */
244 { termp_ms_pre
, NULL
}, /* Ms */
245 { NULL
, NULL
}, /* No */
246 { termp_ns_pre
, NULL
}, /* Ns */
247 { termp_xx_pre
, NULL
}, /* Nx */
248 { termp_xx_pre
, NULL
}, /* Ox */
249 { NULL
, NULL
}, /* Pc */
250 { termp_pf_pre
, termp_pf_post
}, /* Pf */
251 { termp_pq_pre
, termp_pq_post
}, /* Po */
252 { termp_pq_pre
, termp_pq_post
}, /* Pq */
253 { NULL
, NULL
}, /* Qc */
254 { termp_sq_pre
, termp_sq_post
}, /* Ql */
255 { termp_qq_pre
, termp_qq_post
}, /* Qo */
256 { termp_qq_pre
, termp_qq_post
}, /* Qq */
257 { NULL
, NULL
}, /* Re */
258 { termp_rs_pre
, NULL
}, /* Rs */
259 { NULL
, NULL
}, /* Sc */
260 { termp_sq_pre
, termp_sq_post
}, /* So */
261 { termp_sq_pre
, termp_sq_post
}, /* Sq */
262 { termp_sm_pre
, NULL
}, /* Sm */
263 { termp_sx_pre
, NULL
}, /* Sx */
264 { termp_sy_pre
, NULL
}, /* Sy */
265 { NULL
, NULL
}, /* Tn */
266 { termp_xx_pre
, NULL
}, /* Ux */
267 { NULL
, NULL
}, /* Xc */
268 { NULL
, NULL
}, /* Xo */
269 { termp_fo_pre
, termp_fo_post
}, /* Fo */
270 { NULL
, NULL
}, /* Fc */
271 { termp_op_pre
, termp_op_post
}, /* Oo */
272 { NULL
, NULL
}, /* Oc */
273 { NULL
, NULL
}, /* Bk */
274 { NULL
, NULL
}, /* Ek */
275 { termp_bt_pre
, NULL
}, /* Bt */
276 { NULL
, NULL
}, /* Hf */
277 { NULL
, NULL
}, /* Fr */
278 { termp_ud_pre
, NULL
}, /* Ud */
279 { termp_lb_pre
, termp_lb_post
}, /* Lb */
280 { termp_pp_pre
, NULL
}, /* Lp */
281 { termp_lk_pre
, NULL
}, /* Lk */
282 { termp_mt_pre
, NULL
}, /* Mt */
283 { termp_brq_pre
, termp_brq_post
}, /* Brq */
284 { termp_brq_pre
, termp_brq_post
}, /* Bro */
285 { NULL
, NULL
}, /* Brc */
286 { NULL
, NULL
}, /* %C */
287 { NULL
, NULL
}, /* Es */
288 { NULL
, NULL
}, /* En */
289 { termp_xx_pre
, NULL
}, /* Dx */
290 { NULL
, NULL
}, /* %Q */
294 extern size_t strlcpy(char *, const char *, size_t);
295 extern size_t strlcat(char *, const char *, size_t);
298 static int arg_hasattr(int, const struct mdoc_node
*);
299 static int arg_getattrs(const int *, int *, size_t,
300 const struct mdoc_node
*);
301 static int arg_getattr(int, const struct mdoc_node
*);
302 static size_t arg_offset(const struct mdoc_argv
*);
303 static size_t arg_width(const struct mdoc_argv
*, int);
304 static int arg_listtype(const struct mdoc_node
*);
305 static int fmt_block_vspace(struct termp
*,
306 const struct mdoc_node
*,
307 const struct mdoc_node
*);
308 static void print_node(DECL_ARGS
);
309 static void print_head(struct termp
*,
310 const struct mdoc_meta
*);
311 static void print_body(DECL_ARGS
);
312 static void print_foot(struct termp
*,
313 const struct mdoc_meta
*);
317 mdoc_run(struct termp
*p
, const struct mdoc
*m
)
320 * Main output function. When this is called, assume that the
321 * tree is properly formed.
324 print_head(p
, mdoc_meta(m
));
325 assert(mdoc_node(m
));
326 assert(MDOC_ROOT
== mdoc_node(m
)->type
);
327 if (mdoc_node(m
)->child
)
328 print_body(p
, NULL
, mdoc_meta(m
), mdoc_node(m
)->child
);
329 print_foot(p
, mdoc_meta(m
));
335 print_body(DECL_ARGS
)
338 print_node(p
, pair
, meta
, node
);
341 print_body(p
, pair
, meta
, node
->next
);
346 print_node(DECL_ARGS
)
349 struct termpair npair
;
351 /* Pre-processing. */
355 npair
.offset
= npair
.rmargin
= 0;
359 if (MDOC_TEXT
!= node
->type
) {
360 if (termacts
[node
->tok
].pre
)
361 if ( ! (*termacts
[node
->tok
].pre
)(p
, &npair
, meta
, node
))
363 } else /* MDOC_TEXT == node->type */
364 term_word(p
, node
->string
);
368 p
->flags
|= npair
.flag
;
370 if (dochild
&& node
->child
)
371 print_body(p
, &npair
, meta
, node
->child
);
373 p
->flags
&= ~npair
.flag
;
375 /* Post-processing. */
377 if (MDOC_TEXT
!= node
->type
)
378 if (termacts
[node
->tok
].post
)
379 (*termacts
[node
->tok
].post
)(p
, &npair
, meta
, node
);
384 print_foot(struct termp
*p
, const struct mdoc_meta
*meta
)
390 * Output the footer in new-groff style, that is, three columns
391 * with the middle being the manual date and flanking columns
392 * being the operating system:
397 if (NULL
== (buf
= malloc(p
->rmargin
)))
399 if (NULL
== (os
= malloc(p
->rmargin
)))
402 tm
= localtime(&meta
->date
);
404 if (0 == strftime(buf
, p
->rmargin
, "%B %d, %Y", tm
))
407 (void)strlcpy(os
, meta
->os
, p
->rmargin
);
412 p
->rmargin
= (p
->maxrmargin
- strlen(buf
) + 1) / 2;
413 p
->flags
|= TERMP_NOSPACE
| TERMP_NOBREAK
;
418 p
->offset
= p
->rmargin
;
419 p
->rmargin
= p
->maxrmargin
- strlen(os
);
420 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
425 p
->offset
= p
->rmargin
;
426 p
->rmargin
= p
->maxrmargin
;
427 p
->flags
&= ~TERMP_NOBREAK
;
428 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
434 p
->rmargin
= p
->maxrmargin
;
443 print_head(struct termp
*p
, const struct mdoc_meta
*meta
)
447 p
->rmargin
= p
->maxrmargin
;
450 if (NULL
== (buf
= malloc(p
->rmargin
)))
452 if (NULL
== (title
= malloc(p
->rmargin
)))
456 * The header is strange. It has three components, which are
457 * really two with the first duplicated. It goes like this:
459 * IDENTIFIER TITLE IDENTIFIER
461 * The IDENTIFIER is NAME(SECTION), which is the command-name
462 * (if given, or "unknown" if not) followed by the manual page
463 * section. These are given in `Dt'. The TITLE is a free-form
464 * string depending on the manual volume. If not specified, it
465 * switches on the manual section.
469 (void)strlcpy(buf
, meta
->vol
, p
->rmargin
);
472 (void)strlcat(buf
, " (", p
->rmargin
);
473 (void)strlcat(buf
, meta
->arch
, p
->rmargin
);
474 (void)strlcat(buf
, ")", p
->rmargin
);
477 (void)snprintf(title
, p
->rmargin
, "%s(%d)",
478 meta
->title
, meta
->msec
);
481 p
->rmargin
= (p
->maxrmargin
- strlen(buf
) + 1) / 2;
482 p
->flags
|= TERMP_NOBREAK
| TERMP_NOSPACE
;
487 p
->offset
= p
->rmargin
;
488 p
->rmargin
= p
->maxrmargin
- strlen(title
);
489 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
494 p
->offset
= p
->rmargin
;
495 p
->rmargin
= p
->maxrmargin
;
496 p
->flags
&= ~TERMP_NOBREAK
;
497 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
503 p
->rmargin
= p
->maxrmargin
;
504 p
->flags
&= ~TERMP_NOSPACE
;
512 arg_width(const struct mdoc_argv
*arg
, int pos
)
517 assert(pos
< (int)arg
->sz
&& pos
>= 0);
518 assert(arg
->value
[pos
]);
520 if (0 == (len
= (int)strlen(arg
->value
[pos
])))
523 for (i
= 0; i
< len
- 1; i
++)
524 if ( ! isdigit((u_char
)arg
->value
[pos
][i
]))
528 if ('n' == arg
->value
[pos
][len
- 1] ||
529 'm' == arg
->value
[pos
][len
- 1]) {
530 v
= (size_t)atoi(arg
->value
[pos
]);
535 return(strlen(arg
->value
[pos
]) + 2);
540 arg_listtype(const struct mdoc_node
*n
)
544 assert(MDOC_BLOCK
== n
->type
);
546 len
= (int)(n
->args
? n
->args
->argc
: 0);
548 for (i
= 0; i
< len
; i
++)
549 switch (n
->args
->argv
[i
].arg
) {
569 return(n
->args
->argv
[i
].arg
);
574 /* FIXME: mandated by parser. */
576 errx(1, "list type not supported");
582 arg_offset(const struct mdoc_argv
*arg
)
586 if (0 == strcmp(*arg
->value
, "left"))
588 if (0 == strcmp(*arg
->value
, "indent"))
590 if (0 == strcmp(*arg
->value
, "indent-two"))
591 return((INDENT
+ 1) * 2);
593 /* FIXME: needs to support field-widths (10n, etc.). */
595 return(strlen(*arg
->value
));
600 arg_hasattr(int arg
, const struct mdoc_node
*n
)
603 return(-1 != arg_getattr(arg
, n
));
608 arg_getattr(int v
, const struct mdoc_node
*n
)
612 return(arg_getattrs(&v
, &val
, 1, n
) ? val
: -1);
617 arg_getattrs(const int *keys
, int *vals
,
618 size_t sz
, const struct mdoc_node
*n
)
625 for (k
= i
= 0; i
< (int)n
->args
->argc
; i
++)
626 for (j
= 0; j
< (int)sz
; j
++)
627 if (n
->args
->argv
[i
].arg
== keys
[j
]) {
637 fmt_block_vspace(struct termp
*p
,
638 const struct mdoc_node
*bl
,
639 const struct mdoc_node
*node
)
641 const struct mdoc_node
*n
;
645 if (arg_hasattr(MDOC_Compact
, bl
))
648 for (n
= node
; n
; n
= n
->parent
) {
649 if (MDOC_BLOCK
!= n
->type
)
651 if (MDOC_Ss
== n
->tok
)
653 if (MDOC_Sh
== n
->tok
)
667 termp_dq_pre(DECL_ARGS
)
670 if (MDOC_BODY
!= node
->type
)
673 term_word(p
, "\\(lq");
674 p
->flags
|= TERMP_NOSPACE
;
681 termp_dq_post(DECL_ARGS
)
684 if (MDOC_BODY
!= node
->type
)
687 p
->flags
|= TERMP_NOSPACE
;
688 term_word(p
, "\\(rq");
694 termp_it_pre(DECL_ARGS
)
696 const struct mdoc_node
*bl
, *n
;
698 int i
, type
, keys
[3], vals
[3], sv
;
699 size_t width
, offset
;
701 if (MDOC_BLOCK
== node
->type
)
702 return(fmt_block_vspace(p
, node
->parent
->parent
, node
));
704 bl
= node
->parent
->parent
->parent
;
706 /* Save parent attributes. */
708 pair
->offset
= p
->offset
;
709 pair
->rmargin
= p
->rmargin
;
710 pair
->flag
= p
->flags
;
712 /* Get list width and offset. */
714 keys
[0] = MDOC_Width
;
715 keys
[1] = MDOC_Offset
;
716 keys
[2] = MDOC_Column
;
718 vals
[0] = vals
[1] = vals
[2] = -1;
722 (void)arg_getattrs(keys
, vals
, 3, bl
);
724 type
= arg_listtype(bl
);
726 /* Calculate real width and offset. */
730 if (MDOC_BODY
== node
->type
)
732 for (i
= 0, n
= node
->prev
; n
; n
= n
->prev
, i
++)
734 (&bl
->args
->argv
[vals
[2]], i
);
735 assert(i
< (int)bl
->args
->argv
[vals
[2]].sz
);
736 width
= arg_width(&bl
->args
->argv
[vals
[2]], i
);
738 offset
+= arg_offset(&bl
->args
->argv
[vals
[1]]);
742 width
= arg_width(&bl
->args
->argv
[vals
[0]], 0);
744 offset
+= arg_offset(&bl
->args
->argv
[vals
[1]]);
749 * List-type can override the width in the case of fixed-head
750 * values (bullet, dash/hyphen, enum). Tags need a non-zero
776 * Whitespace control. Inset bodies need an initial space,
777 * while diagonal bodies need two.
785 if (MDOC_BODY
== node
->type
)
786 p
->flags
&= ~TERMP_NOSPACE
;
788 p
->flags
|= TERMP_NOSPACE
;
791 p
->flags
|= TERMP_NOSPACE
;
796 * Style flags. Diagnostic heads need TTYPE_DIAG.
801 if (MDOC_HEAD
== node
->type
)
802 p
->flags
|= ttypes
[TTYPE_DIAG
];
809 * Pad and break control. This is the tricker part. Lists with
810 * set right-margins for the head get TERMP_NOBREAK because, if
811 * they overrun the margin, they wrap to the new margin.
812 * Correspondingly, the body for these types don't left-pad, as
813 * the head will pad out to to the right.
826 if (MDOC_HEAD
== node
->type
)
827 p
->flags
|= TERMP_NOBREAK
;
829 p
->flags
|= TERMP_NOLPAD
;
830 if (MDOC_HEAD
== node
->type
&& MDOC_Tag
== type
)
831 if (NULL
== node
->next
||
832 NULL
== node
->next
->child
)
833 p
->flags
|= TERMP_NONOBREAK
;
836 if (MDOC_HEAD
== node
->type
) {
838 if (MDOC_BODY
== node
->next
->type
)
839 p
->flags
&= ~TERMP_NOBREAK
;
841 p
->flags
|= TERMP_NOBREAK
;
843 p
->flags
|= TERMP_NOLPAD
;
847 if (MDOC_HEAD
== node
->type
)
848 p
->flags
|= TERMP_NOBREAK
;
855 * Margin control. Set-head-width lists have their right
856 * margins shortened. The body for these lists has the offset
857 * necessarily lengthened. Everybody gets the offset.
872 if (MDOC_HEAD
== node
->type
)
873 p
->rmargin
= p
->offset
+ width
;
878 p
->rmargin
= p
->offset
+ width
;
885 * The dash, hyphen, bullet and enum lists all have a special
886 * HEAD character (temporarily bold, in some cases).
890 if (MDOC_HEAD
== node
->type
)
893 p
->flags
|= TERMP_BOLD
;
894 term_word(p
, "\\[bu]");
899 p
->flags
|= TERMP_BOLD
;
900 term_word(p
, "\\(hy");
903 (pair
->ppair
->ppair
->count
)++;
904 (void)snprintf(buf
, sizeof(buf
), "%d.",
905 pair
->ppair
->ppair
->count
);
912 p
->flags
= sv
; /* Restore saved flags. */
915 * If we're not going to process our children, indicate so here.
928 if (MDOC_HEAD
== node
->type
)
932 if (MDOC_BODY
== node
->type
)
945 termp_it_post(DECL_ARGS
)
949 if (MDOC_BODY
!= node
->type
&& MDOC_HEAD
!= node
->type
)
952 type
= arg_listtype(node
->parent
->parent
->parent
);
960 if (MDOC_BODY
== node
->type
)
964 if (MDOC_HEAD
== node
->type
)
972 p
->offset
= pair
->offset
;
973 p
->rmargin
= pair
->rmargin
;
974 p
->flags
= pair
->flag
;
980 termp_nm_pre(DECL_ARGS
)
983 if (SEC_SYNOPSIS
== node
->sec
)
986 pair
->flag
|= ttypes
[TTYPE_PROG
];
987 p
->flags
|= ttypes
[TTYPE_PROG
];
989 if (NULL
== node
->child
)
990 term_word(p
, meta
->name
);
998 termp_fl_pre(DECL_ARGS
)
1001 pair
->flag
|= ttypes
[TTYPE_CMD_FLAG
];
1002 p
->flags
|= ttypes
[TTYPE_CMD_FLAG
];
1003 term_word(p
, "\\-");
1004 p
->flags
|= TERMP_NOSPACE
;
1011 termp_ar_pre(DECL_ARGS
)
1014 pair
->flag
|= ttypes
[TTYPE_CMD_ARG
];
1021 termp_ns_pre(DECL_ARGS
)
1024 p
->flags
|= TERMP_NOSPACE
;
1031 termp_pp_pre(DECL_ARGS
)
1041 termp_st_pre(DECL_ARGS
)
1045 if (node
->child
&& (cp
= mdoc_a2st(node
->child
->string
)))
1053 termp_rs_pre(DECL_ARGS
)
1056 if (MDOC_BLOCK
== node
->type
&& node
->prev
)
1064 termp_rv_pre(DECL_ARGS
)
1068 /* FIXME: mandated by parser. */
1070 if (-1 == (i
= arg_getattr(MDOC_Std
, node
)))
1071 errx(1, "expected -std argument");
1072 if (1 != node
->args
->argv
[i
].sz
)
1073 errx(1, "expected -std argument");
1076 term_word(p
, "The");
1078 p
->flags
|= ttypes
[TTYPE_FUNC_NAME
];
1079 term_word(p
, *node
->args
->argv
[i
].value
);
1080 p
->flags
&= ~ttypes
[TTYPE_FUNC_NAME
];
1081 p
->flags
|= TERMP_NOSPACE
;
1083 term_word(p
, "() function returns the value 0 if successful;");
1084 term_word(p
, "otherwise the value -1 is returned and the");
1085 term_word(p
, "global variable");
1087 p
->flags
|= ttypes
[TTYPE_VAR_DECL
];
1088 term_word(p
, "errno");
1089 p
->flags
&= ~ttypes
[TTYPE_VAR_DECL
];
1091 term_word(p
, "is set to indicate the error.");
1099 termp_ex_pre(DECL_ARGS
)
1103 /* FIXME: mandated by parser? */
1105 if (-1 == (i
= arg_getattr(MDOC_Std
, node
)))
1106 errx(1, "expected -std argument");
1107 if (1 != node
->args
->argv
[i
].sz
)
1108 errx(1, "expected -std argument");
1110 term_word(p
, "The");
1111 p
->flags
|= ttypes
[TTYPE_PROG
];
1112 term_word(p
, *node
->args
->argv
[i
].value
);
1113 p
->flags
&= ~ttypes
[TTYPE_PROG
];
1114 term_word(p
, "utility exits 0 on success, and >0 if an error occurs.");
1122 termp_nd_pre(DECL_ARGS
)
1125 if (MDOC_BODY
!= node
->type
)
1128 * XXX: signed off by jmc@openbsd.org. This technically
1129 * produces a minus sign after the Nd, which is wrong, but is
1130 * consistent with the historic OpenBSD tmac file.
1132 #if defined(__OpenBSD__) || defined(__linux__)
1133 term_word(p
, "\\-");
1135 term_word(p
, "\\(em");
1143 termp_bl_post(DECL_ARGS
)
1146 if (MDOC_BLOCK
== node
->type
)
1153 termp_op_post(DECL_ARGS
)
1156 if (MDOC_BODY
!= node
->type
)
1158 p
->flags
|= TERMP_NOSPACE
;
1159 term_word(p
, "\\(rB");
1165 termp_xr_pre(DECL_ARGS
)
1167 const struct mdoc_node
*n
;
1169 assert(node
->child
&& MDOC_TEXT
== node
->child
->type
);
1172 term_word(p
, n
->string
);
1173 if (NULL
== (n
= n
->next
))
1175 p
->flags
|= TERMP_NOSPACE
;
1177 p
->flags
|= TERMP_NOSPACE
;
1178 term_word(p
, n
->string
);
1179 p
->flags
|= TERMP_NOSPACE
;
1187 termp_vt_pre(DECL_ARGS
)
1190 /* FIXME: this can be "type name". */
1191 pair
->flag
|= TTYPE_VAR_DECL
;
1198 termp_vt_post(DECL_ARGS
)
1201 if (node
->sec
== SEC_SYNOPSIS
)
1208 termp_fd_pre(DECL_ARGS
)
1211 pair
->flag
|= TTYPE_FUNC_DECL
;
1218 termp_fd_post(DECL_ARGS
)
1221 if (node
->sec
!= SEC_SYNOPSIS
)
1224 if (node
->next
&& MDOC_Fd
!= node
->next
->tok
)
1231 termp_sh_pre(DECL_ARGS
)
1234 switch (node
->type
) {
1237 pair
->flag
|= ttypes
[TTYPE_SECTION
];
1251 termp_sh_post(DECL_ARGS
)
1254 switch (node
->type
) {
1270 termp_op_pre(DECL_ARGS
)
1273 switch (node
->type
) {
1275 term_word(p
, "\\(lB");
1276 p
->flags
|= TERMP_NOSPACE
;
1287 termp_bt_pre(DECL_ARGS
)
1290 term_word(p
, "is currently in beta test.");
1297 termp_lb_pre(DECL_ARGS
)
1301 assert(node
->child
&& MDOC_TEXT
== node
->child
->type
);
1302 lb
= mdoc_a2lib(node
->child
->string
);
1307 term_word(p
, "library");
1314 termp_lb_post(DECL_ARGS
)
1323 termp_ud_pre(DECL_ARGS
)
1326 term_word(p
, "currently under development.");
1333 termp_d1_pre(DECL_ARGS
)
1336 if (MDOC_BLOCK
!= node
->type
)
1339 pair
->offset
= INDENT
+ 1;
1340 p
->offset
+= pair
->offset
;
1347 termp_d1_post(DECL_ARGS
)
1350 if (MDOC_BLOCK
!= node
->type
)
1353 p
->offset
-= pair
->offset
;
1359 termp_aq_pre(DECL_ARGS
)
1362 if (MDOC_BODY
!= node
->type
)
1364 term_word(p
, "\\(la");
1365 p
->flags
|= TERMP_NOSPACE
;
1372 termp_aq_post(DECL_ARGS
)
1375 if (MDOC_BODY
!= node
->type
)
1377 p
->flags
|= TERMP_NOSPACE
;
1378 term_word(p
, "\\(ra");
1384 termp_ft_pre(DECL_ARGS
)
1387 if (SEC_SYNOPSIS
== node
->sec
)
1388 if (node
->prev
&& MDOC_Fo
== node
->prev
->tok
)
1390 pair
->flag
|= ttypes
[TTYPE_FUNC_TYPE
];
1397 termp_ft_post(DECL_ARGS
)
1400 if (SEC_SYNOPSIS
== node
->sec
)
1407 termp_fn_pre(DECL_ARGS
)
1409 const struct mdoc_node
*n
;
1411 assert(node
->child
&& MDOC_TEXT
== node
->child
->type
);
1413 /* FIXME: can be "type funcname" "type varname"... */
1415 p
->flags
|= ttypes
[TTYPE_FUNC_NAME
];
1416 term_word(p
, node
->child
->string
);
1417 p
->flags
&= ~ttypes
[TTYPE_FUNC_NAME
];
1419 p
->flags
|= TERMP_NOSPACE
;
1422 for (n
= node
->child
->next
; n
; n
= n
->next
) {
1423 p
->flags
|= ttypes
[TTYPE_FUNC_ARG
];
1424 term_word(p
, n
->string
);
1425 p
->flags
&= ~ttypes
[TTYPE_FUNC_ARG
];
1432 if (SEC_SYNOPSIS
== node
->sec
)
1441 termp_fn_post(DECL_ARGS
)
1444 if (node
->sec
== SEC_SYNOPSIS
&& node
->next
)
1451 termp_sx_pre(DECL_ARGS
)
1454 pair
->flag
|= ttypes
[TTYPE_LINK
];
1461 termp_fa_pre(DECL_ARGS
)
1463 struct mdoc_node
*n
;
1465 if (node
->parent
->tok
!= MDOC_Fo
) {
1466 pair
->flag
|= ttypes
[TTYPE_FUNC_ARG
];
1470 for (n
= node
->child
; n
; n
= n
->next
) {
1471 p
->flags
|= ttypes
[TTYPE_FUNC_ARG
];
1472 term_word(p
, n
->string
);
1473 p
->flags
&= ~ttypes
[TTYPE_FUNC_ARG
];
1478 if (node
->child
&& node
->next
&& node
->next
->tok
== MDOC_Fa
)
1487 termp_va_pre(DECL_ARGS
)
1490 pair
->flag
|= ttypes
[TTYPE_VAR_DECL
];
1497 termp_bd_pre(DECL_ARGS
)
1502 * This is fairly tricky due primarily to crappy documentation.
1503 * If -ragged or -filled are specified, the block does nothing
1504 * but change the indentation.
1506 * If, on the other hand, -unfilled or -literal are specified,
1507 * then the game changes. Text is printed exactly as entered in
1508 * the display: if a macro line, a newline is appended to the
1509 * line. Blank lines are allowed.
1512 if (MDOC_BLOCK
== node
->type
)
1513 return(fmt_block_vspace(p
, node
, node
));
1514 else if (MDOC_BODY
!= node
->type
)
1517 /* FIXME: display type should be mandated by parser. */
1519 if (NULL
== node
->parent
->args
)
1520 errx(1, "missing display type");
1522 pair
->offset
= p
->offset
;
1524 for (type
= -1, i
= 0;
1525 i
< (int)node
->parent
->args
->argc
; i
++) {
1526 switch (node
->parent
->args
->argv
[i
].arg
) {
1531 case (MDOC_Unfilled
):
1533 case (MDOC_Literal
):
1534 type
= node
->parent
->args
->argv
[i
].arg
;
1535 i
= (int)node
->parent
->args
->argc
;
1542 if (NULL
== node
->parent
->args
)
1543 errx(1, "missing display type");
1545 i
= arg_getattr(MDOC_Offset
, node
->parent
);
1547 if (1 != node
->parent
->args
->argv
[i
].sz
)
1548 errx(1, "expected single value");
1549 p
->offset
+= arg_offset(&node
->parent
->args
->argv
[i
]);
1553 case (MDOC_Literal
):
1555 case (MDOC_Unfilled
):
1562 * Tricky. Iterate through all children. If we're on a
1563 * different parse line, append a newline and then the contents.
1567 p
->flags
|= TERMP_LITERAL
;
1568 ln
= node
->child
? node
->child
->line
: 0;
1570 for (node
= node
->child
; node
; node
= node
->next
) {
1571 if (ln
< node
->line
) {
1573 p
->flags
|= TERMP_NOSPACE
;
1576 print_node(p
, pair
, meta
, node
);
1585 termp_bd_post(DECL_ARGS
)
1588 if (MDOC_BODY
!= node
->type
)
1592 p
->flags
&= ~TERMP_LITERAL
;
1593 p
->offset
= pair
->offset
;
1594 p
->flags
|= TERMP_NOSPACE
;
1600 termp_qq_pre(DECL_ARGS
)
1603 if (MDOC_BODY
!= node
->type
)
1606 p
->flags
|= TERMP_NOSPACE
;
1613 termp_qq_post(DECL_ARGS
)
1616 if (MDOC_BODY
!= node
->type
)
1618 p
->flags
|= TERMP_NOSPACE
;
1625 termp_bx_post(DECL_ARGS
)
1629 p
->flags
|= TERMP_NOSPACE
;
1630 term_word(p
, "BSD");
1636 termp_xx_pre(DECL_ARGS
)
1641 switch (node
->tok
) {
1646 pp
= "DragonFlyBSD";
1672 termp_sq_pre(DECL_ARGS
)
1675 if (MDOC_BODY
!= node
->type
)
1677 term_word(p
, "\\(oq");
1678 p
->flags
|= TERMP_NOSPACE
;
1685 termp_sq_post(DECL_ARGS
)
1688 if (MDOC_BODY
!= node
->type
)
1690 p
->flags
|= TERMP_NOSPACE
;
1691 term_word(p
, "\\(aq");
1697 termp_pf_pre(DECL_ARGS
)
1700 p
->flags
|= TERMP_IGNDELIM
;
1707 termp_pf_post(DECL_ARGS
)
1710 p
->flags
&= ~TERMP_IGNDELIM
;
1711 p
->flags
|= TERMP_NOSPACE
;
1717 termp_ss_pre(DECL_ARGS
)
1720 switch (node
->type
) {
1727 pair
->flag
|= ttypes
[TTYPE_SSECTION
];
1728 p
->offset
= HALFINDENT
;
1740 termp_ss_post(DECL_ARGS
)
1743 switch (node
->type
) {
1756 termp_pa_pre(DECL_ARGS
)
1759 pair
->flag
|= ttypes
[TTYPE_FILE
];
1766 termp_em_pre(DECL_ARGS
)
1769 pair
->flag
|= ttypes
[TTYPE_EMPH
];
1776 termp_cd_pre(DECL_ARGS
)
1779 pair
->flag
|= ttypes
[TTYPE_CONFIG
];
1787 termp_cm_pre(DECL_ARGS
)
1790 pair
->flag
|= ttypes
[TTYPE_CMD_FLAG
];
1797 termp_ic_pre(DECL_ARGS
)
1800 pair
->flag
|= ttypes
[TTYPE_CMD
];
1807 termp_in_pre(DECL_ARGS
)
1810 pair
->flag
|= ttypes
[TTYPE_INCLUDE
];
1811 p
->flags
|= ttypes
[TTYPE_INCLUDE
];
1813 if (SEC_SYNOPSIS
== node
->sec
)
1814 term_word(p
, "#include");
1817 p
->flags
|= TERMP_NOSPACE
;
1824 termp_in_post(DECL_ARGS
)
1827 p
->flags
|= TERMP_NOSPACE
;
1830 if (SEC_SYNOPSIS
!= node
->sec
)
1835 * XXX Not entirely correct. If `.In foo bar' is specified in
1836 * the SYNOPSIS section, then it produces a single break after
1837 * the <foo>; mandoc asserts a vertical space. Since this
1838 * construction is rarely used, I think it's fine.
1840 if (node
->next
&& MDOC_In
!= node
->next
->tok
)
1847 termp_at_pre(DECL_ARGS
)
1854 att
= mdoc_a2att(node
->child
->string
);
1865 termp_brq_pre(DECL_ARGS
)
1868 if (MDOC_BODY
!= node
->type
)
1870 term_word(p
, "\\(lC");
1871 p
->flags
|= TERMP_NOSPACE
;
1878 termp_brq_post(DECL_ARGS
)
1881 if (MDOC_BODY
!= node
->type
)
1883 p
->flags
|= TERMP_NOSPACE
;
1884 term_word(p
, "\\(rC");
1890 termp_bq_pre(DECL_ARGS
)
1893 if (MDOC_BODY
!= node
->type
)
1895 term_word(p
, "\\(lB");
1896 p
->flags
|= TERMP_NOSPACE
;
1903 termp_bq_post(DECL_ARGS
)
1906 if (MDOC_BODY
!= node
->type
)
1908 p
->flags
|= TERMP_NOSPACE
;
1909 term_word(p
, "\\(rB");
1915 termp_pq_pre(DECL_ARGS
)
1918 if (MDOC_BODY
!= node
->type
)
1920 term_word(p
, "\\&(");
1921 p
->flags
|= TERMP_NOSPACE
;
1928 termp_pq_post(DECL_ARGS
)
1931 if (MDOC_BODY
!= node
->type
)
1939 termp_fo_pre(DECL_ARGS
)
1941 const struct mdoc_node
*n
;
1943 if (MDOC_BODY
== node
->type
) {
1945 p
->flags
|= TERMP_NOSPACE
;
1947 } else if (MDOC_HEAD
!= node
->type
)
1950 /* XXX - groff shows only first parameter */
1952 p
->flags
|= ttypes
[TTYPE_FUNC_NAME
];
1953 for (n
= node
->child
; n
; n
= n
->next
) {
1954 assert(MDOC_TEXT
== n
->type
);
1955 term_word(p
, n
->string
);
1957 p
->flags
&= ~ttypes
[TTYPE_FUNC_NAME
];
1965 termp_fo_post(DECL_ARGS
)
1968 if (MDOC_BODY
!= node
->type
)
1970 p
->flags
|= TERMP_NOSPACE
;
1972 p
->flags
|= TERMP_NOSPACE
;
1980 termp_bf_pre(DECL_ARGS
)
1982 const struct mdoc_node
*n
;
1984 if (MDOC_HEAD
== node
->type
)
1986 else if (MDOC_BLOCK
!= node
->type
)
1989 if (NULL
== (n
= node
->head
->child
)) {
1990 if (arg_hasattr(MDOC_Emphasis
, node
))
1991 pair
->flag
|= ttypes
[TTYPE_EMPH
];
1992 else if (arg_hasattr(MDOC_Symbolic
, node
))
1993 pair
->flag
|= ttypes
[TTYPE_SYMB
];
1998 assert(MDOC_TEXT
== n
->type
);
1999 if (0 == strcmp("Em", n
->string
))
2000 pair
->flag
|= ttypes
[TTYPE_EMPH
];
2001 else if (0 == strcmp("Sy", n
->string
))
2002 pair
->flag
|= ttypes
[TTYPE_SYMB
];
2010 termp_sy_pre(DECL_ARGS
)
2013 pair
->flag
|= ttypes
[TTYPE_SYMB
];
2020 termp_ms_pre(DECL_ARGS
)
2023 pair
->flag
|= ttypes
[TTYPE_SYMBOL
];
2031 termp_sm_pre(DECL_ARGS
)
2034 assert(node
->child
&& MDOC_TEXT
== node
->child
->type
);
2035 if (0 == strcmp("on", node
->child
->string
)) {
2036 p
->flags
&= ~TERMP_NONOSPACE
;
2037 p
->flags
&= ~TERMP_NOSPACE
;
2039 p
->flags
|= TERMP_NONOSPACE
;
2047 termp_ap_pre(DECL_ARGS
)
2050 p
->flags
|= TERMP_NOSPACE
;
2051 term_word(p
, "\\(aq");
2052 p
->flags
|= TERMP_NOSPACE
;
2059 termp__j_pre(DECL_ARGS
)
2062 pair
->flag
|= ttypes
[TTYPE_REF_JOURNAL
];
2069 termp__t_pre(DECL_ARGS
)
2073 p
->flags
|= TERMP_NOSPACE
;
2080 termp__t_post(DECL_ARGS
)
2083 p
->flags
|= TERMP_NOSPACE
;
2085 termp____post(p
, pair
, meta
, node
);
2091 termp____post(DECL_ARGS
)
2094 p
->flags
|= TERMP_NOSPACE
;
2095 term_word(p
, node
->next
? "," : ".");
2101 termp_lk_pre(DECL_ARGS
)
2103 const struct mdoc_node
*n
;
2105 assert(node
->child
);
2108 if (NULL
== n
->next
) {
2109 pair
->flag
|= ttypes
[TTYPE_LINK_ANCHOR
];
2113 p
->flags
|= ttypes
[TTYPE_LINK_ANCHOR
];
2114 term_word(p
, n
->string
);
2115 p
->flags
|= TERMP_NOSPACE
;
2117 p
->flags
&= ~ttypes
[TTYPE_LINK_ANCHOR
];
2119 p
->flags
|= ttypes
[TTYPE_LINK_TEXT
];
2120 for (n
= n
->next
; n
; n
= n
->next
)
2121 term_word(p
, n
->string
);
2123 p
->flags
&= ~ttypes
[TTYPE_LINK_TEXT
];
2130 termp_mt_pre(DECL_ARGS
)
2133 pair
->flag
|= ttypes
[TTYPE_LINK_ANCHOR
];