]>
git.cameronkatri.com Git - mandoc.git/blob - mdoc_man.c
1 /* $Id: mdoc_man.c,v 1.19 2012/07/08 13:57:53 schwarze Exp $ */
3 * Copyright (c) 2011, 2012 Ingo Schwarze <schwarze@openbsd.org>
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.
31 #define DECL_ARGS const struct mdoc_meta *m, \
32 const struct mdoc_node *n, \
36 int mode_space
; /* spacing mode: 1 = on */
37 int need_space
; /* next word needs prior ws */
38 int mode_keep
; /* currently inside a keep */
39 int need_nl
; /* next word needs prior nl */
43 int (*cond
)(DECL_ARGS
); /* DON'T run actions */
44 int (*pre
)(DECL_ARGS
); /* pre-node action */
45 void (*post
)(DECL_ARGS
); /* post-node action */
46 const char *prefix
; /* pre-node string constant */
47 const char *suffix
; /* post-node string constant */
50 static int cond_body(DECL_ARGS
);
51 static int cond_head(DECL_ARGS
);
52 static void post_bd(DECL_ARGS
);
53 static void post_bk(DECL_ARGS
);
54 static void post_dl(DECL_ARGS
);
55 static void post_enc(DECL_ARGS
);
56 static void post_fa(DECL_ARGS
);
57 static void post_fn(DECL_ARGS
);
58 static void post_fo(DECL_ARGS
);
59 static void post_in(DECL_ARGS
);
60 static void post_lb(DECL_ARGS
);
61 static void post_nm(DECL_ARGS
);
62 static void post_percent(DECL_ARGS
);
63 static void post_pf(DECL_ARGS
);
64 static void post_sect(DECL_ARGS
);
65 static void post_sp(DECL_ARGS
);
66 static void post_vt(DECL_ARGS
);
67 static int pre_ap(DECL_ARGS
);
68 static int pre_bd(DECL_ARGS
);
69 static int pre_bk(DECL_ARGS
);
70 static int pre_br(DECL_ARGS
);
71 static int pre_bx(DECL_ARGS
);
72 static int pre_dl(DECL_ARGS
);
73 static int pre_enc(DECL_ARGS
);
74 static int pre_fa(DECL_ARGS
);
75 static int pre_fn(DECL_ARGS
);
76 static int pre_fo(DECL_ARGS
);
77 static int pre_in(DECL_ARGS
);
78 static int pre_it(DECL_ARGS
);
79 static int pre_nm(DECL_ARGS
);
80 static int pre_ns(DECL_ARGS
);
81 static int pre_pp(DECL_ARGS
);
82 static int pre_sm(DECL_ARGS
);
83 static int pre_sp(DECL_ARGS
);
84 static int pre_sect(DECL_ARGS
);
85 static int pre_vt(DECL_ARGS
);
86 static int pre_ux(DECL_ARGS
);
87 static int pre_xr(DECL_ARGS
);
88 static void print_word(struct mman
*, const char *);
89 static void print_offs(struct mman
*, const char *);
90 static void print_node(DECL_ARGS
);
92 static const struct manact manacts
[MDOC_MAX
+ 1] = {
93 { NULL
, pre_ap
, NULL
, NULL
, NULL
}, /* Ap */
94 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Dd */
95 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Dt */
96 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Os */
97 { NULL
, pre_sect
, post_sect
, ".SH", NULL
}, /* Sh */
98 { NULL
, pre_sect
, post_sect
, ".SS", NULL
}, /* Ss */
99 { NULL
, pre_pp
, NULL
, NULL
, NULL
}, /* Pp */
100 { cond_body
, pre_dl
, post_dl
, NULL
, NULL
}, /* D1 */
101 { cond_body
, pre_dl
, post_dl
, NULL
, NULL
}, /* Dl */
102 { cond_body
, pre_bd
, post_bd
, NULL
, NULL
}, /* Bd */
103 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Ed */
104 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Bl */
105 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* El */
106 { NULL
, pre_it
, NULL
, NULL
, NULL
}, /* _It */
107 { NULL
, pre_enc
, post_enc
, "\\fI", "\\fP" }, /* Ad */
108 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* _An */
109 { NULL
, pre_enc
, post_enc
, "\\fI", "\\fP" }, /* Ar */
110 { NULL
, pre_enc
, post_enc
, "\\fB", "\\fP" }, /* Cd */
111 { NULL
, pre_enc
, post_enc
, "\\fB", "\\fP" }, /* Cm */
112 { NULL
, pre_enc
, post_enc
, "\\fR", "\\fP" }, /* Dv */
113 { NULL
, pre_enc
, post_enc
, "\\fR", "\\fP" }, /* Er */
114 { NULL
, pre_enc
, post_enc
, "\\fR", "\\fP" }, /* Ev */
115 { NULL
, pre_enc
, post_enc
, "The \\fB",
116 "\\fP\nutility exits 0 on success, and >0 if an error occurs."
118 { NULL
, pre_fa
, post_fa
, NULL
, NULL
}, /* Fa */
119 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* _Fd */
120 { NULL
, pre_enc
, post_enc
, "\\fB-", "\\fP" }, /* Fl */
121 { NULL
, pre_fn
, post_fn
, NULL
, NULL
}, /* Fn */
122 { NULL
, pre_enc
, post_enc
, "\\fI", "\\fP" }, /* Ft */
123 { NULL
, pre_enc
, post_enc
, "\\fB", "\\fP" }, /* Ic */
124 { NULL
, pre_in
, post_in
, NULL
, NULL
}, /* In */
125 { NULL
, pre_enc
, post_enc
, "\\fR", "\\fP" }, /* Li */
126 { cond_head
, pre_enc
, NULL
, "\\- ", NULL
}, /* Nd */
127 { NULL
, pre_nm
, post_nm
, NULL
, NULL
}, /* Nm */
128 { cond_body
, pre_enc
, post_enc
, "[", "]" }, /* Op */
129 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Ot */
130 { NULL
, pre_enc
, post_enc
, "\\fI", "\\fP" }, /* Pa */
131 { NULL
, pre_enc
, post_enc
, "The \\fB",
132 "\\fP\nfunction returns the value 0 if successful;\n"
133 "otherwise the value -1 is returned and the global\n"
134 "variable \\fIerrno\\fP is set to indicate the error."
136 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* St */
137 { NULL
, pre_enc
, post_enc
, "\\fI", "\\fP" }, /* Va */
138 { NULL
, pre_vt
, post_vt
, NULL
, NULL
}, /* Vt */
139 { NULL
, pre_xr
, NULL
, NULL
, NULL
}, /* Xr */
140 { NULL
, NULL
, post_percent
, NULL
, NULL
}, /* _%A */
141 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* _%B */
142 { NULL
, NULL
, post_percent
, NULL
, NULL
}, /* _%D */
143 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* _%I */
144 { NULL
, pre_enc
, post_percent
, "\\fI", "\\fP" }, /* %J */
145 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* _%N */
146 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* _%O */
147 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* _%P */
148 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* _%R */
149 { NULL
, pre_enc
, post_percent
, "\"", "\"" }, /* %T */
150 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* _%V */
151 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Ac */
152 { cond_body
, pre_enc
, post_enc
, "<", ">" }, /* Ao */
153 { cond_body
, pre_enc
, post_enc
, "<", ">" }, /* Aq */
154 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* At */
155 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Bc */
156 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* _Bf */
157 { cond_body
, pre_enc
, post_enc
, "[", "]" }, /* Bo */
158 { cond_body
, pre_enc
, post_enc
, "[", "]" }, /* Bq */
159 { NULL
, pre_ux
, NULL
, "BSD/OS", NULL
}, /* Bsx */
160 { NULL
, pre_bx
, NULL
, NULL
, NULL
}, /* Bx */
161 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Db */
162 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Dc */
163 { cond_body
, pre_enc
, post_enc
, "``", "''" }, /* Do */
164 { cond_body
, pre_enc
, post_enc
, "``", "''" }, /* Dq */
165 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* _Ec */
166 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* _Ef */
167 { NULL
, pre_enc
, post_enc
, "\\fI", "\\fP" }, /* Em */
168 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* _Eo */
169 { NULL
, pre_ux
, NULL
, "FreeBSD", NULL
}, /* Fx */
170 { NULL
, pre_enc
, post_enc
, "\\fB", "\\fP" }, /* Ms */
171 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* No */
172 { NULL
, pre_ns
, NULL
, NULL
, NULL
}, /* Ns */
173 { NULL
, pre_ux
, NULL
, "NetBSD", NULL
}, /* Nx */
174 { NULL
, pre_ux
, NULL
, "OpenBSD", NULL
}, /* Ox */
175 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Pc */
176 { NULL
, NULL
, post_pf
, NULL
, NULL
}, /* Pf */
177 { cond_body
, pre_enc
, post_enc
, "(", ")" }, /* Po */
178 { cond_body
, pre_enc
, post_enc
, "(", ")" }, /* Pq */
179 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Qc */
180 { cond_body
, pre_enc
, post_enc
, "`", "'" }, /* Ql */
181 { cond_body
, pre_enc
, post_enc
, "\"", "\"" }, /* Qo */
182 { cond_body
, pre_enc
, post_enc
, "\"", "\"" }, /* Qq */
183 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Re */
184 { cond_body
, pre_pp
, NULL
, NULL
, NULL
}, /* Rs */
185 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Sc */
186 { cond_body
, pre_enc
, post_enc
, "`", "'" }, /* So */
187 { cond_body
, pre_enc
, post_enc
, "`", "'" }, /* Sq */
188 { NULL
, pre_sm
, NULL
, NULL
, NULL
}, /* Sm */
189 { NULL
, pre_enc
, post_enc
, "\\fI", "\\fP" }, /* Sx */
190 { NULL
, pre_enc
, post_enc
, "\\fB", "\\fP" }, /* Sy */
191 { NULL
, pre_enc
, post_enc
, "\\fR", "\\fP" }, /* Tn */
192 { NULL
, pre_ux
, NULL
, "UNIX", NULL
}, /* Ux */
193 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* _Xc */
194 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* _Xo */
195 { NULL
, pre_fo
, post_fo
, NULL
, NULL
}, /* Fo */
196 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Fc */
197 { cond_body
, pre_enc
, post_enc
, "[", "]" }, /* Oo */
198 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Oc */
199 { NULL
, pre_bk
, post_bk
, NULL
, NULL
}, /* Bk */
200 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Ek */
201 { NULL
, pre_ux
, NULL
, "is currently in beta test.", NULL
}, /* Bt */
202 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Hf */
203 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Fr */
204 { NULL
, pre_ux
, NULL
, "currently under development.", NULL
}, /* Ud */
205 { NULL
, NULL
, post_lb
, NULL
, NULL
}, /* Lb */
206 { NULL
, pre_pp
, NULL
, NULL
, NULL
}, /* Lp */
207 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* _Lk */
208 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* _Mt */
209 { cond_body
, pre_enc
, post_enc
, "{", "}" }, /* Brq */
210 { cond_body
, pre_enc
, post_enc
, "{", "}" }, /* Bro */
211 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Brc */
212 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* _%C */
213 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* _Es */
214 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* _En */
215 { NULL
, pre_ux
, NULL
, "DragonFly", NULL
}, /* Dx */
216 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* _%Q */
217 { NULL
, pre_br
, NULL
, NULL
, NULL
}, /* br */
218 { NULL
, pre_sp
, post_sp
, NULL
, NULL
}, /* sp */
219 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* _%U */
220 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* _Ta */
221 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* ROOT */
225 print_word(struct mman
*mm
, const char *s
)
230 * If we need a newline, print it now and start afresh.
235 } else if (mm
->need_space
&& '\0' != s
[0])
237 * If we need a space, only print it before
238 * (1) a nonzero length word;
239 * (2) a word that is non-punctuation; and
240 * (3) if punctuation, non-terminating puncutation.
242 if (NULL
== strchr(".,:;)]?!", s
[0]) || '\0' != s
[1]) {
251 * Reassign needing space if we're not following opening
254 mm
->need_space
= mm
->mode_space
&&
255 (('(' != s
[0] && '[' != s
[0]) || '\0' != s
[1]);
266 putchar((unsigned char)*s
);
273 print_offs(struct mman
*mm
, const char *v
)
279 if (NULL
== v
|| '\0' == *v
|| 0 == strcmp(v
, "left"))
281 else if (0 == strcmp(v
, "indent"))
283 else if (0 == strcmp(v
, "indent-two"))
285 else if (a2roffsu(v
, &su
, SCALE_MAX
)) {
291 snprintf(buf
, sizeof(buf
), "%ldn", sz
);
296 man_man(void *arg
, const struct man
*man
)
300 * Dump the keep buffer.
301 * We're guaranteed by now that this exists (is non-NULL).
302 * Flush stdout afterward, just in case.
304 fputs(mparse_getkeep(man_mparse(man
)), stdout
);
309 man_mdoc(void *arg
, const struct mdoc
*mdoc
)
311 const struct mdoc_meta
*m
;
312 const struct mdoc_node
*n
;
318 printf(".TH \"%s\" \"%s\" \"%s\" \"%s\" \"%s\"",
319 m
->title
, m
->msec
, m
->date
, m
->os
, m
->vol
);
321 memset(&mm
, 0, sizeof(struct mman
));
325 print_node(m
, n
, &mm
);
330 print_node(DECL_ARGS
)
332 const struct mdoc_node
*prev
, *sub
;
333 const struct manact
*act
;
337 * Break the line if we were parsed subsequent the current node.
338 * This makes the page structure be more consistent.
340 prev
= n
->prev
? n
->prev
: n
->parent
;
341 if (prev
&& prev
->line
< n
->line
&&
342 MDOC_Fo
!= prev
->tok
&& MDOC_Ns
!= prev
->tok
)
349 if (MDOC_TEXT
== n
->type
) {
351 * Make sure that we don't happen to start with a
352 * control character at the start of a line.
354 if (mm
->need_nl
&& ('.' == *n
->string
||
355 '\'' == *n
->string
)) {
356 print_word(mm
, "\\&");
359 print_word(mm
, n
->string
);
362 * Conditionally run the pre-node action handler for a
365 act
= manacts
+ n
->tok
;
366 cond
= NULL
== act
->cond
|| (*act
->cond
)(m
, n
, mm
);
367 if (cond
&& act
->pre
)
368 do_sub
= (*act
->pre
)(m
, n
, mm
);
372 * Conditionally run all child nodes.
373 * Note that this iterates over children instead of using
374 * recursion. This prevents unnecessary depth in the stack.
377 for (sub
= n
->child
; sub
; sub
= sub
->next
)
378 print_node(m
, sub
, mm
);
381 * Lastly, conditionally run the post-node handler.
383 if (cond
&& act
->post
)
384 (*act
->post
)(m
, n
, mm
);
391 return(MDOC_HEAD
== n
->type
);
398 return(MDOC_BODY
== n
->type
);
402 * Output a font encoding before a node, e.g., \fR.
403 * This obviously has no trailing space.
410 prefix
= manacts
[n
->tok
].prefix
;
413 print_word(mm
, prefix
);
419 * Output a font encoding subsequent a node, e.g., \fP.
426 suffix
= manacts
[n
->tok
].suffix
;
430 print_word(mm
, suffix
);
431 if (MDOC_Fl
== n
->tok
&& 0 == n
->nchild
)
436 * Used in listings (percent = %A, e.g.).
437 * FIXME: this is incomplete.
438 * It doesn't print a nice ", and" for lists.
441 post_percent(DECL_ARGS
)
454 * Print before a section header.
460 if (MDOC_HEAD
!= n
->type
)
463 print_word(mm
, manacts
[n
->tok
].prefix
);
464 print_word(mm
, "\"");
470 * Print subsequent a section header.
476 if (MDOC_HEAD
!= n
->type
)
479 print_word(mm
, "\"");
497 if (0 == n
->norm
->Bd
.comp
) {
499 print_word(mm
, ".sp");
501 if (DISP_unfilled
== n
->norm
->Bd
.type
||
502 DISP_literal
== n
->norm
->Bd
.type
) {
504 print_word(mm
, ".nf");
507 print_word(mm
, ".RS");
508 print_offs(mm
, n
->norm
->Bd
.offs
);
518 print_word(mm
, ".RE");
519 if (DISP_unfilled
== n
->norm
->Bd
.type
||
520 DISP_literal
== n
->norm
->Bd
.type
) {
522 print_word(mm
, ".fi");
546 if (MDOC_BODY
== n
->type
)
555 print_word(mm
, ".br");
566 print_word(mm
, n
->string
);
570 print_word(mm
, "BSD");
576 print_word(mm
, n
->string
);
585 print_word(mm
, ".RS 6n");
595 print_word(mm
, ".RE");
603 if (MDOC_Fa
== n
->tok
)
607 print_word(mm
, "\\fI");
609 print_node(m
, n
, mm
);
611 print_word(mm
, "\\fP");
612 if (NULL
!= (n
= n
->next
))
622 if (NULL
!= n
->next
&& MDOC_Fa
== n
->next
->tok
)
634 if (MDOC_SYNPRETTY
& n
->flags
) {
636 print_word(mm
, ".br");
639 print_word(mm
, "\\fB");
641 print_node(m
, n
, mm
);
643 print_word(mm
, "\\fP(");
645 return(pre_fa(m
, n
->next
, mm
));
653 if (MDOC_SYNPRETTY
& n
->flags
) {
656 print_word(mm
, ".br");
667 if (MDOC_SYNPRETTY
& n
->flags
) {
669 print_word(mm
, ".br");
672 print_word(mm
, "\\fB");
693 print_word(mm
, "\\fP");
707 if (MDOC_SYNPRETTY
& n
->flags
) {
709 print_word(mm
, ".br");
711 print_word(mm
, "\\fB#include <");
713 print_word(mm
, "<\\fI");
723 if (MDOC_SYNPRETTY
& n
->flags
) {
724 print_word(mm
, ">\\fP");
726 print_word(mm
, ".br");
729 print_word(mm
, "\\fP>");
735 const struct mdoc_node
*bln
;
737 if (MDOC_HEAD
== n
->type
) {
739 print_word(mm
, ".TP");
740 bln
= n
->parent
->parent
->prev
;
741 switch (bln
->norm
->Bl
.type
) {
743 print_word(mm
, "4n");
745 print_word(mm
, "\\fBo\\fP");
748 if (bln
->norm
->Bl
.width
)
749 print_word(mm
, bln
->norm
->Bl
.width
);
761 if (SEC_LIBRARY
== n
->sec
) {
763 print_word(mm
, ".br");
772 if (MDOC_ELEM
!= n
->type
&& MDOC_HEAD
!= n
->type
)
774 if (MDOC_SYNPRETTY
& n
->flags
) {
776 print_word(mm
, ".br");
779 print_word(mm
, "\\fB");
781 if (NULL
== n
->child
)
782 print_word(mm
, m
->name
);
790 if (MDOC_ELEM
!= n
->type
&& MDOC_HEAD
!= n
->type
)
793 print_word(mm
, "\\fP");
816 if (MDOC_It
== n
->parent
->tok
)
817 print_word(mm
, ".sp");
819 print_word(mm
, ".PP");
821 return(MDOC_Rs
== n
->tok
);
828 assert(n
->child
&& MDOC_TEXT
== n
->child
->type
);
829 if (0 == strcmp("on", n
->child
->string
))
841 print_word(mm
, ".sp");
856 if (MDOC_SYNPRETTY
& n
->flags
) {
866 print_word(mm
, ".br");
869 print_word(mm
, "\\fI");
878 if (MDOC_SYNPRETTY
& n
->flags
&& MDOC_BODY
!= n
->type
)
882 print_word(mm
, "\\fP");
883 if (MDOC_SYNPRETTY
& n
->flags
) {
885 print_word(mm
, ".br");
897 print_node(m
, n
, mm
);
903 print_node(m
, n
, mm
);
912 print_word(mm
, manacts
[n
->tok
].prefix
);
913 if (NULL
== n
->child
)
916 print_word(mm
, "\\~");