Debian Codegraph @

texk/texlive/linked_scripts/context/perl/mptopdf.pl

raw
1 eval '(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
28 use Config ;
29 use Getopt::Long ;
30 use strict ;
31 use File::Basename ;
32
33 $Getopt::Long::passthrough = 1 ; # no error message
34 $Getopt::Long::autoabbrev = 1 ; # partial switch accepted
35
36 my $Help = 0 ;
37 my $Latex = 0 ;
38 my $TeXexec = 0 ;
39 my $RawMP = 1 ;
40 my $MetaFun = 0 ;
41 my $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
51 my $program = "MPtoPDF 1.4.1" ;
52 my $pattern = "@ARGV" ; # was $ARGV[0]
53 my $miktex = 0 ;
54 my $done = 0 ;
55 my $report = '' ;
56 my $mplatexswitch = " --tex=latex " ;
57 my $texexecswitch = " --tex=\"texexec --batch --once --nomp --mptex\" " ; # untested
58
59 my $dosish = ($Config{'osname'} =~ /^(ms)?dos|^os\/2|^mswin/i) ;
60 my $escapeshell = (($ENV{'SHELL'}) && ($ENV{'SHELL'} =~ m/sh/i ));
61
62 if ($ENV{"TEXSYSTEM"}) {
63 $miktex = ($ENV{"TEXSYSTEM"} =~ /miktex/io) ;
64 }
65
66 my @files ;
67 my $command = my $mpbin = '' ;
68
69 # agressive copy, works for open files like in gs
70
71 sub 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 (<INP>) {
78 print OUT $_ ;
79 }
80 close (INP) ;
81 close (OUT) ;
82 }
83
84 if (($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 (<INP>) {
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
127 foreach 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
158 if ($report eq '') {
159 $report = '*' ;
160 }
161
162 if ($done) {
163 print "\n$program : $pattern is converted to$report\n" ;
164 } else {
165 print "\n$program : no filename matches $pattern\n" ;
166 }