]>
git.cameronkatri.com Git - mandoc.git/blob - mdoc_man.c
1 /* $Id: mdoc_man.c,v 1.17 2012/07/08 10:19:37 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 int pre_ap(DECL_ARGS
);
67 static int pre_bd(DECL_ARGS
);
68 static int pre_bk(DECL_ARGS
);
69 static int pre_br(DECL_ARGS
);
70 static int pre_bx(DECL_ARGS
);
71 static int pre_dl(DECL_ARGS
);
72 static int pre_enc(DECL_ARGS
);
73 static int pre_fa(DECL_ARGS
);
74 static int pre_fn(DECL_ARGS
);
75 static int pre_fo(DECL_ARGS
);
76 static int pre_in(DECL_ARGS
);
77 static int pre_it(DECL_ARGS
);
78 static int pre_nm(DECL_ARGS
);
79 static int pre_ns(DECL_ARGS
);
80 static int pre_pp(DECL_ARGS
);
81 static int pre_sm(DECL_ARGS
);
82 static int pre_sp(DECL_ARGS
);
83 static int pre_sect(DECL_ARGS
);
84 static int pre_ux(DECL_ARGS
);
85 static int pre_xr(DECL_ARGS
);
86 static void print_word(struct mman
*, const char *);
87 static void print_offs(struct mman
*, const char *);
88 static void print_node(DECL_ARGS
);
90 static const struct manact manacts
[MDOC_MAX
+ 1] = {
91 { NULL
, pre_ap
, NULL
, NULL
, NULL
}, /* Ap */
92 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Dd */
93 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Dt */
94 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Os */
95 { NULL
, pre_sect
, post_sect
, ".SH", NULL
}, /* Sh */
96 { NULL
, pre_sect
, post_sect
, ".SS", NULL
}, /* Ss */
97 { NULL
, pre_pp
, NULL
, NULL
, NULL
}, /* Pp */
98 { cond_body
, pre_dl
, post_dl
, NULL
, NULL
}, /* D1 */
99 { cond_body
, pre_dl
, post_dl
, NULL
, NULL
}, /* Dl */
100 { cond_body
, pre_bd
, post_bd
, NULL
, NULL
}, /* Bd */
101 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Ed */
102 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Bl */
103 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* El */
104 { NULL
, pre_it
, NULL
, NULL
, NULL
}, /* _It */
105 { NULL
, pre_enc
, post_enc
, "\\fI", "\\fP" }, /* Ad */
106 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* _An */
107 { NULL
, pre_enc
, post_enc
, "\\fI", "\\fP" }, /* Ar */
108 { NULL
, pre_enc
, post_enc
, "\\fB", "\\fP" }, /* Cd */
109 { NULL
, pre_enc
, post_enc
, "\\fB", "\\fP" }, /* Cm */
110 { NULL
, pre_enc
, post_enc
, "\\fR", "\\fP" }, /* Dv */
111 { NULL
, pre_enc
, post_enc
, "\\fR", "\\fP" }, /* Er */
112 { NULL
, pre_enc
, post_enc
, "\\fR", "\\fP" }, /* Ev */
113 { NULL
, pre_enc
, post_enc
, "The \\fB",
114 "\\fP\nutility exits 0 on success, and >0 if an error occurs."
116 { NULL
, pre_fa
, post_fa
, NULL
, NULL
}, /* Fa */
117 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* _Fd */
118 { NULL
, pre_enc
, post_enc
, "\\fB-", "\\fP" }, /* Fl */
119 { NULL
, pre_fn
, post_fn
, NULL
, NULL
}, /* Fn */
120 { NULL
, pre_enc
, post_enc
, "\\fI", "\\fP" }, /* Ft */
121 { NULL
, pre_enc
, post_enc
, "\\fB", "\\fP" }, /* Ic */
122 { NULL
, pre_in
, post_in
, NULL
, NULL
}, /* In */
123 { NULL
, pre_enc
, post_enc
, "\\fR", "\\fP" }, /* Li */
124 { cond_head
, pre_enc
, NULL
, "\\- ", NULL
}, /* Nd */
125 { NULL
, pre_nm
, post_nm
, NULL
, NULL
}, /* Nm */
126 { cond_body
, pre_enc
, post_enc
, "[", "]" }, /* Op */
127 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Ot */
128 { NULL
, pre_enc
, post_enc
, "\\fI", "\\fP" }, /* Pa */
129 { NULL
, pre_enc
, post_enc
, "The \\fB",
130 "\\fP\nfunction returns the value 0 if successful;\n"
131 "otherwise the value -1 is returned and the global\n"
132 "variable \\fIerrno\\fP is set to indicate the error."
134 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* St */
135 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* _Va */
136 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* _Vt */
137 { NULL
, pre_xr
, NULL
, NULL
, NULL
}, /* Xr */
138 { NULL
, NULL
, post_percent
, NULL
, NULL
}, /* _%A */
139 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* _%B */
140 { NULL
, NULL
, post_percent
, NULL
, NULL
}, /* _%D */
141 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* _%I */
142 { NULL
, pre_enc
, post_percent
, "\\fI", "\\fP" }, /* %J */
143 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* _%N */
144 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* _%O */
145 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* _%P */
146 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* _%R */
147 { NULL
, pre_enc
, post_percent
, "\"", "\"" }, /* %T */
148 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* _%V */
149 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Ac */
150 { cond_body
, pre_enc
, post_enc
, "<", ">" }, /* Ao */
151 { cond_body
, pre_enc
, post_enc
, "<", ">" }, /* Aq */
152 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* At */
153 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Bc */
154 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* _Bf */
155 { cond_body
, pre_enc
, post_enc
, "[", "]" }, /* Bo */
156 { cond_body
, pre_enc
, post_enc
, "[", "]" }, /* Bq */
157 { NULL
, pre_ux
, NULL
, "BSD/OS", NULL
}, /* Bsx */
158 { NULL
, pre_bx
, NULL
, NULL
, NULL
}, /* Bx */
159 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Db */
160 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Dc */
161 { cond_body
, pre_enc
, post_enc
, "``", "''" }, /* Do */
162 { cond_body
, pre_enc
, post_enc
, "``", "''" }, /* Dq */
163 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* _Ec */
164 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* _Ef */
165 { NULL
, pre_enc
, post_enc
, "\\fI", "\\fP" }, /* Em */
166 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* _Eo */
167 { NULL
, pre_ux
, NULL
, "FreeBSD", NULL
}, /* Fx */
168 { NULL
, pre_enc
, post_enc
, "\\fB", "\\fP" }, /* Ms */
169 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* No */
170 { NULL
, pre_ns
, NULL
, NULL
, NULL
}, /* Ns */
171 { NULL
, pre_ux
, NULL
, "NetBSD", NULL
}, /* Nx */
172 { NULL
, pre_ux
, NULL
, "OpenBSD", NULL
}, /* Ox */
173 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Pc */
174 { NULL
, NULL
, post_pf
, NULL
, NULL
}, /* Pf */
175 { cond_body
, pre_enc
, post_enc
, "(", ")" }, /* Po */
176 { cond_body
, pre_enc
, post_enc
, "(", ")" }, /* Pq */
177 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Qc */
178 { cond_body
, pre_enc
, post_enc
, "`", "'" }, /* Ql */
179 { cond_body
, pre_enc
, post_enc
, "\"", "\"" }, /* Qo */
180 { cond_body
, pre_enc
, post_enc
, "\"", "\"" }, /* Qq */
181 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Re */
182 { cond_body
, pre_pp
, NULL
, NULL
, NULL
}, /* Rs */
183 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Sc */
184 { cond_body
, pre_enc
, post_enc
, "`", "'" }, /* So */
185 { cond_body
, pre_enc
, post_enc
, "`", "'" }, /* Sq */
186 { NULL
, pre_sm
, NULL
, NULL
, NULL
}, /* Sm */
187 { NULL
, pre_enc
, post_enc
, "\\fI", "\\fP" }, /* Sx */
188 { NULL
, pre_enc
, post_enc
, "\\fB", "\\fP" }, /* Sy */
189 { NULL
, pre_enc
, post_enc
, "\\fR", "\\fP" }, /* Tn */
190 { NULL
, pre_ux
, NULL
, "UNIX", NULL
}, /* Ux */
191 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* _Xc */
192 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* _Xo */
193 { NULL
, pre_fo
, post_fo
, NULL
, NULL
}, /* Fo */
194 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Fc */
195 { cond_body
, pre_enc
, post_enc
, "[", "]" }, /* Oo */
196 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Oc */
197 { NULL
, pre_bk
, post_bk
, NULL
, NULL
}, /* Bk */
198 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Ek */
199 { NULL
, pre_ux
, NULL
, "is currently in beta test.", NULL
}, /* Bt */
200 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Hf */
201 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Fr */
202 { NULL
, pre_ux
, NULL
, "currently under development.", NULL
}, /* Ud */
203 { NULL
, NULL
, post_lb
, NULL
, NULL
}, /* Lb */
204 { NULL
, pre_pp
, NULL
, NULL
, NULL
}, /* Lp */
205 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* _Lk */
206 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* _Mt */
207 { cond_body
, pre_enc
, post_enc
, "{", "}" }, /* Brq */
208 { cond_body
, pre_enc
, post_enc
, "{", "}" }, /* Bro */
209 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Brc */
210 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* _%C */
211 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* _Es */
212 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* _En */
213 { NULL
, pre_ux
, NULL
, "DragonFly", NULL
}, /* Dx */
214 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* _%Q */
215 { NULL
, pre_br
, NULL
, NULL
, NULL
}, /* br */
216 { NULL
, pre_sp
, post_sp
, NULL
, NULL
}, /* sp */
217 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* _%U */
218 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* _Ta */
219 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* ROOT */
223 print_word(struct mman
*mm
, const char *s
)
228 * If we need a newline, print it now and start afresh.
233 } else if (mm
->need_space
&& '\0' != s
[0])
235 * If we need a space, only print it before
236 * (1) a nonzero length word;
237 * (2) a word that is non-punctuation; and
238 * (3) if punctuation, non-terminating puncutation.
240 if (NULL
== strchr(".,:;)]?!", s
[0]) || '\0' != s
[1]) {
249 * Reassign needing space if we're not following opening
252 mm
->need_space
= mm
->mode_space
&&
253 (('(' != s
[0] && '[' != s
[0]) || '\0' != s
[1]);
264 putchar((unsigned char)*s
);
271 print_offs(struct mman
*mm
, const char *v
)
277 if (NULL
== v
|| '\0' == *v
|| 0 == strcmp(v
, "left"))
279 else if (0 == strcmp(v
, "indent"))
281 else if (0 == strcmp(v
, "indent-two"))
283 else if (a2roffsu(v
, &su
, SCALE_MAX
)) {
289 snprintf(buf
, sizeof(buf
), "%ldn", sz
);
294 man_man(void *arg
, const struct man
*man
)
298 * Dump the keep buffer.
299 * We're guaranteed by now that this exists (is non-NULL).
300 * Flush stdout afterward, just in case.
302 fputs(mparse_getkeep(man_mparse(man
)), stdout
);
307 man_mdoc(void *arg
, const struct mdoc
*mdoc
)
309 const struct mdoc_meta
*m
;
310 const struct mdoc_node
*n
;
316 printf(".TH \"%s\" \"%s\" \"%s\" \"%s\" \"%s\"",
317 m
->title
, m
->msec
, m
->date
, m
->os
, m
->vol
);
319 memset(&mm
, 0, sizeof(struct mman
));
323 print_node(m
, n
, &mm
);
328 print_node(DECL_ARGS
)
330 const struct mdoc_node
*prev
, *sub
;
331 const struct manact
*act
;
335 * Break the line if we were parsed subsequent the current node.
336 * This makes the page structure be more consistent.
338 prev
= n
->prev
? n
->prev
: n
->parent
;
339 if (prev
&& prev
->line
< n
->line
&&
340 MDOC_Fo
!= prev
->tok
&& MDOC_Ns
!= prev
->tok
)
347 if (MDOC_TEXT
== n
->type
) {
349 * Make sure that we don't happen to start with a
350 * control character at the start of a line.
352 if (mm
->need_nl
&& ('.' == *n
->string
||
353 '\'' == *n
->string
)) {
354 print_word(mm
, "\\&");
357 print_word(mm
, n
->string
);
360 * Conditionally run the pre-node action handler for a
363 act
= manacts
+ n
->tok
;
364 cond
= NULL
== act
->cond
|| (*act
->cond
)(m
, n
, mm
);
365 if (cond
&& act
->pre
)
366 do_sub
= (*act
->pre
)(m
, n
, mm
);
370 * Conditionally run all child nodes.
371 * Note that this iterates over children instead of using
372 * recursion. This prevents unnecessary depth in the stack.
375 for (sub
= n
->child
; sub
; sub
= sub
->next
)
376 print_node(m
, sub
, mm
);
379 * Lastly, conditionally run the post-node handler.
381 if (cond
&& act
->post
)
382 (*act
->post
)(m
, n
, mm
);
389 return(MDOC_HEAD
== n
->type
);
396 return(MDOC_BODY
== n
->type
);
400 * Output a font encoding before a node, e.g., \fR.
401 * This obviously has no trailing space.
408 prefix
= manacts
[n
->tok
].prefix
;
411 print_word(mm
, prefix
);
417 * Output a font encoding subsequent a node, e.g., \fP.
424 suffix
= manacts
[n
->tok
].suffix
;
428 print_word(mm
, suffix
);
429 if (MDOC_Fl
== n
->tok
&& 0 == n
->nchild
)
434 * Used in listings (percent = %A, e.g.).
435 * FIXME: this is incomplete.
436 * It doesn't print a nice ", and" for lists.
439 post_percent(DECL_ARGS
)
452 * Print before a section header.
458 if (MDOC_HEAD
!= n
->type
)
461 print_word(mm
, manacts
[n
->tok
].prefix
);
462 print_word(mm
, "\"");
468 * Print subsequent a section header.
474 if (MDOC_HEAD
!= n
->type
)
477 print_word(mm
, "\"");
495 if (0 == n
->norm
->Bd
.comp
) {
497 print_word(mm
, ".sp");
499 if (DISP_unfilled
== n
->norm
->Bd
.type
||
500 DISP_literal
== n
->norm
->Bd
.type
) {
502 print_word(mm
, ".nf");
505 print_word(mm
, ".RS");
506 print_offs(mm
, n
->norm
->Bd
.offs
);
516 print_word(mm
, ".RE");
517 if (DISP_unfilled
== n
->norm
->Bd
.type
||
518 DISP_literal
== n
->norm
->Bd
.type
) {
520 print_word(mm
, ".fi");
544 if (MDOC_BODY
== n
->type
)
553 print_word(mm
, ".br");
564 print_word(mm
, n
->string
);
568 print_word(mm
, "BSD");
574 print_word(mm
, n
->string
);
583 print_word(mm
, ".RS 6n");
593 print_word(mm
, ".RE");
601 if (MDOC_Fa
== n
->tok
)
605 print_word(mm
, "\\fI");
607 print_node(m
, n
, mm
);
609 print_word(mm
, "\\fP");
610 if (NULL
!= (n
= n
->next
))
620 if (NULL
!= n
->next
&& MDOC_Fa
== n
->next
->tok
)
632 if (MDOC_SYNPRETTY
& n
->flags
) {
634 print_word(mm
, ".br");
637 print_word(mm
, "\\fB");
639 print_node(m
, n
, mm
);
641 print_word(mm
, "\\fP(");
643 return(pre_fa(m
, n
->next
, mm
));
651 if (MDOC_SYNPRETTY
& n
->flags
) {
654 print_word(mm
, ".br");
665 if (MDOC_SYNPRETTY
& n
->flags
) {
667 print_word(mm
, ".br");
670 print_word(mm
, "\\fB");
691 print_word(mm
, "\\fP");
705 if (MDOC_SYNPRETTY
& n
->flags
) {
707 print_word(mm
, ".br");
709 print_word(mm
, "\\fB#include <");
711 print_word(mm
, "<\\fI");
721 if (MDOC_SYNPRETTY
& n
->flags
) {
722 print_word(mm
, ">\\fP");
724 print_word(mm
, ".br");
727 print_word(mm
, "\\fP>");
733 const struct mdoc_node
*bln
;
735 if (MDOC_HEAD
== n
->type
) {
737 print_word(mm
, ".TP");
738 bln
= n
->parent
->parent
->prev
;
739 switch (bln
->norm
->Bl
.type
) {
741 print_word(mm
, "4n");
743 print_word(mm
, "\\fBo\\fP");
746 if (bln
->norm
->Bl
.width
)
747 print_word(mm
, bln
->norm
->Bl
.width
);
759 if (SEC_LIBRARY
== n
->sec
) {
761 print_word(mm
, ".br");
770 if (MDOC_ELEM
!= n
->type
&& MDOC_HEAD
!= n
->type
)
772 if (MDOC_SYNPRETTY
& n
->flags
) {
774 print_word(mm
, ".br");
777 print_word(mm
, "\\fB");
779 if (NULL
== n
->child
)
780 print_word(mm
, m
->name
);
788 if (MDOC_ELEM
!= n
->type
&& MDOC_HEAD
!= n
->type
)
791 print_word(mm
, "\\fP");
814 if (MDOC_It
== n
->parent
->tok
)
815 print_word(mm
, ".sp");
817 print_word(mm
, ".PP");
819 return(MDOC_Rs
== n
->tok
);
826 assert(n
->child
&& MDOC_TEXT
== n
->child
->type
);
827 if (0 == strcmp("on", n
->child
->string
))
839 print_word(mm
, ".sp");
857 print_node(m
, n
, mm
);
863 print_node(m
, n
, mm
);
872 print_word(mm
, manacts
[n
->tok
].prefix
);
873 if (NULL
== n
->child
)
876 print_word(mm
, "\\~");