-
Notifications
You must be signed in to change notification settings - Fork 0
/
watch.pl
227 lines (183 loc) · 4.62 KB
/
watch.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#use feature 'signatures';
#no warnings "experimental::signatures";
use strict;
my $GNUPLOT = "gnuplot";
my $PDFLATEX = "pdflatex";
my $BIBTEX = "bibtex";
my $OPEN = "open -a /Applications/Skim.app";
use IPC::Open2;
my $cmd = shift @ARGV;
if ($cmd eq "render_latex") {
my $filename = shift @ARGV;
my $instring = shift @ARGV;
my $jobname = shift @ARGV;
render_latex($filename, $instring, $jobname);
} elsif ($cmd eq "render_gnuplot") {
my $filename = shift @ARGV;
render_gnuplot($filename);
} elsif ($cmd eq "watch") {
my $makestring = shift @ARGV;
my @files = @ARGV;
watch($makestring, @files);
} elsif ($cmd eq "watch_gnuplot_piped") {
my $cmd = shift @ARGV;
my $pipestring = shift @ARGV;
my @files = @ARGV;
watch_gnuplot_piped($cmd, $pipestring, @files);
} else {
die "unknown input";
}
sub cmd_read { # ($cmd, $waitfor) {
my ($cmd, $waitfor) = @_;
my ($out, $in);
my $pid = open2($out, $in, $cmd);
my $ret = "";
local $/ = $waitfor;
while (my $temp = <$out>) {
$ret .= $temp;
if ($waitfor eq ".") {
print $temp;
} else {
print ".";
}
flush stdout;
}
close($out);
close($in);
waitpid($pid, 0);
my $status = $? >> 8;
if ($waitfor eq ".") {
} else {
print "\n";
}
return ($status, $ret);
}
sub watch { #($makestring, @files) {
my ($makestring, @files) = @_;
my $do = "make $makestring";
print "watching $makestring\n";
my $stats = {};
my $delay = 1;
while (1) {
my $has_changed = 0;
for my $file (@files) {
my $mtime = (stat($file))[9];
if ($stats->{$file} != $mtime) {
if (! $has_changed) {
print "$file changed\n";
}
$has_changed = 1;
}
$stats->{$file} = $mtime;
}
if ($has_changed) {
#print "$do";
my ($status, $lines) = cmd_read($do, ".");
if ($status) {
print "HAD ERROR\n";
} else {
print "done\n";
}
$has_changed = 0;
}
sleep($delay);
}
}
sub render_gnuplot { #($filename) {
my ($filename) = @_;
my $do = "$GNUPLOT \"$filename\"";
print " gnuplot\n";
my ($status, $out) = cmd_read($do, \1024);
}
sub render_latex { #($filename, $instring) {
my ($filename, $instring, $jobname) = @_;
my $run_bibtex = 1;
my $run_latex = 1;
my $first = 1;
my $count = 0;
my @last_warns;
while ($run_latex) {
$run_latex = 0;
my $do = "$PDFLATEX --jobname=$jobname -synctex=1 -interaction nonstopmode \"$instring\"";
print " pdflatex";
my ($status, $out) = cmd_read($do, \1024);
$count += 1;
my @lwarns = ($out =~ m/LaTeX Warning: ([^.]*?\.)/gm);
my @undefs = ($out =~ m/! Undefined control sequence.\nl.([0-9]+) (.*?)\n/g);
if (@undefs) {
while (@undefs) {
my $line = shift @undefs;
my $cmd = shift @undefs;
print " ERROR: undefined command $cmd on line $line\n";
}
exit(1);
}
my @others = ($out =~ m/^! (.*?)$/gm);
if (@others) {
foreach my $error (@others) {
print " ERROR: $error\n";
}
exit(1);
}
@last_warns = @lwarns;
if ($out =~ m/Rerun to get cross-references right/ or
$out =~ m/Rerun to get citations correct/) {
#print "need to rerun latex\n";
$run_latex = 1;
}
if ($out =~ m/There were undefined references/ or
$out =~ m/There were undefined citations/) {
#print "need to run bibtex\n";
if ($count <= 1) {
$run_bibtex = 1;
}
}
if ($run_bibtex) {
$run_latex = 1;
my $do = "$BIBTEX $jobname";
print " bibtex";
my ($status, $out) = cmd_read($do, \1024);
#print "$out\n";
$run_bibtex = 0;
}
if ($count > 3 or ! $run_latex) {
foreach my $warn (@lwarns) {
print " LaTeX Warning: $warn\n";
}
}
if ($count > 3) {
print " COULD NOT FULLY COMPLETE RENDERING PROCESS, PERHAPS MISSING CITATIONS?\n";
exit 0;
}
}
}
sub watch_gnuplot_piped { #($cmd, $pipestring, @files) {
my ($cmd, $pipestring, @files) = @_;
print "watching $cmd, piping '$pipestring'\n";
my $stats = {};
my $delay = 1;
my ($out, $in);
my $pid = open2($out, $in, $cmd);
$out->blocking(0);
$in->autoflush();
print read_pipe($out);
while (1) {
my $has_changed = 0;
for my $file (@files) {
my $mtime = (stat($file))[9];
if ($stats->{$file} != $mtime) {
if (! $has_changed) {
print "$file changed\n";
}
$has_changed = 1;
}
$stats->{$file} = $mtime;
}
if ($has_changed) {
print $in $pipestring . "\n";
print read_pipe($out);
$has_changed = 0;
}
sleep($delay);
}
}