mptopdf.pl /size: 4557 b    last modification: 2020-07-01 14:35
1eval '(exit $?0)' && eval 'exec perl -S $0 ${1+"$@"}' && eval 'exec perl -S $0 $argv:q'
2        if 0;
3
4# MikTeX users can set environment variable TEXSYSTEM to "miktex".
5
6# There have been suggestions to patch this script for dealing with different
7# output templates (a relative new metapost feature) but these have given
8# unwanted side effects. One can always wrap this script in another script
9# to deal with whatever patterns needed. We won't touch what has worked for
10# ages. (December 2019)
11
12#D \module
13#D   [       file=mptopdf.pl,
14#D        version=2010.05.28, %  2000.05.29
15#D          title=converting MP to PDF,
16#D       subtitle=\MPTOPDF,
17#D         author=Hans Hagen,
18#D           date=\currentdate,
19#D            url=www.pragma-ade.nl,
20#D      copyright={PRAGMA ADE / Hans Hagen \& Ton Otten}]
21#C
22#C This module is part of the \CONTEXT\ macro||package and is
23#C therefore copyrighted by \PRAGMA. See licen-en.pdf for
24#C details.
25
26# use File::Copy ; # not in every perl
27
28use Config ;
29use Getopt::Long ;
30use strict ;
31use File::Basename ;
32
33$Getopt::Long::passthrough = 1 ; # no error message
34$Getopt::Long::autoabbrev  = 1 ; # partial switch accepted
35
36my $Help    = 0 ;
37my $Latex   = 0 ;
38my $TeXexec = 0 ;
39my $RawMP   = 1 ;
40my $MetaFun = 0 ;
41my $PassOn  = '' ;
42
43&GetOptions
44  ( "help"    => \$Help  ,
45    "rawmp"   => \$RawMP, # option is now default, but keep for compat
46    "metafun" => \$MetaFun,
47    "passon"  => \$PassOn, # option is ignored, but keep for compat
48    "latex"   => \$Latex,
49    "texexec" => \$TeXexec) ;
50
51my $program       = "MPtoPDF 1.4.1" ;
52my $pattern       = "@ARGV" ; # was $ARGV[0]
53my $miktex        = 0 ;
54my $done          = 0 ;
55my $report        = '' ;
56my $mplatexswitch = " --tex=latex " ;
57my $texexecswitch = " --tex=\"texexec --batch --once --nomp --mptex\" " ; # untested
58
59my $dosish      = ($Config{'osname'} =~ /^(ms)?dos|^os\/2|^mswin/i) ;
60my $escapeshell = (($ENV{'SHELL'}) && ($ENV{'SHELL'} =~ m/sh/i ));
61
62if ($ENV{"TEXSYSTEM"}) {
63    $miktex = ($ENV{"TEXSYSTEM"} =~ /miktex/io) ;
64}
65
66my @files ;
67my $command = my $mpbin = ''  ;
68
69# agressive copy, works for open files like in gs
70
71sub CopyFile {
72    my ($From,$To) = @_ ;
73    return unless open(INP,"<$From") ;
74    return unless open(OUT,">$To") ;
75    binmode INP ;
76    binmode OUT ;
77    while () {
78        print OUT $_ ;
79    }
80    close (INP) ;
81    close (OUT) ;
82}
83
84if (($pattern eq '')||($Help)) {
85    print "\n$program : provide MP output file (or pattern)\n" ;
86    exit ;
87} elsif ($pattern =~ /\.mp$/io) {
88    shift @ARGV ; my $rest = join(" ", @ARGV) ;
89    if (open(INP,$pattern)) {
90        while () {
91            if (/(documentstyle|documentclass|begin\{document\})/io) {
92                $Latex = 1 ; last ;
93            }
94        }
95        close (INP) ;
96    }
97    if ($Latex) {
98      $rest .= " $mplatexswitch" ;
99    }
100    if ($TeXexec) {
101      $rest .= " $texexecswitch" ;
102    }
103    if ($MetaFun) {
104      $mpbin = "mpost --progname=mpost --mem=metafun" ;
105    } else {
106      $mpbin = "mpost --mem=mpost" ;
107    }
108    my $runner = "$mpbin $rest $pattern" ;
109    print "\n$program : running '$runner'\n" ;
110    my $error =  system ($runner) ;
111    if ($error) {
112        print "\n$program : error while processing mp file\n" ;
113        exit 1 ;
114    } else {
115        $pattern =~ s/\.mp$//io ;
116        @files = glob "$pattern.*" ;
117    }
118} elsif (-e $pattern) {
119    @files = ($pattern) ;
120} elsif ($pattern =~ /.\../o) {
121    @files = glob "$pattern" ;
122} else {
123    $pattern .= '.*' ;
124    @files = glob "$pattern" ;
125}
126
127foreach my $file (@files) {
128    $_ = $file ;
129    if (s/\.(\d+|mps)$// && -e $file) {
130        if ($miktex) {
131            $command = "pdftex -undump=mptopdf" ;
132        } else {
133            $command = "pdftex -fmt=mptopdf -progname=context" ;
134        }
135        if ($dosish) {
136            $command = "$command \\relax $file" ;
137        } else {
138            $command = "$command \\\\relax $file" ;
139        }
140        my $error = system($command) ;
141        if ($error) {
142            print "\n$program : error while processing tex file\n" ;
143            exit 1 ;
144        }
145        my $pdfsrc = basename($_).".pdf";
146        rename ($pdfsrc, "$_-$1.pdf") ;
147        if (-e $pdfsrc) {
148            CopyFile ($pdfsrc, "$_-$1.pdf") ;
149        }
150        if ($done) {
151            $report .= " +" ;
152        }
153        $report .= " $_-$1.pdf" ;
154        ++$done  ;
155    }
156}
157
158if ($report eq '') {
159    $report = '*' ;
160}
161
162if ($done) {
163    print "\n$program : $pattern is converted to$report\n" ;
164} else {
165    print "\n$program : no filename matches $pattern\n" ;
166}
167