Merge branch 'vendor/DIFFUTILS'
[dragonfly.git] / contrib / mdocml / mdoc_man.c
1 /*      $Id: mdoc_man.c,v 1.9 2011/10/24 21:47:59 schwarze Exp $ */
2 /*
3  * Copyright (c) 2011 Ingo Schwarze <schwarze@openbsd.org>
4  *
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.
8  *
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.
16  */
17 #ifdef HAVE_CONFIG_H
18 #include "config.h"
19 #endif
20
21 #include <stdio.h>
22 #include <string.h>
23
24 #include "mandoc.h"
25 #include "man.h"
26 #include "mdoc.h"
27 #include "main.h"
28
29 #define DECL_ARGS const struct mdoc_meta *m, \
30                   const struct mdoc_node *n, \
31                   struct mman *mm
32
33 struct  mman {
34         int               need_space; /* next word needs prior ws */
35         int               need_nl; /* next word needs prior nl */
36 };
37
38 struct  manact {
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 */
44 };
45
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);
72
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."
98             }, /* Ex */
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."
116                 }, /* Rv */
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 */
203 };
204
205 static void
206 print_word(struct mman *mm, const char *s)
207 {
208
209         if (mm->need_nl) {
210                 /* 
211                  * If we need a newline, print it now and start afresh.
212                  */
213                 putchar('\n');
214                 mm->need_space = 0;
215                 mm->need_nl = 0;
216         } else if (mm->need_space && '\0' != s[0])
217                 /*
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.
222                  */
223                 if (NULL == strchr(".,:;)]?!", s[0]) || '\0' != s[1])
224                         putchar(' ');
225
226         /*
227          * Reassign needing space if we're not following opening
228          * punctuation.
229          */
230         mm->need_space = 
231                 ('(' != s[0] && '[' != s[0]) || '\0' != s[1];
232
233         for ( ; *s; s++) {
234                 switch (*s) {
235                 case (ASCII_NBRSP):
236                         printf("\\~");
237                         break;
238                 case (ASCII_HYPH):
239                         putchar('-');
240                         break;
241                 default:
242                         putchar((unsigned char)*s);
243                         break;
244                 }
245         }
246 }
247
248 void
249 man_man(void *arg, const struct man *man)
250 {
251
252         /*
253          * Dump the keep buffer.
254          * We're guaranteed by now that this exists (is non-NULL).
255          * Flush stdout afterward, just in case.
256          */
257         fputs(mparse_getkeep(man_mparse(man)), stdout);
258         fflush(stdout);
259 }
260
261 void
262 man_mdoc(void *arg, const struct mdoc *mdoc)
263 {
264         const struct mdoc_meta *m;
265         const struct mdoc_node *n;
266         struct mman             mm;
267
268         m = mdoc_meta(mdoc);
269         n = mdoc_node(mdoc);
270
271         printf(".TH \"%s\" \"%s\" \"%s\" \"%s\" \"%s\"",
272                         m->title, m->msec, m->date, m->os, m->vol);
273
274         memset(&mm, 0, sizeof(struct mman));
275
276         mm.need_nl = 1;
277         print_node(m, n, &mm);
278         putchar('\n');
279 }
280
281 static void
282 print_node(DECL_ARGS)
283 {
284         const struct mdoc_node  *prev, *sub;
285         const struct manact     *act;
286         int                      cond, do_sub;
287         
288         /*
289          * Break the line if we were parsed subsequent the current node.
290          * This makes the page structure be more consistent.
291          */
292         prev = n->prev ? n->prev : n->parent;
293         if (prev && prev->line < n->line)
294                 mm->need_nl = 1;
295
296         act = NULL;
297         cond = 0;
298         do_sub = 1;
299
300         if (MDOC_TEXT == n->type) {
301                 /*
302                  * Make sure that we don't happen to start with a
303                  * control character at the start of a line.
304                  */
305                 if (mm->need_nl && ('.' == *n->string || 
306                                         '\'' == *n->string)) {
307                         print_word(mm, "\\&");
308                         mm->need_space = 0;
309                 }
310                 print_word(mm, n->string);
311         } else {
312                 /*
313                  * Conditionally run the pre-node action handler for a
314                  * node.
315                  */
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);
320         }
321
322         /* 
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.
326          */
327         if (do_sub)
328                 for (sub = n->child; sub; sub = sub->next)
329                         print_node(m, sub, mm);
330
331         /*
332          * Lastly, conditionally run the post-node handler.
333          */
334         if (cond && act->post)
335                 (*act->post)(m, n, mm);
336 }
337
338 static int
339 cond_head(DECL_ARGS)
340 {
341
342         return(MDOC_HEAD == n->type);
343 }
344
345 static int
346 cond_body(DECL_ARGS)
347 {
348
349         return(MDOC_BODY == n->type);
350 }
351
352 /*
353  * Output a font encoding before a node, e.g., \fR.
354  * This obviously has no trailing space.
355  */
356 static int
357 pre_enc(DECL_ARGS)
358 {
359         const char      *prefix;
360
361         prefix = manacts[n->tok].prefix;
362         if (NULL == prefix)
363                 return(1);
364         print_word(mm, prefix);
365         mm->need_space = 0;
366         return(1);
367 }
368
369 /*
370  * Output a font encoding subsequent a node, e.g., \fP.
371  */
372 static void
373 post_enc(DECL_ARGS)
374 {
375         const char *suffix;
376
377         suffix = manacts[n->tok].suffix;
378         if (NULL == suffix)
379                 return;
380         mm->need_space = 0;
381         print_word(mm, suffix);
382 }
383
384 /*
385  * Used in listings (percent = %A, e.g.).
386  * FIXME: this is incomplete. 
387  * It doesn't print a nice ", and" for lists.
388  */
389 static void
390 post_percent(DECL_ARGS)
391 {
392
393         post_enc(m, n, mm);
394         if (n->next)
395                 print_word(mm, ",");
396         else {
397                 print_word(mm, ".");
398                 mm->need_nl = 1;
399         }
400 }
401
402 /*
403  * Print before a section header.
404  */
405 static int
406 pre_sect(DECL_ARGS)
407 {
408
409         if (MDOC_HEAD != n->type)
410                 return(1);
411         mm->need_nl = 1;
412         print_word(mm, manacts[n->tok].prefix);
413         print_word(mm, "\"");
414         mm->need_space = 0;
415         return(1);
416 }
417
418 /*
419  * Print subsequent a section header.
420  */
421 static void
422 post_sect(DECL_ARGS)
423 {
424
425         if (MDOC_HEAD != n->type)
426                 return;
427         mm->need_space = 0;
428         print_word(mm, "\"");
429         mm->need_nl = 1;
430 }
431
432 static int
433 pre_ap(DECL_ARGS)
434 {
435
436         mm->need_space = 0;
437         print_word(mm, "'");
438         mm->need_space = 0;
439         return(0);
440 }
441
442 static int
443 pre_bd(DECL_ARGS)
444 {
445
446         if (DISP_unfilled == n->norm->Bd.type ||
447             DISP_literal  == n->norm->Bd.type) {
448                 mm->need_nl = 1;
449                 print_word(mm, ".nf");
450         }
451         mm->need_nl = 1;
452         return(1);
453 }
454
455 static void
456 post_bd(DECL_ARGS)
457 {
458
459         if (DISP_unfilled == n->norm->Bd.type ||
460             DISP_literal  == n->norm->Bd.type) {
461                 mm->need_nl = 1;
462                 print_word(mm, ".fi");
463         }
464         mm->need_nl = 1;
465 }
466
467 static int
468 pre_br(DECL_ARGS)
469 {
470
471         mm->need_nl = 1;
472         print_word(mm, ".br");
473         mm->need_nl = 1;
474         return(0);
475 }
476
477 static int
478 pre_bx(DECL_ARGS)
479 {
480
481         n = n->child;
482         if (n) {
483                 print_word(mm, n->string);
484                 mm->need_space = 0;
485                 n = n->next;
486         }
487         print_word(mm, "BSD");
488         if (NULL == n)
489                 return(0);
490         mm->need_space = 0;
491         print_word(mm, "-");
492         mm->need_space = 0;
493         print_word(mm, n->string);
494         return(0);
495 }
496
497 static int
498 pre_dl(DECL_ARGS)
499 {
500
501         mm->need_nl = 1;
502         print_word(mm, ".RS 6n");
503         mm->need_nl = 1;
504         return(1);
505 }
506
507 static void
508 post_dl(DECL_ARGS)
509 {
510
511         mm->need_nl = 1;
512         print_word(mm, ".RE");
513         mm->need_nl = 1;
514 }
515
516 static int
517 pre_it(DECL_ARGS)
518 {
519         const struct mdoc_node *bln;
520
521         if (MDOC_HEAD == n->type) {
522                 mm->need_nl = 1;
523                 print_word(mm, ".TP");
524                 bln = n->parent->parent->prev;
525                 switch (bln->norm->Bl.type) {
526                 case (LIST_bullet):
527                         print_word(mm, "4n");
528                         mm->need_nl = 1;
529                         print_word(mm, "\\fBo\\fP");
530                         break;
531                 default:
532                         if (bln->norm->Bl.width)
533                                 print_word(mm, bln->norm->Bl.width);
534                         break;
535                 }
536                 mm->need_nl = 1;
537         }
538         return(1);
539 }
540
541 static int
542 pre_nm(DECL_ARGS)
543 {
544
545         if (MDOC_ELEM != n->type && MDOC_HEAD != n->type)
546                 return(1);
547         print_word(mm, "\\fB");
548         mm->need_space = 0;
549         if (NULL == n->child)
550                 print_word(mm, m->name);
551         return(1);
552 }
553
554 static void
555 post_nm(DECL_ARGS)
556 {
557
558         if (MDOC_ELEM != n->type && MDOC_HEAD != n->type)
559                 return;
560         mm->need_space = 0;
561         print_word(mm, "\\fP");
562 }
563
564 static int
565 pre_ns(DECL_ARGS)
566 {
567
568         mm->need_space = 0;
569         return(0);
570 }
571
572 static void
573 post_pf(DECL_ARGS)
574 {
575
576         mm->need_space = 0;
577 }
578
579 static int
580 pre_pp(DECL_ARGS)
581 {
582
583         mm->need_nl = 1;
584         if (MDOC_It == n->parent->tok)
585                 print_word(mm, ".sp");
586         else
587                 print_word(mm, ".PP");
588         mm->need_nl = 1;
589         return(1);
590 }
591
592 static int
593 pre_sp(DECL_ARGS)
594 {
595
596         mm->need_nl = 1;
597         print_word(mm, ".sp");
598         return(1);
599 }
600
601 static void
602 post_sp(DECL_ARGS)
603 {
604
605         mm->need_nl = 1;
606 }
607
608 static int
609 pre_xr(DECL_ARGS)
610 {
611
612         n = n->child;
613         if (NULL == n)
614                 return(0);
615         print_node(m, n, mm);
616         n = n->next;
617         if (NULL == n)
618                 return(0);
619         mm->need_space = 0;
620         print_word(mm, "(");
621         print_node(m, n, mm);
622         print_word(mm, ")");
623         return(0);
624 }
625
626 static int
627 pre_ux(DECL_ARGS)
628 {
629
630         print_word(mm, manacts[n->tok].prefix);
631         if (NULL == n->child)
632                 return(0);
633         mm->need_space = 0;
634         print_word(mm, "\\~");
635         mm->need_space = 0;
636         return(1);
637 }