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