d8dad3f21bd9187d1cdcf527df48306b0da0dbcb
[platform/upstream/groff.git] / contrib / groffer / roff2.pl
1 #! /usr/bin/env perl
2
3 # roff2* - transform roff files into other formats
4
5 # Source file position: <groff-source>/contrib/groffer/roff2.pl
6 # Installed position: <prefix>/bin/roff2*
7
8 # Copyright (C) 2006-2014  Free Software Foundation, Inc.
9 # Written by Bernd Warken <groff-bernd.warken-72@web.de>.
10
11 # This file is part of `groffer', which is part of `groff'.
12
13 # `groff' is free software; you can redistribute it and/or modify it
14 # under the terms of the GNU General Public License as published by
15 # the Free Software Foundation, either version 3 of the License, or
16 # (at your option) any later version.
17
18 # `groff' is distributed in the hope that it will be useful, but
19 # WITHOUT ANY WARRANTY; without even the implied warranty of
20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
21 # General Public License for more details.
22
23 # You should have received a copy of the GNU General Public License
24 # along with this program. If not, see
25 # <http://www.gnu.org/licenses/gpl-2.0.html>.
26
27 ########################################################################
28
29 require v5.6;
30
31 use strict;
32 use warnings;
33 use File::Spec;
34
35 # temporary dir and files
36 use File::Temp qw/ tempfile /;
37
38 my $Dev_Null = File::Spec->devnull();
39 my $Mode;
40 my $Name;
41 {
42   my ($v, $d);
43   ($v, $d, $Name) = File::Spec->splitpath($0);
44   die "wrong program name: $Name;"
45     if $Name !~ /^roff2[a-z]/;
46 }
47 $Mode = $Name;
48 $Mode =~ s/^roff2//;
49
50 my $Groff_Version = `groff --version 2>$Dev_Null`;
51 die "$Name error: groff does not work;" if $?;
52 my $Groffer_Version = `groffer --version 2>$Dev_Null`;
53 my $Has_Groffer = ! $?;
54
55 if ($Has_Groffer) {
56   foreach (@ARGV) {
57     last if $_ eq '--';
58     next if $_ eq '-';
59     if ($_ eq '-v' || '--version' =~ m|^$_|) {
60       print $Name, ' in ', $Groffer_Version;
61       exit 0;
62     }
63     if ($_ eq '-h' || '--help' =~ m|^$_|) {
64       &usage_with_groffer();
65       exit 0;
66     }
67   }
68   system('groffer', '--to-stdout', "--$Mode", @ARGV);
69   exit $?;
70 } else {
71   my @filespec;
72   my $double_minus;
73   foreach (@ARGV) {
74     if ($double_minus) {
75       push @filespec, $_;
76       next;
77     }
78     if ($_ eq '--') {
79       $double_minus = 1;
80       next;
81     }
82     if ($_ eq '-') {
83       push @filespec, '-';
84       next;
85     }
86     if ($_ eq '-v' || '--version' =~ m|^$_|) {
87       print $Name, ' in ', $Groff_Version;
88       exit 0;
89     }
90     if ($_ eq '-h' || '--help' =~ m|^$_|) {
91       &usage_without_groffer();
92       exit 0;
93     }
94     if ($_ =~ /^-/) {
95       &error_no_groffer();
96       &error_no_options();
97       next;
98     }
99     if (-f && -r) {
100       push @filespec, $_;
101     } else {
102       &error_no_groffer();
103       print STDERR "$_ is not an existing, readable file.\n";
104     }
105   }
106
107   @filespec = ('-') unless @filespec;
108   my $has_stdin;
109   foreach (@filespec) {
110     if ($_ eq '-') {
111       $has_stdin =1;
112       last;
113     }
114   }
115
116   if ($has_stdin) {
117     my $tempdir;
118     foreach ($ENV{'GROFF_TMPDIR'}, $ENV{'TMPDIR'}, $ENV{'TMP'},
119              $ENV{'TEMP'}, $ENV{'TEMPDIR'},
120              File::Spec->catfile($ENV{'HOME'}, 'tmp')) {
121       if ($_ && -d $_ && -w $_) {
122         $tempdir = $_;
123         last;
124       }
125     }
126     my $template = $Name . '_XXXX';
127     my ($fh, $stdin);
128     if ($tempdir) {
129       ($fh, $stdin) = tempfile($template, UNLINK => 1, DIR => $tempdir) ||
130         die "$Name: could not create temporary file;";
131     } else {
132       ($fh, $stdin) = tempfile($template, UNLINK => 1) ||
133         die "$Name: could not create temporary file;";
134     }
135     open $fh, ">$stdin";
136     print $fh $_ foreach <STDIN>;
137
138     foreach (@filespec) {
139       $_ = $stdin if $_ eq '-';
140     }
141   }                             # if $has_stdin
142
143   my $grog;
144   my $groff_options = "-T$Mode";
145   $groff_options = '-TX75-12 -Z' if $Mode eq 'x';
146   $groff_options = '-Tlatin1' if $Mode eq 'text';
147   if ($Mode eq 'pdf') {
148     my $ps2pdf;
149     my @path = File::Spec->path();
150     foreach (@path) {
151       my $file = File::Spec->catfile($_, 'ps2pdf');
152       if (-f $file && -x $file) {
153         $ps2pdf = "$file -";
154         last;
155       }
156     }
157     unless ($ps2pdf) {
158       foreach (@path) {
159         my $file = File::Spec->catfile($_, 'gs');
160         if (-f $file && -x $file) {
161           $ps2pdf = $file . ' -q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite ' .
162             '-sOutputFile=- -c save pop -f -';
163           last;
164         }
165       }
166     }
167     $grog = `grog -Tps @filespec`;
168     chomp $grog;
169     system("$grog | $ps2pdf");
170     exit $?;
171   } else {
172     $grog = `grog $groff_options @filespec`;
173     chomp $grog;
174     system($grog);
175     exit $?;
176   }
177 }
178
179 my $error_no_groffer;
180 sub error_no_groffer {
181   return 1 if $error_no_groffer;
182   $error_no_groffer = 1;
183   print STDERR "$Name: groffer is not available.\n";
184 }
185
186 my $error_no_options;
187 sub error_no_options {
188   return 1 if $error_no_options;
189   $error_no_groffer = 1;
190   print STDERR "$Name: groffer options are not allowed.\n";
191 }
192
193 sub usage_with_groffer {
194   print <<EOF;
195 usage: $Name [option]... [--] [filespec]...
196
197 -h | --help     print usage information
198 -v | --version  print version information
199
200 All other options are arbitrary options of `groffer'; the options
201 override the behavior of this program.
202
203 filespec's are the same as in `groffer': either the names of existing,
204 readable files or `-' for standard input or a search pattern for man
205 pages.  No `filespec' assumes standard input automatically.
206 EOF
207 }
208
209 sub usage_without_groffer {
210   print <<EOF;
211 usage: $Name [option]... [--] [filespec]...
212
213 -h | --help     print usage information
214 -v | --version  print version information
215
216 No other options are allowed because `groffer' is not available.
217
218 The only `filespec's allowed are the names of existing, readable files
219 or `-' for standard input.  No `filespec' assumes standard input
220 automatically.
221 EOF
222 }
223
224
225 1;
226 ########################################################################
227 ### Emacs settings
228 # Local Variables:
229 # mode: CPerl
230 # End: