1#!/usr/local/bin/perl
2# Written by Adrian Mariano, additional features by Eric Backus,
3# yet more features by Tim Mann.
4
5# Script to translate a texinfo file into an nroff manual page.
6
7$version="1.01.tpm1";
8
9$html=0;
10$ignore=0;
11$tex=0;
12$doman=0;
13$title=0;
14$diditem=0;
15$justdidlp=1;
16$noman=0;
17$info=0;
18$manprefix="";
19$args=($#ARGV < 0) ? "stdin" : "@ARGV";
20$itemxcomma=0;
21
22printf(".\\\"Do not edit this file.  It was created from %s\n", $args);
23printf(".\\\"using texi2man version %s on %s", $version, `date`);
24printf(".\\\"If you want a typeset version, you will get better\n");
25printf(".\\\"results with the original file.\n.\\\"\n");
26
27sub parse
28{
29    if (s/\@c man //) { print; next; }
30    if (/\@c noman/) { $noman=1; next; }
31    if (/\@c end noman/) { $noman=0; next; }
32    if ($noman) { next; }
33
34    if (/\@c ifman\s*(.*)/) { $doman=1; $manprefix = $1; next; }
35    if (/\@c end ifman/) { $doman=0; $manprefix = ""; next; }
36
37    if (/^\\input/) { next; }
38    if (/^\*/) { next; }
39    if (/^INFO-DIR-SECTION/) { next; }
40    if (/^START-INFO-DIR-ENTRY/) { next; }
41    if (/^END-INFO-DIR-ENTRY/) { next; }
42
43    if (/\@titlepage/) { $title=1; next; }
44    if (/\@end titlepage/) { $title=0; next; }
45    if (/\@tex/) { $tex=1; next; }
46    if (/\@end tex/) { $tex=0; next; }
47    if (/\@ignore/) { $ignore=1; next; }
48    if (/\@end ignore/) { $ignore=0; next; }
49    if (/\@ifinfo/) { $info=1; next; }
50    if (/\@end ifinfo/) { $info=0; next; }
51    if (/\@ifhtml/) { $html=1; next; }
52    if (/\@end ifhtml/) { $html=0; next; }
53    if (/\@ifnothtml/) { next; }
54    if (/\@end ifhtml/) { next; }
55    if (/\@iftex/) { $tex=1; next; }
56    if (/\@end iftex/) { $tex=0; next; }
57    if (/\@ifnottex/) { next; }
58    if (/\@end iftex/) { next; }
59    if (!$doman && ($ignore || $html || $title || $tex || $info)) { next; }
60
61    s/\\/\\\\/g;
62    s/\@-/\\-/g;
63    s/\@cite\{([^}]*)}/\`$1\'/g;
64    s/\@code\{([^}]*)}/\`$1\'/g;
65    s/\@email\{([^}]*)}/\`$1\'/g;
66    s/\@file\{([^}]*)}/\`$1\'/g;
67    s/\@kbd\{([^}]*)}/\`$1\'/g;
68    s/\@samp\{([^}]*)}/\`$1\'/g;
69    s/\@url\{([^}]*)}/\`$1\'/g;
70    s/\@dfn\{([^}]*)}/\"$1\"/g;
71    s/\@key\{([^}]*)}/<$1>/g;
72    s/\@emph\{([^}]*)}/\\fI$1\\fR/g;
73    s/\@strong\{([^}]*)}/\\fB$1\\fR/g;
74    s/\@var\{([^}]*)}/\U$1\E/g;
75    s/\@sc\{([^}]*)}/\U$1\E/g;
76    s/\@w\{([^}]*)}/$1/g;
77    s/\@pxref\{([^}]*)}/See \\fI$1\\fR/g;
78    s/\@xref\{([^}]*)}/See \\fI$1\\fR/g;
79    s/\@ref\{([^}]*)}/\\fI$1\\fR/g;
80    s/\@footnote\{([^}]*)}/[$1]/g;
81    s/\@minus\{}/-/g;
82    s/\@copyright\{}/(C)/g;
83    s/\@noindent//;
84    s/\@\{/{/g;
85    s/\@}/}/g;
86    s/\@\@/@/g;
87    s/\\'\\'/\"/g;
88    s/\\`\\`/\"/g;
89    s/---/ -- /g;
90
91    s/\@value\{([^\s]+)}/$value{$1}/eg;
92    if (/\@set\s+([^\s]+)\s+(.*)$/) { $value{$1} = $2; next; }
93    if (/\@clear\s+([^\s]+)\s+(.*)$/) { delete $value{$1}; next; }
94
95    if ($itemxcomma) {
96        # If multiple items have the same description, put the items
97        # on one line, separated by commas
98        if (/\@itemx (.*)/) { printf(", $1"); $diditem=1; next; }
99        elsif ($diditem) { printf("\n"); $diditem=0; }
100	if (/\@item (.*)/) {
101	    printf("%s.TP\n%s.B $1", $manprefix, $manprefix);
102	    $diditem=1;
103	    next;
104	}
105    } else {
106        # If multiple items have the same description, put the items
107        # on separate lines
108	if (/\@item (.*)/) {
109	    printf("%s.TP\n%s.B $1\n", $manprefix, $manprefix);
110	    next;
111	}
112	if (/\@itemx (.*)/) {
113	    printf("%s.PD 0\n%s.TP\n%s.B $1\n%s.PD\n",
114		   $manprefix, $manprefix, $manprefix, $manprefix);
115	    next;
116	}
117    }
118
119    if (s/\@chapter (.*)/.SH \U$1\E/)
120    {
121	printf("%s%s", $manprefix, $_);
122	$justdidlp=1;
123	next;
124    }
125    if (s/\@unnumbered (.*)/.SH \U$1\E/)
126    {
127	printf("%s%s", $manprefix, $_);
128	$justdidlp=1;
129	next;
130    }
131    if (s/\@section (.*)/$1/)
132    {
133	printf("%s.SS %s", $manprefix, $_);
134	$justdidlp=1;
135	next;
136    }
137
138    if (/\@example/) {
139	printf("%s.nf\n", $manprefix);
140	$manprefix = $manprefix . "    ";
141	$example=1;
142	next;
143    }
144    if (/\@end example/) {
145	$manprefix = substr($manprefix, 0, -4);
146	printf("%s.fi\n", $manprefix);
147	$example=0;
148	next;
149    }
150
151    if (/\@display/) { printf("%s.nf\n", $manprefix); $example=1; next; }
152    if (/\@end display/) { printf("%s.fi\n", $manprefix); $example=0; next; }
153    if (/\@format/) { printf("%s.nf\n", $manprefix); $example=1; next; }
154    if (/\@end format/) { printf("%s.fi\n", $manprefix); $example=0; next; }
155    if (/\@smallexample/) { printf("%s.nf\n", $manprefix); $example=1; next; }
156    if (/\@end smallexample/) { printf("%s.fi\n", $manprefix); $example=0; next; }
157    if (!$example && /^\s*$/ && !$doman)
158    {
159	if ($justdidlp) { next; }
160#	printf(".PP\n");
161	printf("\n");
162	$justdidlp=1;
163	next;
164    }
165    if (/\@end table/) {
166	printf("%s.PP\n", $manprefix);
167	$justdidlp=1;
168    }
169
170    if (/\@include (.*)/) {
171	my $INCL;
172        open($INCL, $1) or open($INCL, "../$1") or die "$1: $!";
173        while (<$INCL>) {
174            parse();
175        }
176        next;
177    }
178
179    if (/^\@/) { next; }
180
181    printf("%s%s", $manprefix, $_);
182
183    if (!$doman) { $justdidlp=0; }
184}
185
186while(<>)
187{
188    parse();
189}
190