1 /* $Id: mdoc_man.c,v 1.9 2011/10/24 21:47:59 schwarze Exp $ */
3 * Copyright (c) 2011 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.
29 #define DECL_ARGS const struct mdoc_meta *m, \
30 const struct mdoc_node *n, \
34 int need_space; /* next word needs prior ws */
35 int need_nl; /* next word needs prior nl */
39 int (*cond)(DECL_ARGS); /* DON'T run actions */
40 int (*pre)(DECL_ARGS); /* pre-node action */
41 void (*post)(DECL_ARGS); /* post-node action */
42 const char *prefix; /* pre-node string constant */
43 const char *suffix; /* post-node string constant */
46 static int cond_body(DECL_ARGS);
47 static int cond_head(DECL_ARGS);
48 static void post_bd(DECL_ARGS);
49 static void post_dl(DECL_ARGS);
50 static void post_enc(DECL_ARGS);
51 static void post_nm(DECL_ARGS);
52 static void post_percent(DECL_ARGS);
53 static void post_pf(DECL_ARGS);
54 static void post_sect(DECL_ARGS);
55 static void post_sp(DECL_ARGS);
56 static int pre_ap(DECL_ARGS);
57 static int pre_bd(DECL_ARGS);
58 static int pre_br(DECL_ARGS);
59 static int pre_bx(DECL_ARGS);
60 static int pre_dl(DECL_ARGS);
61 static int pre_enc(DECL_ARGS);
62 static int pre_it(DECL_ARGS);
63 static int pre_nm(DECL_ARGS);
64 static int pre_ns(DECL_ARGS);
65 static int pre_pp(DECL_ARGS);
66 static int pre_sp(DECL_ARGS);
67 static int pre_sect(DECL_ARGS);
68 static int pre_ux(DECL_ARGS);
69 static int pre_xr(DECL_ARGS);
70 static void print_word(struct mman *, const char *);
71 static void print_node(DECL_ARGS);
73 static const struct manact manacts[MDOC_MAX + 1] = {
74 { NULL, pre_ap, NULL, NULL, NULL }, /* Ap */
75 { NULL, NULL, NULL, NULL, NULL }, /* Dd */
76 { NULL, NULL, NULL, NULL, NULL }, /* Dt */
77 { NULL, NULL, NULL, NULL, NULL }, /* Os */
78 { NULL, pre_sect, post_sect, ".SH", NULL }, /* Sh */
79 { NULL, pre_sect, post_sect, ".SS", NULL }, /* Ss */
80 { NULL, pre_pp, NULL, NULL, NULL }, /* Pp */
81 { cond_body, pre_dl, post_dl, NULL, NULL }, /* D1 */
82 { cond_body, pre_dl, post_dl, NULL, NULL }, /* Dl */
83 { cond_body, pre_bd, post_bd, NULL, NULL }, /* Bd */
84 { NULL, NULL, NULL, NULL, NULL }, /* Ed */
85 { NULL, NULL, NULL, NULL, NULL }, /* Bl */
86 { NULL, NULL, NULL, NULL, NULL }, /* El */
87 { NULL, pre_it, NULL, NULL, NULL }, /* _It */
88 { NULL, pre_enc, post_enc, "\\fI", "\\fP" }, /* Ad */
89 { NULL, NULL, NULL, NULL, NULL }, /* _An */
90 { NULL, pre_enc, post_enc, "\\fI", "\\fP" }, /* Ar */
91 { NULL, pre_enc, post_enc, "\\fB", "\\fP" }, /* Cd */
92 { NULL, pre_enc, post_enc, "\\fB", "\\fP" }, /* Cm */
93 { NULL, pre_enc, post_enc, "\\fR", "\\fP" }, /* Dv */
94 { NULL, pre_enc, post_enc, "\\fR", "\\fP" }, /* Er */
95 { NULL, pre_enc, post_enc, "\\fR", "\\fP" }, /* Ev */
96 { NULL, pre_enc, post_enc, "The \\fB",
97 "\\fP\nutility exits 0 on success, and >0 if an error occurs."
99 { NULL, NULL, NULL, NULL, NULL }, /* _Fa */
100 { NULL, NULL, NULL, NULL, NULL }, /* _Fd */
101 { NULL, pre_enc, post_enc, "\\fB-", "\\fP" }, /* Fl */
102 { NULL, NULL, NULL, NULL, NULL }, /* _Fn */
103 { NULL, NULL, NULL, NULL, NULL }, /* _Ft */
104 { NULL, pre_enc, post_enc, "\\fB", "\\fP" }, /* Ic */
105 { NULL, NULL, NULL, NULL, NULL }, /* _In */
106 { NULL, pre_enc, post_enc, "\\fR", "\\fP" }, /* Li */
107 { cond_head, pre_enc, NULL, "\\- ", NULL }, /* Nd */
108 { NULL, pre_nm, post_nm, NULL, NULL }, /* Nm */
109 { cond_body, pre_enc, post_enc, "[", "]" }, /* Op */
110 { NULL, NULL, NULL, NULL, NULL }, /* Ot */
111 { NULL, pre_enc, post_enc, "\\fI", "\\fP" }, /* Pa */
112 { NULL, pre_enc, post_enc, "The \\fB",
113 "\\fP\nfunction returns the value 0 if successful;\n"
114 "otherwise the value -1 is returned and the global\n"
115 "variable \\fIerrno\\fP is set to indicate the error."
117 { NULL, NULL, NULL, NULL, NULL }, /* St */
118 { NULL, NULL, NULL, NULL, NULL }, /* _Va */
119 { NULL, NULL, NULL, NULL, NULL }, /* _Vt */
120 { NULL, pre_xr, NULL, NULL, NULL }, /* Xr */
121 { NULL, NULL, post_percent, NULL, NULL }, /* _%A */
122 { NULL, NULL, NULL, NULL, NULL }, /* _%B */
123 { NULL, NULL, post_percent, NULL, NULL }, /* _%D */
124 { NULL, NULL, NULL, NULL, NULL }, /* _%I */
125 { NULL, pre_enc, post_percent, "\\fI", "\\fP" }, /* %J */
126 { NULL, NULL, NULL, NULL, NULL }, /* _%N */
127 { NULL, NULL, NULL, NULL, NULL }, /* _%O */
128 { NULL, NULL, NULL, NULL, NULL }, /* _%P */
129 { NULL, NULL, NULL, NULL, NULL }, /* _%R */
130 { NULL, pre_enc, post_percent, "\"", "\"" }, /* %T */
131 { NULL, NULL, NULL, NULL, NULL }, /* _%V */
132 { NULL, NULL, NULL, NULL, NULL }, /* Ac */
133 { cond_body, pre_enc, post_enc, "<", ">" }, /* Ao */
134 { cond_body, pre_enc, post_enc, "<", ">" }, /* Aq */
135 { NULL, NULL, NULL, NULL, NULL }, /* At */
136 { NULL, NULL, NULL, NULL, NULL }, /* Bc */
137 { NULL, NULL, NULL, NULL, NULL }, /* _Bf */
138 { cond_body, pre_enc, post_enc, "[", "]" }, /* Bo */
139 { cond_body, pre_enc, post_enc, "[", "]" }, /* Bq */
140 { NULL, pre_ux, NULL, "BSD/OS", NULL }, /* Bsx */
141 { NULL, pre_bx, NULL, NULL, NULL }, /* Bx */
142 { NULL, NULL, NULL, NULL, NULL }, /* Db */
143 { NULL, NULL, NULL, NULL, NULL }, /* Dc */
144 { cond_body, pre_enc, post_enc, "``", "''" }, /* Do */
145 { cond_body, pre_enc, post_enc, "``", "''" }, /* Dq */
146 { NULL, NULL, NULL, NULL, NULL }, /* _Ec */
147 { NULL, NULL, NULL, NULL, NULL }, /* _Ef */
148 { NULL, pre_enc, post_enc, "\\fI", "\\fP" }, /* Em */
149 { NULL, NULL, NULL, NULL, NULL }, /* _Eo */
150 { NULL, pre_ux, NULL, "FreeBSD", NULL }, /* Fx */
151 { NULL, pre_enc, post_enc, "\\fB", "\\fP" }, /* Ms */
152 { NULL, NULL, NULL, NULL, NULL }, /* No */
153 { NULL, pre_ns, NULL, NULL, NULL }, /* Ns */
154 { NULL, pre_ux, NULL, "NetBSD", NULL }, /* Nx */
155 { NULL, pre_ux, NULL, "OpenBSD", NULL }, /* Ox */
156 { NULL, NULL, NULL, NULL, NULL }, /* Pc */
157 { NULL, NULL, post_pf, NULL, NULL }, /* Pf */
158 { cond_body, pre_enc, post_enc, "(", ")" }, /* Po */
159 { cond_body, pre_enc, post_enc, "(", ")" }, /* Pq */
160 { NULL, NULL, NULL, NULL, NULL }, /* Qc */
161 { cond_body, pre_enc, post_enc, "`", "'" }, /* Ql */
162 { cond_body, pre_enc, post_enc, "\"", "\"" }, /* Qo */
163 { cond_body, pre_enc, post_enc, "\"", "\"" }, /* Qq */
164 { NULL, NULL, NULL, NULL, NULL }, /* Re */
165 { cond_body, pre_pp, NULL, NULL, NULL }, /* Rs */
166 { NULL, NULL, NULL, NULL, NULL }, /* Sc */
167 { cond_body, pre_enc, post_enc, "`", "'" }, /* So */
168 { cond_body, pre_enc, post_enc, "`", "'" }, /* Sq */
169 { NULL, NULL, NULL, NULL, NULL }, /* _Sm */
170 { NULL, pre_enc, post_enc, "\\fI", "\\fP" }, /* Sx */
171 { NULL, pre_enc, post_enc, "\\fB", "\\fP" }, /* Sy */
172 { NULL, pre_enc, post_enc, "\\fR", "\\fP" }, /* Tn */
173 { NULL, pre_ux, NULL, "UNIX", NULL }, /* Ux */
174 { NULL, NULL, NULL, NULL, NULL }, /* _Xc */
175 { NULL, NULL, NULL, NULL, NULL }, /* _Xo */
176 { NULL, NULL, NULL, NULL, NULL }, /* _Fo */
177 { NULL, NULL, NULL, NULL, NULL }, /* _Fc */
178 { cond_body, pre_enc, post_enc, "[", "]" }, /* Oo */
179 { NULL, NULL, NULL, NULL, NULL }, /* Oc */
180 { NULL, NULL, NULL, NULL, NULL }, /* _Bk */
181 { NULL, NULL, NULL, NULL, NULL }, /* _Ek */
182 { NULL, pre_ux, NULL, "is currently in beta test.", NULL }, /* Bt */
183 { NULL, NULL, NULL, NULL, NULL }, /* Hf */
184 { NULL, NULL, NULL, NULL, NULL }, /* Fr */
185 { NULL, pre_ux, NULL, "currently under development.", NULL }, /* Ud */
186 { NULL, NULL, NULL, NULL, NULL }, /* _Lb */
187 { NULL, pre_pp, NULL, NULL, NULL }, /* Lp */
188 { NULL, NULL, NULL, NULL, NULL }, /* _Lk */
189 { NULL, NULL, NULL, NULL, NULL }, /* _Mt */
190 { cond_body, pre_enc, post_enc, "{", "}" }, /* Brq */
191 { cond_body, pre_enc, post_enc, "{", "}" }, /* Bro */
192 { NULL, NULL, NULL, NULL, NULL }, /* Brc */
193 { NULL, NULL, NULL, NULL, NULL }, /* _%C */
194 { NULL, NULL, NULL, NULL, NULL }, /* _Es */
195 { NULL, NULL, NULL, NULL, NULL }, /* _En */
196 { NULL, pre_ux, NULL, "DragonFly", NULL }, /* Dx */
197 { NULL, NULL, NULL, NULL, NULL }, /* _%Q */
198 { NULL, pre_br, NULL, NULL, NULL }, /* br */
199 { NULL, pre_sp, post_sp, NULL, NULL }, /* sp */
200 { NULL, NULL, NULL, NULL, NULL }, /* _%U */
201 { NULL, NULL, NULL, NULL, NULL }, /* _Ta */
202 { NULL, NULL, NULL, NULL, NULL }, /* ROOT */
206 print_word(struct mman *mm, const char *s)
211 * If we need a newline, print it now and start afresh.
216 } else if (mm->need_space && '\0' != s[0])
218 * If we need a space, only print it before
219 * (1) a nonzero length word;
220 * (2) a word that is non-punctuation; and
221 * (3) if punctuation, non-terminating puncutation.
223 if (NULL == strchr(".,:;)]?!", s[0]) || '\0' != s[1])
227 * Reassign needing space if we're not following opening
231 ('(' != s[0] && '[' != s[0]) || '\0' != s[1];
242 putchar((unsigned char)*s);
249 man_man(void *arg, const struct man *man)
253 * Dump the keep buffer.
254 * We're guaranteed by now that this exists (is non-NULL).
255 * Flush stdout afterward, just in case.
257 fputs(mparse_getkeep(man_mparse(man)), stdout);
262 man_mdoc(void *arg, const struct mdoc *mdoc)
264 const struct mdoc_meta *m;
265 const struct mdoc_node *n;
271 printf(".TH \"%s\" \"%s\" \"%s\" \"%s\" \"%s\"",
272 m->title, m->msec, m->date, m->os, m->vol);
274 memset(&mm, 0, sizeof(struct mman));
277 print_node(m, n, &mm);
282 print_node(DECL_ARGS)
284 const struct mdoc_node *prev, *sub;
285 const struct manact *act;
289 * Break the line if we were parsed subsequent the current node.
290 * This makes the page structure be more consistent.
292 prev = n->prev ? n->prev : n->parent;
293 if (prev && prev->line < n->line)
300 if (MDOC_TEXT == n->type) {
302 * Make sure that we don't happen to start with a
303 * control character at the start of a line.
305 if (mm->need_nl && ('.' == *n->string ||
306 '\'' == *n->string)) {
307 print_word(mm, "\\&");
310 print_word(mm, n->string);
313 * Conditionally run the pre-node action handler for a
316 act = manacts + n->tok;
317 cond = NULL == act->cond || (*act->cond)(m, n, mm);
318 if (cond && act->pre)
319 do_sub = (*act->pre)(m, n, mm);
323 * Conditionally run all child nodes.
324 * Note that this iterates over children instead of using
325 * recursion. This prevents unnecessary depth in the stack.
328 for (sub = n->child; sub; sub = sub->next)
329 print_node(m, sub, mm);
332 * Lastly, conditionally run the post-node handler.
334 if (cond && act->post)
335 (*act->post)(m, n, mm);
342 return(MDOC_HEAD == n->type);
349 return(MDOC_BODY == n->type);
353 * Output a font encoding before a node, e.g., \fR.
354 * This obviously has no trailing space.
361 prefix = manacts[n->tok].prefix;
364 print_word(mm, prefix);
370 * Output a font encoding subsequent a node, e.g., \fP.
377 suffix = manacts[n->tok].suffix;
381 print_word(mm, suffix);
385 * Used in listings (percent = %A, e.g.).
386 * FIXME: this is incomplete.
387 * It doesn't print a nice ", and" for lists.
390 post_percent(DECL_ARGS)
403 * Print before a section header.
409 if (MDOC_HEAD != n->type)
412 print_word(mm, manacts[n->tok].prefix);
413 print_word(mm, "\"");
419 * Print subsequent a section header.
425 if (MDOC_HEAD != n->type)
428 print_word(mm, "\"");
446 if (DISP_unfilled == n->norm->Bd.type ||
447 DISP_literal == n->norm->Bd.type) {
449 print_word(mm, ".nf");
459 if (DISP_unfilled == n->norm->Bd.type ||
460 DISP_literal == n->norm->Bd.type) {
462 print_word(mm, ".fi");
472 print_word(mm, ".br");
483 print_word(mm, n->string);
487 print_word(mm, "BSD");
493 print_word(mm, n->string);
502 print_word(mm, ".RS 6n");
512 print_word(mm, ".RE");
519 const struct mdoc_node *bln;
521 if (MDOC_HEAD == n->type) {
523 print_word(mm, ".TP");
524 bln = n->parent->parent->prev;
525 switch (bln->norm->Bl.type) {
527 print_word(mm, "4n");
529 print_word(mm, "\\fBo\\fP");
532 if (bln->norm->Bl.width)
533 print_word(mm, bln->norm->Bl.width);
545 if (MDOC_ELEM != n->type && MDOC_HEAD != n->type)
547 print_word(mm, "\\fB");
549 if (NULL == n->child)
550 print_word(mm, m->name);
558 if (MDOC_ELEM != n->type && MDOC_HEAD != n->type)
561 print_word(mm, "\\fP");
584 if (MDOC_It == n->parent->tok)
585 print_word(mm, ".sp");
587 print_word(mm, ".PP");
597 print_word(mm, ".sp");
615 print_node(m, n, mm);
621 print_node(m, n, mm);
630 print_word(mm, manacts[n->tok].prefix);
631 if (NULL == n->child)
634 print_word(mm, "\\~");