3 use File
::Temp qw
/ :POSIX /;
10 my $topdir = substr(`pwd`, 0, -1);
12 if (index($topdir, "/maxima/doc/info") != -1) {
13 $topdir =~ s/\/maxima\/doc\
/info//;
19 my $strip_topdir = $ENV{"STRIP_TOPDIR"} || $topdir;
21 my $maxima_command = $ENV{"MAXIMA_EXAMPLE_COMMAND"} || "../../maxima-local";
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 = ();
48 $res =~ s/$strip_topdir//g;
50 # Escape "@" to "@@", as an unescaped at-sign might be interpreted as the
51 # beginning of a command.
72 if ($in_example_input) {
73 if ($_ =~ $example_input_end) {
75 $in_example_input = 0;
76 $in_example_output = 1;
78 elsif ($_ =~ /\@c */) {
81 $fixed = substr($_, 9);
84 $fixed = substr($_, 3);
87 push @example_input_buf, r_trim
($fixed);
91 print STDERR
"Warning: line $line_cnt - example input lines must begin with \'\@c \'.\n";
95 elsif ($in_example_output) {
96 if (/$example_output_end/) {
99 my $com = "$maxima_command > $tempf << \\EOF\n";
100 foreach my $l (@example_input_buf) {
108 "Error: line $line_cnt - maxima invocation failed.\n";
109 print @example_output_buf;
110 print "\@end example\n";
113 if (open(RESULT
, $tempf)) {
114 @example_result_buf = <RESULT
>;
118 if (!unlink($tempf)) {
120 print STDERR
"Error: line $line_cnt - can't delete temp file $tempf\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) {
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"));
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;
159 print "\@end group\n" if scalar @group >= 2;
162 print "\@end example\n";
166 print STDERR
"Error: line $line_cnt - can't open temp file $tempf\n";
167 print @example_output_buf;
168 print "\@end example\n";
174 @example_result_buf = ();
175 @example_input_buf = ();
176 @example_output_buf = ();
177 $in_example_output = 0;
180 push @example_output_buf, $_;
183 elsif (/$example_input_beg/) {
185 $in_example_input = 1;
192 if ($in_example_input) {
194 print STDERR
"Error: line $line_cnt - EOF while end of example input is expected.\n";
196 elsif ($in_example_output) {
198 print STDERR
"Error: line $line_cnt - EOF while end of example is expected.\n";
199 print @example_output_buf;