Imported Upstream version 1.22.4
[platform/upstream/groff.git] / contrib / groffer / groffer.pl
1 #! /usr/bin/env perl
2
3 # groffer - display groff files
4
5 # Source file position: <groff-source>/contrib/groffer/groffer.pl
6 # Installed position: <prefix>/bin/groffer
7
8 # Copyright (C) 2006-2018 Free Software Foundation, Inc.
9
10 # Written by Bernd Warken <groff-bernd.warken-72@web.de>.
11
12 # Last update: 27 Aug 2015
13
14 # This file is part of 'groffer', which is part of 'groff'.
15
16 # 'groff' is free software; you can redistribute it and/or modify it
17 # under the terms of the GNU General Public License as published by
18 # the Free Software Foundation, either version 2 of the License, or
19 # (at your option) any later version.
20
21 # 'groff' is distributed in the hope that it will be useful, but
22 # WITHOUT ANY WARRANTY; without even the implied warranty of
23 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
24 # General Public License for more details.
25
26 # You should have received a copy of the GNU General Public License
27 # along with this program.  If not, see
28 # <http://www.gnu.org/licenses/gpl-2.0.html>.
29
30 ########################################################################
31
32 use strict;
33 use warnings;
34 #use diagnostics;
35
36 # temporary dir and files
37 use File::Temp qw/ tempfile tempdir /;
38
39 # needed for temporary dir
40 use File::Spec;
41
42 # for 'copy' and 'move'
43 use File::Copy;
44
45 # for fileparse, dirname and basename
46 use File::Basename;
47
48 # current working directory
49 use Cwd;
50
51 # $Bin is the directory where this script is located
52 use FindBin;
53
54
55 ########################################################################
56 # system variables and exported variables
57 ########################################################################
58
59 require 5.6.1;
60
61 our $Dev_Null;
62 our $Umask;
63 our @Path;
64 our $Start_Dir;
65
66 our $tmpdir = '';
67 our ($fh_cat, $tmp_cat);
68 our ($fh_stdin, $tmp_stdin);
69
70 our @Addopts_Groff;
71 our %Debug;
72 our %Opt;
73
74 our $Has_Compression;
75 our $Has_bzip;
76
77 our $Output_File_Name;
78
79 our $Apropos_Prog;
80 our $Filespec_Arg;
81 our $Filespec_Is_Man;
82 our $Macro_Pkg;
83 our $Manspec;
84 our $No_Filespecs;
85 our $Special_Filespec;
86 our $Special_Setup;
87
88 our %Man;
89
90 BEGIN {
91   $Dev_Null = File::Spec->devnull();
92
93   $Umask = umask 077;
94
95   $Start_Dir = getcwd;
96
97   # flush after each print or write command
98   $| = 1;
99 }
100
101
102 ########################################################################
103 # read-only variables with double-@ construct
104 ########################################################################
105
106 our $File_split_env_sh;
107 our $File_version_sh;
108 our $Groff_Version;
109
110 my $before_make;                # script before run of 'make'
111 {
112   my $at = '@';
113   $before_make = 1 if '@VERSION@' eq "${at}VERSION${at}";
114 }
115
116 my %at_at;
117 my $groffer_libdir;
118
119 if ($before_make) {
120   my $groffer_source_dir = $FindBin::Bin;
121   $at_at{'BINDIR'} = $groffer_source_dir;
122   $at_at{'G'} = '';
123   $at_at{'LIBDIR'} = '';
124   $groffer_libdir = $groffer_source_dir;
125   $File_version_sh = File::Spec->catfile($groffer_source_dir, 'version.sh');
126   $Groff_Version = '';
127 } else {
128   $Groff_Version = '@VERSION@';
129   $at_at{'BINDIR'} = '@BINDIR@';
130   $at_at{'G'} = '@g@';
131   $at_at{'LIBDIR'} = '@libdir@';
132   $groffer_libdir = '@groffer_dir@';
133   $File_version_sh = File::Spec->catfile($groffer_libdir, 'version.sh');
134 }
135
136 die "$groffer_libdir is not an existing directory;"
137   unless -d $groffer_libdir;
138
139 unshift(@INC, $groffer_libdir);
140
141 $File_split_env_sh = File::Spec->catfile($groffer_libdir, 'split_env.sh');
142 die "$File_split_env_sh does not exist;" unless -f "$File_split_env_sh";
143
144 require 'subs.pl';
145 require 'main_subs.pl';
146 require 'man.pl';
147
148 @Path = &path_uniq( File::Spec->path() );
149
150 if ( &where_is_prog('gzip') ) {
151   $Has_Compression = 1;
152   $Has_bzip = 1 if &where_is_prog('bzip2');
153 }
154
155
156 ########################################################################
157 # modes, viewers, man sections, and defaults
158 ########################################################################
159
160 # configuration files
161 our @Conf_Files = (File::Spec->catfile(File::Spec->rootdir(),
162                                       'etc', 'groff', 'groffer.conf'),
163                   File::Spec->catfile("$ENV{'HOME'}", '.groff',
164                                       'groffer.conf')
165                  );
166
167 our @Default_Modes = ('pdf', 'pdf2', 'html', 'xhtml', 'ps', 'x', 'dvi',
168                       'tty');
169 our $Default_Resolution = 100;
170 our $Default_tty_Device = 'utf8';
171
172 our @Macro_Packages = ('-man', '-mdoc', '-me', '-mm', '-mom', '-ms');
173
174 our %Viewer_tty = ('DVI' => [],
175                   'HTML' => ['lynx', 'w3m'],
176                   'PDF' => [],
177                   'PS' => [],
178                   'TTY' => ['less -r -R', 'more', 'pager'],
179                   'X' => [],
180                  );
181
182 our %Viewer_X = ('DVI' => ['kdvi', 'xdvi', 'dvilx'],
183                 'HTML' => ['konqueror', 'epiphany'. 'mozilla-firefox',
184                            'firefox', 'mozilla', 'netscape', 'galeon',
185                            'opera', 'amaya','arena', 'mosaic'],
186                 'XHTML' => ['konqueror', 'epiphany'. 'mozilla-firefox',
187                            'firefox', 'mozilla', 'netscape', 'galeon',
188                            'opera', 'amaya','arena', 'mosaic'],
189                 'PDF' => ['okular', 'kpdf', 'acroread', 'evince',
190                           'xpdf -z 150', 'gpdf', 'xpdf', 'zathura'.
191                           'epdfview', 'qpdfview', 'apvlv', 'qpdfview',
192                           'kghostview --scale 1.45', 'gv', 'ggv'],
193                 'PS' => ['okular', 'evince', 'gv',
194                          'gs', 'gs_x11', 'ghostscript', 'ghostview',
195                          'kghostview --scale 1.45', 'ggv', 'kpdf'],
196                 'TTY' => ['xless'],
197                 'X' => ['gxditview', 'xditview'],
198               );
199
200 %Man = ('ALL' => 0,
201         'AUTO_SEC' => ['1', '2', '3', '4', '5', '6', '7', '8', '9',
202                           'n', 'o'],
203         'ENABLE' => 1,
204            'EXT' => '',
205            'FORCE' => 0,
206            'IS_SETUP' => 0,
207            'MANSPEC' => {},
208            'LANG' => '',
209            'LANG2' => '',
210            'PATH' => [],
211            'SEC' => [],
212            'SEC_CHARS' => '',
213            'SYS' => [],
214           );
215 $Man{'AUTO_SEC_CHARS'} = join('', @{$Man{'AUTO_SEC'}});
216
217
218 ########################################################################
219 # given options
220 ########################################################################
221
222 our %Opts_Cmdline_Short;
223 our %Opts_Cmdline_Long;
224 our $Opts_Cmdline_Long_Str;
225 our %Opts_Cmdline_Double;
226 our %Opts_Groff_Short;
227
228 &main_set_options();
229
230
231 ########################################################################
232 # $MANOPT
233 ########################################################################
234
235 # handle environment variable $MANOPT
236 our @Manopt;
237
238 &main_parse_MANOPT();
239
240
241 ########################################################################
242 # configuration files, $GROFFER_OPT, and command line, main_config_params()
243 ########################################################################
244
245 our @Options;
246 our @Filespecs;
247 our @Starting_Conf;
248 our @Starting_ARGV = @ARGV;
249
250 &main_config_params();
251
252 if (0) {
253   print STDERR "<$_>\n" foreach @ARGV;
254 }
255
256
257 ########################################################################
258 # main_parse_params()
259 ########################################################################
260
261 $Opt{'XRM'} = [];
262
263 our $i = 0;
264 our $n = $#Options;
265
266 &main_parse_params();
267
268
269 ########################################################################
270 # main_set_mode()
271 ########################################################################
272
273 our $Viewer_Background;
274 our $PDF_Did_Not_Work;
275 our $PDF_Has_gs;
276 our $PDF_Has_ps2pdf;
277 our %Display = ('MODE' => '',
278                'PROG' => '',
279                'ARGS' => ''
280               );
281
282 &main_set_mode();
283
284
285 ########################################################################
286 # groffer temporary directory, main_temp()
287 ########################################################################
288
289 &main_temp();
290
291
292 ########################################################################
293 # tmp functions and compression
294 ########################################################################
295
296 ########################################################################
297 # main_do_fileargs() and related subs
298 ########################################################################
299
300 our @REG_TITLE = ();
301
302 &main_do_fileargs();
303
304
305
306 ########################################################################
307 # main_set_resources()
308 ########################################################################
309
310 &main_set_resources();
311
312
313 ########################################################################
314 # set resources
315 ########################################################################
316
317 our $groggy;
318 our $modefile;
319 our $addopts;
320
321 &main_display();
322
323 &clean_up();
324
325
326 1;
327 ########################################################################
328 ### Emacs settings
329 # Local Variables:
330 # mode: CPerl
331 # End: