]>
git.cameronkatri.com Git - mandoc.git/blob - mdoc_man.c
958a1e99f3d646fd7a2d19a55480b32699b4b26d
1 /* $Id: mdoc_man.c,v 1.13 2012/07/07 13:57:19 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_nm(DECL_ARGS
);
57 static void post_percent(DECL_ARGS
);
58 static void post_pf(DECL_ARGS
);
59 static void post_sect(DECL_ARGS
);
60 static void post_sp(DECL_ARGS
);
61 static int pre_ap(DECL_ARGS
);
62 static int pre_bd(DECL_ARGS
);
63 static int pre_bk(DECL_ARGS
);
64 static int pre_br(DECL_ARGS
);
65 static int pre_bx(DECL_ARGS
);
66 static int pre_dl(DECL_ARGS
);
67 static int pre_enc(DECL_ARGS
);
68 static int pre_it(DECL_ARGS
);
69 static int pre_nm(DECL_ARGS
);
70 static int pre_ns(DECL_ARGS
);
71 static int pre_pp(DECL_ARGS
);
72 static int pre_sm(DECL_ARGS
);
73 static int pre_sp(DECL_ARGS
);
74 static int pre_sect(DECL_ARGS
);
75 static int pre_ux(DECL_ARGS
);
76 static int pre_xr(DECL_ARGS
);
77 static void print_word(struct mman
*, const char *);
78 static void print_offs(struct mman
*, const char *);
79 static void print_node(DECL_ARGS
);
81 static const struct manact manacts
[MDOC_MAX
+ 1] = {
82 { NULL
, pre_ap
, NULL
, NULL
, NULL
}, /* Ap */
83 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Dd */
84 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Dt */
85 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Os */
86 { NULL
, pre_sect
, post_sect
, ".SH", NULL
}, /* Sh */
87 { NULL
, pre_sect
, post_sect
, ".SS", NULL
}, /* Ss */
88 { NULL
, pre_pp
, NULL
, NULL
, NULL
}, /* Pp */
89 { cond_body
, pre_dl
, post_dl
, NULL
, NULL
}, /* D1 */
90 { cond_body
, pre_dl
, post_dl
, NULL
, NULL
}, /* Dl */
91 { cond_body
, pre_bd
, post_bd
, NULL
, NULL
}, /* Bd */
92 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Ed */
93 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Bl */
94 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* El */
95 { NULL
, pre_it
, NULL
, NULL
, NULL
}, /* _It */
96 { NULL
, pre_enc
, post_enc
, "\\fI", "\\fP" }, /* Ad */
97 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* _An */
98 { NULL
, pre_enc
, post_enc
, "\\fI", "\\fP" }, /* Ar */
99 { NULL
, pre_enc
, post_enc
, "\\fB", "\\fP" }, /* Cd */
100 { NULL
, pre_enc
, post_enc
, "\\fB", "\\fP" }, /* Cm */
101 { NULL
, pre_enc
, post_enc
, "\\fR", "\\fP" }, /* Dv */
102 { NULL
, pre_enc
, post_enc
, "\\fR", "\\fP" }, /* Er */
103 { NULL
, pre_enc
, post_enc
, "\\fR", "\\fP" }, /* Ev */
104 { NULL
, pre_enc
, post_enc
, "The \\fB",
105 "\\fP\nutility exits 0 on success, and >0 if an error occurs."
107 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* _Fa */
108 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* _Fd */
109 { NULL
, pre_enc
, post_enc
, "\\fB-", "\\fP" }, /* Fl */
110 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* _Fn */
111 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* _Ft */
112 { NULL
, pre_enc
, post_enc
, "\\fB", "\\fP" }, /* Ic */
113 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* _In */
114 { NULL
, pre_enc
, post_enc
, "\\fR", "\\fP" }, /* Li */
115 { cond_head
, pre_enc
, NULL
, "\\- ", NULL
}, /* Nd */
116 { NULL
, pre_nm
, post_nm
, NULL
, NULL
}, /* Nm */
117 { cond_body
, pre_enc
, post_enc
, "[", "]" }, /* Op */
118 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Ot */
119 { NULL
, pre_enc
, post_enc
, "\\fI", "\\fP" }, /* Pa */
120 { NULL
, pre_enc
, post_enc
, "The \\fB",
121 "\\fP\nfunction returns the value 0 if successful;\n"
122 "otherwise the value -1 is returned and the global\n"
123 "variable \\fIerrno\\fP is set to indicate the error."
125 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* St */
126 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* _Va */
127 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* _Vt */
128 { NULL
, pre_xr
, NULL
, NULL
, NULL
}, /* Xr */
129 { NULL
, NULL
, post_percent
, NULL
, NULL
}, /* _%A */
130 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* _%B */
131 { NULL
, NULL
, post_percent
, NULL
, NULL
}, /* _%D */
132 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* _%I */
133 { NULL
, pre_enc
, post_percent
, "\\fI", "\\fP" }, /* %J */
134 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* _%N */
135 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* _%O */
136 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* _%P */
137 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* _%R */
138 { NULL
, pre_enc
, post_percent
, "\"", "\"" }, /* %T */
139 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* _%V */
140 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Ac */
141 { cond_body
, pre_enc
, post_enc
, "<", ">" }, /* Ao */
142 { cond_body
, pre_enc
, post_enc
, "<", ">" }, /* Aq */
143 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* At */
144 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Bc */
145 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* _Bf */
146 { cond_body
, pre_enc
, post_enc
, "[", "]" }, /* Bo */
147 { cond_body
, pre_enc
, post_enc
, "[", "]" }, /* Bq */
148 { NULL
, pre_ux
, NULL
, "BSD/OS", NULL
}, /* Bsx */
149 { NULL
, pre_bx
, NULL
, NULL
, NULL
}, /* Bx */
150 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Db */
151 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Dc */
152 { cond_body
, pre_enc
, post_enc
, "``", "''" }, /* Do */
153 { cond_body
, pre_enc
, post_enc
, "``", "''" }, /* Dq */
154 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* _Ec */
155 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* _Ef */
156 { NULL
, pre_enc
, post_enc
, "\\fI", "\\fP" }, /* Em */
157 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* _Eo */
158 { NULL
, pre_ux
, NULL
, "FreeBSD", NULL
}, /* Fx */
159 { NULL
, pre_enc
, post_enc
, "\\fB", "\\fP" }, /* Ms */
160 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* No */
161 { NULL
, pre_ns
, NULL
, NULL
, NULL
}, /* Ns */
162 { NULL
, pre_ux
, NULL
, "NetBSD", NULL
}, /* Nx */
163 { NULL
, pre_ux
, NULL
, "OpenBSD", NULL
}, /* Ox */
164 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Pc */
165 { NULL
, NULL
, post_pf
, NULL
, NULL
}, /* Pf */
166 { cond_body
, pre_enc
, post_enc
, "(", ")" }, /* Po */
167 { cond_body
, pre_enc
, post_enc
, "(", ")" }, /* Pq */
168 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Qc */
169 { cond_body
, pre_enc
, post_enc
, "`", "'" }, /* Ql */
170 { cond_body
, pre_enc
, post_enc
, "\"", "\"" }, /* Qo */
171 { cond_body
, pre_enc
, post_enc
, "\"", "\"" }, /* Qq */
172 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Re */
173 { cond_body
, pre_pp
, NULL
, NULL
, NULL
}, /* Rs */
174 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Sc */
175 { cond_body
, pre_enc
, post_enc
, "`", "'" }, /* So */
176 { cond_body
, pre_enc
, post_enc
, "`", "'" }, /* Sq */
177 { NULL
, pre_sm
, NULL
, NULL
, NULL
}, /* Sm */
178 { NULL
, pre_enc
, post_enc
, "\\fI", "\\fP" }, /* Sx */
179 { NULL
, pre_enc
, post_enc
, "\\fB", "\\fP" }, /* Sy */
180 { NULL
, pre_enc
, post_enc
, "\\fR", "\\fP" }, /* Tn */
181 { NULL
, pre_ux
, NULL
, "UNIX", NULL
}, /* Ux */
182 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* _Xc */
183 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* _Xo */
184 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* _Fo */
185 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* _Fc */
186 { cond_body
, pre_enc
, post_enc
, "[", "]" }, /* Oo */
187 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Oc */
188 { NULL
, pre_bk
, post_bk
, NULL
, NULL
}, /* Bk */
189 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Ek */
190 { NULL
, pre_ux
, NULL
, "is currently in beta test.", NULL
}, /* Bt */
191 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Hf */
192 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Fr */
193 { NULL
, pre_ux
, NULL
, "currently under development.", NULL
}, /* Ud */
194 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* _Lb */
195 { NULL
, pre_pp
, NULL
, NULL
, NULL
}, /* Lp */
196 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* _Lk */
197 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* _Mt */
198 { cond_body
, pre_enc
, post_enc
, "{", "}" }, /* Brq */
199 { cond_body
, pre_enc
, post_enc
, "{", "}" }, /* Bro */
200 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Brc */
201 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* _%C */
202 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* _Es */
203 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* _En */
204 { NULL
, pre_ux
, NULL
, "DragonFly", NULL
}, /* Dx */
205 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* _%Q */
206 { NULL
, pre_br
, NULL
, NULL
, NULL
}, /* br */
207 { NULL
, pre_sp
, post_sp
, NULL
, NULL
}, /* sp */
208 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* _%U */
209 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* _Ta */
210 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* ROOT */
214 print_word(struct mman
*mm
, const char *s
)
219 * If we need a newline, print it now and start afresh.
224 } else if (mm
->need_space
&& '\0' != s
[0])
226 * If we need a space, only print it before
227 * (1) a nonzero length word;
228 * (2) a word that is non-punctuation; and
229 * (3) if punctuation, non-terminating puncutation.
231 if (NULL
== strchr(".,:;)]?!", s
[0]) || '\0' != s
[1]) {
240 * Reassign needing space if we're not following opening
243 mm
->need_space
= mm
->mode_space
&&
244 (('(' != s
[0] && '[' != s
[0]) || '\0' != s
[1]);
255 putchar((unsigned char)*s
);
262 print_offs(struct mman
*mm
, const char *v
)
268 if (NULL
== v
|| '\0' == *v
|| 0 == strcmp(v
, "left"))
270 else if (0 == strcmp(v
, "indent"))
272 else if (0 == strcmp(v
, "indent-two"))
274 else if (a2roffsu(v
, &su
, SCALE_MAX
)) {
280 snprintf(buf
, sizeof(buf
), "%ldn", sz
);
285 man_man(void *arg
, const struct man
*man
)
289 * Dump the keep buffer.
290 * We're guaranteed by now that this exists (is non-NULL).
291 * Flush stdout afterward, just in case.
293 fputs(mparse_getkeep(man_mparse(man
)), stdout
);
298 man_mdoc(void *arg
, const struct mdoc
*mdoc
)
300 const struct mdoc_meta
*m
;
301 const struct mdoc_node
*n
;
307 printf(".TH \"%s\" \"%s\" \"%s\" \"%s\" \"%s\"",
308 m
->title
, m
->msec
, m
->date
, m
->os
, m
->vol
);
310 memset(&mm
, 0, sizeof(struct mman
));
314 print_node(m
, n
, &mm
);
319 print_node(DECL_ARGS
)
321 const struct mdoc_node
*prev
, *sub
;
322 const struct manact
*act
;
326 * Break the line if we were parsed subsequent the current node.
327 * This makes the page structure be more consistent.
329 prev
= n
->prev
? n
->prev
: n
->parent
;
330 if (prev
&& prev
->line
< n
->line
&& MDOC_Ns
!= prev
->tok
)
337 if (MDOC_TEXT
== n
->type
) {
339 * Make sure that we don't happen to start with a
340 * control character at the start of a line.
342 if (mm
->need_nl
&& ('.' == *n
->string
||
343 '\'' == *n
->string
)) {
344 print_word(mm
, "\\&");
347 print_word(mm
, n
->string
);
350 * Conditionally run the pre-node action handler for a
353 act
= manacts
+ n
->tok
;
354 cond
= NULL
== act
->cond
|| (*act
->cond
)(m
, n
, mm
);
355 if (cond
&& act
->pre
)
356 do_sub
= (*act
->pre
)(m
, n
, mm
);
360 * Conditionally run all child nodes.
361 * Note that this iterates over children instead of using
362 * recursion. This prevents unnecessary depth in the stack.
365 for (sub
= n
->child
; sub
; sub
= sub
->next
)
366 print_node(m
, sub
, mm
);
369 * Lastly, conditionally run the post-node handler.
371 if (cond
&& act
->post
)
372 (*act
->post
)(m
, n
, mm
);
379 return(MDOC_HEAD
== n
->type
);
386 return(MDOC_BODY
== n
->type
);
390 * Output a font encoding before a node, e.g., \fR.
391 * This obviously has no trailing space.
398 prefix
= manacts
[n
->tok
].prefix
;
401 print_word(mm
, prefix
);
407 * Output a font encoding subsequent a node, e.g., \fP.
414 suffix
= manacts
[n
->tok
].suffix
;
418 print_word(mm
, suffix
);
419 if (MDOC_Fl
== n
->tok
&& 0 == n
->nchild
)
424 * Used in listings (percent = %A, e.g.).
425 * FIXME: this is incomplete.
426 * It doesn't print a nice ", and" for lists.
429 post_percent(DECL_ARGS
)
442 * Print before a section header.
448 if (MDOC_HEAD
!= n
->type
)
451 print_word(mm
, manacts
[n
->tok
].prefix
);
452 print_word(mm
, "\"");
458 * Print subsequent a section header.
464 if (MDOC_HEAD
!= n
->type
)
467 print_word(mm
, "\"");
485 if (0 == n
->norm
->Bd
.comp
) {
487 print_word(mm
, ".sp");
489 if (DISP_unfilled
== n
->norm
->Bd
.type
||
490 DISP_literal
== n
->norm
->Bd
.type
) {
492 print_word(mm
, ".nf");
495 print_word(mm
, ".RS");
496 print_offs(mm
, n
->norm
->Bd
.offs
);
506 print_word(mm
, ".RE");
507 if (DISP_unfilled
== n
->norm
->Bd
.type
||
508 DISP_literal
== n
->norm
->Bd
.type
) {
510 print_word(mm
, ".fi");
534 if (MDOC_BODY
== n
->type
)
543 print_word(mm
, ".br");
554 print_word(mm
, n
->string
);
558 print_word(mm
, "BSD");
564 print_word(mm
, n
->string
);
573 print_word(mm
, ".RS 6n");
583 print_word(mm
, ".RE");
590 const struct mdoc_node
*bln
;
592 if (MDOC_HEAD
== n
->type
) {
594 print_word(mm
, ".TP");
595 bln
= n
->parent
->parent
->prev
;
596 switch (bln
->norm
->Bl
.type
) {
598 print_word(mm
, "4n");
600 print_word(mm
, "\\fBo\\fP");
603 if (bln
->norm
->Bl
.width
)
604 print_word(mm
, bln
->norm
->Bl
.width
);
616 if (MDOC_ELEM
!= n
->type
&& MDOC_HEAD
!= n
->type
)
618 if (MDOC_SYNPRETTY
& n
->flags
) {
620 print_word(mm
, ".br");
623 print_word(mm
, "\\fB");
625 if (NULL
== n
->child
)
626 print_word(mm
, m
->name
);
634 if (MDOC_ELEM
!= n
->type
&& MDOC_HEAD
!= n
->type
)
637 print_word(mm
, "\\fP");
660 if (MDOC_It
== n
->parent
->tok
)
661 print_word(mm
, ".sp");
663 print_word(mm
, ".PP");
665 return(MDOC_Rs
== n
->tok
);
672 assert(n
->child
&& MDOC_TEXT
== n
->child
->type
);
673 if (0 == strcmp("on", n
->child
->string
))
685 print_word(mm
, ".sp");
703 print_node(m
, n
, mm
);
709 print_node(m
, n
, mm
);
718 print_word(mm
, manacts
[n
->tok
].prefix
);
719 if (NULL
== n
->child
)
722 print_word(mm
, "\\~");