Fix bug #1848: taytorat leaks internal gensyms from multivar expansions
[maxima.git] / doc / info / update_examples
blob449b04a89bbffc5047b982b832a225c15677edf5
1 #!/usr/bin/env perl
3 use File::Temp qw/ :POSIX /;
5 use Text::Tabs;
7 use warnings;
8 use strict;
10 my $topdir = substr(`pwd`, 0, -1);
12 if (index($topdir, "/maxima/doc/info") != -1) {
13 $topdir =~ s/\/maxima\/doc\/info//;
15 else {
16 $topdir = '';
19 my $strip_topdir = $ENV{"STRIP_TOPDIR"} || $topdir;
21 my $maxima_command = $ENV{"MAXIMA_EXAMPLE_COMMAND"} || "../../maxima-local";
23 my $line_cnt = 0;
24 my $error_cnt = 0;
25 my $warning_cnt = 0;
27 my $example_input_beg = '^@c ===beg===';
28 my $example_input_end = '^@c ===end===';
30 #my $example_output_beg = '^@example[ \t]*\n';
31 my $example_output_end = '^@end[ \t]+example[ \t]*\n';
33 my $in_example_input = 0;
34 my $in_example_output = 0;
36 my @example_input_buf = ();
37 my @example_output_buf = ();
38 my @example_result_buf = ();
40 sub rem_codes {
41 my $res;
43 $res = $_[0];
45 $res =~ s/\cB//g;
46 $res =~ s/\cE//g;
48 $res =~ s/$strip_topdir//g;
50 # Escape "@" to "@@", as an unescaped at-sign might be interpreted as the
51 # beginning of a command.
52 $res =~s/\@/\@\@/g;
54 $res =~ s/\{/\@\{/g;
55 $res =~ s/\}/\@\}/g;
57 $res = expand($res);
59 return $res;
62 sub r_trim {
63 my $res;
64 $res = $_[0];
65 $res =~ s/\s+$/\n/;
66 return $res;
69 while (<>) {
70 $line_cnt++;
72 if ($in_example_input) {
73 if ($_ =~ $example_input_end) {
74 print $_;
75 $in_example_input = 0;
76 $in_example_output = 1;
78 elsif ($_ =~ /\@c */) {
79 my $fixed;
80 if (/^\@c input:/) {
81 $fixed = substr($_, 9);
83 else {
84 $fixed = substr($_, 3);
86 print $_;
87 push @example_input_buf, r_trim($fixed);
89 else {
90 $warning_cnt++;
91 print STDERR "Warning: line $line_cnt - example input lines must begin with \'\@c \'.\n";
92 print $_;
95 elsif ($in_example_output) {
96 if (/$example_output_end/) {
97 my $tempf = tmpnam();
99 my $com = "$maxima_command > $tempf << \\EOF\n";
100 foreach my $l (@example_input_buf) {
101 $com .= $l;
103 $com .= "EOF";
105 if (system($com)) {
106 $error_cnt++;
107 print STDERR
108 "Error: line $line_cnt - maxima invocation failed.\n";
109 print @example_output_buf;
110 print "\@end example\n";
112 else {
113 if (open(RESULT, $tempf)) {
114 @example_result_buf = <RESULT>;
116 close RESULT;
118 if (!unlink($tempf)) {
119 $error_cnt++;
120 print STDERR "Error: line $line_cnt - can't delete temp file $tempf\n";
123 print "\@example\n";
125 until ($#example_result_buf == -1 or $example_result_buf[0] =~ /^\cB/) {
126 shift @example_result_buf;
129 until ($#example_result_buf == -1 or $#example_input_buf == -1) {
130 my @group;
132 until ($#example_result_buf == -1 or $example_result_buf[0] =~ /\cE/) {
133 push @group, rem_codes($example_result_buf[0]);
134 shift @example_result_buf;
137 if ($#example_input_buf != -1) {
138 push @group, rem_codes(substr(substr($example_result_buf[0], 0, index($example_result_buf[0], "\cE") + 1), 0, -1)) . rem_codes(substr($example_input_buf[0], 0, -1)) . "\n";
139 shift @example_input_buf;
140 if ($example_result_buf[0] =~ /\cE/ and not $example_result_buf[0] =~ /\cE$/) {
141 $example_result_buf[0] = substr($example_result_buf[0], index($example_result_buf[0], "\cE"));
143 else {
144 shift @example_result_buf;
148 until ($#example_input_buf == -1 or $example_input_buf[0] =~ /^[\S]/) {
149 push @group, rem_codes(substr($example_input_buf[0], 0, -1)) . "\n";
150 shift @example_input_buf;
152 until ($#example_result_buf == -1 or $example_result_buf[0] =~ /^\cB/) {
153 push @group, rem_codes($example_result_buf[0]);
154 shift @example_result_buf;
157 print "\@group\n" if scalar @group >= 2;
158 print @group;
159 print "\@end group\n" if scalar @group >= 2;
162 print "\@end example\n";
164 else {
165 $error_cnt++;
166 print STDERR "Error: line $line_cnt - can't open temp file $tempf\n";
167 print @example_output_buf;
168 print "\@end example\n";
172 close RESULT;
174 @example_result_buf = ();
175 @example_input_buf = ();
176 @example_output_buf = ();
177 $in_example_output = 0;
179 else {
180 push @example_output_buf, $_;
183 elsif (/$example_input_beg/) {
184 print $_;
185 $in_example_input = 1;
187 else {
188 print $_;
192 if ($in_example_input) {
193 $error_cnt++;
194 print STDERR "Error: line $line_cnt - EOF while end of example input is expected.\n";
196 elsif ($in_example_output) {
197 $error_cnt++;
198 print STDERR "Error: line $line_cnt - EOF while end of example is expected.\n";
199 print @example_output_buf;
202 exit $error_cnt;