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 # Set maxima command to MAXIMA_EXAMPLE_COMMAND if set. Otherwise use
22 # maxima-local. We use --no-init to make sure no init files are
23 # loaded that could change the output. But we need ./maxima-init.lisp
24 # to set up things to work properly for the update. (Hence the -p.)
25 my $maxima_command = $ENV{"MAXIMA_EXAMPLE_COMMAND"} || "../../maxima-local --no-init -p maxima-init.lisp";
31 my $example_input_beg = '^@c ===beg===';
32 my $example_input_end = '^@c ===end===';
34 #my $example_output_beg = '^@example[ \t]*\n';
35 my $example_output_end = '^@end[ \t]+example[ \t]*\n';
37 my $in_example_input = 0;
38 my $in_example_output = 0;
40 my @example_input_buf = ();
41 my @example_output_buf = ();
42 my @example_result_buf = ();
52 $res =~ s/$strip_topdir//g;
54 # Escape "@" to "@@", as an unescaped at-sign might be interpreted as the
55 # beginning of a command.
76 if ($in_example_input) {
77 if ($_ =~ $example_input_end) {
79 $in_example_input = 0;
80 $in_example_output = 1;
82 elsif ($_ =~ /\@c */) {
85 $fixed = substr($_, 9);
88 $fixed = substr($_, 3);
91 push @example_input_buf, r_trim
($fixed);
95 print STDERR
"Warning: line $line_cnt - example input lines must begin with \'\@c \'.\n";
99 elsif ($in_example_output) {
100 if (/$example_output_end/) {
101 my $tempf = tmpnam
();
103 my $com = "$maxima_command > $tempf << \\EOF\n";
104 foreach my $l (@example_input_buf) {
112 "Error: line $line_cnt - maxima invocation failed.\n";
113 print @example_output_buf;
114 print "\@end example\n";
117 if (open(RESULT
, $tempf)) {
118 @example_result_buf = <RESULT
>;
122 if (!unlink($tempf)) {
124 print STDERR
"Error: line $line_cnt - can't delete temp file $tempf\n";
129 until ($#example_result_buf == -1 or $example_result_buf[0] =~ /^\cB/) {
130 shift @example_result_buf;
133 until ($#example_result_buf == -1 or $#example_input_buf == -1) {
136 until ($#example_result_buf == -1 or $example_result_buf[0] =~ /\cE/) {
137 push @group, rem_codes
($example_result_buf[0]);
138 shift @example_result_buf;
141 if ($#example_input_buf != -1) {
142 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";
143 shift @example_input_buf;
144 if ($example_result_buf[0] =~ /\cE/ and not $example_result_buf[0] =~ /\cE$/) {
145 $example_result_buf[0] = substr($example_result_buf[0], index($example_result_buf[0], "\cE"));
148 shift @example_result_buf;
152 until ($#example_input_buf == -1 or $example_input_buf[0] =~ /^[\S]/) {
153 push @group, rem_codes
(substr($example_input_buf[0], 0, -1)) . "\n";
154 shift @example_input_buf;
156 until ($#example_result_buf == -1 or $example_result_buf[0] =~ /^\cB/) {
157 push @group, rem_codes
($example_result_buf[0]);
158 shift @example_result_buf;
161 print "\@group\n" if scalar @group >= 2;
163 print "\@end group\n" if scalar @group >= 2;
166 print "\@end example\n";
170 print STDERR
"Error: line $line_cnt - can't open temp file $tempf\n";
171 print @example_output_buf;
172 print "\@end example\n";
178 @example_result_buf = ();
179 @example_input_buf = ();
180 @example_output_buf = ();
181 $in_example_output = 0;
184 push @example_output_buf, $_;
187 elsif (/$example_input_beg/) {
189 $in_example_input = 1;
196 if ($in_example_input) {
198 print STDERR
"Error: line $line_cnt - EOF while end of example input is expected.\n";
200 elsif ($in_example_output) {
202 print STDERR
"Error: line $line_cnt - EOF while end of example is expected.\n";
203 print @example_output_buf;