[f36635] | 1 | #!/usr/local/bin/perl |
---|
[341696] | 2 | # $Id$ |
---|
[f36635] | 3 | ################################################################### |
---|
| 4 | # Computer Algebra System SINGULAR |
---|
| 5 | # |
---|
| 6 | # doc2tex: utility to generate the Singular manual |
---|
| 7 | # |
---|
| 8 | #### |
---|
[a70441f] | 9 | # @c example [error] [no_comp] [unix_only] |
---|
[f36635] | 10 | # -> the text till the next @c example is feed into Singular, |
---|
| 11 | # the text is then substituted by |
---|
[84dee1] | 12 | # @c computed example $ex_prefix $doc_file:$line |
---|
[f36635] | 13 | # <text from the input> |
---|
| 14 | # @expansion{} <corresponding output> |
---|
| 15 | # .... |
---|
[84dee1] | 16 | # @c end computed example $ex_prefix $doc_file:$line |
---|
| 17 | # reuse computed examples if ($reuse && -r $ex_prefix.inc) |
---|
[f36635] | 18 | # cut absolute directory names from the loaded messages |
---|
| 19 | # substituted @,{ and } by @@, @{ resp. @} |
---|
| 20 | # wrap around output lines longer than $ex_length = 73; |
---|
| 21 | # Processing is aborted if error occures in Singular run, |
---|
[4b72f6] | 22 | # unless 'error' is specified |
---|
| 23 | # if no_comp is given, then computation is not run |
---|
[a70441f] | 24 | # if unix_only is given, then computation is only run |
---|
| 25 | # under unix |
---|
[4b72f6] | 26 | # |
---|
[f36635] | 27 | # |
---|
| 28 | #### |
---|
| 29 | # @c include file |
---|
| 30 | # -> copy content of file into output file protecting texi special chars |
---|
| 31 | # |
---|
| 32 | #### |
---|
| 33 | # @c ref |
---|
| 34 | # .... |
---|
| 35 | # @c ref |
---|
| 36 | # -> scans intermediate lines for @ref{..} strings |
---|
| 37 | # Creates menu of (sorted) refs for ifinfo |
---|
| 38 | # Creates comma-separated (sorted) refs for iftex, prepended with |
---|
| 39 | # the text before first @ref. |
---|
| 40 | # |
---|
| 41 | #### |
---|
[caf1e0] | 42 | # @c lib libname.lib[:proc] [no_ex, no_fun, Fun, (\w*)section] |
---|
[6bf987] | 43 | # --> replaced by @include $texfile where |
---|
| 44 | # $texfile = $subdir/libname_lib[_noFun,_noEx].tex |
---|
| 45 | # --> if $make, calls "make $texfile" |
---|
| 46 | # --> Error, if $tex_file does not exist |
---|
| 47 | # --> if [:proc] is given, then includes only of respective |
---|
| 48 | # proc body |
---|
[caf1e0] | 49 | # --> Fun overwrites global no_fun |
---|
[013a76] | 50 | # --> if (\w*)section is given, replaces @subsubsection by @$1section |
---|
[6bf987] | 51 | # and pastes in content of tex file directly |
---|
[f36635] | 52 | # |
---|
| 53 | # |
---|
| 54 | ################################################################### |
---|
| 55 | |
---|
[a70441f] | 56 | use Config; |
---|
| 57 | $Win32 = 1 if ($Config{osname} =~ /win/i); |
---|
| 58 | |
---|
[f36635] | 59 | # |
---|
| 60 | # default settings of command-line arguments |
---|
| 61 | # |
---|
| 62 | $Singular = "../Singular/Singular"; # should be overwritten |
---|
[463f21c] | 63 | $Singular_opts = " -teqr12345678 --no-rc"; |
---|
[f36635] | 64 | $clean = 0; |
---|
| 65 | $verbose = 1; |
---|
| 66 | $reuse = 1; |
---|
| 67 | $no_ex = 0; |
---|
[9177e3] | 68 | %exclude_ex = (); |
---|
[65a2bd] | 69 | $no_fun = 0; |
---|
[f36635] | 70 | $doc_subdir = "./d2t_singular"; |
---|
[c04b94] | 71 | $ex_subdir = "./examples"; |
---|
[f36635] | 72 | @include_dirs = (".", "../Singular/LIB"); |
---|
[65a2bd] | 73 | $make = 0; |
---|
[84dee1] | 74 | $make_opts = " --no-print-directory"; |
---|
[f36635] | 75 | |
---|
[a70441f] | 76 | |
---|
[f36635] | 77 | # |
---|
| 78 | # misc. defaults |
---|
| 79 | # |
---|
| 80 | $ex_length = 73; |
---|
| 81 | $ERROR = "\n**** Error:"; |
---|
| 82 | $WARNING = "\n** Warning:"; |
---|
| 83 | |
---|
| 84 | # |
---|
| 85 | # flush stdout and stderr after every write |
---|
| 86 | # |
---|
| 87 | select(STDERR); |
---|
| 88 | $| = 1; |
---|
| 89 | select(STDOUT); |
---|
| 90 | $| = 1; |
---|
| 91 | |
---|
| 92 | # |
---|
| 93 | # get file to convert |
---|
| 94 | # |
---|
| 95 | $doc_file = pop(@ARGV); |
---|
| 96 | if ($doc_file =~ /(.+)\..*$/) |
---|
| 97 | { |
---|
| 98 | die "*** Error: Need .doc file as input\n" . &Usage |
---|
| 99 | unless ($doc_file =~ /(.+)\.doc$/); |
---|
| 100 | } |
---|
| 101 | else |
---|
| 102 | { |
---|
| 103 | if ($doc_file =~ /^-h(elp)?$/) { print &Usage; exit;} |
---|
| 104 | $doc_file .= ".doc"; |
---|
| 105 | } |
---|
| 106 | |
---|
| 107 | # |
---|
| 108 | # parse command line options |
---|
| 109 | # |
---|
| 110 | $args = join(" ", @ARGV); |
---|
| 111 | while (@ARGV && $ARGV[0] =~ /^-/) |
---|
| 112 | { |
---|
| 113 | $_ = shift(@ARGV); |
---|
| 114 | if (/^-S(ingular)?$/) { $Singular = shift(@ARGV); next;} |
---|
[fdbd6d] | 115 | if (/^-o(utput)?$/) { $tex_file = shift(@ARGV); next;} |
---|
[f36635] | 116 | if (/^-no_r(euse)?$/) { $reuse = 0; next;} |
---|
| 117 | if (/^-c(lean)?$/) { $clean = 1; next;} |
---|
| 118 | if (/^-no_e(x)?$/) { $no_ex = 1; next;} |
---|
[9177e3] | 119 | if (/^-exclude$/) { $exclude_ex{shift(@ARGV)} = 1;next;} |
---|
[65a2bd] | 120 | if (/^-no_fu(n)?$/) { $no_fun = 1;next;} |
---|
[84dee1] | 121 | if (/^-m(ake)?$/) { $make = shift(@ARGV);next;} |
---|
[c04b94] | 122 | if (/^-d(ocdir)?$/) { $doc_subdir = shift(@ARGV); next;} |
---|
| 123 | if (/^-e(xdir)?$/) { $ex_subdir = shift(@ARGV); next;} |
---|
[f36635] | 124 | if (/^-I$/) { unshift(@include_dirs, shift(@ARGV)); next;} |
---|
| 125 | if (/^-v(erbose)?$/) { $verbose = shift(@ARGV); next;} |
---|
| 126 | if (/^-h(elp)?$/) { print &Usage; exit;} |
---|
[65a2bd] | 127 | Error("Unknown Option $_\n" .&Usage); |
---|
[f36635] | 128 | } |
---|
| 129 | $verbose = ($verbose < 0 ? 0 : $verbose); |
---|
[84dee1] | 130 | $make_opts .= " -s" unless $verbose > 1; |
---|
[f36635] | 131 | |
---|
| 132 | # |
---|
| 133 | # construct filenames |
---|
| 134 | # |
---|
| 135 | ($doc_dir = $doc_file) =~ s/(.+)\.doc$/$1/; |
---|
| 136 | if ($doc_dir =~ /(.*)\//) |
---|
| 137 | { |
---|
| 138 | $doc = $'; |
---|
| 139 | $doc_dir = $1; |
---|
| 140 | } |
---|
| 141 | else |
---|
| 142 | { |
---|
| 143 | $doc = $doc_dir; |
---|
| 144 | $doc_dir = "."; |
---|
| 145 | } |
---|
[65a2bd] | 146 | unless ($tex_file) |
---|
| 147 | { |
---|
| 148 | $tex_file = ($no_ex ? "$doc_dir/${doc}_noEx.tex" : "$doc_dir/$doc.tex"); |
---|
| 149 | } |
---|
| 150 | |
---|
[f36635] | 151 | # |
---|
| 152 | # open files |
---|
| 153 | # |
---|
| 154 | open(DOC, "<$doc_file") |
---|
[acfbb5a] | 155 | || Error("can't open $doc_file for reading: $!\n" . &Usage); |
---|
| 156 | open(TEX, ">$tex_file") || Error("can't open $tex_file for writing: $!\n"); |
---|
[84dee1] | 157 | print "(d2t $doc_file==>$tex_file" if ($verbose); |
---|
[f36635] | 158 | if (-d $doc_subdir) |
---|
| 159 | { |
---|
[c04b94] | 160 | print "<docdir:$doc_subdir>" if ($verbose > 1); |
---|
[f36635] | 161 | } |
---|
| 162 | else |
---|
| 163 | { |
---|
| 164 | mkdir($doc_subdir, oct(755)) |
---|
[acfbb5a] | 165 | || Error("can't create directory $doc_subdir: $!\n"); |
---|
[c04b94] | 166 | print "(docdir:$doc_subdir)" if ($verbose > 1); |
---|
| 167 | } |
---|
| 168 | if (-d $ex_subdir) |
---|
| 169 | { |
---|
| 170 | print "<exdir:$ex_subdir>" if ($verbose > 1); |
---|
| 171 | } |
---|
| 172 | else |
---|
| 173 | { |
---|
| 174 | mkdir($ex_subdir, oct(755)) |
---|
| 175 | || Error("can't create directory $ex_subdir: $!\n"); |
---|
| 176 | print "(exdir:$ex_subdir)" if ($verbose > 1); |
---|
[f36635] | 177 | } |
---|
| 178 | |
---|
| 179 | ####################################################################### |
---|
| 180 | # |
---|
| 181 | # go ! |
---|
| 182 | # |
---|
| 183 | while (<DOC>) |
---|
| 184 | { |
---|
| 185 | $line++; |
---|
| 186 | if (/^\@c\s*example/) {&HandleExample; next;} |
---|
| 187 | if (/^\@c\s*include\s+/) {&HandleInclude; next;} |
---|
| 188 | if (/^\@c\s*ref\s*$/) {&HandleRef; next;} |
---|
| 189 | if (/^\@c\s*lib\s+/) {&HandleLib; next;} |
---|
| 190 | if (/^\@setfilename/) {print TEX "\@setfilename $doc.hlp\n"; next;} |
---|
[84dee1] | 191 | |
---|
[c04b94] | 192 | if (/^\@\w*section\s*/ || /^\@\w*chapter\s*/ || /^\@\w*heading\s*/) |
---|
[84dee1] | 193 | { |
---|
[c04b94] | 194 | $section = $'; |
---|
| 195 | $section =~ s/^\s*(.*)\s*$/$1/; |
---|
[012576] | 196 | if ($section =~ /\(/) |
---|
| 197 | { |
---|
| 198 | $section =~ s/\(/_/g |
---|
| 199 | } |
---|
| 200 | if ($section =~ /\)/) |
---|
| 201 | { |
---|
| 202 | $section =~ s/\)/_/g |
---|
| 203 | } |
---|
[c04b94] | 204 | if ($section =~ /\w/) |
---|
| 205 | { |
---|
| 206 | $section =~ s/\s/_/g |
---|
| 207 | } |
---|
| 208 | else |
---|
| 209 | { |
---|
| 210 | $section = "unknown"; |
---|
| 211 | } |
---|
[84dee1] | 212 | } |
---|
[9ebcbf] | 213 | |
---|
| 214 | # handle math |
---|
| 215 | $in_info = 1 if /^\@ifinfo/; |
---|
| 216 | $in_info = 0 if /^\@end\s*ifinfo/; |
---|
| 217 | s[\@math\{(.*?)\}][&HandleMath($1)]eg unless $in_info; |
---|
| 218 | |
---|
[f36635] | 219 | print TEX $_; |
---|
| 220 | |
---|
[354f3b] | 221 | if (! $printed_header && /^\@c/) |
---|
| 222 | { |
---|
| 223 | $printed_header = 1; |
---|
| 224 | print TEX <<EOT; |
---|
| 225 | \@comment This file was generated by doc2tex.pl from $doc_file |
---|
| 226 | \@comment DO NOT EDIT DIRECTLY, BUT EDIT $doc_file INSTEAD |
---|
| 227 | EOT |
---|
| 228 | } |
---|
| 229 | |
---|
[f36635] | 230 | if (/^\@bye$/) {last;} |
---|
| 231 | } |
---|
| 232 | |
---|
| 233 | # |
---|
| 234 | # wrap up |
---|
| 235 | # |
---|
| 236 | close(TEX); |
---|
[84dee1] | 237 | print "==>$tex_file)\n" if ($verbose); |
---|
[f36635] | 238 | |
---|
[9ebcbf] | 239 | sub HandleMath |
---|
| 240 | { |
---|
| 241 | my $what = shift; |
---|
| 242 | return <<EOT; |
---|
| 243 | |
---|
| 244 | \@ifinfo |
---|
| 245 | \@math{$what} |
---|
| 246 | \@end ifinfo |
---|
| 247 | \@tex |
---|
| 248 | \$$what\$ |
---|
| 249 | \@end tex |
---|
| 250 | EOT |
---|
| 251 | } |
---|
[f36635] | 252 | |
---|
| 253 | ###################################################################### |
---|
[a70441f] | 254 | # @c example [error] [no_comp] [unix_only] |
---|
[354f3b] | 255 | # -> the text till the next @c example is fed into Singular, |
---|
[f36635] | 256 | # the text is then substituted by |
---|
[84dee1] | 257 | # @c computed example $ex_prefix $doc_file:$line |
---|
[f36635] | 258 | # <text from the input> |
---|
| 259 | # @expansion{} <corresponding output> |
---|
| 260 | # .... |
---|
[84dee1] | 261 | # @c end computed example $ex_prefix $doc_file:$line |
---|
| 262 | # reuse computed examples if ($reuse && -r $ex_prefix.inc) |
---|
[f36635] | 263 | # cut absolute directory names from the loaded messages |
---|
| 264 | # substituted @,{ and } by @@, @{ resp. @} |
---|
| 265 | # wrap around output lines longer than $ex_length = 73; |
---|
| 266 | # Processing is aborted if error occures in Singular run, |
---|
[4b72f6] | 267 | # unless 'error' is specified |
---|
| 268 | # If [no_comp] is given, actual computation is not run |
---|
[a70441f] | 269 | # if [unix_only] is given, then computation is only run |
---|
| 270 | # under unix |
---|
| 271 | |
---|
[f36635] | 272 | sub HandleExample |
---|
| 273 | { |
---|
[a70441f] | 274 | my($inc_file, $ex_file, $lline, $thisexample, $error_ok, $cache, $no_comp, |
---|
[9177e3] | 275 | $unix_only, $tag); |
---|
[f36635] | 276 | |
---|
| 277 | $lline = $line; |
---|
[84dee1] | 278 | $section = 'unknown' unless $section; |
---|
| 279 | $ex_prefix = $section; |
---|
[c04b94] | 280 | $ex_prefix .= "_$examples{$section}" if $examples{$section}; |
---|
[84dee1] | 281 | $examples{$section}++; |
---|
[f36635] | 282 | |
---|
[9177e3] | 283 | if (/tag:(\w+)/) |
---|
| 284 | { |
---|
| 285 | $tag = $1; |
---|
| 286 | } |
---|
| 287 | else |
---|
| 288 | { |
---|
| 289 | $tag = 'NOTAG'; |
---|
| 290 | } |
---|
| 291 | |
---|
| 292 | if ($no_ex or $exclude_ex{$tag}) |
---|
[f36635] | 293 | { |
---|
[84dee1] | 294 | print "{$ex_prefix}" if ($verbose); |
---|
| 295 | print TEX "\@c skipped computation of example $ex_prefix $doc_file:$lline \n"; |
---|
[f36635] | 296 | |
---|
| 297 | } |
---|
[84dee1] | 298 | else |
---|
| 299 | { |
---|
[c04b94] | 300 | $inc_file = "$ex_subdir/$ex_prefix.inc"; |
---|
| 301 | $ex_file = "$ex_subdir/$ex_prefix.sing"; |
---|
[84dee1] | 302 | if (-r $inc_file && -r $ex_file && -s $inc_file && -s $ex_file) |
---|
| 303 | { |
---|
| 304 | $cache = 1; |
---|
[c04b94] | 305 | open(SING, "<$ex_file") || ($cache = 0); |
---|
[84dee1] | 306 | } |
---|
| 307 | } |
---|
[f36635] | 308 | |
---|
| 309 | $thisexample = ''; |
---|
| 310 | $error_ok = 1 if /error/; |
---|
[4b72f6] | 311 | $no_comp = 1 if /no_comp/; |
---|
[a70441f] | 312 | $unix_only = 1 if /unix_only/ && $Win32; |
---|
| 313 | |
---|
| 314 | # print content in example file till next @c example |
---|
| 315 | print TEX "// only supported on Unix platforms\n" |
---|
| 316 | if $unix_only; |
---|
| 317 | |
---|
[f36635] | 318 | while (<DOC>) |
---|
| 319 | { |
---|
| 320 | $line++; |
---|
| 321 | last if (/^\@c\s*example\s*$/); |
---|
[119b42] | 322 | # s/^\s*//; # remove preceeding white spaces |
---|
[9177e3] | 323 | if ($no_ex || $exclude_ex{$tag} || $no_comp || $unix_only) |
---|
[f36635] | 324 | { |
---|
| 325 | &protect_texi; |
---|
| 326 | print TEX $_; |
---|
| 327 | } |
---|
| 328 | else |
---|
| 329 | { |
---|
[84dee1] | 330 | $thisexample .= $_; |
---|
[c04b94] | 331 | if ($cache && $_ && $_ ne <SING>) |
---|
[84dee1] | 332 | { |
---|
| 333 | $cache = 0; |
---|
[c04b94] | 334 | close(SING); |
---|
[84dee1] | 335 | } |
---|
[f36635] | 336 | } |
---|
| 337 | } |
---|
[c04b94] | 338 | close(SING) if $cache; |
---|
[acfbb5a] | 339 | Error("no matching '\@c example' found for $doc_file:$lline\n") |
---|
[f36635] | 340 | unless (/^\@c\s*example\s*$/); |
---|
| 341 | |
---|
| 342 | # done, if no examples |
---|
[9177e3] | 343 | return if ($no_ex || $exclude_ex{$tag} || $no_comp || $unix_only); |
---|
[f36635] | 344 | |
---|
| 345 | # check whether it can be reused |
---|
[84dee1] | 346 | if ($reuse && $cache) |
---|
[f36635] | 347 | { |
---|
[354f3b] | 348 | my $ok = 1; |
---|
[84dee1] | 349 | print "<$ex_prefix>" if ($verbose); |
---|
| 350 | print TEX "\@c reused example $ex_prefix $doc_file:$lline \n"; |
---|
| 351 | open(INC, "<$inc_file") || Error("can't open $inc_file for reading: $!\n"); |
---|
[354f3b] | 352 | while (<INC>) |
---|
| 353 | { |
---|
| 354 | if (/error occurred/ && $ok && !error_ok) |
---|
| 355 | { |
---|
| 356 | Warn("Result file $inc_file contains errors. Force re-computation by removing $inc_file"); |
---|
| 357 | $ok = 0; |
---|
| 358 | } |
---|
| 359 | print TEX $_; |
---|
| 360 | } |
---|
[84dee1] | 361 | close(INC); |
---|
[f36635] | 362 | } |
---|
| 363 | else |
---|
| 364 | { |
---|
[84dee1] | 365 | print "($ex_prefix" if ($verbose == 1); |
---|
| 366 | my ($res_file); |
---|
[c04b94] | 367 | $res_file = "$ex_subdir/$ex_prefix.res"; |
---|
[f36635] | 368 | |
---|
[84dee1] | 369 | print TEX "\@c computed example $ex_prefix $doc_file:$lline \n"; |
---|
[f36635] | 370 | |
---|
| 371 | # run singular |
---|
[acfbb5a] | 372 | open(EX, ">$ex_file") || Error("can't open $ex_file for writing: $!\n"); |
---|
[c04b94] | 373 | print EX "$thisexample"; |
---|
[f36635] | 374 | close(EX); |
---|
| 375 | |
---|
[a70441f] | 376 | unless ($Singular_OK) |
---|
| 377 | { |
---|
| 378 | if (system("echo '\$' | $Singular $Singular_opts > $res_file")) |
---|
| 379 | { |
---|
| 380 | $Singular .= '.exe' if ($Win32 && $Singular !~ /\.exe$/); |
---|
| 381 | Error("CanŽt run '$Singular $Singular_opts': $@") |
---|
| 382 | if (system("echo '\$' | $Singular $Singular_opts > $res_file")); |
---|
| 383 | } |
---|
| 384 | $Singular_OK = 1 |
---|
| 385 | } |
---|
| 386 | |
---|
[c04b94] | 387 | &System("echo '\$' | $Singular $Singular_opts $ex_file > $res_file"); |
---|
[f36635] | 388 | print ")" if ($verbose == 1); |
---|
| 389 | |
---|
[acfbb5a] | 390 | open(RES, "<$res_file") || Error("can't open $res_file for reading: $!\n"); |
---|
| 391 | open(INC, ">$inc_file") || Error("can't open $inc_file for writing: $!\n"); |
---|
[f36635] | 392 | |
---|
| 393 | # get result, manipulate it and put it into inc file |
---|
| 394 | while (<RES>) |
---|
| 395 | { |
---|
[c04b94] | 396 | last if (/^STDIN\s*([0-9]+)..\$/); |
---|
[f36635] | 397 | # check for error |
---|
[84dee1] | 398 | Error("while running example $ex_prefix from $doc_file:$lline.\nCall: '$Singular $Singular_opts $ex_file > $res_file'\n") |
---|
[f36635] | 399 | if (/error occurred/ && ! $error_ok); |
---|
| 400 | # remove stuff from echo |
---|
| 401 | if (/^$ex_file\s*([0-9]+)../) |
---|
| 402 | { |
---|
| 403 | $_ = $'; |
---|
| 404 | &protect_texi; |
---|
| 405 | } |
---|
| 406 | else |
---|
| 407 | { |
---|
| 408 | local($to_do, $done); |
---|
| 409 | # remove absolute path names from laoded messages |
---|
| 410 | s/^(\/\/ \*\* loaded )(.*)\/(.+).lib(.*)/$1$3.lib$4/; |
---|
| 411 | # shorten error occurred in messages |
---|
| 412 | s/\? error occurred in [^ ]* line/\? error occurred in line/; |
---|
| 413 | # break after $ex_length characters |
---|
| 414 | $to_do = $_; |
---|
[1173547] | 415 | while (length($to_do) > $ex_length && $to_do =~ /\w/ && |
---|
| 416 | substr($to_do, $ex_length) !~ /^\s*$/) |
---|
[f36635] | 417 | { |
---|
[1173547] | 418 | |
---|
[f36635] | 419 | $done .= substr($to_do, 0, $ex_length)."\\\n "; |
---|
[1173547] | 420 | $to_do = substr($to_do, $ex_length); |
---|
[f36635] | 421 | } |
---|
| 422 | $_ = $done.$to_do if($done); |
---|
| 423 | &protect_texi; |
---|
| 424 | $_ = "\@expansion{} ".$_; |
---|
| 425 | } |
---|
| 426 | print INC $_; |
---|
[84dee1] | 427 | print TEX $_; |
---|
[f36635] | 428 | } |
---|
| 429 | close(RES); |
---|
| 430 | close(INC); |
---|
| 431 | unlink $ex_file, $res_file, $inc_file if ($clean); |
---|
| 432 | } |
---|
[84dee1] | 433 | print TEX "\@c end example $ex_prefix $doc_file:$lline\n"; |
---|
[f36635] | 434 | } |
---|
| 435 | |
---|
| 436 | ###################################################################### |
---|
| 437 | # @c include file |
---|
| 438 | # -> copy content of file into output file protecting texi special chars |
---|
| 439 | sub HandleInclude |
---|
| 440 | { |
---|
| 441 | s/^\@c\s*include\s+([^\s]+)\s/$1/; |
---|
[460c1e1] | 442 | s/\s*$//; |
---|
[f36635] | 443 | unless (&Open(*INC, "<$_")) |
---|
| 444 | { |
---|
| 445 | warn "$WARNING HandleInclude: can't open $_ for reading\n"; |
---|
| 446 | print TEX "\@c include file $_ not found\n"; |
---|
| 447 | return; |
---|
| 448 | } |
---|
| 449 | print "<$_>" if ($verbose); |
---|
| 450 | print TEX "\@c begin included file $_ from $doc_file:$line\n"; |
---|
| 451 | while (<INC>) |
---|
| 452 | { |
---|
| 453 | &protect_texi; |
---|
| 454 | print TEX $_; |
---|
| 455 | } |
---|
| 456 | print TEX "\@c end included file from $doc_file:$line\n"; |
---|
| 457 | close (INC); |
---|
| 458 | } |
---|
| 459 | |
---|
| 460 | ###################################################################### |
---|
| 461 | # @c ref |
---|
| 462 | # .... |
---|
| 463 | # @c ref |
---|
| 464 | # -> scans intermediate lines for @ref{..} strings |
---|
| 465 | # Creates menu of (sorted) refs for ifinfo |
---|
| 466 | # Creates comma-separated (sorted) refs for iftex, prepended with |
---|
| 467 | # the text before first @ref. |
---|
| 468 | |
---|
| 469 | sub HandleRef |
---|
| 470 | { |
---|
| 471 | local(%refs, @refs, $header, $lline, $lref); |
---|
| 472 | |
---|
| 473 | print TEX "\@c inserted refs from $doc_file:$line\n"; |
---|
| 474 | |
---|
| 475 | # scan lines use %ref to remove duplicates |
---|
| 476 | $lline = $line; |
---|
| 477 | while (<DOC>) |
---|
| 478 | { |
---|
| 479 | $line++; |
---|
| 480 | last if (/^\@c\s*ref\s*$/); |
---|
| 481 | |
---|
[c143315] | 482 | while (/\@ref{(.*?)}[;\.]/) |
---|
[f36635] | 483 | { |
---|
| 484 | $refs{$1} = 1; |
---|
| 485 | $_ = $'; |
---|
| 486 | unless ($header) |
---|
| 487 | { |
---|
| 488 | $header = $`; |
---|
| 489 | $header = " " unless ($header); |
---|
| 490 | } |
---|
| 491 | } |
---|
[8a0ba8] | 492 | $header = $_ unless ($header) |
---|
[f36635] | 493 | } |
---|
[8a0ba8] | 494 | chomp $header; |
---|
[f36635] | 495 | die "$ERRROR no matching \@c ref found for $doc_file:$lline\n" |
---|
| 496 | unless (/^\@c\s*ref\s*$/); |
---|
| 497 | # sort refs |
---|
| 498 | @refs = sort(keys(%refs)); |
---|
| 499 | # put them out |
---|
[8a0ba8] | 500 | print TEX "\@ifinfo\n"; |
---|
| 501 | print TEX "\@menu\n"; |
---|
| 502 | if ($header && $header ne " ") |
---|
| 503 | { |
---|
| 504 | print TEX "$header\n"; |
---|
| 505 | } |
---|
| 506 | else |
---|
| 507 | { |
---|
| 508 | print TEX "See also:\n"; |
---|
| 509 | } |
---|
[f36635] | 510 | foreach $ref (@refs) {print TEX "* ".$ref."::\n";} |
---|
| 511 | print TEX "\@end menu\n\@end ifinfo\n\@iftex\n"; |
---|
| 512 | |
---|
| 513 | if ($header ne " ") |
---|
| 514 | { |
---|
| 515 | print TEX "$header\n" unless ($header eq " "); |
---|
| 516 | } |
---|
| 517 | else |
---|
| 518 | { |
---|
[9282f3] | 519 | print TEX "\@strong{See also:}\n"; |
---|
[f36635] | 520 | } |
---|
| 521 | $lref = pop(@refs); |
---|
| 522 | foreach $ref (@refs) {print TEX "\@ref{".$ref."};\n";} |
---|
| 523 | print TEX "\@ref{".$lref."}.\n" if ($lref); |
---|
| 524 | print TEX "\@end iftex\n\@c end inserted refs from $doc_file:$lline\n"; |
---|
| 525 | } |
---|
| 526 | |
---|
| 527 | ################################################################### |
---|
| 528 | # |
---|
[caf1e0] | 529 | # @c lib libname.lib[:proc] [no_ex, no_fun, Fun, (\w*)section] |
---|
[65a2bd] | 530 | # --> replaced by @include $texfile where |
---|
| 531 | # $texfile = $subdir/libname_lib[_noFun,_noEx].tex |
---|
| 532 | # --> if $make, calls "make $texfile" |
---|
| 533 | # --> Error, if $tex_file does not exist |
---|
| 534 | # --> if [:proc] is given, then includes only of respective |
---|
| 535 | # proc body |
---|
[caf1e0] | 536 | # --> Fun overwrites global no_fun |
---|
[013a76] | 537 | # --> if (\w*)section is given, replaces @subsubsection by @$1section |
---|
[65a2bd] | 538 | # and pastes in content of tex file directly |
---|
[f36635] | 539 | |
---|
| 540 | sub HandleLib |
---|
| 541 | { |
---|
[65a2bd] | 542 | my($lib, $proc, $n_fun, $n_ex, $section, $tex_file); |
---|
[acfbb5a] | 543 | |
---|
[f36635] | 544 | if (/^\@c\s*lib\s+([^\.]+)\.lib(.*)/) |
---|
| 545 | { |
---|
| 546 | $lib = $1; |
---|
| 547 | $_ = $2; |
---|
| 548 | } |
---|
| 549 | else |
---|
| 550 | { |
---|
| 551 | warn "$WARNING need .lib file to process '$_'\n"; |
---|
| 552 | print TEX $_; |
---|
| 553 | return; |
---|
| 554 | } |
---|
| 555 | |
---|
[65a2bd] | 556 | $proc = $1 if (/^:(.*?) /); |
---|
| 557 | $n_fun = 1 if ($no_fun || /no_fun/); |
---|
[caf1e0] | 558 | $n_fun = 0 if (/Fun/); |
---|
[a70441f] | 559 | $n_ex = 1 if ($no_ex || /no_ex/ || (/unix_only/ && $Win32)); |
---|
[6bf987] | 560 | $section = $1 if /(\w*)section/; |
---|
[65a2bd] | 561 | |
---|
| 562 | # contruct tex file name |
---|
| 563 | $tex_file = "$doc_subdir/$lib"."_lib"; |
---|
| 564 | if ($n_fun) |
---|
[460c1e1] | 565 | { |
---|
[65a2bd] | 566 | $tex_file .= "_noFun"; |
---|
[460c1e1] | 567 | } |
---|
[65a2bd] | 568 | elsif ($n_ex) |
---|
[460c1e1] | 569 | { |
---|
[65a2bd] | 570 | $tex_file .= "_noEx"; |
---|
[460c1e1] | 571 | } |
---|
[65a2bd] | 572 | $tex_file .= ".tex"; |
---|
| 573 | |
---|
| 574 | if ($make) |
---|
[f36635] | 575 | { |
---|
[84dee1] | 576 | print "<lib $lib " if ($verbose); |
---|
[4f2df66] | 577 | System("$make $make_opts VERBOSE=$verbose $tex_file"); |
---|
[f36635] | 578 | } |
---|
| 579 | |
---|
[65a2bd] | 580 | # make sure file exists |
---|
| 581 | if (-r $tex_file) |
---|
[f36635] | 582 | { |
---|
[65a2bd] | 583 | if ($verbose) |
---|
[acfbb5a] | 584 | { |
---|
[84dee1] | 585 | print "<lib $lib " unless $make; |
---|
| 586 | print "$proc>"; |
---|
[acfbb5a] | 587 | } |
---|
[f36635] | 588 | } |
---|
[acfbb5a] | 589 | else |
---|
| 590 | { |
---|
[65a2bd] | 591 | Error("Can't read $tex_file\n") unless -r $tex_file; |
---|
[f36635] | 592 | } |
---|
| 593 | |
---|
[65a2bd] | 594 | # see whether we have to paste something in |
---|
| 595 | if ($proc || $section) |
---|
[f36635] | 596 | { |
---|
[65a2bd] | 597 | open(LTEX, "<$tex_file") |
---|
| 598 | || Error("Can't open $tex_file for reading: $!\n"); |
---|
[f36635] | 599 | |
---|
[65a2bd] | 600 | print TEX "\@c start include of docu for $lib.lib:$proc\n"; |
---|
[013a76] | 601 | print TEX "\@c replaced \@subsubsection by \@$section\n" if ($section); |
---|
[65a2bd] | 602 | if ($proc) |
---|
[f36635] | 603 | { |
---|
[65a2bd] | 604 | my $found = 0; |
---|
| 605 | while (<LTEX>) |
---|
[f36635] | 606 | { |
---|
[65a2bd] | 607 | $found = 1 if /c ---content $proc---/; |
---|
| 608 | if ($found) |
---|
[460c1e1] | 609 | { |
---|
[013a76] | 610 | s/subsubsection/${section}section/ if $section; |
---|
[65a2bd] | 611 | print TEX $_; |
---|
[460c1e1] | 612 | } |
---|
[65a2bd] | 613 | last if $found && /c ---end content $proc---/; |
---|
[f36635] | 614 | } |
---|
[65a2bd] | 615 | if ($found) |
---|
[acfbb5a] | 616 | { |
---|
[caf1e0] | 617 | Error("no end content found for lib proc docu for $lib.lib:$proc $doc_file:$line in $tex_file\n") |
---|
[65a2bd] | 618 | unless (/c ---end content $proc---/); |
---|
| 619 | print TEX "\@c generated lib proc docu for $lib.lib:$proc $doc_file:$line \n"; |
---|
[acfbb5a] | 620 | } |
---|
| 621 | else |
---|
| 622 | { |
---|
[caf1e0] | 623 | Error("did not find lib proc docu for $lib.lib:$proc $doc_file:$line in $tex_file\n"); |
---|
[acfbb5a] | 624 | } |
---|
| 625 | } |
---|
[65a2bd] | 626 | else |
---|
[dc3a44] | 627 | { |
---|
[65a2bd] | 628 | while (<LTEX>) |
---|
| 629 | { |
---|
[013a76] | 630 | s/subsubsection/${section}section/; |
---|
[65a2bd] | 631 | print TEX $_; |
---|
| 632 | } |
---|
[dc3a44] | 633 | } |
---|
[65a2bd] | 634 | print TEX "\@c end include of docu for $lib.lib:$proc\n"; |
---|
| 635 | close(LTEX); |
---|
[acfbb5a] | 636 | } |
---|
| 637 | else |
---|
| 638 | { |
---|
[65a2bd] | 639 | print TEX "\@c include of docu for $lib.lib\n"; |
---|
| 640 | print TEX "\@include $tex_file\n"; |
---|
[dc3a44] | 641 | } |
---|
[f36635] | 642 | } |
---|
| 643 | |
---|
| 644 | #################################################################### |
---|
| 645 | # Auxillary routines |
---|
| 646 | # |
---|
| 647 | |
---|
| 648 | # protect texi special characters |
---|
| 649 | sub protect_texi |
---|
| 650 | { |
---|
| 651 | s/\@/\@\@/g; |
---|
| 652 | s/{/\@{/g; |
---|
| 653 | s/}/\@}/g; |
---|
| 654 | } |
---|
| 655 | |
---|
| 656 | # open w.r.t. include_dirs |
---|
| 657 | sub Open |
---|
| 658 | { |
---|
| 659 | local(*FH, $file) = @_; |
---|
| 660 | local($mode); |
---|
| 661 | $file =~ s/^(.{1})(.*)/$2/; |
---|
| 662 | $mode = $1; |
---|
| 663 | |
---|
| 664 | foreach $dir (@include_dirs) |
---|
| 665 | { |
---|
| 666 | return $dir if(open(FH, $mode.$dir."/".$file)); |
---|
| 667 | } |
---|
| 668 | } |
---|
| 669 | |
---|
| 670 | # system call with echo on verbose > 1 and die on fail |
---|
| 671 | sub System |
---|
| 672 | { |
---|
| 673 | local($call) = @_; |
---|
[84dee1] | 674 | print " d2t system:\n$call\n" if ($verbose > 1); |
---|
[acfbb5a] | 675 | Error("non-zero exit status of system call: '$call': $!\n") |
---|
[f36635] | 676 | if (system($call)); |
---|
| 677 | } |
---|
| 678 | |
---|
[acfbb5a] | 679 | sub Error |
---|
| 680 | { |
---|
| 681 | print "$ERROR $_[0]"; |
---|
| 682 | close(TEX); |
---|
| 683 | unlink $tex_file if $tex_file && -e $tex_file; |
---|
| 684 | exit(1); |
---|
| 685 | } |
---|
| 686 | |
---|
[354f3b] | 687 | sub Warn |
---|
| 688 | { |
---|
| 689 | print "$WARNING $_[0]\n"; |
---|
| 690 | } |
---|
[f36635] | 691 | # |
---|
| 692 | # leave this here --otherwise fontification in my emacs gets screwd up |
---|
| 693 | # |
---|
| 694 | sub Usage |
---|
| 695 | { |
---|
| 696 | return <<EOT; |
---|
| 697 | This is doc2tex: a utility to generate Singular texinfo from doc file |
---|
| 698 | To convert a doc file to texinfo: $0 [options] input_file.doc |
---|
| 699 | where options can be (abbreviated to shortest possible prefix): |
---|
| 700 | -Singular prog: use 'prog' as Singular program to generate ex output |
---|
| 701 | (default: '../Singular/Singular') |
---|
| 702 | -output file : use 'file' as output file |
---|
| 703 | (default: input_file.tex) |
---|
| 704 | -clean : delete intermediate files |
---|
[84dee1] | 705 | -make cmd : use cmd as make command to generate tex files for libraries |
---|
[f36635] | 706 | -no_reuse : don't reuse intermediate files |
---|
| 707 | -no_ex : skip computation of examples |
---|
[65a2bd] | 708 | -no_fun : don't include help for library functions |
---|
[c04b94] | 709 | -docdir dir : put intermediate doc/tex files into 'dir' |
---|
[f36635] | 710 | (default: './d2t_singular') |
---|
[c04b94] | 711 | -exdir dir : put example files into 'dir' |
---|
| 712 | (default: './examples') |
---|
[f36635] | 713 | -I dir : look also into 'dir' for include and lib files |
---|
| 714 | (default: ".", "../Singular/LIB") |
---|
| 715 | -verbose val : Set verbosity to 'val' (0=quiet, 1=prot, >1=all) |
---|
| 716 | -help : print help and exit |
---|
| 717 | EOT |
---|
| 718 | } |
---|
| 719 | |
---|