3 # gperl - add Perl part to groff files, this is the preprocessor for that
5 # Source file position: <groff-source>/contrib/gperl/gperl.pl
6 # Installed position: <prefix>/bin/gperl
8 # Copyright (C) 2014-2018 Free Software Foundation, Inc.
10 # Written by Bernd Warken <groff-bernd.warken-72@web.de>.
12 my $version = '1.2.6';
14 # This file is part of 'gperl', which is part of 'groff'.
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.
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.
26 # You can find a copy of the GNU General Public License in the internet
27 # at <http://www.gnu.org/licenses/gpl-2.0.html>.
29 ########################################################################
35 # temporary dir and files
36 use File::Temp qw/ tempfile tempdir /;
38 # needed for temporary dir
41 # for 'copy' and 'move'
44 # for fileparse, dirname and basename
47 # current working directory
50 # $Bin is the directory where this script is located
54 ########################################################################
55 # system variables and exported variables
56 ########################################################################
58 $\ = "\n"; # final part for print command
60 ########################################################################
61 # read-only variables with double-@ construct
62 ########################################################################
64 our $File_split_env_sh;
68 my $before_make; # script before run of 'make'
71 $before_make = 1 if '@VERSION@' eq "${at}VERSION${at}";
75 my $file_perl_test_pl;
79 my $gperl_source_dir = $FindBin::Bin;
80 $at_at{'BINDIR'} = $gperl_source_dir;
83 $at_at{'BINDIR'} = '@BINDIR@';
88 ########################################################################
90 ########################################################################
93 if ( /^(-h|--h|--he|--hel|--help)$/ ) {
94 print q(Usage for the 'gperl' program:);
95 print 'gperl [-] [--] [filespec...] normal file name arguments';
96 print 'gperl [-h|--help] gives usage information';
97 print 'gperl [-v|--version] displays the version number';
98 print q(This program is a 'groff' preprocessor that handles Perl ) .
99 q(parts in 'roff' files.);
101 } elsif ( /^(-v|--v|--ve|--ver|--vers|--versi|--versio|--version)$/ ) {
102 print q('gperl' version ) . $version;
108 #######################################################################
110 #######################################################################
114 my $template = 'gperl_' . "$$" . '_XXXX';
116 foreach ($ENV{'GROFF_TMPDIR'}, $ENV{'TMPDIR'}, $ENV{'TMP'}, $ENV{'TEMP'},
117 $ENV{'TEMPDIR'}, 'tmp', $ENV{'HOME'},
118 File::Spec->catfile($ENV{'HOME'}, 'tmp')) {
119 if ($_ && -d $_ && -w $_) {
120 eval { $tmpdir = tempdir( $template,
121 CLEANUP => 1, DIR => "$_" ); };
125 $out_file = File::Spec->catfile($tmpdir, $template);
129 ########################################################################
131 ########################################################################
139 my $is_dot_Perl = $line =~ /^[.']\s*Perl(|\s+.*)$/;
141 unless ( $is_dot_Perl ) { # not a '.Perl' line
142 if ( $perl_mode ) { # is running in Perl mode
144 } else { # normal line, not Perl-related
152 # now the line is a '.Perl' line
155 $args =~ s/\s+$//; # remove final spaces
156 $args =~ s/^[.']\s*Perl\s*//; # omit .Perl part, leave the arguments
158 my @args = split /\s+/, $args;
162 if ( @args == 0 || @args == 1 && $args[0] eq 'start' ) {
163 # For '.Perl' no args or first arg 'start' means opening 'Perl' mode.
164 # Everything else means an ending command.
166 # '.Perl' was started twice, ignore
167 print STDERR q('.Perl' starter was run several times);
169 } else { # new Perl start
171 open OUT, '>', $out_file;
177 # now the line must be a Perl ending line (stop)
179 unless ( $perl_mode ) {
180 print STDERR 'gperl: there was a Perl ending without being in ' .
182 print STDERR ' ' . $line;
186 $perl_mode = 0; # 'Perl' stop calling is correct
187 close OUT; # close the storing of 'Perl' commands
190 # run this 'Perl' part, later on about storage of the result
191 # array stores prints with \n
192 my @print_res = `perl $out_file`;
194 # remove 'stop' arg if exists
195 shift @args if ( $args[0] eq 'stop' );
198 # no args for saving, so @print_res doesn't matter
215 push @mode_names, $mode;
219 my $n_res = @print_res;
220 my $n_vars = @var_names;
222 if ( $n_vars < $n_res ) {
223 print STDERR 'gperl: not enough variables for Perl part: ' .
224 $n_vars . ' variables for ' . $n_res . ' output lines.';
225 } elsif ( $n_vars > $n_res ) {
226 print STDERR 'gperl: too many variablenames for Perl part: ' .
227 $n_vars . ' variables for ' . $n_res . ' output lines.';
229 if ( $n_vars < $n_res ) {
230 print STDERR 'gperl: not enough variables for Perl part: ' .
231 $n_vars . ' variables for ' . $n_res . ' output lines.';
235 $n_min = $n_vars if ( $n_vars < $n_res );
236 exit unless ( $n_min );
237 $n_min -= 1; # for starting with 0
239 for my $i ( 0..$n_min ) {
240 my $value = $print_res[$i];
242 print $mode_names[$i] . ' ' . $var_names[$i] . ' ' . $value;
248 ########################################################################